VirtualInstrumentInterval.hh 3.59 KB
/*
 * VirtualInstrumentInterval.hh
 *
 *  Created on: Jan 17, 2013
 *      Author: f.casimir
 */

#ifndef VIRTUALINSTRUMENTINTERVAL_HH_
#define VIRTUALINSTRUMENTINTERVAL_HH_

#include <string>
#include <map>
#include <vector>

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

#include "Worker.hh"

#include "DD.hh"

#include "TimeInterval.hh"

namespace AMDA {
namespace DDServerInterface {
namespace VI {
	class ParamFlow;
	typedef boost::shared_ptr<ParamFlow> ParamFlowSPtr;

}
class VirtualInstrument;

/**
 * @class VirtualInstrumentInterval
 * @brief Class responsible to get data from a time interval for dispatch them into some DataFlow.
 */
class VirtualInstrumentInterval : public AMDA::Helpers::Worker {
	typedef std::list<TimeTableCatalog::TimeInterval> TimeIntervalList;

public:
	/**
	 * Constructor
	 */
	VirtualInstrumentInterval(VirtualInstrument& pVI, TimeIntervalList* pTimeIntervalList);
	/**
	 * Destructor
	 */
	virtual ~VirtualInstrumentInterval();

	// Getter methods
	int getId() const {	return _id;	}

    // Other methods
	VI::ParamFlowSPtr getParamFlow(const std::string& pParamName);

	/**
	 * @return nbData Available or 0 if no Data
	 *  blocking operation
	 */
	boost::shared_future<unsigned int> getAsync() {
		AMDA::Helpers::Task<unsigned int>* task = new AMDA::Helpers::Task<unsigned int>(
				boost::bind(&VirtualInstrumentInterval::getOneDDDataBloc, this));
		boost::shared_future<unsigned int> future(task->getFuture());
		_taskList.push(task); // assuming internal_queue member
		return future;
	}

	/**
	 * @brief Define if time interval was set to another one.
	 */
	bool isTimeIntToProcessChanged() { return _timeIntToProcessChanged; }


protected:
	typedef boost::weak_ptr<VI::ParamFlow> ParamFlowWPtr;
	typedef std::map<std::string,std::vector<ParamFlowWPtr> > ParamFlowList;

	/**
	 * @details Close DD_Client and re-initialize _id.
	 */
	void ddClose();

	/**
	 * set pointer for read data in DD_serveur
	 * @param synchronized if true, the interval time itimeInt must be updated with real start time
	 * @return true if the TimeInterval changed otherwise false if there is no more TimeInterval.
	 */
	bool setTimeDD();

	/**
	 * @brief Call _step pointer function which is different in function of the program step.
	 */
	unsigned int getOneDDDataBloc();

private:

	/**
	 * @brief Construct _paramList (it is the first step) and call writeData.
	 */
	unsigned int initAndWriteData();
	/**
	 * @details It is the midle step.
	 * Call DDClient::getMultiParam and dispatch results to ParamFlow.
	 * When no more data set the _tep function pointer to writeEmptyData.
	 */
	unsigned int writeData();
	/**
	 * @brief write NULL Packet.
	 */
	unsigned int writeEmptyData();

	unsigned int writeNoData();

	unsigned int (VirtualInstrumentInterval::*_step)();

	VirtualInstrument& _vi; /*!< Reference an associated Virtual Instrument */

	TimeIntervalList& _timeIntervalList;
	TimeIntervalList::iterator _currentTimeIntToProcess;
	/**
	 * @brief Define if data retrieving for a time interval changed.
	 */
	bool _timeIntToProcessChanged;

	double _realTimeInt;       /*!< Time interval to extract data */
	int _id;               /*!<  id of param in DD_server */
	char** _paramList;     /*!< Need to call GetMultiData to DD_server */
	std::string _paramNameListForLog;     /*!< Need to display log */
	DD_Client _ddClient;   /*!<  Connection DD_client for DD_Server */
	ParamFlowList _paramFlowList; /*! List of ParamFlow => ObserverList */
	char _strStartTime[TIMELENGTH];
	char _strTimeInt[TIMELENGTH];

	bool _nodata;
};

} /* namespace DDServerInterface */
} /* namespace AMDA */
#endif /* VIRTUALINSTRUMENTINTERVAL_HH_ */