AbstractEventSourceFactory.cpp

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #include <fesa-core/RealTime/AbstractEventSourceFactory.h>
00004 
00005 #include <fesa-core/RealTime/AbstractRealTimeLabObjectFactory.h>
00006 #include <fesa-core/RealTime/TimerEventSource.h>
00007 #include <fesa-core/RealTime/OnDemandEventSource.h>
00008 #include <fesa-core/RealTime/OnDemandConsumerImpl.h>
00009 #include <fesa-core/Diagnostic/FesaStream.h>
00010 #include <fesa-core/Exception/FesaException.h>
00011 #include <fesa-core/Core/AbstractEquipment.h>
00012 #include <fesa-core/Core/AbstractMsgQueue.h>
00013 #include <fesa-core/Core/MsgQueueFactory.h>
00014 
00015 #include <cmw-log/Logger.h>
00016 
00017 #include <iostream>
00018 
00019 
00020 namespace
00021 {
00022 
00023 CMW::Log::Logger& logger = CMW::Log::LoggerFactory::getLogger("FESA.FWK.fesa-core.RealTime.AbstractEventSourceFactory");
00024 
00025 }
00026 
00027 namespace fesa
00028 {
00029 
00030 AbstractEventSourceFactory::EventSourceMap AbstractEventSourceFactory::eventSourceMap_;
00031 
00032 AbstractEventSourceFactory::AbstractEventSourceFactory()
00033 {
00034 
00035 }
00036 
00037 AbstractEventSourceFactory::~AbstractEventSourceFactory()
00038 {
00039 }
00040 
00041 AbstractEventSource* AbstractEventSourceFactory::createEventSource(const std::string& className, const std::string& eventSourceName,const EventSourceType& type)
00042 {
00043     if(eventSourceExists(className,eventSourceName))
00044     {
00045         throw FesaException(__FILE__, __LINE__, FesaErrorEventSourceExists.c_str(), className.c_str(), eventSourceName.c_str());
00046     }
00047 
00048     AbstractEventSource* source;
00049 
00050     switch(type)
00051     {
00052         case TimingSource:
00053         {
00054             source = createTimingEventSource(eventSourceName);
00055             break;
00056         }
00057         case TimerSource:
00058         {
00059             source = createTimerEventSource(eventSourceName);
00060             break;
00061         }
00062         case OnDemandSource:
00063         {
00064             source = createOnDemandEventSource(className,eventSourceName);
00065             break;
00066         }
00067         case OnSubscriptionSource:
00068         {
00069             source = createOnSubscriptionEventSource(eventSourceName);
00070             break;
00071         }
00072         case CustomSource:
00073         {
00074             source = createCustomEventSource(eventSourceName);
00075             break;
00076         }
00077         default:
00078         {
00079             //This anyhow can never happen ... remove default-case?
00080             throw FesaException(__FILE__, __LINE__, FesaErrorAbstractEventSourceNotCreated.c_str(), eventSourceName.c_str());
00081         }
00082     }
00083 
00084     EventSourceKey key = generateKey(className,eventSourceName);
00085     eventSourceMap_.insert(std::make_pair(key,source));
00086 
00087     return source;
00088 }
00089 
00090 AbstractEventSource* AbstractEventSourceFactory::createTimingEventSource(const std::string& eventSourceName)
00091 {
00092     if( eventSourceName != TIMING_EVENT_SOURCE_NAME )
00093     {
00094         throw FesaException(__FILE__, __LINE__, FesaErrorWrongEventSourceName.c_str(),eventSourceName.c_str(), TIMING_EVENT_SOURCE_NAME.c_str(), TIMING_EVENT_SOURCE_NAME.c_str());
00095     }
00096 
00097     if (AbstractEquipment::getInstance()->getTimingSimulationMode())
00098     {
00099         return labFactory_->createTimingSimulationEventSource(eventSourceName);
00100     }
00101     else
00102     {
00103         return labFactory_->createTimingEventSource(eventSourceName);
00104     }
00105 }
00106 
00107 AbstractEventSource* AbstractEventSourceFactory::createTimerEventSource(const std::string& eventSourceName)
00108 {
00109     if(eventSourceName != TIMER_EVENT_SOURCE_NAME )
00110     {
00111         throw FesaException(__FILE__, __LINE__, FesaErrorWrongEventSourceName.c_str(),eventSourceName.c_str(), TIMER_EVENT_SOURCE_NAME.c_str(), TIMER_EVENT_SOURCE_NAME.c_str());
00112     }
00113 
00114     return new TimerEventSource();
00115 }
00116 
00117 AbstractEventSource* AbstractEventSourceFactory::createOnDemandEventSource(const std::string& className,const std::string& eventSourceName)
00118 {
00119     int32_t size = AbstractEquipment::getInstance()->getProcessConfiguration()->getIntValue(PropertyTag::MSG_NUM_MAX);
00120     AbstractMsgQueue* queue = MsgQueueFactory::getOrCreateMsgQueue(className + eventSourceName, size, true);
00121     boost::shared_ptr<OnDemandConsumer> eventConsumer(new OnDemandConsumerImpl(queue));
00122     return new OnDemandEventSource(eventSourceName,eventConsumer);
00123 }
00124 
00125 void AbstractEventSourceFactory::deleteEventSources()
00126 {
00127     EventSourceMap::iterator iter;
00128     for (iter = eventSourceMap_.begin(); iter != eventSourceMap_.end(); iter++)
00129     {
00130         delete iter->second;
00131     }
00132     eventSourceMap_.clear();
00133 }
00134 
00135 void AbstractEventSourceFactory::startEventSources()
00136 {
00137     EventSourceMap::iterator eventSourceMapIter;
00138     for (eventSourceMapIter = eventSourceMap_.begin(); eventSourceMapIter != eventSourceMap_.end(); eventSourceMapIter++)
00139     {
00140         const std::string name = eventSourceMapIter->first.first;
00141         std::ostringstream stream;
00142         stream << "Starting event source: " << name << ", ClassName: " << eventSourceMapIter->first.second;
00143         LOG_DEBUG_IF(logger, stream.str());
00144         eventSourceMapIter->second->start(false, name);
00145     }
00146 }
00147 
00148 void AbstractEventSourceFactory::stopEventSources()
00149 {
00150     EventSourceMap::iterator iter;
00151     for (iter = eventSourceMap_.begin(); iter != eventSourceMap_.end(); iter++)
00152     {
00153         std::ostringstream stream;
00154         stream << "Stopping event source: " << iter->first.first << " of class: " << iter->first.second;
00155         LOG_INFO_IF(logger, stream.str());
00156         iter->second->stop();
00157     }
00158 }
00159 
00160 AbstractEventSource* AbstractEventSourceFactory::getEventSource(const std::string& className, const std::string& eventSourceName)
00161 {
00162     EventSourceKey key = generateKey(className,eventSourceName);
00163     EventSourceMap::iterator iter = eventSourceMap_.find(key);
00164     if(iter == eventSourceMap_.end())
00165     {
00166         throw FesaException(__FILE__, __LINE__, FesaErrorEventSourceNotFound.c_str(), className.c_str(), eventSourceName.c_str());
00167     }
00168     return iter->second;
00169 }
00170 
00171 bool AbstractEventSourceFactory::eventSourceExists(const std::string& className, const std::string& eventSourceName)
00172 {
00173     EventSourceKey key = generateKey(className,eventSourceName);
00174     EventSourceMap::iterator iter = eventSourceMap_.find(key);
00175     if(iter == eventSourceMap_.end())
00176     {
00177         return false;
00178     }
00179     return true;
00180 }
00181 
00182 AbstractEventSourceFactory::EventSourceKey AbstractEventSourceFactory::generateKey(const std::string& className, const std::string& eventSourceName)
00183 {
00184     std::string fixedClassName = className;
00185     if(eventSourceName == TIMER_EVENT_SOURCE_NAME || eventSourceName == TIMING_EVENT_SOURCE_NAME || eventSourceName == ON_SUBSCRIPTION_EVENT_SOURCE_NAME )
00186     {
00187         //Timing,Timer and On-Subscription-Event-Source only exist once per equipment.
00188         //So the className does not matter for them and has to stay empty.
00189         fixedClassName = "";
00190     }
00191     return std::make_pair(fixedClassName,eventSourceName);
00192 }
00193 
00194 void AbstractEventSourceFactory::printConfigAll(FesaStream* fesaStream)
00195 {
00196     EventSourceMap::iterator iter;
00197     *fesaStream << "\nEVENT-SOURCE-CONFIGS -------------------------------------------------------" << std::endl;
00198     for (iter = eventSourceMap_.begin(); iter != eventSourceMap_.end(); iter++)
00199     {
00200         iter->second->printConfig(fesaStream);
00201     }
00202 }
00203 
00204 void AbstractEventSourceFactory::printStateAll(FesaStream* fesaStream, double elapsedTime)
00205 {
00206     EventSourceMap::iterator iter;
00207     *fesaStream << "\nEVENT-SOURCE-STATES -------------------------------------------------------" << std::endl;
00208     for (iter = eventSourceMap_.begin(); iter != eventSourceMap_.end(); iter++)
00209     {
00210         iter->second->printState(fesaStream, elapsedTime);
00211     }
00212 }
00213 
00214 } // fesa
00215 

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1