/*
 * (c) 2005 Research Group Dr. Fuhrmann, University of Karlsruhe, Germany
 * All rights reserved.
 *
 * Author:
 *         Stefan Denk <denk@ira.uka.de>
 *         Kendy Kutzner <kutzner@ira.uka.de>
 *
 * $Id: videosource.cc,v 1.4 2006/01/05 10:31:44 denk Exp $
 *
 */


// stdlib

#include <sstream>
using namespace std;

// vidtrans

#include "videosource.h"

/**
 * Construct a cvideosource with given values.
 * If lastupdate is ommitted or 0, set the current wallclock time as lastupdate.
 */

cvideosource::cvideosource(const string & transport, const hashes_t & hashes, time_t lastupdate){
	_transport = transport;
	_lastupdate = lastupdate;
	_hashes = hashes;
	if (_lastupdate == 0)
	{
		_lastupdate = time(NULL);
	}
}

/**
 * We have new information about this video source. Someone published the video
 * source. Lets see if our information is newer. If the received information is
 * more current, update our idea of _lastupdate.
 * \param newlastupdate The just received update timer.
 */
void
cvideosource::updatelastupdate(time_t newlastupdate){
	if (_lastupdate < newlastupdate){
		/*printf("%s: '%s' lastupdate change from %li to %li\n",
				__PRETTY_FUNCTION__,
				tostring().c_str(),
				_lastupdate,
				newlastupdate;);*/
		_lastupdate = newlastupdate;
	}
}

/**
 * Returns the first three hash values of a given videosource.
 * This information is needed by the clientthread to formulate the correct 
 * request for a set task.
 */
void cvideosource::first3hashes(struct firsthashes * f3h)
{
	int counter = 0;
	
	for (hashes_t::const_iterator i = _hashes.begin(); i!=_hashes.end(); i++)
	{
		if (counter == 0) f3h->hash1 = (*i).hash;
		if (counter == 1) f3h->hash2 = (*i).hash;
		if (counter == 2) f3h->hash3 = (*i).hash;
		f3h->length += (*i).len;
		counter++;
	}	
}

string cvideosource::tostring(){
	stringstream s;
	s << _transport << " ";
	s << _lastupdate << " ";
	bool firsthash = true;
	for (hashes_t::const_iterator i = _hashes.begin(); i!=_hashes.end(); i++){
		if (!firsthash)
			s << "," ;
		s << (*i) ;
		firsthash=false;
	}
	s << "\n";
	return s.str();
}

bool cvideosource::fromstring(string *s){
	//dsyslog("%s: parsing string '%s'", __PRETTY_FUNCTION__, s->c_str());
	int pos;
	pos = s->find(' ');
	if (pos == -1)
	{
		return false;
		printf("parsing of videosource screwed up - 1st\n");
	}
	_transport = s->substr(0, pos);
	//dsyslog("%s: transport '%s'", __PRETTY_FUNCTION__, _transport.c_str());
	s->erase(0, pos+1);
	pos = s->find(' ');
	if (pos == -1)
	{
		return false;
	}
	_lastupdate = atoi(s->substr(0, pos).c_str());
	//dsyslog("%s: lastupdate '%li'", __PRETTY_FUNCTION__, _lastupdate);
	s->erase(0, pos+1);
	string t = s->substr(0, s->size());
	pos = t.find(',');
	while (pos!=-1){
		video_hash h (t.substr(0, pos+1));
		//dsyslog("%s: hash '%s'", __PRETTY_FUNCTION__, h.tostring().c_str());
		_hashes.push_back(h);
		t.erase(0, pos+1);
		pos = t.find(',');
	}
	//dsyslog("%s: done", __PRETTY_FUNCTION__);
	return true;
}

/**
 * Compare two video sources. Currently, we only compare transport address and
 * timestamp. This limiting makes the comparison faster. What a single source
 * once claimed as the correct hash list for this minute can't be changed any
 * more.
 */
bool
cvideosource::operator==(cvideosource & other) const{
	bool returnvalue = ((_transport == other._transport)); ///\bug FIX ME!!!            && (_lastupdate == other._lastupdate));
	///\bug comparison between long int and time_t!
	return returnvalue; 
}

