Parameter.cc 4.67 KB
/**
 * Parameter.cc
 *
 *  Created on: 16 oct. 2012
 *      Author: AKKA IS
 */


#include "Parameter.hh"

#include "ParamGet.hh"
#include "ParamOutput.hh"
#include "ParamData.hh"
#include "Process.hh"
#include "ParamInterval.hh"
#include "CalibrationInfoWriter.hh"

#include "ParamMgr.hh"
#include "DataSetMgr.hh"
#include "InstrumentMgr.hh"
#include "MissionMgr.hh"

using namespace std;

using namespace AMDA::Info;

namespace AMDA {
namespace Parameters {

  Parameter::Parameter(ParameterManager& pParamMng, string nameParam) :
		  _id(nameParam),
		  _parameterManager(pParamMng),
		  _clientInitCall(0),
		  _timeResolution(0) ,
		  _gapThreshold(pParamMng.getDefaultGapThreshold()) ,
		  establishConnectionToDo(true){
  }

  Parameter::~Parameter() {
	  	  IntervalList::iterator it = _intervalList.begin();
	  if (it != _intervalList.end()) {
		  (*it)->setDataWriter(NULL);
	  }
	  for(auto x : _intervalList) { delete x; }
	  for(auto x : _calibrationInfoWriterList) { delete x;}

  }

  /**
   * Update client list.
   */
  void Parameter::openConnection(DataClient *pDataClient) {
	DataClientList::iterator it =  _dataClientList.find(pDataClient);
	if ( it ==  _dataClientList.end()) {
		_dataClientList.insert( make_pair( pDataClient,static_cast<ParamInterval*>(NULL)));
	}

	//To call once time
	if ( establishConnectionToDo ) {
		Process* process = dynamic_cast<Process*>(_dataWriter.get());
		if ( process )  {
			process->establishConnection();
		}
		establishConnectionToDo=false;
	}
  }

  /**
   * Update client list.
   */
  void Parameter::closeConnection(DataClient *pDataClient) {
	DataClientList::iterator it =  _dataClientList.find(pDataClient);
	if ( it !=  _dataClientList.end()) {
		auto lInterval = it->second;
		if (lInterval) { lInterval->removeClient(pDataClient); }
	}
  }

  /**
   * Compute the biggest interval with all client init information.
   */
  TimeStamp  Parameter::init(DataClient *pDataClient, TimeIntervalListSPtr pTimeIntervalList) {

	  TimeStamp lTimeStamp;
	  DataClientList::iterator it =  _dataClientList.find(pDataClient);
	if ( it ==  _dataClientList.end()) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN));
	} else {
		if ( ! it->second ) {
			IntervalList::iterator interval = _intervalList.begin();
			if (interval == _intervalList.end()) {
				//Interval creation
				_intervalList.push_back(new ParamInterval(TimeIntervalListSPtr(pTimeIntervalList),_dataWriter.get()));
				interval = --_intervalList.end();
				TimeStamp lTimeStampInterval = (*interval)->init();
				if(lTimeStampInterval > lTimeStamp) {
					lTimeStamp = lTimeStampInterval;
				}

				/// When the first _dataWriter has initialized the Calibration processing are possible
				///
				for(auto x : _calibrationInfoWriterList) { (*x)(*this); }
			} else {
				for ( ; interval !=  _intervalList.end() && ! (*interval)->getIsEqual(pTimeIntervalList.get()); ++interval );
				if (interval == _intervalList.end()) {
					//Interval creation
					_intervalList.push_back(new ParamInterval(TimeIntervalListSPtr(pTimeIntervalList),_dataWriter->clone(*this)));
					interval = --_intervalList.end();
					TimeStamp lTimeStampInterval = (*interval)->init();
					if(lTimeStampInterval > lTimeStamp) {
						lTimeStamp = lTimeStampInterval;
					}
				}
			}
			(*interval)->addClient(pDataClient);
			it->second=(*interval);
			++_clientInitCall;
		}
	}

	if ( _clientInitCall == _dataClientList.size()) {
		IntervalList::iterator interval = _intervalList.begin();
		for ( ; interval !=  _intervalList.end(); ++interval ) {
			(*interval)->active();
		}
		_clientInitCall=0;
	}

	// Exemple lecture ParamInfo, DataSetInfo, InstrumentInfo, MissionInfo
#if 0
	ParamInfoSPtr paramInfo = ParamMgr::getInstance()->getParamInfoFromId(getInfoId());
	if (paramInfo) {
		DataSetInfoSPtr dataSetInfo = DataSetMgr::getInstance()->getDataSetInfoFromId(paramInfo->getDatasetId());
		if (dataSetInfo) {
			InstrumentInfoSPtr instrumentInfo	= InstrumentMgr::getInstance()->getInstrumentInfoFromId(dataSetInfo->getInstrumentId());
			if (instrumentInfo) {
				MissionInfoSPtr mission	= MissionMgr::getInstance()->getMissionInfoFromId(instrumentInfo->getMissionId());
			}
		}
	}
#endif

	return lTimeStamp;
  }

boost::shared_future<ParamDataIndexInfo> Parameter::getAsync(DataClient * pClient) {
	return _dataClientList[pClient]->getAsync(pClient);
}

ParamDataSPtr& Parameter::getParamData(DataClient* pClient) { return _dataClientList[pClient]->getParamData();}

	void Parameter::delegateOtherTaskTo(ParameterSPtr& pParam) {
		if ( pParam.get() != this) {
			pParam->_calibrationInfoWriterList.splice(pParam->_calibrationInfoWriterList.begin(),_calibrationInfoWriterList);
		}
	}

}/* namespace Parameters */
} /* namespace AMDA */