/*
 * (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.h,v 1.5 2005/12/27 12:12:33 kutzner Exp $
 *
 */

#ifndef _VIDEOSOURCE_H_
#define _VIDEOSOURCE_H_

#include <list>
using namespace std;
#include <stdint.h>
#include "video_hash.h"

/// stores the first three hash values and the size of the videodata block from one cvideosource.
struct firsthashes
{
	uint32_t hash1;
	uint32_t hash2;
	uint32_t hash3;
	unsigned long length;
};

/** memorize on foreign video source
 * A video source is characterized by a transport address where to fetch that block and a list of hashes what to fetch.
 * Further it contains the time this video source was last announced by the source.
 */
class cvideosource {
	private:
		/** Where to fetch this block.
		 * A transport address where one can connect to fetch this block.
		 * It should be in a format understandable by the client thread.
		 * Suggested is a format like 'tcp://hostname.domain:port'
		 */
		string _transport;
		/** Time of last announce.
		 * When the block was last announced by the source.
		 * In units of seconds since the unix epoch.
		 */
		time_t _lastupdate;
		/** Hash values representing this block.
		 * Hashes representing this block.
		 */
		hashes_t _hashes;
	public:
		/** ctor for initialization.
		 * Clears all internal values.
		 */
		cvideosource(){_transport.clear(); _hashes.clear(); _lastupdate = 0;}
		/// Copy Ctor
		cvideosource(const cvideosource & other):_transport(other._transport),_lastupdate(other._lastupdate),_hashes(other._hashes){};
		/// Ctor with given values.
		cvideosource(const string & transport, const hashes_t & hashes, time_t lastupdate=0);
		/** Convert a video source to a string.
		 * Convert the internal data to a string transportable over a network.
		 * ASCII representation of a single video source:
		 * -# name of the transport
		 *    - should not contain a space
		 *    - is interpreted by the transport class
		 * -# a single space
		 * -# time of the last announcement of this source
		 *    - a single integer representing number of seconds since the epoch
		 * -# a comma seperated list of hash values
		 *    - the values are interpreted by the video_hash class
		 * -# a newline
		 * \return A new string
		 */
		string tostring();
		/** Takes the first video source from the string.
		 * Convert the string to a local object.
		 * Modifies the string by removing all valid parts.
		 * \return true iff something was found.
		 */
		bool fromstring(string *s);
		/** Stores the first three hashes in the struct f3h, along with the total size of the cvideosource.
		 * This is needed for the clientthread to ask for the right block and length.
		 * \param firsthashes * f3h pointer to the data structure.
		 */
		void first3hashes(struct firsthashes * f3h);
		/** Time of last announce.
		 * \return Time of last announce.
		 */
		time_t lastupdate() {return _lastupdate;}
		/// Make the lastupdate member current
		void updatelastupdate(time_t);
		/** Where to get is.
		 * \return A transport address where to fetch these blocks.
		 */
		string transport() {return _transport;}
		/// Compare two video sources
		bool operator==(cvideosource & other) const;
};



/** List of video sources.
 * For each aggregation target we want to memorize a list of video sources.
 */
typedef list<cvideosource*> sourcelist_t;



#endif // _VIDEOSOURCE_H_
