FairMQ/fairmq/devices/GenericSampler.tpl
Alexey Rybalchenko 1302e77a16 Several FairMQ fixes and improvements:
- FairMQ: add possibility to poll on multiple channels.
- FairMQ: include command channel when polling on blocking calls (for unblocking without termination).
- FairMQ: move signal handler inside of FairMQDevice class (call FairMQDevice::CatchSignals() in the main function).
- FairMQ: add 'bool CheckCurrentState(statename)' (instead of 'GetCurrentState() == statename' that cannot be thread safe).
- FairMQDevice: add 'InteractiveStateLoop()' method that can be used to change states from the command line.
- FairMQDevice: add automatic transition to IDLE state if Run() exits without an external event.
- FairMQDevice: implement device reset.
- FairMQDevice: use unordered_map for device channels.
- FairMQChannel: improve address validation for channels.
- FairMQChannel: add ExpectsAnotherPart() method to check if another msg part is expected (old approach still works).
- FairMQ: remove invalid transition from the run files.
- FairMQFileSink: disable ROOT termination signal handler.
- Tutorial3: spawn xterm windows from start scripts without overlapping for better visibility.
- FairMQ Examples: update protobuf test and move its files to a common directory.
- FairMQStateMachine: improve feedback on invalid transitions (more readable).
2015-09-28 12:17:22 +02:00

205 lines
5.6 KiB
Smarty

/*
* File: GenericSampler.tpl
* Author: winckler
*
* Created on November 24, 2014, 3:59 PM
*/
template <typename T, typename U, typename K, typename L>
base_GenericSampler<T,U,K,L>::base_GenericSampler()
: fOutChanName("data-out")
, fNumEvents(0)
, fCurrentIdx(0)
, fEventRate(1)
, fEventCounter(0)
, fContinuous(false)
{
}
template <typename T, typename U, typename K, typename L>
base_GenericSampler<T,U,K,L>::~base_GenericSampler()
{
}
template <typename T, typename U, typename K, typename L>
void base_GenericSampler<T,U,K,L>::SetTransport(FairMQTransportFactory* factory)
{
FairMQDevice::SetTransport(factory);
}
template <typename T, typename U, typename K, typename L>
void base_GenericSampler<T,U,K,L>::InitTask()
{
BindingSendPart();
BindingGetSocketNumber();
BindingGetCurrentIndex();
source_type::InitSampler();
fNumEvents = source_type::GetNumberOfEvent();
}
template <typename T, typename U, typename K, typename L>
void base_GenericSampler<T,U,K,L>::Run()
{
// boost::thread resetEventCounter(boost::bind(&GenericSampler::ResetEventCounter, this));
int sentMsgs = 0;
boost::timer::auto_cpu_timer timer;
LOG(INFO) << "Number of events to process: " << fNumEvents;
do
{
for (fCurrentIdx = 0; fCurrentIdx < fNumEvents; fCurrentIdx++)
{
for(auto& p : fChannels[fOutChanName])
{
FairMQMessage* msg = fTransportFactory->CreateMessage();
serialization_type::SetMessage(msg);
source_type::SetIndex(fCurrentIdx);
ExecuteTasks();
p.Send(serialization_type::SerializeMsg(source_type::GetOutData()));
if (msg)
msg->CloseMessage();
sentMsgs++;
if(fChannels[fOutChanName].size()>1)
fCurrentIdx++;
// Optional event rate limiting
// --fEventCounter;
// while (fEventCounter == 0) {
// boost::this_thread::sleep(boost::posix_time::milliseconds(1));
// }
if (!CheckCurrentState(RUNNING))
break;
}
// if more than one socket, remove the last incrementation
if(fChannels[fOutChanName].size()>1)
fCurrentIdx--;
}
}
while (CheckCurrentState(RUNNING) && fContinuous);
boost::timer::cpu_times const elapsed_time(timer.elapsed());
LOG(INFO) << "Sent everything in:\n" << boost::timer::format(elapsed_time, 2);
LOG(INFO) << "Sent " << sentMsgs << " messages!";
}
template <typename T, typename U, typename K, typename L>
void base_GenericSampler<T,U,K,L>::SendPart(int socketIdx)
{
fCurrentIdx++;
if(fCurrentIdx<fNumEvents)
{
FairMQMessage* msg = fTransportFactory->CreateMessage();
serialization_type::SetMessage(msg);
source_type::SetIndex(fCurrentIdx);
fChannels[fOutChanName].at(socketIdx).Send(serialization_type::SerializeMsg(source_type::GetOutData()), "snd-more");
if (msg)
msg->CloseMessage();
}
}
template <typename T, typename U, typename K, typename L>
int base_GenericSampler<T,U,K,L>::GetSocketNumber() const
{
return fChannels.at(fOutChanName).size();
}
template <typename T, typename U, typename K, typename L>
int base_GenericSampler<T,U,K,L>::GetCurrentIndex() const
{
return fCurrentIdx;
}
template <typename T, typename U, typename K, typename L>
void base_GenericSampler<T,U,K,L>::SetContinuous(bool flag)
{
fContinuous = flag;
}
template <typename T, typename U, typename K, typename L>
void base_GenericSampler<T,U,K,L>::ResetEventCounter()
{
while (GetCurrentState() == RUNNING)
{
try
{
fEventCounter = fEventRate / 100;
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
}
catch (boost::thread_interrupted &)
{
LOG(DEBUG) << "resetEventCounter interrupted";
break;
}
}
LOG(DEBUG) << ">>>>>>> stopping resetEventCounter <<<<<<<";
}
template <typename T, typename U, typename K, typename L>
void base_GenericSampler<T,U,K,L>::SetProperty(const int key, const int value)
{
switch (key)
{
case EventRate:
fEventRate = value;
break;
default:
FairMQDevice::SetProperty(key, value);
break;
}
}
template <typename T, typename U, typename K, typename L>
int base_GenericSampler<T,U,K,L>::GetProperty(const int key, const int default_/*= 0*/)
{
switch (key)
{
case EventRate:
return fEventRate;
default:
return FairMQDevice::GetProperty(key, default_);
}
}
template <typename T, typename U, typename K, typename L>
void base_GenericSampler<T,U,K,L>::SetProperty(const int key, const std::string& value)
{
switch (key)
{
case OutChannelName:
fOutChanName = value;
break;
default:
FairMQDevice::SetProperty(key, value);
break;
}
}
template <typename T, typename U, typename K, typename L>
std::string base_GenericSampler<T,U,K,L>::GetProperty(const int key, const std::string& default_)
{
switch (key)
{
case OutChannelName:
return fOutChanName;
default:
return FairMQDevice::GetProperty(key, default_);
}
}
template<typename T, typename U>
using GenericSampler = base_GenericSampler<T,U,int,std::function<void()> >;
typedef std::map<int, std::function<void()> > SamplerTasksMap;