ParamInterval.hh 3.29 KB
/*
 * ParamInterval.hh
 *
 *  Created on: Dec 11, 2012
 *      Author: f.casimir
 */

#ifndef PARAMINTERVAL_HH_
#define PARAMINTERVAL_HH_

#include <map>

#include <boost/shared_ptr.hpp>

#include "Worker.hh"
#include "ParamDataIndexInfo.hh"
#include "DataWriter.hh"
#include "TimeStamp.hh"


namespace AMDA {
namespace Parameters {

class DataClient;

/**
 * @brief Synchronize DataClient and get new Data via a DataWriter if necessary
 */
class ParamInterval : public AMDA::Helpers::Worker {
public:
	typedef std::map<DataClient *,ParamDataIndexInfo> DataClientInfoList;

	ParamInterval(TimeIntervalListSPtr pTimeIntervalList, DataWriter* dataWriter);
	~ParamInterval();

	/**
	 * @brief compare two time interval list
	 * @return true  if both are equal
	 */
	bool getIsEqual(TimeIntervalList* pTimeIntervalList) {
		if (_timeIntervalList->size() != pTimeIntervalList->size()) {
			return false;
		} else {
			TimeIntervalList::const_iterator itA = _timeIntervalList->begin();
			TimeIntervalList::const_iterator itB = pTimeIntervalList->begin();
			while (itA != _timeIntervalList->end()) {
				if ( (itA->_startTime != itB->_startTime) || (itA->_stopTime != itB->_stopTime) ) {
					return false;
				} else {
					++itA;
					++itB;
				}
			}
			return true;
		}
	}

	/**
	 * @brief paramData of _dataWriter  getter
	 */
	ParamDataSPtr& getParamData() { return _dataWriter->getParamData();}

	/**
	 * @brief Place interval on existing or creates
	 * @return the last time of all file object which can modify the type of ParamData of Parameter
	 */
	TimeStamp  init() {return _dataWriter->init(_timeIntervalList) ;};

    // Others methods

	/**
	 * @brief add a new data consumer
	 */
    void addClient(DataClient *pDataClient) { _dataClient.insert( std::make_pair(pDataClient,ParamDataIndexInfo())); }
	/**
	 * @brief Remove a new data consumer
	 */
    void removeClient(DataClient *pDataClient) { auto it = _dataClient.find(pDataClient); if ( it != _dataClient.end()) { _dataClient.erase(it); } }

    /**
	 * @brief call by a client for get more data
	 * @details call in a thread the this->get() method
	 * @return in a future object, the nbData Available or 0 if no Data
	 *  get of future object is a blocking operation
	 *  the client must consume this new data
	 */
	boost::shared_future<ParamDataIndexInfo> getAsync(DataClient * pClient) {
		AMDA::Helpers::Task<ParamDataIndexInfo>* task = new AMDA::Helpers::Task<ParamDataIndexInfo>(
				boost::bind(&ParamInterval::get, this, pClient));
		boost::shared_future<ParamDataIndexInfo> future(task->getFuture());
		_taskList.push(task); // assuming internal_queue member
		return future;

	}


	/**
	 * @brief _dataWriter setter
	 */
	void setDataWriter(DataWriter* dataWriter) {
		_dataWriter = dataWriter;
	}

protected:

	/**
	 * @brief return index of available data for a data client
	 * @details ask to dataWriter to update new data if necessary and synchronize all dataClient
	 */
	ParamDataIndexInfo get( DataClient *);

	static log4cxx::LoggerPtr _logger;

private:

	TimeIntervalListSPtr _timeIntervalList;

	/**
	 * @brief DataWriter can write data in _paramData
	 */
	DataWriter*         _dataWriter;

	/**
	 * @brief map of ParamDataIndexInfo, with DataClient key
	 */
	DataClientInfoList  _dataClient;
};

} /* namespace Parameters */
} /* namespace AMDA */
#endif /* PARAMINTERVAL_HH_ */