Update FairMQStateMachine & introduce FairMQChannels

Organize sockets as a map of vectors of FairMQChannels.

Update FairMQStateMachine by removing SETTINGINPUT, SETTINGOUTPUT,
BIND and CONNECT states and by adding INITIALIZING_TASK, RESETTING_TASK
and RESETTING_DEVICE states. Run states functions in their own thread.
This commit is contained in:
Alexey Rybalchenko 2015-04-29 13:25:42 +02:00
parent a2ebbbe450
commit 7fda980710
54 changed files with 1674 additions and 1573 deletions

View File

@ -69,7 +69,9 @@ set(SRCS
"FairMQTransportFactory.cxx" "FairMQTransportFactory.cxx"
"FairMQMessage.cxx" "FairMQMessage.cxx"
"FairMQSocket.cxx" "FairMQSocket.cxx"
"FairMQChannel.cxx"
"FairMQDevice.cxx" "FairMQDevice.cxx"
"FairMQPoller.cxx"
"devices/FairMQBenchmarkSampler.cxx" "devices/FairMQBenchmarkSampler.cxx"
"devices/FairMQSink.cxx" "devices/FairMQSink.cxx"
"devices/FairMQBuffer.cxx" "devices/FairMQBuffer.cxx"
@ -79,7 +81,6 @@ set(SRCS
"options/FairProgOptions.cxx" "options/FairProgOptions.cxx"
"options/FairMQProgOptions.cxx" "options/FairMQProgOptions.cxx"
"options/FairMQParser.cxx" "options/FairMQParser.cxx"
"FairMQPoller.cxx"
"examples/req-rep/FairMQExampleClient.cxx" "examples/req-rep/FairMQExampleClient.cxx"
"examples/req-rep/FairMQExampleServer.cxx" "examples/req-rep/FairMQExampleServer.cxx"
) )
@ -150,10 +151,10 @@ GENERATE_LIBRARY()
set(Exe_Names set(Exe_Names
bsampler bsampler
sink
buffer buffer
splitter splitter
merger merger
sink
proxy proxy
example_client example_client
example_server example_server
@ -172,10 +173,10 @@ set(Exe_Names
set(Exe_Source set(Exe_Source
run/runBenchmarkSampler.cxx run/runBenchmarkSampler.cxx
run/runSink.cxx
run/runBuffer.cxx run/runBuffer.cxx
run/runSplitter.cxx run/runSplitter.cxx
run/runMerger.cxx run/runMerger.cxx
run/runSink.cxx
run/runProxy.cxx run/runProxy.cxx
examples/req-rep/runExampleClient.cxx examples/req-rep/runExampleClient.cxx
examples/req-rep/runExampleServer.cxx examples/req-rep/runExampleServer.cxx

122
fairmq/FairMQChannel.cxx Normal file
View File

@ -0,0 +1,122 @@
/********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* FairMQChannel.cxx
*
* @since 2015-06-02
* @author A. Rybalchenko
*/
#include <set>
#include "FairMQChannel.h"
#include "FairMQLogger.h"
using namespace std;
FairMQChannel::FairMQChannel()
: fType("unspecified")
, fMethod("unspecified")
, fAddress("unspecified")
, fSndBufSize(1000)
, fRcvBufSize(1000)
, fRateLogging(1)
, fSocket()
, fChannelName("")
{
}
FairMQChannel::FairMQChannel(const string& type, const string& method, const string& address)
: fType(type)
, fMethod(method)
, fAddress(address)
, fSndBufSize(1000)
, fRcvBufSize(1000)
, fRateLogging(1)
, fSocket()
, fChannelName("")
{
}
bool FairMQChannel::ValidateChannel()
{
stringstream ss;
ss << "Validating channel " << fChannelName << "... ";
const string socketTypeNames[] = { "sub", "pub", "pull", "push", "req", "rep", "xsub", "xpub", "dealer", "router", "pair" };
const set<string> socketTypes(socketTypeNames, socketTypeNames + sizeof(socketTypeNames) / sizeof(string));
if (socketTypes.find(fType) == socketTypes.end())
{
ss << "INVALID";
LOG(DEBUG) << ss.str();
LOG(DEBUG) << "Invalid channel type: " << fType;
return false;
}
const string socketMethodNames[] = { "bind", "connect" };
const set<string> socketMethods(socketMethodNames, socketMethodNames + sizeof(socketMethodNames) / sizeof(string));
if (socketMethods.find(fMethod) == socketMethods.end())
{
ss << "INVALID";
LOG(DEBUG) << ss.str();
LOG(DEBUG) << "Invalid channel method: " << fMethod;
return false;
}
if (fAddress == "unspecified" && fAddress == "")
{
ss << "INVALID";
LOG(DEBUG) << ss.str();
LOG(DEBUG) << "invalid channel address: " << fAddress;
return false;
}
if (fSndBufSize < 0)
{
ss << "INVALID";
LOG(DEBUG) << ss.str();
LOG(DEBUG) << "invalid channel send buffer size: " << fSndBufSize;
return false;
}
if (fRcvBufSize < 0)
{
ss << "INVALID";
LOG(DEBUG) << ss.str();
LOG(DEBUG) << "invalid channel receive buffer size: " << fRcvBufSize;
return false;
}
ss << "VALID";
LOG(DEBUG) << ss.str();
return true;
}
int FairMQChannel::Send(FairMQMessage* msg, const string& flag)
{
return fSocket->Send(msg, flag);
}
int FairMQChannel::Send(FairMQMessage* msg, const int flags)
{
return fSocket->Send(msg, flags);
}
int FairMQChannel::Receive(FairMQMessage* msg, const string& flag)
{
return fSocket->Receive(msg, flag);
}
int FairMQChannel::Receive(FairMQMessage* msg, const int flags)
{
return fSocket->Receive(msg, flags);
}
FairMQChannel::~FairMQChannel()
{
}

52
fairmq/FairMQChannel.h Normal file
View File

@ -0,0 +1,52 @@
/********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* FairMQChannel.h
*
* @since 2015-06-02
* @author A. Rybalchenko
*/
#ifndef FAIRMQCHANNEL_H_
#define FAIRMQCHANNEL_H_
#include <string>
#include "FairMQSocket.h"
class FairMQChannel
{
friend class FairMQDevice;
public:
FairMQChannel();
FairMQChannel(const std::string& type, const std::string& method, const std::string& address);
virtual ~FairMQChannel();
bool ValidateChannel();
// Wrappers for the socket methods to simplify the usage of channels
int Send(FairMQMessage* msg, const std::string& flag = "");
int Send(FairMQMessage* msg, const int flags);
int Receive(FairMQMessage* msg, const std::string& flag = "");
int Receive(FairMQMessage* msg, const int flags);
std::string fType;
std::string fMethod;
std::string fAddress;
int fSndBufSize;
int fRcvBufSize;
int fRateLogging;
FairMQSocket* fSocket;
private:
std::string fChannelName;
};
#endif /* FAIRMQCHANNEL_H_ */

View File

@ -21,25 +21,25 @@ FairMQConfigurable::FairMQConfigurable()
{ {
} }
void FairMQConfigurable::SetProperty(const int key, const string& value, const int slot /*= 0*/) void FairMQConfigurable::SetProperty(const int key, const string& value)
{ {
LOG(ERROR) << "Reached end of the property list. SetProperty(" << key << ", " << value << ") has no effect."; LOG(ERROR) << "Reached end of the property list. SetProperty(" << key << ", " << value << ") has no effect.";
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
string FairMQConfigurable::GetProperty(const int key, const string& default_ /*= ""*/, const int slot /*= 0*/) string FairMQConfigurable::GetProperty(const int key, const string& default_ /*= ""*/)
{ {
LOG(ERROR) << "Reached end of the property list. The requested property " << key << " was not found."; LOG(ERROR) << "Reached end of the property list. The requested property " << key << " was not found.";
return default_; return default_;
} }
void FairMQConfigurable::SetProperty(const int key, const int value, const int slot /*= 0*/) void FairMQConfigurable::SetProperty(const int key, const int value)
{ {
LOG(ERROR) << "Reached end of the property list. SetProperty(" << key << ", " << value << ") has no effect."; LOG(ERROR) << "Reached end of the property list. SetProperty(" << key << ", " << value << ") has no effect.";
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int FairMQConfigurable::GetProperty(const int key, const int default_ /*= 0*/, const int slot /*= 0*/) int FairMQConfigurable::GetProperty(const int key, const int default_ /*= 0*/)
{ {
LOG(ERROR) << "Reached end of the property list. The requested property " << key << " was not found."; LOG(ERROR) << "Reached end of the property list. The requested property " << key << " was not found.";
return default_; return default_;

View File

@ -25,10 +25,10 @@ class FairMQConfigurable
Last = 1 Last = 1
}; };
FairMQConfigurable(); FairMQConfigurable();
virtual void SetProperty(const int key, const std::string& value, const int slot = 0); virtual void SetProperty(const int key, const std::string& value);
virtual std::string GetProperty(const int key, const std::string& default_ = "", const int slot = 0); virtual std::string GetProperty(const int key, const std::string& default_ = "");
virtual void SetProperty(const int key, const int value, const int slot = 0); virtual void SetProperty(const int key, const int value);
virtual int GetProperty(const int key, const int default_ = 0, const int slot = 0); virtual int GetProperty(const int key, const int default_ = 0);
virtual ~FairMQConfigurable(); virtual ~FairMQConfigurable();
}; };

View File

@ -12,6 +12,9 @@
* @author D. Klein, A. Rybalchenko * @author D. Klein, A. Rybalchenko
*/ */
#include <list>
#include <algorithm> // for std::sort()
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include <boost/random/mersenne_twister.hpp> // for choosing random port in range #include <boost/random/mersenne_twister.hpp> // for choosing random port in range
#include <boost/random/uniform_int_distribution.hpp> // for choosing random port in range #include <boost/random/uniform_int_distribution.hpp> // for choosing random port in range
@ -23,233 +26,310 @@
using namespace std; using namespace std;
FairMQDevice::FairMQDevice() FairMQDevice::FairMQDevice()
: fId() : fChannels()
, fId()
, fMaxInitializationTime(120)
, fNumIoThreads(1) , fNumIoThreads(1)
, fNumInputs(0)
, fNumOutputs(0)
, fPortRangeMin(22000) , fPortRangeMin(22000)
, fPortRangeMax(32000) , fPortRangeMax(32000)
, fInputAddress()
, fInputMethod()
, fInputSocketType()
, fInputSndBufSize()
, fInputRcvBufSize()
, fInputRateLogging()
, fOutputAddress()
, fOutputMethod()
, fOutputSocketType()
, fOutputSndBufSize()
, fOutputRcvBufSize()
, fOutputRateLogging()
, fPayloadInputs(new vector<FairMQSocket*>())
, fPayloadOutputs(new vector<FairMQSocket*>())
, fLogIntervalInMs(1000) , fLogIntervalInMs(1000)
, fCommandSocket()
, fTransportFactory(NULL) , fTransportFactory(NULL)
{ {
} }
void FairMQDevice::InitWrapper()
{
LOG(INFO) << "DEVICE: Initializing...";
fCommandSocket = fTransportFactory->CreateSocket("pair", "device-commands", 1);
fCommandSocket->Bind("inproc://commands");
// List to store the uninitialized channels.
list<FairMQChannel*> uninitializedChannels;
for (map< string,vector<FairMQChannel> >::iterator mi = fChannels.begin(); mi != fChannels.end(); ++mi)
{
for (vector<FairMQChannel>::iterator vi = (mi->second).begin(); vi != (mi->second).end(); ++vi)
{
// set channel name: name + vector index
stringstream ss;
ss << mi->first << "[" << vi - (mi->second).begin() << "]";
vi->fChannelName = ss.str();
// fill the uninitialized list
uninitializedChannels.push_back(&(*vi));
}
}
// go over the list of channels until all are initialized (and removed from the uninitialized list)
int numAttempts = 0;
int maxAttempts = fMaxInitializationTime;
do {
list<FairMQChannel*>::iterator itr = uninitializedChannels.begin();
while (itr != uninitializedChannels.end())
{
if ((*itr)->ValidateChannel())
{
if (InitChannel(*(*itr)))
{
uninitializedChannels.erase(itr++);
}
else
{
LOG(ERROR) << "failed to initialize channel " << (*itr)->fChannelName;
++itr;
}
}
else
{
++itr;
}
}
++numAttempts;
if (numAttempts > maxAttempts)
{
LOG(ERROR) << "could not initialize all channels after " << maxAttempts << " attempts";
// TODO: goto ERROR state;
exit(EXIT_FAILURE);
}
if (numAttempts != 0)
{
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
} while (!uninitializedChannels.empty());
Init();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fInitializingMutex);
fInitializingFinished = true;
fInitializingCondition.notify_one();
ChangeState(DEVICE_READY);
}
void FairMQDevice::Init() void FairMQDevice::Init()
{ {
LOG(INFO) << ">>>>>>> Init <<<<<<<";
for (int i = 0; i < fNumInputs; ++i)
{
fInputAddress.push_back("ipc://default"); // default value, can be overwritten in configuration
fInputMethod.push_back("connect"); // default value, can be overwritten in configuration
fInputSocketType.push_back("sub"); // default value, can be overwritten in configuration
fInputSndBufSize.push_back(10000); // default value, can be overwritten in configuration
fInputRcvBufSize.push_back(10000); // default value, can be overwritten in configuration
fInputRateLogging.push_back(1); // default value, can be overwritten in configuration
}
for (int i = 0; i < fNumOutputs; ++i)
{
fOutputAddress.push_back("ipc://default"); // default value, can be overwritten in configuration
fOutputMethod.push_back("bind"); // default value, can be overwritten in configuration
fOutputSocketType.push_back("pub"); // default value, can be overwritten in configuration
fOutputSndBufSize.push_back(10000); // default value, can be overwritten in configuration
fOutputRcvBufSize.push_back(10000); // default value, can be overwritten in configuration
fOutputRateLogging.push_back(1); // default value, can be overwritten in configuration
}
} }
void FairMQDevice::InitInput() bool FairMQDevice::InitChannel(FairMQChannel& ch)
{ {
LOG(INFO) << ">>>>>>> InitInput <<<<<<<"; LOG(DEBUG) << "Initializing channel " << ch.fChannelName << " to <" << ch.fType << "> data";
// initialize the socket
ch.fSocket = fTransportFactory->CreateSocket(ch.fType, ch.fChannelName, 1);
// set high water marks
ch.fSocket->SetOption("snd-hwm", &(ch.fSndBufSize), sizeof(ch.fSndBufSize));
ch.fSocket->SetOption("rcv-hwm", &(ch.fRcvBufSize), sizeof(ch.fRcvBufSize));
for (int i = 0; i < fNumInputs; ++i) // TODO: make it work with ipc
if (ch.fMethod == "bind")
{ {
FairMQSocket* socket = fTransportFactory->CreateSocket(fInputSocketType.at(i), i, fNumIoThreads); // number of attempts when choosing a random port
int maxAttempts = 1000;
int numAttempts = 0;
socket->SetOption("snd-hwm", &fInputSndBufSize.at(i), sizeof(fInputSndBufSize.at(i))); // initialize random generator
socket->SetOption("rcv-hwm", &fInputRcvBufSize.at(i), sizeof(fInputRcvBufSize.at(i))); boost::random::mt19937 gen(getpid());
boost::random::uniform_int_distribution<> randomPort(fPortRangeMin, fPortRangeMax);
fPayloadInputs->push_back(socket); LOG(DEBUG) << "Binding channel " << ch.fChannelName << " on " << ch.fAddress;
}
}
void FairMQDevice::InitOutput() // try to bind to the saved port. In case of failure, try random one.
{ if (!ch.fSocket->Bind(ch.fAddress))
LOG(INFO) << ">>>>>>> InitOutput <<<<<<<";
for (int i = 0; i < fNumOutputs; ++i)
{
FairMQSocket* socket = fTransportFactory->CreateSocket(fOutputSocketType.at(i), i, fNumIoThreads);
socket->SetOption("snd-hwm", &fOutputSndBufSize.at(i), sizeof(fOutputSndBufSize.at(i)));
socket->SetOption("rcv-hwm", &fOutputRcvBufSize.at(i), sizeof(fOutputRcvBufSize.at(i)));
fPayloadOutputs->push_back(socket);
}
}
void FairMQDevice::Bind()
{
LOG(INFO) << ">>>>>>> binding <<<<<<<";
int maxAttempts = 1000;
int numAttempts = 0;
boost::random::mt19937 gen(getpid());
boost::random::uniform_int_distribution<> randomPort(fPortRangeMin, fPortRangeMax);
for (int i = 0; i < fNumOutputs; ++i)
{
if (fOutputMethod.at(i) == "bind")
{ {
if (!fPayloadOutputs->at(i)->Bind(fOutputAddress.at(i))) LOG(DEBUG) << "Could not bind to configured port, trying random port in range " << fPortRangeMin << "-" << fPortRangeMax;
do {
++numAttempts;
if (numAttempts > maxAttempts)
{
LOG(ERROR) << "could not bind to any port in the given range after " << maxAttempts << " attempts";
return false;
}
size_t pos = ch.fAddress.rfind(":");
stringstream newPort;
newPort << (int)randomPort(gen);
ch.fAddress = ch.fAddress.substr(0, pos + 1) + newPort.str();
LOG(DEBUG) << "Binding channel " << ch.fChannelName << " on " << ch.fAddress;
} while (!ch.fSocket->Bind(ch.fAddress));
}
}
else
{
LOG(DEBUG) << "Connecting channel " << ch.fChannelName << " to " << ch.fAddress;
ch.fSocket->Connect(ch.fAddress);
}
return true;
}
void FairMQDevice::InitTaskWrapper()
{
InitTask();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fInitializingTaskMutex);
fInitializingTaskFinished = true;
fInitializingTaskCondition.notify_one();
ChangeState(READY);
}
void FairMQDevice::InitTask()
{
}
bool SortSocketsByAddress(const FairMQChannel &lhs, const FairMQChannel &rhs)
{
return lhs.fAddress < rhs.fAddress;
}
void FairMQDevice::SortChannel(const string& name, const bool reindex)
{
if (fChannels.find(name) != fChannels.end())
{
sort(fChannels[name].begin(), fChannels[name].end(), SortSocketsByAddress);
if (reindex)
{
for (vector<FairMQChannel>::iterator vi = fChannels[name].begin(); vi != fChannels[name].end(); ++vi)
{ {
LOG(DEBUG) << "binding output #" << i << " on " << fOutputAddress.at(i) << " failed, trying to find port in range"; // set channel name: name + vector index
LOG(INFO) << "port range: " << fPortRangeMin << " - " << fPortRangeMax; stringstream ss;
do { ss << name << "[" << vi - fChannels[name].begin() << "]";
++numAttempts; vi->fChannelName = ss.str();
stringstream ss;
ss << (int)randomPort(gen);
string portString = ss.str();
size_t pos = fOutputAddress.at(i).rfind(":");
fOutputAddress.at(i) = fOutputAddress.at(i).substr(0, pos + 1) + portString;
if (numAttempts > maxAttempts)
{
LOG(ERROR) << "could not bind output " << i << " to any port in the given range";
break;
}
} while (!fPayloadOutputs->at(i)->Bind(fOutputAddress.at(i)));
} }
} }
} }
else
numAttempts = 0;
for (int i = 0; i < fNumInputs; ++i)
{ {
if (fInputMethod.at(i) == "bind") LOG(ERROR) << "Sorting failed: no channel with the name \"" << name << "\".";
{
if (!fPayloadInputs->at(i)->Bind(fInputAddress.at(i)))
{
LOG(DEBUG) << "binding input #" << i << " on " << fInputAddress.at(i) << " failed, trying to find port in range";
LOG(INFO) << "port range: " << fPortRangeMin << " - " << fPortRangeMax;
do {
++numAttempts;
stringstream ss;
ss << (int)randomPort(gen);
string portString = ss.str();
size_t pos = fInputAddress.at(i).rfind(":");
fInputAddress.at(i) = fInputAddress.at(i).substr(0, pos + 1) + portString;
if (numAttempts > maxAttempts)
{
LOG(ERROR) << "could not bind input " << i << " to any port in the given range";
break;
}
} while (!fPayloadInputs->at(i)->Bind(fInputAddress.at(i)));
}
}
} }
} }
void FairMQDevice::Connect() void FairMQDevice::PrintChannel(const string& name)
{ {
LOG(INFO) << ">>>>>>> connecting <<<<<<<"; if (fChannels.find(name) != fChannels.end())
for (int i = 0; i < fNumOutputs; ++i)
{ {
if (fOutputMethod.at(i) == "connect") for (vector<FairMQChannel>::iterator vi = fChannels[name].begin(); vi != fChannels[name].end(); ++vi)
{ {
fPayloadOutputs->at(i)->Connect(fOutputAddress.at(i)); LOG(INFO) << vi->fChannelName << ": "
<< vi->fType << " | "
<< vi->fMethod << " | "
<< vi->fAddress << " | "
<< vi->fSndBufSize << " | "
<< vi->fRcvBufSize << " | "
<< vi->fRateLogging;
} }
} }
else
for (int i = 0; i < fNumInputs; ++i)
{ {
if (fInputMethod.at(i) == "connect") LOG(ERROR) << "Printing failed: no channel with the name \"" << name << "\".";
{
fPayloadInputs->at(i)->Connect(fInputAddress.at(i));
}
} }
} }
void FairMQDevice::RunWrapper()
{
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
Run();
try {
rateLogger.interrupt();
rateLogger.join();
} catch(boost::thread_resource_error& e) {
LOG(ERROR) << e.what();
}
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
}
void FairMQDevice::Run() void FairMQDevice::Run()
{ {
} }
void FairMQDevice::Pause() void FairMQDevice::Pause()
{
while (GetCurrentState() == PAUSED)
{
try
{
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
LOG(DEBUG) << "paused...";
}
catch (boost::thread_interrupted&)
{
LOG(INFO) << "FairMQDevice::Pause() interrupted";
break;
}
}
}
void FairMQDevice::ResetTaskWrapper()
{
ResetTask();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fResetTaskMutex);
fResetTaskFinished = true;
fResetTaskCondition.notify_one();
ChangeState(DEVICE_READY);
}
void FairMQDevice::ResetTask()
{
}
void FairMQDevice::ResetWrapper()
{
Reset();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fResetMutex);
fResetFinished = true;
fResetCondition.notify_one();
ChangeState(IDLE);
}
void FairMQDevice::Reset()
{ {
} }
// Method for setting properties represented as a string. // Method for setting properties represented as a string.
void FairMQDevice::SetProperty(const int key, const string& value, const int slot /*= 0*/) void FairMQDevice::SetProperty(const int key, const string& value)
{ {
switch (key) switch (key)
{ {
case Id: case Id:
fId = value; fId = value;
break; break;
case InputAddress:
fInputAddress.erase(fInputAddress.begin() + slot);
fInputAddress.insert(fInputAddress.begin() + slot, value);
break;
case OutputAddress:
fOutputAddress.erase(fOutputAddress.begin() + slot);
fOutputAddress.insert(fOutputAddress.begin() + slot, value);
break;
case InputMethod:
fInputMethod.erase(fInputMethod.begin() + slot);
fInputMethod.insert(fInputMethod.begin() + slot, value);
break;
case OutputMethod:
fOutputMethod.erase(fOutputMethod.begin() + slot);
fOutputMethod.insert(fOutputMethod.begin() + slot, value);
break;
case InputSocketType:
fInputSocketType.erase(fInputSocketType.begin() + slot);
fInputSocketType.insert(fInputSocketType.begin() + slot, value);
break;
case OutputSocketType:
fOutputSocketType.erase(fOutputSocketType.begin() + slot);
fOutputSocketType.insert(fOutputSocketType.begin() + slot, value);
break;
default: default:
FairMQConfigurable::SetProperty(key, value, slot); FairMQConfigurable::SetProperty(key, value);
break; break;
} }
} }
// Method for setting properties represented as an integer. // Method for setting properties represented as an integer.
void FairMQDevice::SetProperty(const int key, const int value, const int slot /*= 0*/) void FairMQDevice::SetProperty(const int key, const int value)
{ {
switch (key) switch (key)
{ {
case NumIoThreads: case NumIoThreads:
fNumIoThreads = value; fNumIoThreads = value;
break; break;
case NumInputs: case MaxInitializationTime:
fNumInputs = value; fMaxInitializationTime = value;
break;
case NumOutputs:
fNumOutputs = value;
break; break;
case PortRangeMin: case PortRangeMin:
fPortRangeMin = value; fPortRangeMin = value;
@ -260,95 +340,41 @@ void FairMQDevice::SetProperty(const int key, const int value, const int slot /*
case LogIntervalInMs: case LogIntervalInMs:
fLogIntervalInMs = value; fLogIntervalInMs = value;
break; break;
case InputSndBufSize:
fInputSndBufSize.erase(fInputSndBufSize.begin() + slot);
fInputSndBufSize.insert(fInputSndBufSize.begin() + slot, value);
break;
case InputRcvBufSize:
fInputRcvBufSize.erase(fInputRcvBufSize.begin() + slot);
fInputRcvBufSize.insert(fInputRcvBufSize.begin() + slot, value);
break;
case InputRateLogging:
case LogInputRate: // keep this for backwards compatibility for a while
fInputRateLogging.erase(fInputRateLogging.begin() + slot);
fInputRateLogging.insert(fInputRateLogging.begin() + slot, value);
break;
case OutputSndBufSize:
fOutputSndBufSize.erase(fOutputSndBufSize.begin() + slot);
fOutputSndBufSize.insert(fOutputSndBufSize.begin() + slot, value);
break;
case OutputRcvBufSize:
fOutputRcvBufSize.erase(fOutputRcvBufSize.begin() + slot);
fOutputRcvBufSize.insert(fOutputRcvBufSize.begin() + slot, value);
break;
case OutputRateLogging:
case LogOutputRate: // keep this for backwards compatibility for a while
fOutputRateLogging.erase(fOutputRateLogging.begin() + slot);
fOutputRateLogging.insert(fOutputRateLogging.begin() + slot, value);
break;
default: default:
FairMQConfigurable::SetProperty(key, value, slot); FairMQConfigurable::SetProperty(key, value);
break; break;
} }
} }
// Method for getting properties represented as an string. // Method for getting properties represented as an string.
string FairMQDevice::GetProperty(const int key, const string& default_ /*= ""*/, const int slot /*= 0*/) string FairMQDevice::GetProperty(const int key, const string& default_ /*= ""*/)
{ {
switch (key) switch (key)
{ {
case Id: case Id:
return fId; return fId;
case InputAddress:
return fInputAddress.at(slot);
case OutputAddress:
return fOutputAddress.at(slot);
case InputMethod:
return fInputMethod.at(slot);
case OutputMethod:
return fOutputMethod.at(slot);
case InputSocketType:
return fInputSocketType.at(slot);
case OutputSocketType:
return fOutputSocketType.at(slot);
default: default:
return FairMQConfigurable::GetProperty(key, default_, slot); return FairMQConfigurable::GetProperty(key, default_);
} }
} }
// Method for getting properties represented as an integer. // Method for getting properties represented as an integer.
int FairMQDevice::GetProperty(const int key, const int default_ /*= 0*/, const int slot /*= 0*/) int FairMQDevice::GetProperty(const int key, const int default_ /*= 0*/)
{ {
switch (key) switch (key)
{ {
case NumIoThreads: case NumIoThreads:
return fNumIoThreads; return fNumIoThreads;
case NumInputs: case MaxInitializationTime:
return fNumInputs; return fMaxInitializationTime;
case NumOutputs:
return fNumOutputs;
case PortRangeMin: case PortRangeMin:
return fPortRangeMin; return fPortRangeMin;
case PortRangeMax: case PortRangeMax:
return fPortRangeMax; return fPortRangeMax;
case LogIntervalInMs: case LogIntervalInMs:
return fLogIntervalInMs; return fLogIntervalInMs;
case InputSndBufSize:
return fInputSndBufSize.at(slot);
case InputRcvBufSize:
return fInputRcvBufSize.at(slot);
case InputRateLogging:
case LogInputRate: // keep this for backwards compatibility for a while
return fInputRateLogging.at(slot);
case OutputSndBufSize:
return fOutputSndBufSize.at(slot);
case OutputRcvBufSize:
return fOutputRcvBufSize.at(slot);
case OutputRateLogging:
case LogOutputRate: // keep this for backwards compatibility for a while
return fOutputRateLogging.at(slot);
default: default:
return FairMQConfigurable::GetProperty(key, default_, slot); return FairMQConfigurable::GetProperty(key, default_);
} }
} }
@ -364,67 +390,55 @@ void FairMQDevice::LogSocketRates()
timestamp_t msSinceLastLog; timestamp_t msSinceLastLog;
int numFilteredInputs = 0; int numFilteredSockets = 0;
int numFilteredOutputs = 0; vector<FairMQSocket*> filteredSockets;
vector<FairMQSocket*> filteredInputs; vector<string> filteredChannelNames;
vector<FairMQSocket*> filteredOutputs;
// iterate over the channels map
for (map< string,vector<FairMQChannel> >::iterator mi = fChannels.begin(); mi != fChannels.end(); ++mi)
{
// iterate over the channels vector
for (vector<FairMQChannel>::iterator vi = (mi->second).begin(); vi != (mi->second).end(); ++vi)
{
if (vi->fRateLogging == 1)
{
filteredSockets.push_back(vi->fSocket);
stringstream ss;
ss << mi->first << "[" << vi - (mi->second).begin() << "]";
filteredChannelNames.push_back(ss.str());
++numFilteredSockets;
}
}
}
vector<unsigned long> bytesIn(numFilteredSockets);
vector<unsigned long> msgIn(numFilteredSockets);
vector<unsigned long> bytesOut(numFilteredSockets);
vector<unsigned long> msgOut(numFilteredSockets);
vector<unsigned long> bytesInNew(numFilteredSockets);
vector<unsigned long> msgInNew(numFilteredSockets);
vector<unsigned long> bytesOutNew(numFilteredSockets);
vector<unsigned long> msgOutNew(numFilteredSockets);
vector<double> mbPerSecIn(numFilteredSockets);
vector<double> msgPerSecIn(numFilteredSockets);
vector<double> mbPerSecOut(numFilteredSockets);
vector<double> msgPerSecOut(numFilteredSockets);
int i = 0; int i = 0;
for (vector<FairMQSocket*>::iterator itr = fPayloadInputs->begin(); itr != fPayloadInputs->end(); itr++) for (vector<FairMQSocket*>::iterator itr = filteredSockets.begin(); itr != filteredSockets.end(); ++itr)
{
if (fInputRateLogging.at(i) > 0)
{
filteredInputs.push_back((*itr));
++numFilteredInputs;
}
++i;
}
i = 0;
for (vector<FairMQSocket*>::iterator itr = fPayloadOutputs->begin(); itr != fPayloadOutputs->end(); itr++)
{
if (fOutputRateLogging.at(i) > 0)
{
filteredOutputs.push_back((*itr));
++numFilteredOutputs;
}
++i;
}
vector<unsigned long> bytesIn(numFilteredInputs);
vector<unsigned long> msgIn(numFilteredInputs);
vector<unsigned long> bytesOut(numFilteredOutputs);
vector<unsigned long> msgOut(numFilteredOutputs);
vector<unsigned long> bytesInNew(numFilteredInputs);
vector<unsigned long> msgInNew(numFilteredInputs);
vector<unsigned long> bytesOutNew(numFilteredOutputs);
vector<unsigned long> msgOutNew(numFilteredOutputs);
vector<double> mbPerSecIn(numFilteredInputs);
vector<double> msgPerSecIn(numFilteredInputs);
vector<double> mbPerSecOut(numFilteredOutputs);
vector<double> msgPerSecOut(numFilteredOutputs);
i = 0;
for (vector<FairMQSocket*>::iterator itr = filteredInputs.begin(); itr != filteredInputs.end(); itr++)
{ {
bytesIn.at(i) = (*itr)->GetBytesRx(); bytesIn.at(i) = (*itr)->GetBytesRx();
msgIn.at(i) = (*itr)->GetMessagesRx();
++i;
}
i = 0;
for (vector<FairMQSocket*>::iterator itr = filteredOutputs.begin(); itr != filteredOutputs.end(); itr++)
{
bytesOut.at(i) = (*itr)->GetBytesTx(); bytesOut.at(i) = (*itr)->GetBytesTx();
msgIn.at(i) = (*itr)->GetMessagesRx();
msgOut.at(i) = (*itr)->GetMessagesTx(); msgOut.at(i) = (*itr)->GetMessagesTx();
++i; ++i;
} }
t0 = get_timestamp(); t0 = get_timestamp();
while (fState == RUNNING) while (GetCurrentState() == RUNNING)
{ {
try try
{ {
@ -434,32 +448,27 @@ void FairMQDevice::LogSocketRates()
i = 0; i = 0;
for (vector<FairMQSocket*>::iterator itr = filteredInputs.begin(); itr != filteredInputs.end(); itr++) for (vector<FairMQSocket*>::iterator itr = filteredSockets.begin(); itr != filteredSockets.end(); itr++)
{ {
bytesInNew.at(i) = (*itr)->GetBytesRx(); bytesInNew.at(i) = (*itr)->GetBytesRx();
mbPerSecIn.at(i) = ((double)(bytesInNew.at(i) - bytesIn.at(i)) / (1024. * 1024.)) / (double)msSinceLastLog * 1000.; mbPerSecIn.at(i) = ((double)(bytesInNew.at(i) - bytesIn.at(i)) / (1024. * 1024.)) / (double)msSinceLastLog * 1000.;
bytesIn.at(i) = bytesInNew.at(i); bytesIn.at(i) = bytesInNew.at(i);
msgInNew.at(i) = (*itr)->GetMessagesRx(); msgInNew.at(i) = (*itr)->GetMessagesRx();
msgPerSecIn.at(i) = (double)(msgInNew.at(i) - msgIn.at(i)) / (double)msSinceLastLog * 1000.; msgPerSecIn.at(i) = (double)(msgInNew.at(i) - msgIn.at(i)) / (double)msSinceLastLog * 1000.;
msgIn.at(i) = msgInNew.at(i); msgIn.at(i) = msgInNew.at(i);
LOG(DEBUG) << "#" << fId << "." << (*itr)->GetId() << ": " << msgPerSecIn.at(i) << " msg/s, " << mbPerSecIn.at(i) << " MB/s";
++i;
}
i = 0;
for (vector<FairMQSocket*>::iterator itr = filteredOutputs.begin(); itr != filteredOutputs.end(); itr++)
{
bytesOutNew.at(i) = (*itr)->GetBytesTx(); bytesOutNew.at(i) = (*itr)->GetBytesTx();
mbPerSecOut.at(i) = ((double)(bytesOutNew.at(i) - bytesOut.at(i)) / (1024. * 1024.)) / (double)msSinceLastLog * 1000.; mbPerSecOut.at(i) = ((double)(bytesOutNew.at(i) - bytesOut.at(i)) / (1024. * 1024.)) / (double)msSinceLastLog * 1000.;
bytesOut.at(i) = bytesOutNew.at(i); bytesOut.at(i) = bytesOutNew.at(i);
msgOutNew.at(i) = (*itr)->GetMessagesTx(); msgOutNew.at(i) = (*itr)->GetMessagesTx();
msgPerSecOut.at(i) = (double)(msgOutNew.at(i) - msgOut.at(i)) / (double)msSinceLastLog * 1000.; msgPerSecOut.at(i) = (double)(msgOutNew.at(i) - msgOut.at(i)) / (double)msSinceLastLog * 1000.;
msgOut.at(i) = msgOutNew.at(i); msgOut.at(i) = msgOutNew.at(i);
LOG(DEBUG) << "#" << fId << "." << (*itr)->GetId() << ": " << msgPerSecOut.at(i) << " msg/s, " << mbPerSecOut.at(i) << " MB/s"; LOG(DEBUG) << filteredChannelNames.at(i) << ": "
<< "in: " << msgPerSecIn.at(i) << " msg (" << mbPerSecIn.at(i) << " MB), "
<< "out: " << msgPerSecOut.at(i) << " msg (" << mbPerSecOut.at(i) << " MB)";
++i; ++i;
} }
@ -469,59 +478,57 @@ void FairMQDevice::LogSocketRates()
} }
catch (boost::thread_interrupted&) catch (boost::thread_interrupted&)
{ {
LOG(INFO) << "FairMQDevice::LogSocketRates() interrupted"; // LOG(DEBUG) << "FairMQDevice::LogSocketRates() interrupted";
break; break;
} }
} }
LOG(INFO) << ">>>>>>> stopping FairMQDevice::LogSocketRates() <<<<<<<"; // LOG(DEBUG) << "FairMQDevice::LogSocketRates() stopping";
}
void FairMQDevice::SendCommand(const string& command)
{
FairMQMessage* cmd = fTransportFactory->CreateMessage(command.size());
memcpy(cmd->GetData(), command.c_str(), command.size());
fCommandSocket->Send(cmd, 0);
} }
void FairMQDevice::Shutdown() void FairMQDevice::Shutdown()
{ {
LOG(INFO) << ">>>>>>> closing inputs <<<<<<<"; LOG(DEBUG) << "Closing sockets...";
for (vector<FairMQSocket*>::iterator itr = fPayloadInputs->begin(); itr != fPayloadInputs->end(); itr++)
// iterate over the channels map
for (map< string,vector<FairMQChannel> >::iterator mi = fChannels.begin(); mi != fChannels.end(); ++mi)
{ {
(*itr)->Close(); // iterate over the channels vector
for (vector<FairMQChannel>::iterator vi = (mi->second).begin(); vi != (mi->second).end(); ++vi)
{
vi->fSocket->Close();
}
} }
LOG(INFO) << ">>>>>>> closing outputs <<<<<<<"; fCommandSocket->Close();
for (vector<FairMQSocket*>::iterator itr = fPayloadOutputs->begin(); itr != fPayloadOutputs->end(); itr++)
{ LOG(DEBUG) << "Closed all sockets!";
(*itr)->Close();
}
} }
void FairMQDevice::Terminate() void FairMQDevice::Terminate()
{ {
// Termination signal has to be sent only once to any socket. // Termination signal has to be sent only once to any socket.
// Find available socket and send termination signal to it. fCommandSocket->Terminate();
if (fPayloadInputs->size() > 0)
{
fPayloadInputs->at(0)->Terminate();
}
else if (fPayloadOutputs->size() > 0)
{
fPayloadOutputs->at(0)->Terminate();
}
else
{
LOG(ERROR) << "No sockets available to terminate.";
}
} }
FairMQDevice::~FairMQDevice() FairMQDevice::~FairMQDevice()
{ {
for (vector<FairMQSocket*>::iterator itr = fPayloadInputs->begin(); itr != fPayloadInputs->end(); itr++) // iterate over the channels map
for (map< string,vector<FairMQChannel> >::iterator mi = fChannels.begin(); mi != fChannels.end(); ++mi)
{ {
delete (*itr); // iterate over the channels vector
for (vector<FairMQChannel>::iterator vi = (mi->second).begin(); vi != (mi->second).end(); ++vi)
{
delete vi->fSocket;
}
} }
for (vector<FairMQSocket*>::iterator itr = fPayloadOutputs->begin(); itr != fPayloadOutputs->end(); itr++) delete fCommandSocket;
{
delete (*itr);
}
delete fPayloadInputs;
delete fPayloadOutputs;
} }

View File

@ -18,11 +18,13 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <map>
#include "FairMQConfigurable.h" #include "FairMQConfigurable.h"
#include "FairMQStateMachine.h" #include "FairMQStateMachine.h"
#include "FairMQTransportFactory.h" #include "FairMQTransportFactory.h"
#include "FairMQSocket.h" #include "FairMQSocket.h"
#include "FairMQChannel.h"
class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
{ {
@ -30,25 +32,10 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
enum enum
{ {
Id = FairMQConfigurable::Last, Id = FairMQConfigurable::Last,
MaxInitializationTime,
NumIoThreads, NumIoThreads,
NumInputs,
NumOutputs,
PortRangeMin, PortRangeMin,
PortRangeMax, PortRangeMax,
InputAddress,
InputMethod,
InputSocketType,
InputSndBufSize,
InputRcvBufSize,
InputRateLogging,
OutputAddress,
OutputMethod,
OutputSocketType,
OutputSndBufSize,
OutputRcvBufSize,
OutputRateLogging,
LogInputRate, // keep this for backwards compatibility for a while
LogOutputRate, // keep this for backwards compatibility for a while
LogIntervalInMs, LogIntervalInMs,
Last Last
}; };
@ -57,57 +44,59 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
virtual void LogSocketRates(); virtual void LogSocketRates();
virtual void SetProperty(const int key, const std::string& value, const int slot = 0); void SortChannel(const std::string& name, const bool reindex = true);
virtual std::string GetProperty(const int key, const std::string& default_ = "", const int slot = 0); void PrintChannel(const std::string& name);
virtual void SetProperty(const int key, const int value, const int slot = 0);
virtual int GetProperty(const int key, const int default_ = 0, const int slot = 0); virtual void SetProperty(const int key, const std::string& value);
virtual std::string GetProperty(const int key, const std::string& default_ = "");
virtual void SetProperty(const int key, const int value);
virtual int GetProperty(const int key, const int default_ = 0);
virtual void SetTransport(FairMQTransportFactory* factory); virtual void SetTransport(FairMQTransportFactory* factory);
virtual ~FairMQDevice(); virtual ~FairMQDevice();
std::map< std::string,std::vector<FairMQChannel> > fChannels;
protected: protected:
std::string fId; std::string fId;
int fNumIoThreads; int fMaxInitializationTime;
int fNumInputs; int fNumIoThreads;
int fNumOutputs;
int fPortRangeMin; int fPortRangeMin;
int fPortRangeMax; int fPortRangeMax;
std::vector<std::string> fInputAddress;
std::vector<std::string> fInputMethod;
std::vector<std::string> fInputSocketType;
std::vector<int> fInputSndBufSize;
std::vector<int> fInputRcvBufSize;
std::vector<int> fInputRateLogging;
std::vector<std::string> fOutputAddress;
std::vector<std::string> fOutputMethod;
std::vector<std::string> fOutputSocketType;
std::vector<int> fOutputSndBufSize;
std::vector<int> fOutputRcvBufSize;
std::vector<int> fOutputRateLogging;
std::vector<FairMQSocket*>* fPayloadInputs;
std::vector<FairMQSocket*>* fPayloadOutputs;
int fLogIntervalInMs; int fLogIntervalInMs;
FairMQSocket* fCommandSocket;
FairMQTransportFactory* fTransportFactory; FairMQTransportFactory* fTransportFactory;
void InitWrapper();
virtual void Init(); virtual void Init();
virtual void Run();
virtual void Pause();
virtual void Shutdown();
virtual void InitOutput();
virtual void InitInput();
virtual void Bind();
virtual void Connect();
virtual void Terminate(); void InitTaskWrapper();
virtual void InitTask();
void RunWrapper();
virtual void Run();
virtual void Pause();
void ResetTaskWrapper();
virtual void ResetTask();
void ResetWrapper();
virtual void Reset();
virtual void Shutdown();
void Terminate();
void SendCommand(const std::string& command);
bool InitChannel(FairMQChannel&);
private: private:
/// Copy Constructor /// Copy Constructor

View File

@ -18,20 +18,23 @@
int FairMQLogger::fMinLogLevel = FairMQLogger::DEBUG; int FairMQLogger::fMinLogLevel = FairMQLogger::DEBUG;
FairMQLogger::FairMQLogger() FairMQLogger::FairMQLogger()
: os(), fLogLevel(DEBUG) : os()
, fLogLevel(DEBUG)
{ {
} }
FairMQLogger::~FairMQLogger() FairMQLogger::~FairMQLogger()
{ {
if(fLogLevel>=FairMQLogger::fMinLogLevel && fLogLevel<FairMQLogger::NOLOG) if (fLogLevel >= FairMQLogger::fMinLogLevel && fLogLevel < FairMQLogger::NOLOG)
{
std::cout << os.str() << std::endl; std::cout << os.str() << std::endl;
}
} }
std::ostringstream& FairMQLogger::Log(int type) std::ostringstream& FairMQLogger::Log(int type)
{ {
std::string type_str; std::string type_str;
fLogLevel=type; fLogLevel = type;
switch (type) switch (type)
{ {
case DEBUG : case DEBUG :

View File

@ -40,9 +40,9 @@ class FairMQLogger
virtual ~FairMQLogger(); virtual ~FairMQLogger();
std::ostringstream& Log(int type); std::ostringstream& Log(int type);
static void SetLogLevel(int loglevel) static void SetLogLevel(int logLevel)
{ {
FairMQLogger::fMinLogLevel = loglevel; FairMQLogger::fMinLogLevel = logLevel;
} }
private: private:

View File

@ -16,6 +16,7 @@
#define FAIRMQSOCKET_H_ #define FAIRMQSOCKET_H_
#include <string> #include <string>
#include "FairMQMessage.h" #include "FairMQMessage.h"
class FairMQSocket class FairMQSocket
@ -36,10 +37,10 @@ class FairMQSocket
virtual bool Bind(const std::string& address) = 0; virtual bool Bind(const std::string& address) = 0;
virtual void Connect(const std::string& address) = 0; virtual void Connect(const std::string& address) = 0;
virtual int Send(FairMQMessage* msg, const std::string& flag="") = 0; virtual int Send(FairMQMessage* msg, const std::string& flag = "") = 0;
virtual int Send(FairMQMessage* msg, const int flags) = 0; virtual int Send(FairMQMessage* msg, const int flags = 0) = 0;
virtual int Receive(FairMQMessage* msg, const std::string& flag="") = 0; virtual int Receive(FairMQMessage* msg, const std::string& flag = "") = 0;
virtual int Receive(FairMQMessage* msg, const int flags) = 0; virtual int Receive(FairMQMessage* msg, const int flags = 0) = 0;
virtual void* GetSocket() = 0; virtual void* GetSocket() = 0;
virtual int GetSocket(int nothing) = 0; virtual int GetSocket(int nothing) = 0;

View File

@ -16,7 +16,13 @@
#include "FairMQLogger.h" #include "FairMQLogger.h"
FairMQStateMachine::FairMQStateMachine() FairMQStateMachine::FairMQStateMachine()
: fRunningFinished(false) : fInitializingFinished(false)
, fInitializingCondition()
, fInitializingMutex()
, fInitializingTaskFinished(false)
, fInitializingTaskCondition()
, fInitializingTaskMutex()
, fRunningFinished(false)
, fRunningCondition() , fRunningCondition()
, fRunningMutex() , fRunningMutex()
{ {
@ -39,20 +45,17 @@ bool FairMQStateMachine::ChangeState(int event)
{ {
switch (event) switch (event)
{ {
case INIT: case INIT_DEVICE:
process_event(FairMQFSM::INIT()); process_event(FairMQFSM::INIT_DEVICE());
return true; return true;
case SETOUTPUT: case DEVICE_READY:
process_event(FairMQFSM::SETOUTPUT()); process_event(FairMQFSM::DEVICE_READY());
return true; return true;
case SETINPUT: case INIT_TASK:
process_event(FairMQFSM::SETINPUT()); process_event(FairMQFSM::INIT_TASK());
return true; return true;
case BIND: case READY:
process_event(FairMQFSM::BIND()); process_event(FairMQFSM::READY());
return true;
case CONNECT:
process_event(FairMQFSM::CONNECT());
return true; return true;
case RUN: case RUN:
process_event(FairMQFSM::RUN()); process_event(FairMQFSM::RUN());
@ -60,15 +63,27 @@ bool FairMQStateMachine::ChangeState(int event)
case PAUSE: case PAUSE:
process_event(FairMQFSM::PAUSE()); process_event(FairMQFSM::PAUSE());
return true; return true;
case RESUME:
process_event(FairMQFSM::RESUME());
return true;
case STOP: case STOP:
process_event(FairMQFSM::STOP()); process_event(FairMQFSM::STOP());
return true; return true;
case RESET_DEVICE:
process_event(FairMQFSM::RESET_DEVICE());
return true;
case RESET_TASK:
process_event(FairMQFSM::RESET_TASK());
return true;
case IDLE:
process_event(FairMQFSM::IDLE());
return true;
case END: case END:
process_event(FairMQFSM::END()); process_event(FairMQFSM::END());
return true; return true;
default: default:
LOG(ERROR) << "Requested unsupported state: " << event; LOG(ERROR) << "Requested unsupported state: " << event << std::endl
LOG(ERROR) << "Supported are: INIT, SETOUTPUT, SETINPUT, BIND, CONNECT, RUN, PAUSE, STOP, END"; << "Supported are: INIT_DEVICE, INIT_TASK, RUN, PAUSE, RESUME, STOP, RESET_TASK, RESET_DEVICE, END";
return false; return false;
} }
} }
@ -80,25 +95,21 @@ bool FairMQStateMachine::ChangeState(int event)
bool FairMQStateMachine::ChangeState(std::string event) bool FairMQStateMachine::ChangeState(std::string event)
{ {
if (event == "INIT") if (event == "INIT_DEVICE")
{ {
return ChangeState(INIT); return ChangeState(INIT_DEVICE);
} }
else if (event == "SETOUTPUT") if (event == "DEVICE_READY")
{ {
return ChangeState(SETOUTPUT); return ChangeState(DEVICE_READY);
} }
else if (event == "SETINPUT") if (event == "INIT_TASK")
{ {
return ChangeState(SETINPUT); return ChangeState(INIT_TASK);
} }
else if (event == "BIND") if (event == "READY")
{ {
return ChangeState(BIND); return ChangeState(READY);
}
else if (event == "CONNECT")
{
return ChangeState(CONNECT);
} }
else if (event == "RUN") else if (event == "RUN")
{ {
@ -108,18 +119,146 @@ bool FairMQStateMachine::ChangeState(std::string event)
{ {
return ChangeState(PAUSE); return ChangeState(PAUSE);
} }
else if (event == "RESUME")
{
return ChangeState(RESUME);
}
else if (event == "STOP") else if (event == "STOP")
{ {
return ChangeState(STOP); return ChangeState(STOP);
} }
if (event == "RESET_DEVICE")
{
return ChangeState(RESET_DEVICE);
}
if (event == "RESET_TASK")
{
return ChangeState(RESET_TASK);
}
else if (event == "IDLE")
{
return ChangeState(IDLE);
}
else if (event == "END") else if (event == "END")
{ {
return ChangeState(END); return ChangeState(END);
} }
else else
{ {
LOG(ERROR) << "Requested unsupported state: " << event; LOG(ERROR) << "Requested unsupported state: " << event << std::endl
LOG(ERROR) << "Supported are: INIT, SETOUTPUT, SETINPUT, BIND, CONNECT, RUN, PAUSE, STOP, END"; << "Supported are: INIT_DEVICE, INIT_TASK, RUN, PAUSE, RESUME, STOP, RESET_TASK, RESET_DEVICE, END";
return false; return false;
} }
} }
void FairMQStateMachine::WaitForEndOfState(int event)
{
switch (event)
{
case INIT_DEVICE:
{
boost::unique_lock<boost::mutex> lock(fInitializingMutex);
while (!fInitializingFinished)
{
fInitializingCondition.wait(lock);
}
break;
}
case INIT_TASK:
{
boost::unique_lock<boost::mutex> initTaskLock(fInitializingTaskMutex);
while (!fInitializingTaskFinished)
{
fInitializingTaskCondition.wait(initTaskLock);
}
break;
}
case RUN:
{
boost::unique_lock<boost::mutex> runLock(fRunningMutex);
while (!fRunningFinished)
{
fRunningCondition.wait(runLock);
}
break;
}
case RESET_TASK:
{
boost::unique_lock<boost::mutex> runLock(fResetTaskMutex);
while (!fResetTaskFinished)
{
fResetTaskCondition.wait(runLock);
}
break;
}
case RESET_DEVICE:
{
boost::unique_lock<boost::mutex> runLock(fResetMutex);
while (!fResetFinished)
{
fResetCondition.wait(runLock);
}
break;
}
default:
LOG(ERROR) << "Requested state is either synchronous or does not exist.";
break;
}
}
void FairMQStateMachine::WaitForEndOfState(std::string event)
{
if (event == "INIT_DEVICE")
{
return WaitForEndOfState(INIT_DEVICE);
}
if (event == "DEVICE_READY")
{
return WaitForEndOfState(DEVICE_READY);
}
if (event == "INIT_TASK")
{
return WaitForEndOfState(INIT_TASK);
}
if (event == "READY")
{
return WaitForEndOfState(READY);
}
else if (event == "RUN")
{
return WaitForEndOfState(RUN);
}
else if (event == "PAUSE")
{
return WaitForEndOfState(PAUSE);
}
else if (event == "RESUME")
{
return WaitForEndOfState(RESUME);
}
else if (event == "STOP")
{
return WaitForEndOfState(STOP);
}
if (event == "RESET_DEVICE")
{
return WaitForEndOfState(RESET_DEVICE);
}
if (event == "RESET_TASK")
{
return WaitForEndOfState(RESET_TASK);
}
else if (event == "IDLE")
{
return WaitForEndOfState(IDLE);
}
else if (event == "END")
{
return WaitForEndOfState(END);
}
else
{
LOG(ERROR) << "Requested unsupported state: " << event << std::endl
<< "Supported are: INIT_DEVICE, INIT_TASK, RUN, PAUSE, RESUME, STOP, RESET_TASK, RESET_DEVICE, END";
}
}

View File

@ -15,7 +15,7 @@
#ifndef FAIRMQSTATEMACHINE_H_ #ifndef FAIRMQSTATEMACHINE_H_
#define FAIRMQSTATEMACHINE_H_ #define FAIRMQSTATEMACHINE_H_
#define FAIRMQ_INTERFACE_VERSION 1 #define FAIRMQ_INTERFACE_VERSION 2
#include <string> #include <string>
@ -28,8 +28,6 @@
#include <boost/msm/back/state_machine.hpp> #include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp> #include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp> #include <boost/msm/front/functor_row.hpp>
#include <boost/msm/front/euml/common.hpp>
#include <boost/msm/front/euml/operator.hpp>
#include "FairMQLogger.h" #include "FairMQLogger.h"
@ -39,199 +37,328 @@ namespace msmf = boost::msm::front;
namespace FairMQFSM namespace FairMQFSM
{ {
// defining events for the boost MSM state machine
struct INIT {};
struct SETOUTPUT {};
struct SETINPUT {};
struct BIND {};
struct CONNECT {};
struct PAUSE {};
struct RUN {};
struct STOP {};
struct END {};
// defining the boost MSM state machine // defining events for the boost MSM state machine
struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_> struct INIT_DEVICE {};
struct DEVICE_READY {};
struct INIT_TASK {};
struct READY {};
struct RUN {};
struct PAUSE {};
struct RESUME {};
struct STOP {};
struct RESET_TASK {};
struct RESET_DEVICE {};
struct IDLE {};
struct END {};
// defining the boost MSM state machine
struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
{
FairMQFSM_()
: fState()
, fStateThread()
, fTerminateStateThread()
{}
// Destructor
virtual ~FairMQFSM_() {};
template <class Event, class FSM>
void on_entry(Event const&, FSM&)
{ {
FairMQFSM_() LOG(STATE) << "Entering FairMQ state machine";
: fState() fState = IDLE;
, fRunningStateThread() }
{}
// Destructor template <class Event, class FSM>
virtual ~FairMQFSM_() {}; void on_exit(Event const&, FSM&)
{
LOG(STATE) << "Exiting FairMQ state machine";
}
template <class Event, class FSM> // The list of FSM states
void on_entry(Event const&, FSM&) struct IDLE_FSM : public msm::front::state<> {};
struct INITIALIZING_DEVICE_FSM : public msm::front::state<> {};
struct DEVICE_READY_FSM : public msm::front::state<> {};
struct INITIALIZING_TASK_FSM : public msm::front::state<> {};
struct READY_FSM : public msm::front::state<> {};
struct RUNNING_FSM : public msm::front::state<> {};
struct PAUSED_FSM : public msm::front::state<> {};
struct RESETTING_TASK_FSM : public msm::front::state<> {};
struct RESETTING_DEVICE_FSM : public msm::front::state<> {};
struct EXITING_FSM : public msm::front::state<> {};
// Define initial state
typedef IDLE_FSM initial_state;
// Actions
struct IdleFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering FairMQ state machine"; fsm.fState = IDLE;
fState = IDLE;
} }
template <class Event, class FSM>
void on_exit(Event const&, FSM&)
{
LOG(STATE) << "Exiting FairMQ state machine";
}
// The list of FSM states
struct IDLE_FSM : public msm::front::state<> {};
struct INITIALIZING_FSM : public msm::front::state<> {};
struct SETTINGOUTPUT_FSM : public msm::front::state<> {};
struct SETTINGINPUT_FSM : public msm::front::state<> {};
struct BINDING_FSM : public msm::front::state<> {};
struct CONNECTING_FSM : public msm::front::state<> {};
struct WAITING_FSM : public msm::front::state<> {};
struct RUNNING_FSM : public msm::front::state<> {};
// Define initial state
typedef IDLE_FSM initial_state;
// Actions
struct TestFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM&, SourceState&, TargetState&)
{
// LOG(STATE) << "Transition from " << typeid(SourceState).name() << " to " << typeid(TargetState).name() << " with event:" << typeid(EVT).name();
}
};
struct InitFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = INITIALIZING;
fsm.Init();
}
};
struct SetOutputFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = SETTINGOUTPUT;
fsm.InitOutput();
}
};
struct SetInputFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = SETTINGINPUT;
fsm.InitInput();
}
};
struct BindFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = BINDING;
fsm.Bind();
}
};
struct ConnectFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = CONNECTING;
fsm.Connect();
}
};
struct RunFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = RUNNING;
fsm.fRunningStateThread = boost::thread(boost::bind(&FairMQFSM_::Run, &fsm));
}
};
struct StopFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = IDLE;
fsm.Terminate();
fsm.fRunningStateThread.join();
}
};
struct PauseFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = WAITING;
fsm.fRunningStateThread.join();
fsm.Pause();
}
};
// actions to be overwritten by derived classes
virtual void Init() {}
virtual void Run() {}
virtual void Pause() {}
virtual void Shutdown() {}
virtual void InitOutput() {}
virtual void InitInput() {}
virtual void Bind() {}
virtual void Connect() {}
virtual void Terminate() {} // Termination method called during StopFct action.
// Transition table for FairMQFMS
struct transition_table : mpl::vector<
// Start Event Next Action Guard
// +------------------+----------+------------------+-------------+---------+
msmf::Row<IDLE_FSM, INIT, INITIALIZING_FSM, InitFct, msmf::none>,
msmf::Row<IDLE_FSM, END, msmf::none, TestFct, msmf::none>, // this is an invalid transition...
msmf::Row<INITIALIZING_FSM, SETOUTPUT, SETTINGOUTPUT_FSM, SetOutputFct, msmf::none>,
msmf::Row<SETTINGOUTPUT_FSM, SETINPUT, SETTINGINPUT_FSM, SetInputFct, msmf::none>,
msmf::Row<SETTINGINPUT_FSM, BIND, BINDING_FSM, BindFct, msmf::none>,
msmf::Row<BINDING_FSM, CONNECT, CONNECTING_FSM, ConnectFct, msmf::none>,
msmf::Row<CONNECTING_FSM, PAUSE, WAITING_FSM, PauseFct, msmf::none>,
msmf::Row<CONNECTING_FSM, RUN, RUNNING_FSM, RunFct, msmf::none>,
msmf::Row<WAITING_FSM, RUN, RUNNING_FSM, RunFct, msmf::none>,
msmf::Row<WAITING_FSM, STOP, IDLE_FSM, StopFct, msmf::none>,
msmf::Row<RUNNING_FSM, PAUSE, WAITING_FSM, PauseFct, msmf::none>,
msmf::Row<RUNNING_FSM, STOP, IDLE_FSM, StopFct, msmf::none> >
{
};
// Replaces the default no-transition response.
template <class FSM, class Event>
void no_transition(Event const& e, FSM&, int state)
{
LOG(STATE) << "no transition from state " << state << " on event " << typeid(e).name();
}
// this is to run certain functions (e.g. Run()) as separate task
boost::thread fRunningStateThread;
// backward compatibility to FairMQStateMachine
enum State
{
IDLE,
INITIALIZING,
SETTINGOUTPUT,
SETTINGINPUT,
BINDING,
CONNECTING,
WAITING,
RUNNING
};
State fState;
}; };
typedef msm::back::state_machine<FairMQFSM_> FairMQFSM;
} struct InitDeviceFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
LOG(STATE) << "Entering INITIALIZING DEVICE state";
fsm.fState = INITIALIZING_DEVICE;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::InitWrapper, &fsm));
}
};
struct DeviceReadyFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = DEVICE_READY;
}
};
struct InitTaskFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
LOG(STATE) << "Entering INITIALIZING TASK state";
fsm.fState = INITIALIZING_TASK;
fsm.InitTaskWrapper();
// fsm.fInitializingTaskThread = boost::thread(boost::bind(&FairMQFSM_::InitTaskWrapper, &fsm));
}
};
struct ReadyFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = READY;
}
};
struct RunFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
LOG(STATE) << "Entering RUNNING state";
fsm.fState = RUNNING;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::RunWrapper, &fsm));
}
};
struct PauseFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
LOG(STATE) << "Entering PAUSED state";
fsm.fState = PAUSED;
fsm.SendCommand("pause");
fsm.fStateThread.join();
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::Pause, &fsm));
}
};
struct ResumeFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = RUNNING;
fsm.fStateThread.interrupt();
fsm.fStateThread.join();
LOG(STATE) << "Entering RUNNING state";
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::RunWrapper, &fsm));
}
};
struct StopFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = IDLE;
// fsm.SendCommand("stop");
fsm.fStateThread.join();
}
};
struct ResetTaskFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
LOG(STATE) << "Entering RESETTING TASK state";
fsm.fState = RESETTING_TASK;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::ResetTaskWrapper, &fsm));
}
};
struct ResetDeviceFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
LOG(STATE) << "Entering RESETTING DEVICE state";
fsm.fState = RESETTING_DEVICE;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::ResetWrapper, &fsm));
}
};
struct ExitingFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = EXITING;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::Terminate, &fsm));
fsm.Shutdown();
fsm.fStateThread.join();
}
};
struct ExitingRunFct
{
template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{
fsm.fState = EXITING;
fsm.fTerminateStateThread = boost::thread(boost::bind(&FairMQFSM_::Terminate, &fsm));
fsm.fStateThread.join();
fsm.Shutdown();
fsm.fTerminateStateThread.join();
}
};
// actions to be overwritten by derived classes
virtual void InitWrapper() {}
virtual void Init() {}
virtual void InitTaskWrapper() {}
virtual void InitTask() {}
virtual void RunWrapper() {}
virtual void Run() {}
virtual void Pause() {}
virtual void ResetWrapper() {}
virtual void Reset() {}
virtual void ResetTaskWrapper() {}
virtual void ResetTask() {}
virtual void Shutdown() {}
virtual void Terminate() {} // Termination method called during StopFct action.
virtual void SendCommand(const std::string& command) {} // Method to send commands.
// Transition table for FairMQFMS
struct transition_table : mpl::vector<
// Start Event Next Action Guard
// +-------------------------+-------------+------------------------+---------------+---------+
msmf::Row<IDLE_FSM, INIT_DEVICE, INITIALIZING_DEVICE_FSM, InitDeviceFct, msmf::none>,
msmf::Row<INITIALIZING_DEVICE_FSM, DEVICE_READY, DEVICE_READY_FSM, DeviceReadyFct, msmf::none>,
msmf::Row<DEVICE_READY_FSM, INIT_TASK, INITIALIZING_TASK_FSM, InitTaskFct, msmf::none>,
msmf::Row<INITIALIZING_TASK_FSM, READY, READY_FSM, ReadyFct, msmf::none>,
msmf::Row<READY_FSM, RUN, RUNNING_FSM, RunFct, msmf::none>,
msmf::Row<RUNNING_FSM, PAUSE, PAUSED_FSM, PauseFct, msmf::none>,
msmf::Row<PAUSED_FSM, RESUME, RUNNING_FSM, ResumeFct, msmf::none>,
msmf::Row<RUNNING_FSM, STOP, READY_FSM, StopFct, msmf::none>,
msmf::Row<READY_FSM, RESET_TASK, RESETTING_TASK_FSM, ResetTaskFct, msmf::none>,
msmf::Row<RESETTING_TASK_FSM, DEVICE_READY, DEVICE_READY_FSM, DeviceReadyFct, msmf::none>,
msmf::Row<DEVICE_READY_FSM, RESET_DEVICE, RESETTING_DEVICE_FSM, ResetDeviceFct, msmf::none>,
msmf::Row<RESETTING_DEVICE_FSM, IDLE, IDLE_FSM, IdleFct, msmf::none>,
msmf::Row<RUNNING_FSM, END, EXITING_FSM, ExitingRunFct, msmf::none>, // temporary
msmf::Row<IDLE_FSM, END, EXITING_FSM, ExitingFct, msmf::none> >
{};
// Replaces the default no-transition response.
template <class FSM, class Event>
void no_transition(Event const& e, FSM&, int state)
{
LOG(STATE) << "no transition from state " << GetStateName(state) << " on event " << typeid(e).name();
}
// this is to run certain functions in a separate thread
boost::thread fStateThread;
boost::thread fTerminateStateThread;
// backward compatibility to FairMQStateMachine
enum State
{
IDLE,
INITIALIZING_DEVICE,
DEVICE_READY,
INITIALIZING_TASK,
READY,
RUNNING,
PAUSED,
RESETTING_TASK,
RESETTING_DEVICE,
EXITING
};
std::string GetStateName(int state)
{
switch(state)
{
case IDLE:
return "IDLE";
case INITIALIZING_DEVICE:
return "INITIALIZING_DEVICE";
case DEVICE_READY:
return "DEVICE_READY";
case INITIALIZING_TASK:
return "INITIALIZING_TASK";
case READY:
return "READY";
case RUNNING:
return "RUNNING";
case PAUSED:
return "PAUSED";
case RESETTING_TASK:
return "RESETTING_TASK";
case RESETTING_DEVICE:
return "RESETTING_DEVICE";
case EXITING:
return "EXITING";
default:
return "something went wrong...";
}
}
int GetCurrentState()
{
return fState;
}
private:
State fState;
};
typedef msm::back::state_machine<FairMQFSM_> FairMQFSM;
} // namespace FairMQFSM
class FairMQStateMachine : public FairMQFSM::FairMQFSM class FairMQStateMachine : public FairMQFSM::FairMQFSM
{ {
public: public:
enum Event enum Event
{ {
INIT, INIT_DEVICE,
SETOUTPUT, DEVICE_READY,
SETINPUT, INIT_TASK,
BIND, READY,
CONNECT,
PAUSE,
RUN, RUN,
PAUSE,
RESUME,
STOP, STOP,
RESET_TASK,
RESET_DEVICE,
IDLE,
END END
}; };
FairMQStateMachine(); FairMQStateMachine();
@ -242,10 +369,29 @@ class FairMQStateMachine : public FairMQFSM::FairMQFSM
bool ChangeState(int event); bool ChangeState(int event);
bool ChangeState(std::string event); bool ChangeState(std::string event);
// condition variable to notify parent thread about end of running state. void WaitForEndOfState(int state);
void WaitForEndOfState(std::string state);
// condition variables to notify parent thread about end of state.
bool fInitializingFinished;
boost::condition_variable fInitializingCondition;
boost::mutex fInitializingMutex;
bool fInitializingTaskFinished;
boost::condition_variable fInitializingTaskCondition;
boost::mutex fInitializingTaskMutex;
bool fRunningFinished;
boost::condition_variable fRunningCondition; boost::condition_variable fRunningCondition;
boost::mutex fRunningMutex; boost::mutex fRunningMutex;
bool fRunningFinished;
bool fResetFinished;
boost::condition_variable fResetCondition;
boost::mutex fResetMutex;
bool fResetTaskFinished;
boost::condition_variable fResetTaskCondition;
boost::mutex fResetTaskMutex;
}; };
#endif /* FAIRMQSTATEMACHINE_H_ */ #endif /* FAIRMQSTATEMACHINE_H_ */

View File

@ -18,6 +18,7 @@
#include <string> #include <string>
#include "FairMQMessage.h" #include "FairMQMessage.h"
#include "FairMQChannel.h"
#include "FairMQSocket.h" #include "FairMQSocket.h"
#include "FairMQPoller.h" #include "FairMQPoller.h"
#include "FairMQLogger.h" #include "FairMQLogger.h"
@ -28,8 +29,8 @@ class FairMQTransportFactory
virtual FairMQMessage* CreateMessage() = 0; virtual FairMQMessage* CreateMessage() = 0;
virtual FairMQMessage* CreateMessage(size_t size) = 0; virtual FairMQMessage* CreateMessage(size_t size) = 0;
virtual FairMQMessage* CreateMessage(void* data, size_t size, fairmq_free_fn *ffn = NULL, void* hint = NULL) = 0; virtual FairMQMessage* CreateMessage(void* data, size_t size, fairmq_free_fn *ffn = NULL, void* hint = NULL) = 0;
virtual FairMQSocket* CreateSocket(const std::string& type, int num, int numIoThreads) = 0; virtual FairMQSocket* CreateSocket(const std::string& type, const std::string& name, int numIoThreads) = 0;
virtual FairMQPoller* CreatePoller(const std::vector<FairMQSocket*>& inputs) = 0; virtual FairMQPoller* CreatePoller(const std::vector<FairMQChannel>& channels) = 0;
virtual ~FairMQTransportFactory() {}; virtual ~FairMQTransportFactory() {};
}; };

View File

@ -33,27 +33,19 @@ FairMQBenchmarkSampler::~FairMQBenchmarkSampler()
{ {
} }
void FairMQBenchmarkSampler::Init()
{
FairMQDevice::Init();
}
void FairMQBenchmarkSampler::Run() void FairMQBenchmarkSampler::Run()
{ {
LOG(INFO) << ">>>>>>> Run <<<<<<<";
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
boost::thread resetEventCounter(boost::bind(&FairMQBenchmarkSampler::ResetEventCounter, this)); boost::thread resetEventCounter(boost::bind(&FairMQBenchmarkSampler::ResetEventCounter, this));
void* buffer = operator new[](fEventSize); void* buffer = operator new[](fEventSize);
FairMQMessage* base_msg = fTransportFactory->CreateMessage(buffer, fEventSize); FairMQMessage* baseMsg = fTransportFactory->CreateMessage(buffer, fEventSize);
while (fState == RUNNING) while (GetCurrentState() == RUNNING)
{ {
FairMQMessage* msg = fTransportFactory->CreateMessage(); FairMQMessage* msg = fTransportFactory->CreateMessage();
msg->Copy(base_msg); msg->Copy(baseMsg);
fPayloadOutputs->at(0)->Send(msg); fChannels["data-out"].at(0).Send(msg);
--fEventCounter; --fEventCounter;
@ -65,28 +57,19 @@ void FairMQBenchmarkSampler::Run()
delete msg; delete msg;
} }
delete base_msg; delete baseMsg;
try { try {
rateLogger.interrupt();
rateLogger.join();
resetEventCounter.interrupt(); resetEventCounter.interrupt();
resetEventCounter.join(); resetEventCounter.join();
} catch(boost::thread_resource_error& e) { } catch(boost::thread_resource_error& e) {
LOG(ERROR) << e.what(); LOG(ERROR) << e.what();
} }
FairMQDevice::Shutdown();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }
void FairMQBenchmarkSampler::ResetEventCounter() void FairMQBenchmarkSampler::ResetEventCounter()
{ {
while (true) while (GetCurrentState() == RUNNING)
{ {
try try
{ {
@ -100,61 +83,26 @@ void FairMQBenchmarkSampler::ResetEventCounter()
} }
} }
void FairMQBenchmarkSampler::Log(int intervalInMs) void FairMQBenchmarkSampler::SetProperty(const int key, const string& value)
{
timestamp_t t0;
timestamp_t t1;
unsigned long bytes = fPayloadOutputs->at(0)->GetBytesTx();
unsigned long messages = fPayloadOutputs->at(0)->GetMessagesTx();
unsigned long bytesNew = 0;
unsigned long messagesNew = 0;
double megabytesPerSecond = 0;
double messagesPerSecond = 0;
t0 = get_timestamp();
while (true)
{
boost::this_thread::sleep(boost::posix_time::milliseconds(intervalInMs));
t1 = get_timestamp();
bytesNew = fPayloadOutputs->at(0)->GetBytesTx();
messagesNew = fPayloadOutputs->at(0)->GetMessagesTx();
timestamp_t timeSinceLastLog_ms = (t1 - t0) / 1000.0L;
megabytesPerSecond = ((double)(bytesNew - bytes) / (1024. * 1024.)) / (double)timeSinceLastLog_ms * 1000.;
messagesPerSecond = (double)(messagesNew - messages) / (double)timeSinceLastLog_ms * 1000.;
LOG(DEBUG) << "send " << messagesPerSecond << " msg/s, " << megabytesPerSecond << " MB/s";
bytes = bytesNew;
messages = messagesNew;
t0 = t1;
}
}
void FairMQBenchmarkSampler::SetProperty(const int key, const string& value, const int slot /*= 0*/)
{ {
switch (key) switch (key)
{ {
default: default:
FairMQDevice::SetProperty(key, value, slot); FairMQDevice::SetProperty(key, value);
break; break;
} }
} }
string FairMQBenchmarkSampler::GetProperty(const int key, const string& default_ /*= ""*/, const int slot /*= 0*/) string FairMQBenchmarkSampler::GetProperty(const int key, const string& default_ /*= ""*/)
{ {
switch (key) switch (key)
{ {
default: default:
return FairMQDevice::GetProperty(key, default_, slot); return FairMQDevice::GetProperty(key, default_);
} }
} }
void FairMQBenchmarkSampler::SetProperty(const int key, const int value, const int slot /*= 0*/) void FairMQBenchmarkSampler::SetProperty(const int key, const int value)
{ {
switch (key) switch (key)
{ {
@ -165,12 +113,12 @@ void FairMQBenchmarkSampler::SetProperty(const int key, const int value, const i
fEventRate = value; fEventRate = value;
break; break;
default: default:
FairMQDevice::SetProperty(key, value, slot); FairMQDevice::SetProperty(key, value);
break; break;
} }
} }
int FairMQBenchmarkSampler::GetProperty(const int key, const int default_ /*= 0*/, const int slot /*= 0*/) int FairMQBenchmarkSampler::GetProperty(const int key, const int default_ /*= 0*/)
{ {
switch (key) switch (key)
{ {
@ -179,6 +127,6 @@ int FairMQBenchmarkSampler::GetProperty(const int key, const int default_ /*= 0*
case EventRate: case EventRate:
return fEventRate; return fEventRate;
default: default:
return FairMQDevice::GetProperty(key, default_, slot); return FairMQDevice::GetProperty(key, default_);
} }
} }

View File

@ -29,25 +29,27 @@ class FairMQBenchmarkSampler : public FairMQDevice
enum enum
{ {
InputFile = FairMQDevice::Last, InputFile = FairMQDevice::Last,
EventRate,
EventSize, EventSize,
EventRate,
Last Last
}; };
FairMQBenchmarkSampler(); FairMQBenchmarkSampler();
virtual ~FairMQBenchmarkSampler(); virtual ~FairMQBenchmarkSampler();
void Log(int intervalInMs); void Log(int intervalInMs);
void ResetEventCounter(); void ResetEventCounter();
virtual void SetProperty(const int key, const std::string& value, const int slot = 0);
virtual std::string GetProperty(const int key, const std::string& default_ = "", const int slot = 0); virtual void SetProperty(const int key, const std::string& value);
virtual void SetProperty(const int key, const int value, const int slot = 0); virtual std::string GetProperty(const int key, const std::string& default_ = "");
virtual int GetProperty(const int key, const int default_ = 0, const int slot = 0); virtual void SetProperty(const int key, const int value);
virtual int GetProperty(const int key, const int default_ = 0);
protected: protected:
int fEventSize; int fEventSize;
int fEventRate; int fEventRate;
int fEventCounter; int fEventCounter;
virtual void Init();
virtual void Run(); virtual void Run();
}; };

View File

@ -26,39 +26,17 @@ FairMQBuffer::FairMQBuffer()
void FairMQBuffer::Run() void FairMQBuffer::Run()
{ {
LOG(INFO) << ">>>>>>> Run <<<<<<<"; while (GetCurrentState() == RUNNING)
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
int received = 0;
while (fState == RUNNING)
{ {
FairMQMessage* msg = fTransportFactory->CreateMessage(); FairMQMessage* msg = fTransportFactory->CreateMessage();
received = fPayloadInputs->at(0)->Receive(msg); if (fChannels["data-in"].at(0).Receive(msg) > 0)
if (received > 0)
{ {
fPayloadOutputs->at(0)->Send(msg); fChannels["data-out"].at(0).Send(msg);
received = 0;
} }
delete msg; delete msg;
} }
try {
rateLogger.interrupt();
rateLogger.join();
} catch(boost::thread_resource_error& e) {
LOG(ERROR) << e.what();
}
FairMQDevice::Shutdown();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }
FairMQBuffer::~FairMQBuffer() FairMQBuffer::~FairMQBuffer()

View File

@ -29,30 +29,22 @@ FairMQMerger::~FairMQMerger()
void FairMQMerger::Run() void FairMQMerger::Run()
{ {
LOG(INFO) << ">>>>>>> Run <<<<<<<"; FairMQPoller* poller = fTransportFactory->CreatePoller(fChannels["data-in"]);
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); while (GetCurrentState() == RUNNING)
FairMQPoller* poller = fTransportFactory->CreatePoller(*fPayloadInputs);
int received = 0;
while (fState == RUNNING)
{ {
FairMQMessage* msg = fTransportFactory->CreateMessage(); FairMQMessage* msg = fTransportFactory->CreateMessage();
poller->Poll(100); poller->Poll(100);
for (int i = 0; i < fNumInputs; i++) for (int i = 0; i < fChannels["data-in"].size(); ++i)
{ {
if (poller->CheckInput(i)) if (poller->CheckInput(i))
{ {
received = fPayloadInputs->at(i)->Receive(msg); if (fChannels["data-in"].at(i).Receive(msg))
} {
if (received > 0) fChannels["data-out"].at(0).Send(msg);
{ }
fPayloadOutputs->at(0)->Send(msg);
received = 0;
} }
} }
@ -60,18 +52,4 @@ void FairMQMerger::Run()
} }
delete poller; delete poller;
try {
rateLogger.interrupt();
rateLogger.join();
} catch(boost::thread_resource_error& e) {
LOG(ERROR) << e.what();
}
FairMQDevice::Shutdown();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }

View File

@ -28,37 +28,15 @@ FairMQProxy::~FairMQProxy()
void FairMQProxy::Run() void FairMQProxy::Run()
{ {
LOG(INFO) << ">>>>>>> Run <<<<<<<";
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
FairMQMessage* msg = fTransportFactory->CreateMessage(); FairMQMessage* msg = fTransportFactory->CreateMessage();
int received = 0; while (GetCurrentState() == RUNNING)
while (fState == RUNNING)
{ {
received = fPayloadInputs->at(0)->Receive(msg); if (fChannels["data-in"].at(0).Receive(msg) > 0)
if (received > 0)
{ {
fPayloadOutputs->at(0)->Send(msg); fChannels["data-out"].at(0).Send(msg);
received = 0;
} }
} }
delete msg; delete msg;
try {
rateLogger.interrupt();
rateLogger.join();
} catch(boost::thread_resource_error& e) {
LOG(ERROR) << e.what();
}
FairMQDevice::Shutdown();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }

View File

@ -24,34 +24,14 @@ FairMQSink::FairMQSink()
void FairMQSink::Run() void FairMQSink::Run()
{ {
LOG(INFO) << ">>>>>>> Run <<<<<<<"; while (GetCurrentState() == RUNNING)
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
int received = 0;
while (fState == RUNNING)
{ {
FairMQMessage* msg = fTransportFactory->CreateMessage(); FairMQMessage* msg = fTransportFactory->CreateMessage();
received = fPayloadInputs->at(0)->Receive(msg); fChannels["data-in"].at(0).Receive(msg);
delete msg; delete msg;
} }
try {
rateLogger.interrupt();
rateLogger.join();
} catch(boost::thread_resource_error& e) {
LOG(ERROR) << e.what();
}
FairMQDevice::Shutdown();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }
FairMQSink::~FairMQSink() FairMQSink::~FairMQSink()

View File

@ -28,44 +28,22 @@ FairMQSplitter::~FairMQSplitter()
void FairMQSplitter::Run() void FairMQSplitter::Run()
{ {
LOG(INFO) << ">>>>>>> Run <<<<<<<";
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
int received = 0;
int direction = 0; int direction = 0;
while (fState == RUNNING) while (GetCurrentState() == RUNNING)
{ {
FairMQMessage* msg = fTransportFactory->CreateMessage(); FairMQMessage* msg = fTransportFactory->CreateMessage();
received = fPayloadInputs->at(0)->Receive(msg); if (fChannels["data-in"].at(0).Receive(msg) > 0)
if (received > 0)
{ {
fPayloadOutputs->at(direction)->Send(msg); fChannels["data-out"].at(direction).Send(msg);
direction++; direction++;
if (direction >= fNumOutputs) if (direction >= fChannels["data-out"].size())
{ {
direction = 0; direction = 0;
} }
received = 0;
} }
delete msg; delete msg;
} }
try {
rateLogger.interrupt();
rateLogger.join();
} catch(boost::thread_resource_error& e) {
LOG(ERROR) << e.what();
}
FairMQDevice::Shutdown();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }

View File

@ -21,8 +21,6 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include "FairMQLogger.h" #include "FairMQLogger.h"
/********************************************************************* /*********************************************************************
* -------------- NOTES ----------------------- * -------------- NOTES -----------------------
* All policies must have a default constructor * All policies must have a default constructor
@ -38,87 +36,57 @@
* OutputPolicy::InitOutputFile() * OutputPolicy::InitOutputFile()
**********************************************************************/ **********************************************************************/
#include "FairMQDevice.h" #include "FairMQDevice.h"
template < typename InputPolicy, template <typename InputPolicy, typename OutputPolicy>
typename OutputPolicy> class GenericFileSink : public FairMQDevice, public InputPolicy, public OutputPolicy
class GenericFileSink : public FairMQDevice,
public InputPolicy,
public OutputPolicy
{ {
public: public:
GenericFileSink(): GenericFileSink()
InputPolicy(), : InputPolicy()
OutputPolicy() , OutputPolicy()
{} {}
virtual ~GenericFileSink() virtual ~GenericFileSink()
{} {}
void SetTransport(FairMQTransportFactory* transport) void SetTransport(FairMQTransportFactory* transport)
{ {
FairMQDevice::SetTransport(transport); FairMQDevice::SetTransport(transport);
} }
template <typename... Args> template <typename... Args>
void InitInputContainer(Args... args) void InitInputContainer(Args... args)
{
InputPolicy::InitContainer(std::forward<Args>(args)...);
}
protected:
virtual void Init()
{ {
FairMQDevice::Init(); InputPolicy::InitContainer(std::forward<Args>(args)...);
OutputPolicy::InitOutputFile();
} }
protected: protected:
virtual void InitTask()
{
OutputPolicy::InitOutputFile();
}
virtual void Run() virtual void Run()
{ {
MQLOG(INFO) << ">>>>>>> Run <<<<<<<";
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
int received = 0;
int receivedMsg = 0; int receivedMsg = 0;
while (fState == RUNNING) while (GetCurrentState() == RUNNING)
{ {
FairMQMessage* msg = fTransportFactory->CreateMessage(); FairMQMessage* msg = fTransportFactory->CreateMessage();
received = fPayloadInputs->at(0)->Receive(msg);
if(received>0) if (fChannels["data-in"].at(0).Receive(msg) > 0)
{ {
OutputPolicy::AddToFile(InputPolicy::DeSerializeMsg(msg)); OutputPolicy::AddToFile(InputPolicy::DeSerializeMsg(msg));
receivedMsg++; receivedMsg++;
} }
delete msg; delete msg;
} }
MQLOG(INFO) << "Received " << receivedMsg << " messages!"; MQLOG(INFO) << "Received " << receivedMsg << " messages!";
try
{
rateLogger.interrupt();
rateLogger.join();
}
catch(boost::thread_resource_error& e)
{
MQLOG(ERROR) << e.what();
}
FairMQDevice::Shutdown();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }
}; };
#endif /* GENERICFILESINK_H */ #endif /* GENERICFILESINK_H */

View File

@ -6,9 +6,9 @@
*/ */
template <typename InputPolicy, typename OutputPolicy> template <typename InputPolicy, typename OutputPolicy>
GenericFileSink<InputPolicy, OutputPolicy>::GenericFileSink() : GenericFileSink<InputPolicy, OutputPolicy>::GenericFileSink()
InputPolicy(), : InputPolicy()
OutputPolicy() , OutputPolicy()
{ {
} }
@ -21,17 +21,16 @@ template <typename InputPolicy, typename OutputPolicy>
void GenericFileSink<InputPolicy, OutputPolicy>::SetTransport(FairMQTransportFactory* transport) void GenericFileSink<InputPolicy, OutputPolicy>::SetTransport(FairMQTransportFactory* transport)
{ {
FairMQDevice::SetTransport(transport); FairMQDevice::SetTransport(transport);
//InputPolicy::SetTransport(transport); // InputPolicy::SetTransport(transport);
} }
template <typename InputPolicy, typename OutputPolicy> template <typename InputPolicy, typename OutputPolicy>
void GenericFileSink<InputPolicy, OutputPolicy>::Init() void GenericFileSink<InputPolicy, OutputPolicy>::InitTask()
{ {
FairMQDevice::Init();
InitOutputFile(); InitOutputFile();
//InputPolicy::Init(); // InputPolicy::Init();
//OutputPolicy::Init(); // OutputPolicy::Init();
} }
template <typename InputPolicy, typename OutputPolicy> template <typename InputPolicy, typename OutputPolicy>
@ -43,18 +42,12 @@ void GenericFileSink<InputPolicy, OutputPolicy>::InitOutputFile()
template <typename InputPolicy, typename OutputPolicy> template <typename InputPolicy, typename OutputPolicy>
void GenericFileSink<InputPolicy, OutputPolicy>::Run() void GenericFileSink<InputPolicy, OutputPolicy>::Run()
{ {
MQLOG(INFO) << ">>>>>>> Run <<<<<<<";
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
int received = 0;
int receivedMsg = 0; int receivedMsg = 0;
while (fState == RUNNING) while (GetCurrentState() == RUNNING)
{ {
FairMQMessage* msg = fTransportFactory->CreateMessage(); FairMQMessage* msg = fTransportFactory->CreateMessage();
received = fPayloadInputs->at(0)->Receive(msg); if (fChannels["data-in"].at(0).Receive(msg) > 0)
if(received>0)
{ {
OutputPolicy::AddToFile(InputPolicy::DeSerializeMsg(msg)); OutputPolicy::AddToFile(InputPolicy::DeSerializeMsg(msg));
receivedMsg++; receivedMsg++;
@ -63,20 +56,4 @@ void GenericFileSink<InputPolicy, OutputPolicy>::Run()
} }
MQLOG(INFO) << "Received " << receivedMsg << " messages!"; MQLOG(INFO) << "Received " << receivedMsg << " messages!";
try
{
rateLogger.interrupt();
rateLogger.join();
}
catch(boost::thread_resource_error& e)
{
MQLOG(ERROR) << e.what();
}
FairMQDevice::Shutdown();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }

View File

@ -17,17 +17,12 @@
#include "FairMQPoller.h" #include "FairMQPoller.h"
template < typename MergerPolicy, template <typename MergerPolicy, typename InputPolicy, typename OutputPolicy>
typename InputPolicy, class GenericMerger : public FairMQDevice, public MergerPolicy, public InputPolicy, public OutputPolicy
typename OutputPolicy
>
class GenericMerger : public FairMQDevice,
public MergerPolicy,
public InputPolicy,
public OutputPolicy
{ {
public: public:
GenericMerger() : fBlockingTime(100) GenericMerger()
: fBlockingTime(100)
{} {}
virtual ~GenericMerger() virtual ~GenericMerger()
@ -38,43 +33,34 @@ class GenericMerger : public FairMQDevice,
FairMQDevice::SetTransport(transport); FairMQDevice::SetTransport(transport);
} }
protected: protected:
int fBlockingTime; int fBlockingTime;
virtual void Run() virtual void Run()
{ {
MQLOG(INFO) << ">>>>>>> Run <<<<<<<"; FairMQPoller* poller = fTransportFactory->CreatePoller(fChannels["data-in"]);
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
FairMQPoller* poller = fTransportFactory->CreatePoller(*fPayloadInputs);
int received = 0; int received = 0;
while (fState == RUNNING) while (GetCurrentState() == RUNNING)
{ {
FairMQMessage* msg = fTransportFactory->CreateMessage(); FairMQMessage* msg = fTransportFactory->CreateMessage();
//MergerPolicy:: // MergerPolicy::
poller->Poll(fBlockingTime); poller->Poll(fBlockingTime);
for (int i = 0; i < fNumInputs; i++) for (int i = 0; i < fChannels["datain"].size(); i++)
{ {
if (poller->CheckInput(i)) if (poller->CheckInput(i))
{ {
received = fPayloadInputs->at(i)->Receive(msg); received = fChannels["data-in"].at(i).Receive(msg)
MergerPolicy::Merge(InputPolicy::DeSerializeMsg(msg)); MergerPolicy::Merge(InputPolicy::DeSerializeMsg(msg));
} }
OutputPolicy::SetMessage(msg); OutputPolicy::SetMessage(msg);
if ( received > 0 && MergerPolicy::ReadyToSend() ) if (received > 0 && MergerPolicy::ReadyToSend())
{ {
fPayloadOutputs->at(0)->Send(OutputPolicy::SerializeMsg(MergerPolicy::GetOutputData())); fChannels["data-out"].at(0).Send(OutputPolicy::SerializeMsg(MergerPolicy::GetOutputData()));
received = 0; received = 0;
} }
} }
@ -83,25 +69,8 @@ class GenericMerger : public FairMQDevice,
} }
delete poller; delete poller;
try
{
rateLogger.interrupt();
rateLogger.join();
}
catch(boost::thread_resource_error& e)
{
MQLOG(ERROR) << e.what();
}
FairMQDevice::Shutdown();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }
}; };
#endif /* GENERICMERGER_H */ #endif /* GENERICMERGER_H */

View File

@ -13,11 +13,10 @@
*/ */
#ifndef GENERICPROCESSOR_H #ifndef GENERICPROCESSOR_H
#define GENERICPROCESSOR_H #define GENERICPROCESSOR_H
#include "FairMQDevice.h" #include "FairMQDevice.h"
/********************************************************************* /*********************************************************************
* -------------- NOTES ----------------------- * -------------- NOTES -----------------------
* All policies must have a default constructor * All policies must have a default constructor
@ -41,17 +40,14 @@
* *
**********************************************************************/ **********************************************************************/
template <typename InputPolicy, typename OutputPolicy, typename TaskPolicy>
template < typename InputPolicy, class GenericProcessor : public FairMQDevice, public InputPolicy, public OutputPolicy, public TaskPolicy
typename OutputPolicy,
typename TaskPolicy>
class GenericProcessor: public FairMQDevice,
public InputPolicy,
public OutputPolicy,
public TaskPolicy
{ {
public: public:
GenericProcessor() : InputPolicy(), OutputPolicy(), TaskPolicy() GenericProcessor()
: InputPolicy()
, OutputPolicy()
, TaskPolicy()
{} {}
virtual ~GenericProcessor() virtual ~GenericProcessor()
@ -66,48 +62,45 @@ class GenericProcessor: public FairMQDevice,
} }
template <typename... Args> template <typename... Args>
void InitTask(Args... args) void InitTask(Args... args)
{ {
TaskPolicy::InitTask(std::forward<Args>(args)...); TaskPolicy::InitTask(std::forward<Args>(args)...);
} }
template <typename... Args> template <typename... Args>
void InitInputContainer(Args... args) void InitInputContainer(Args... args)
{ {
InputPolicy::InitContainer(std::forward<Args>(args)...); InputPolicy::InitContainer(std::forward<Args>(args)...);
} }
template <typename... Args> template <typename... Args>
void InitOutputContainer(Args... args) void InitOutputContainer(Args... args)
{ {
OutputPolicy::InitContainer(std::forward<Args>(args)...); OutputPolicy::InitContainer(std::forward<Args>(args)...);
} }
/* /*
* *
// *********************** TODO: implement multipart features // *********************** TODO: implement multipart features
void SendPart() void SendPart()
{ {
fPayloadOutputs->at(0)->Send(OutputPolicy::SerializeMsg(TaskPolicy::GetData()), "snd-more"); fChannels["data-out"].at(0).Send(OutputPolicy::SerializeMsg(TaskPolicy::GetData()), "snd-more");
OutputPolicy::CloseMessage(); OutputPolicy::CloseMessage();
} }
//void SendPart(); // void SendPart();
//bool ReceivePart(); // bool ReceivePart();
bool ReceivePart() bool ReceivePart()
{ {
int64_t more = 0; int64_t more = 0;
size_t more_size = sizeof(more); size_t more_size = sizeof(more);
fPayloadInputs->at(0)->GetOption("rcv-more", &more, &more_size); fChannels["data-in"].at(0).fSocket->GetOption("rcv-more", &more, &more_size);
if(more) if (more)
{ {
InputPolicy::CloseMessage(); InputPolicy::CloseMessage();
//fProcessorTask->GetPayload()->CloseMessage(); // fProcessorTask->GetPayload()->CloseMessage();
fProcessorTask->SetPayload(fTransportFactory->CreateMessage()); fProcessorTask->SetPayload(fTransportFactory->CreateMessage());
return fPayloadInputs->at(0)->Receive(fProcessorTask->GetPayload()); return fChannels["data-in"].at(0).Receive(fProcessorTask->GetPayload());
} }
else else
{ {
@ -117,29 +110,23 @@ class GenericProcessor: public FairMQDevice,
*/ */
protected: protected:
virtual void Init() virtual void InitTask()
{ {
FairMQDevice::Init();
// TODO: implement multipart features // TODO: implement multipart features
//fProcessorTask->InitTask(); // fProcessorTask->InitTask();
//fProcessorTask->SetSendPart(boost::bind(&FairMQProcessor::SendPart, this)); // fProcessorTask->SetSendPart(boost::bind(&FairMQProcessor::SendPart, this));
//fProcessorTask->SetReceivePart(boost::bind(&FairMQProcessor::ReceivePart, this)); // fProcessorTask->SetReceivePart(boost::bind(&FairMQProcessor::ReceivePart, this));
} }
virtual void Run() virtual void Run()
{ {
MQLOG(INFO) << ">>>>>>> Run <<<<<<<";
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
int receivedMsgs = 0; int receivedMsgs = 0;
int sentMsgs = 0; int sentMsgs = 0;
int received = 0;
while ( fState == RUNNING ) while (GetCurrentState() == RUNNING)
{ {
FairMQMessage* msg = fTransportFactory->CreateMessage(); FairMQMessage* msg = fTransportFactory->CreateMessage();
received = fPayloadInputs->at(0)->Receive(msg);
receivedMsgs++; receivedMsgs++;
// InputPolicy::DeSerializeMsg(msg) --> deserialize data of msg and fill output container // InputPolicy::DeSerializeMsg(msg) --> deserialize data of msg and fill output container
@ -149,36 +136,21 @@ class GenericProcessor: public FairMQDevice,
// OutputPolicy::fMessage point to msg // OutputPolicy::fMessage point to msg
OutputPolicy::SetMessage(msg); OutputPolicy::SetMessage(msg);
if (received > 0) if (fChannels["data-in"].at(0).Receive(msg) > 0)
{ {
// TaskPolicy::GetOutputData() --> Get processed output container // TaskPolicy::GetOutputData() --> Get processed output container
// OutputPolicy::message(...) --> Serialize output container and fill fMessage // OutputPolicy::message(...) --> Serialize output container and fill fMessage
fPayloadOutputs->at(0)->Send(OutputPolicy::SerializeMsg(TaskPolicy::GetOutputData())); fChannels["data-out"].at(0).Send(OutputPolicy::SerializeMsg(TaskPolicy::GetOutputData()));
sentMsgs++; sentMsgs++;
received = 0;
} }
if(msg) if (msg)
{
msg->CloseMessage(); msg->CloseMessage();
}
} }
MQLOG(INFO) << "Received " << receivedMsgs << " and sent " << sentMsgs << " messages!"; MQLOG(INFO) << "Received " << receivedMsgs << " and sent " << sentMsgs << " messages!";
try
{
rateLogger.interrupt();
rateLogger.join();
}
catch(boost::thread_resource_error& e)
{
MQLOG(ERROR) << e.what();
}
FairMQDevice::Shutdown();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }
}; };

View File

@ -12,7 +12,6 @@
* Created on November 24, 2014, 3:30 PM * Created on November 24, 2014, 3:30 PM
*/ */
#ifndef GENERICSAMPLER_H #ifndef GENERICSAMPLER_H
#define GENERICSAMPLER_H #define GENERICSAMPLER_H
@ -45,14 +44,10 @@
* *
**********************************************************************/ **********************************************************************/
template <typename SamplerPolicy, template <typename SamplerPolicy, typename OutputPolicy>
typename OutputPolicy> class GenericSampler : public FairMQDevice, public SamplerPolicy, public OutputPolicy
class GenericSampler: public FairMQDevice,
public SamplerPolicy,
public OutputPolicy
{ {
public: public:
enum enum
{ {
InputFile = FairMQDevice::Last, InputFile = FairMQDevice::Last,
@ -63,20 +58,20 @@ class GenericSampler: public FairMQDevice,
GenericSampler(); GenericSampler();
virtual ~GenericSampler(); virtual ~GenericSampler();
virtual void SetTransport(FairMQTransportFactory* factory); virtual void SetTransport(FairMQTransportFactory* factory);
void ResetEventCounter(); void ResetEventCounter();
virtual void ListenToCommands();
template <typename... Args> template <typename... Args>
void SetFileProperties(Args&... args) void SetFileProperties(Args&... args)
{ {
SamplerPolicy::SetFileProperties(args...); SamplerPolicy::SetFileProperties(args...);
} }
virtual void SetProperty(const int key, const std::string& value, const int slot = 0); virtual void SetProperty(const int key, const std::string& value);
virtual std::string GetProperty(const int key, const std::string& default_ = "", const int slot = 0); virtual std::string GetProperty(const int key, const std::string& default_ = "");
virtual void SetProperty(const int key, const int value, const int slot = 0); virtual void SetProperty(const int key, const int value);
virtual int GetProperty(const int key, const int default_ = 0, const int slot = 0); virtual int GetProperty(const int key, const int default_ = 0);
/** /**
* Sends the currently available output of the Sampler Task as part of a multipart message * Sends the currently available output of the Sampler Task as part of a multipart message
@ -87,23 +82,22 @@ class GenericSampler: public FairMQDevice,
// temporary disabled // temporary disabled
//void SendPart(); //void SendPart();
void SetContinuous(bool flag) { fContinuous = flag; } void SetContinuous(bool flag);
protected: protected:
virtual void Init(); virtual void InitTask();
virtual void Run(); virtual void Run();
protected: int64_t fNumEvents;
std::string fInputFile; // Filename of a root file containing the simulated digis. int fEventRate;
std::string fParFile; int fEventCounter;
std::string fBranch; // The name of the sub-detector branch to stream the digis from. bool fContinuous;
int64_t fNumEvents; std::string fInputFile; // Filename of a root file containing the simulated digis.
int fEventRate; std::string fParFile;
int fEventCounter; std::string fBranch; // The name of the sub-detector branch to stream the digis from.
bool fContinuous;
}; };
#include "GenericSampler.tpl" #include "GenericSampler.tpl"
#endif /* GENERICSAMPLER_H */ #endif /* GENERICSAMPLER_H */

View File

@ -6,11 +6,14 @@
*/ */
template <typename SamplerPolicy, typename OutputPolicy> template <typename SamplerPolicy, typename OutputPolicy>
GenericSampler<SamplerPolicy,OutputPolicy>::GenericSampler() : GenericSampler<SamplerPolicy,OutputPolicy>::GenericSampler()
fNumEvents(0), : fNumEvents(0)
fEventRate(1), , fEventRate(1)
fEventCounter(0), , fEventCounter(0)
fContinuous(false) , fContinuous(false)
, fInputFile()
, fParFile()
, fBranch()
{ {
} }
@ -23,47 +26,40 @@ template <typename SamplerPolicy, typename OutputPolicy>
void GenericSampler<SamplerPolicy,OutputPolicy>::SetTransport(FairMQTransportFactory* factory) void GenericSampler<SamplerPolicy,OutputPolicy>::SetTransport(FairMQTransportFactory* factory)
{ {
FairMQDevice::SetTransport(factory); FairMQDevice::SetTransport(factory);
//OutputPolicy::SetTransport(factory); // OutputPolicy::SetTransport(factory);
} }
template <typename SamplerPolicy, typename OutputPolicy> template <typename SamplerPolicy, typename OutputPolicy>
void GenericSampler<SamplerPolicy,OutputPolicy>::Init() void GenericSampler<SamplerPolicy,OutputPolicy>::InitTask()
{ {
FairMQDevice::Init(); SamplerPolicy::InitSampler();
SamplerPolicy::InitSampler(); fNumEvents = SamplerPolicy::GetNumberOfEvent();
fNumEvents=SamplerPolicy::GetNumberOfEvent();
} }
template <typename SamplerPolicy, typename OutputPolicy> template <typename SamplerPolicy, typename OutputPolicy>
void GenericSampler<SamplerPolicy,OutputPolicy>::Run() void GenericSampler<SamplerPolicy,OutputPolicy>::Run()
{ {
LOG(INFO) << ">>>>>>> Run <<<<<<<"; // boost::thread resetEventCounter(boost::bind(&GenericSampler::ResetEventCounter, this));
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); int sentMsgs = 0;
// boost::thread resetEventCounter(boost::bind(&GenericSampler::ResetEventCounter, this));
// boost::thread commandListener(boost::bind(&GenericSampler::ListenToCommands, this));
int sentMsgs = 0; boost::timer::auto_cpu_timer timer;
boost::timer::auto_cpu_timer timer; LOG(INFO) << "Number of events to process: " << fNumEvents;
LOG(INFO) << "Number of events to process: " << fNumEvents; do {
for (int64_t eventNr = 0; eventNr < fNumEvents; ++eventNr)
// while ( fState == RUNNING ) {
do
{
for ( int64_t eventNr = 0 ; eventNr < fNumEvents; ++eventNr )
{ {
//fSamplerTask->SetEventIndex(eventNr); //fSamplerTask->SetEventIndex(eventNr);
FairMQMessage* msg = fTransportFactory->CreateMessage(); FairMQMessage* msg = fTransportFactory->CreateMessage();
OutputPolicy::SetMessage(msg); OutputPolicy::SetMessage(msg);
fPayloadOutputs->at(0)->Send(OutputPolicy::SerializeMsg( fChannels["data-out"].at(0).Send(OutputPolicy::SerializeMsg(SamplerPolicy::GetDataBranch(eventNr)));
SamplerPolicy::GetDataBranch(eventNr)));
++sentMsgs; ++sentMsgs;
if(msg) if (msg)
{
msg->CloseMessage(); msg->CloseMessage();
}
// Optional event rate limiting // Optional event rate limiting
// --fEventCounter; // --fEventCounter;
@ -71,154 +67,111 @@ void GenericSampler<SamplerPolicy,OutputPolicy>::Run()
// boost::this_thread::sleep(boost::posix_time::milliseconds(1)); // boost::this_thread::sleep(boost::posix_time::milliseconds(1));
// } // }
if( fState != RUNNING ) { break; } if (GetCurrentState() != RUNNING)
{
break;
}
} }
} }
while ( fState == RUNNING && fContinuous ); while ( GetCurrentState() == RUNNING && fContinuous );
// } boost::timer::cpu_times const elapsed_time(timer.elapsed());
LOG(INFO) << "Sent everything in:\n" << boost::timer::format(elapsed_time, 2);
boost::timer::cpu_times const elapsed_time(timer.elapsed()); LOG(INFO) << "Sent " << sentMsgs << " messages!";
LOG(INFO) << "Sent everything in:\n" << boost::timer::format(elapsed_time, 2);
LOG(INFO) << "Sent " << sentMsgs << " messages!";
try {
rateLogger.interrupt();
rateLogger.join();
// resetEventCounter.interrupt();
// resetEventCounter.join();
// commandListener.interrupt();
// commandListener.join();
}
catch (boost::thread_resource_error &e) {
LOG(ERROR) << e.what();
}
FairMQDevice::Shutdown();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }
/* /*
template <typename SamplerPolicy, typename OutputPolicy> template <typename SamplerPolicy, typename OutputPolicy>
void GenericSampler<SamplerPolicy,OutputPolicy>::SendPart() void GenericSampler<SamplerPolicy,OutputPolicy>::SendPart()
{ {
fPayloadOutputs->at(0)->Send(OutputPolicy::GetMessage(), "snd-more"); fChannels["data-out"].at(0).Send(OutputPolicy::GetMessage(), "snd-more");
OutputPolicy::CloseMessage(); OutputPolicy::CloseMessage();
} }
*/ */
template <typename SamplerPolicy, typename OutputPolicy>
void GenericSampler<SamplerPolicy,OutputPolicy>::SetContinuous(bool flag)
{
fContinuous = flag;
}
template <typename SamplerPolicy, typename OutputPolicy> template <typename SamplerPolicy, typename OutputPolicy>
void GenericSampler<SamplerPolicy,OutputPolicy>::ResetEventCounter() void GenericSampler<SamplerPolicy,OutputPolicy>::ResetEventCounter()
{ {
while (true) while (GetCurrentState() == RUNNING)
{
try
{ {
fEventCounter = fEventRate / 100; try
boost::this_thread::sleep(boost::posix_time::milliseconds(10)); {
fEventCounter = fEventRate / 100;
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
}
catch (boost::thread_interrupted &)
{
LOG(DEBUG) << "resetEventCounter interrupted";
break;
}
} }
catch (boost::thread_interrupted &) LOG(DEBUG) << ">>>>>>> stopping resetEventCounter <<<<<<<";
}
template <typename SamplerPolicy, typename OutputPolicy>
void GenericSampler<SamplerPolicy,OutputPolicy>::SetProperty(const int key, const std::string& value)
{
switch (key)
{ {
LOG(DEBUG) << "resetEventCounter interrupted"; case InputFile:
break; fInputFile = value;
break;
case ParFile:
fParFile = value;
break;
case Branch:
fBranch = value;
break;
default:
FairMQDevice::SetProperty(key, value);
break;
} }
}
LOG(DEBUG) << ">>>>>>> stopping resetEventCounter <<<<<<<";
} }
template <typename SamplerPolicy, typename OutputPolicy> template <typename SamplerPolicy, typename OutputPolicy>
void GenericSampler<SamplerPolicy,OutputPolicy>::ListenToCommands() std::string GenericSampler<SamplerPolicy,OutputPolicy>::GetProperty(const int key, const std::string& default_/*= ""*/)
{ {
LOG(INFO) << ">>>>>>> ListenToCommands <<<<<<<"; switch (key)
{
int received = 0; case InputFile:
return fInputFile;
while (true) { case ParFile:
try { return fParFile;
FairMQMessage *msg = fTransportFactory->CreateMessage(); case Branch:
return fBranch;
received = fPayloadInputs->at(0)->Receive(msg); default:
return FairMQDevice::GetProperty(key, default_);
if (received > 0) {
// command handling goes here.
LOG(INFO) << "> received command <";
received = 0;
}
delete msg;
boost::this_thread::interruption_point();
} }
catch (boost::thread_interrupted &) { }
LOG(DEBUG) << "commandListener interrupted";
break; template <typename SamplerPolicy, typename OutputPolicy>
void GenericSampler<SamplerPolicy,OutputPolicy>::SetProperty(const int key, const int value)
{
switch (key)
{
case EventRate:
fEventRate = value;
break;
default:
FairMQDevice::SetProperty(key, value);
break;
} }
}
LOG(DEBUG) << ">>>>>>> stopping commandListener <<<<<<<";
} }
template <typename SamplerPolicy, typename OutputPolicy> template <typename SamplerPolicy, typename OutputPolicy>
void GenericSampler<SamplerPolicy,OutputPolicy>::SetProperty(const int key, const std::string& value, const int slot/*= 0*/) int GenericSampler<SamplerPolicy,OutputPolicy>::GetProperty(const int key, const int default_/*= 0*/)
{ {
switch (key) switch (key)
{ {
case InputFile: case EventRate:
fInputFile = value; return fEventRate;
break; default:
case ParFile: return FairMQDevice::GetProperty(key, default_);
fParFile = value; }
break;
case Branch:
fBranch = value;
break;
default:
FairMQDevice::SetProperty(key, value, slot);
break;
}
}
template <typename SamplerPolicy, typename OutputPolicy>
std::string GenericSampler<SamplerPolicy,OutputPolicy>::GetProperty(const int key, const std::string& default_/*= ""*/, const int slot/*= 0*/)
{
switch (key)
{
case InputFile:
return fInputFile;
case ParFile:
return fParFile;
case Branch:
return fBranch;
default:
return FairMQDevice::GetProperty(key, default_, slot);
}
}
template <typename SamplerPolicy, typename OutputPolicy>
void GenericSampler<SamplerPolicy,OutputPolicy>::SetProperty(const int key, const int value, const int slot/*= 0*/)
{
switch (key)
{
case EventRate:
fEventRate = value;
break;
default:
FairMQDevice::SetProperty(key, value, slot);
break;
}
}
template <typename SamplerPolicy, typename OutputPolicy>
int GenericSampler<SamplerPolicy,OutputPolicy>::GetProperty(const int key, const int default_/*= 0*/, const int slot/*= 0*/)
{
switch (key)
{
case EventRate:
return fEventRate;
default:
return FairMQDevice::GetProperty(key, default_, slot);
}
} }

View File

@ -36,9 +36,7 @@ void FairMQExampleClient::CustomCleanup(void *data, void *hint)
void FairMQExampleClient::Run() void FairMQExampleClient::Run()
{ {
// boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); while (GetCurrentState() == RUNNING)
while (fState == RUNNING)
{ {
boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
@ -49,26 +47,17 @@ void FairMQExampleClient::Run()
LOG(INFO) << "Sending \"" << fText << "\" to server."; LOG(INFO) << "Sending \"" << fText << "\" to server.";
fPayloadOutputs->at(0)->Send(request); fChannels["data"].at(0).Send(request);
fPayloadOutputs->at(0)->Receive(reply); fChannels["data"].at(0).Receive(reply);
LOG(INFO) << "Received reply from server: \"" << string(static_cast<char*>(reply->GetData()), reply->GetSize()) << "\""; LOG(INFO) << "Received reply from server: \"" << string(static_cast<char*>(reply->GetData()), reply->GetSize()) << "\"";
delete reply; delete reply;
} }
// rateLogger.interrupt();
// rateLogger.join();
FairMQDevice::Shutdown();
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }
void FairMQExampleClient::SetProperty(const int key, const string& value, const int slot /*= 0*/) void FairMQExampleClient::SetProperty(const int key, const string& value)
{ {
switch (key) switch (key)
{ {
@ -76,12 +65,12 @@ void FairMQExampleClient::SetProperty(const int key, const string& value, const
fText = value; fText = value;
break; break;
default: default:
FairMQDevice::SetProperty(key, value, slot); FairMQDevice::SetProperty(key, value);
break; break;
} }
} }
string FairMQExampleClient::GetProperty(const int key, const string& default_ /*= ""*/, const int slot /*= 0*/) string FairMQExampleClient::GetProperty(const int key, const string& default_ /*= ""*/)
{ {
switch (key) switch (key)
{ {
@ -89,25 +78,25 @@ string FairMQExampleClient::GetProperty(const int key, const string& default_ /*
return fText; return fText;
break; break;
default: default:
return FairMQDevice::GetProperty(key, default_, slot); return FairMQDevice::GetProperty(key, default_);
} }
} }
void FairMQExampleClient::SetProperty(const int key, const int value, const int slot /*= 0*/) void FairMQExampleClient::SetProperty(const int key, const int value)
{ {
switch (key) switch (key)
{ {
default: default:
FairMQDevice::SetProperty(key, value, slot); FairMQDevice::SetProperty(key, value);
break; break;
} }
} }
int FairMQExampleClient::GetProperty(const int key, const int default_ /*= 0*/, const int slot /*= 0*/) int FairMQExampleClient::GetProperty(const int key, const int default_ /*= 0*/)
{ {
switch (key) switch (key)
{ {
default: default:
return FairMQDevice::GetProperty(key, default_, slot); return FairMQDevice::GetProperty(key, default_);
} }
} }

View File

@ -30,12 +30,12 @@ class FairMQExampleClient : public FairMQDevice
FairMQExampleClient(); FairMQExampleClient();
virtual ~FairMQExampleClient(); virtual ~FairMQExampleClient();
static void CustomCleanup(void *data, void* hint); static void CustomCleanup(void* data, void* hint);
virtual void SetProperty(const int key, const std::string& value, const int slot = 0); virtual void SetProperty(const int key, const std::string& value);
virtual std::string GetProperty(const int key, const std::string& default_ = "", const int slot = 0); virtual std::string GetProperty(const int key, const std::string& default_ = "");
virtual void SetProperty(const int key, const int value, const int slot = 0); virtual void SetProperty(const int key, const int value);
virtual int GetProperty(const int key, const int default_ = 0, const int slot = 0); virtual int GetProperty(const int key, const int default_ = 0);
protected: protected:
std::string fText; std::string fText;

View File

@ -31,15 +31,13 @@ void FairMQExampleServer::CustomCleanup(void *data, void *hint)
void FairMQExampleServer::Run() void FairMQExampleServer::Run()
{ {
// boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); while (GetCurrentState() == RUNNING)
while (fState == RUNNING)
{ {
boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
FairMQMessage* request = fTransportFactory->CreateMessage(); FairMQMessage* request = fTransportFactory->CreateMessage();
fPayloadInputs->at(0)->Receive(request); fChannels["data"].at(0).Receive(request);
LOG(INFO) << "Received request from client: \"" << string(static_cast<char*>(request->GetData()), request->GetSize()) << "\""; LOG(INFO) << "Received request from client: \"" << string(static_cast<char*>(request->GetData()), request->GetSize()) << "\"";
@ -51,17 +49,8 @@ void FairMQExampleServer::Run()
FairMQMessage* reply = fTransportFactory->CreateMessage(const_cast<char*>(text->c_str()), text->length(), CustomCleanup, text); FairMQMessage* reply = fTransportFactory->CreateMessage(const_cast<char*>(text->c_str()), text->length(), CustomCleanup, text);
fPayloadInputs->at(0)->Send(reply); fChannels["data"].at(0).Send(reply);
} }
// rateLogger.interrupt();
// rateLogger.join();
FairMQDevice::Shutdown();
boost::lock_guard<boost::mutex> lock(fRunningMutex);
fRunningFinished = true;
fRunningCondition.notify_one();
} }
FairMQExampleServer::~FairMQExampleServer() FairMQExampleServer::~FairMQExampleServer()

View File

@ -32,12 +32,11 @@ FairMQExampleClient client;
static void s_signal_handler(int signal) static void s_signal_handler(int signal)
{ {
cout << endl << "Caught signal " << signal << endl; LOG(INFO) << "Caught signal " << signal;
client.ChangeState(FairMQExampleClient::STOP);
client.ChangeState(FairMQExampleClient::END); client.ChangeState(FairMQExampleClient::END);
cout << "Shutdown complete. Bye!" << endl; LOG(INFO) << "Shutdown complete.";
exit(1); exit(1);
} }
@ -115,33 +114,31 @@ int main(int argc, char** argv)
client.SetProperty(FairMQExampleClient::Id, "client"); client.SetProperty(FairMQExampleClient::Id, "client");
client.SetProperty(FairMQExampleClient::NumIoThreads, 1); client.SetProperty(FairMQExampleClient::NumIoThreads, 1);
client.SetProperty(FairMQExampleClient::NumInputs, 0);
client.SetProperty(FairMQExampleClient::NumOutputs, 1);
client.ChangeState(FairMQExampleClient::INIT); FairMQChannel requestChannel("req", "connect", "tcp://localhost:5005");
requestChannel.fSndBufSize = 10000;
requestChannel.fRcvBufSize = 10000;
requestChannel.fRateLogging = 1;
client.SetProperty(FairMQExampleClient::OutputSocketType, "req", 0); client.fChannels["data"].push_back(requestChannel);
client.SetProperty(FairMQExampleClient::OutputSndBufSize, 10000, 0);
client.SetProperty(FairMQExampleClient::OutputRcvBufSize, 10000, 0);
client.SetProperty(FairMQExampleClient::OutputMethod, "connect", 0);
client.SetProperty(FairMQExampleClient::OutputAddress, "tcp://localhost:5005", 0);
client.SetProperty(FairMQExampleClient::Text, options.text); client.ChangeState(FairMQExampleClient::INIT_DEVICE);
client.WaitForEndOfState(FairMQExampleClient::INIT_DEVICE);
client.ChangeState(FairMQExampleClient::INIT_TASK);
client.WaitForEndOfState(FairMQExampleClient::INIT_TASK);
client.ChangeState(FairMQExampleClient::SETOUTPUT);
client.ChangeState(FairMQExampleClient::SETINPUT);
client.ChangeState(FairMQExampleClient::BIND);
client.ChangeState(FairMQExampleClient::CONNECT);
client.ChangeState(FairMQExampleClient::RUN); client.ChangeState(FairMQExampleClient::RUN);
client.WaitForEndOfState(FairMQExampleClient::RUN);
// wait until the running thread has finished processing.
boost::unique_lock<boost::mutex> lock(client.fRunningMutex);
while (!client.fRunningFinished)
{
client.fRunningCondition.wait(lock);
}
client.ChangeState(FairMQExampleClient::STOP); client.ChangeState(FairMQExampleClient::STOP);
client.ChangeState(FairMQExampleClient::RESET_TASK);
client.WaitForEndOfState(FairMQExampleClient::RESET_TASK);
client.ChangeState(FairMQExampleClient::RESET_DEVICE);
client.WaitForEndOfState(FairMQExampleClient::RESET_DEVICE);
client.ChangeState(FairMQExampleClient::END); client.ChangeState(FairMQExampleClient::END);
return 0; return 0;

View File

@ -30,12 +30,11 @@ FairMQExampleServer server;
static void s_signal_handler(int signal) static void s_signal_handler(int signal)
{ {
cout << endl << "Caught signal " << signal << endl; LOG(INFO) << "Caught signal " << signal;
server.ChangeState(FairMQExampleServer::STOP);
server.ChangeState(FairMQExampleServer::END); server.ChangeState(FairMQExampleServer::END);
cout << "Shutdown complete. Bye!" << endl; LOG(INFO) << "Caught signal " << signal;
exit(1); exit(1);
} }
@ -65,34 +64,31 @@ int main(int argc, char** argv)
server.SetProperty(FairMQExampleServer::Id, "server"); server.SetProperty(FairMQExampleServer::Id, "server");
server.SetProperty(FairMQExampleServer::NumIoThreads, 1); server.SetProperty(FairMQExampleServer::NumIoThreads, 1);
server.SetProperty(FairMQExampleServer::NumInputs, 1);
server.SetProperty(FairMQExampleServer::NumOutputs, 0);
server.ChangeState(FairMQExampleServer::INIT); FairMQChannel replyChannel("rep", "bind", "tcp://*:5005");
replyChannel.fSndBufSize = 10000;
replyChannel.fRcvBufSize = 10000;
replyChannel.fRateLogging = 1;
server.SetProperty(FairMQExampleServer::InputSocketType, "rep", 0); server.fChannels["data"].push_back(replyChannel);
server.SetProperty(FairMQExampleServer::InputSndBufSize, 10000, 0);
server.SetProperty(FairMQExampleServer::InputRcvBufSize, 10000, 0);
server.SetProperty(FairMQExampleServer::InputMethod, "bind", 0);
server.SetProperty(FairMQExampleServer::InputAddress, "tcp://*:5005", 0);
server.ChangeState(FairMQExampleServer::SETOUTPUT); server.ChangeState(FairMQExampleServer::INIT_DEVICE);
server.ChangeState(FairMQExampleServer::SETINPUT); server.WaitForEndOfState(FairMQExampleServer::INIT_DEVICE);
server.ChangeState(FairMQExampleServer::BIND);
server.ChangeState(FairMQExampleServer::CONNECT);
LOG(INFO) << "Listening for requests!"; server.ChangeState(FairMQExampleServer::INIT_TASK);
server.WaitForEndOfState(FairMQExampleServer::INIT_TASK);
server.ChangeState(FairMQExampleServer::RUN); server.ChangeState(FairMQExampleServer::RUN);
server.WaitForEndOfState(FairMQExampleServer::RUN);
// wait until the running thread has finished processing.
boost::unique_lock<boost::mutex> lock(server.fRunningMutex);
while (!server.fRunningFinished)
{
server.fRunningCondition.wait(lock);
}
server.ChangeState(FairMQExampleServer::STOP); server.ChangeState(FairMQExampleServer::STOP);
server.ChangeState(FairMQExampleServer::RESET_TASK);
server.WaitForEndOfState(FairMQExampleServer::RESET_TASK);
server.ChangeState(FairMQExampleServer::RESET_DEVICE);
server.WaitForEndOfState(FairMQExampleServer::RESET_DEVICE);
server.ChangeState(FairMQExampleServer::END); server.ChangeState(FairMQExampleServer::END);
return 0; return 0;

View File

@ -63,7 +63,7 @@ FairMQMessageNN::FairMQMessageNN(void* data, size_t size, fairmq_free_fn *ffn, v
fSize = size; fSize = size;
fReceiving = false; fReceiving = false;
if(ffn) if (ffn)
{ {
ffn(data, hint); ffn(data, hint);
} }
@ -140,8 +140,7 @@ void FairMQMessageNN::Copy(FairMQMessage* msg)
{ {
if (fMessage) if (fMessage)
{ {
int rc = nn_freemsg(fMessage); if (nn_freemsg(fMessage) < 0)
if (rc < 0)
{ {
LOG(ERROR) << "failed freeing message, reason: " << nn_strerror(errno); LOG(ERROR) << "failed freeing message, reason: " << nn_strerror(errno);
} }
@ -160,8 +159,7 @@ void FairMQMessageNN::Copy(FairMQMessage* msg)
inline void FairMQMessageNN::Clear() inline void FairMQMessageNN::Clear()
{ {
int rc = nn_freemsg(fMessage); if (nn_freemsg(fMessage) < 0)
if (rc < 0)
{ {
LOG(ERROR) << "failed freeing message, reason: " << nn_strerror(errno); LOG(ERROR) << "failed freeing message, reason: " << nn_strerror(errno);
} }

View File

@ -15,32 +15,38 @@
#include <nanomsg/nn.h> #include <nanomsg/nn.h>
#include "FairMQPollerNN.h" #include "FairMQPollerNN.h"
#include "FairMQLogger.h"
using namespace std; using namespace std;
FairMQPollerNN::FairMQPollerNN(const vector<FairMQSocket*>& inputs) FairMQPollerNN::FairMQPollerNN(const vector<FairMQChannel>& channels)
: items() : items()
, fNumItems() , fNumItems()
{ {
fNumItems = inputs.size(); fNumItems = channels.size();
items = new nn_pollfd[fNumItems]; items = new nn_pollfd[fNumItems];
for (int i = 0; i < fNumItems; i++) for (int i = 0; i < fNumItems; ++i)
{ {
items[i].fd = inputs.at(i)->GetSocket(1); items[i].fd = channels.at(i).fSocket->GetSocket(1);
items[i].events = NN_POLLIN; items[i].events = NN_POLLIN;
} }
} }
void FairMQPollerNN::Poll(int timeout) void FairMQPollerNN::Poll(int timeout)
{ {
nn_poll(items, fNumItems, timeout); if (nn_poll(items, fNumItems, timeout) < 0)
{
LOG(ERROR) << "polling failed, reason: " << nn_strerror(errno);
}
} }
bool FairMQPollerNN::CheckInput(int index) bool FairMQPollerNN::CheckInput(int index)
{ {
if (items[index].revents & NN_POLLIN) if (items[index].revents & NN_POLLIN)
{
return true; return true;
}
return false; return false;
} }
@ -48,7 +54,9 @@ bool FairMQPollerNN::CheckInput(int index)
bool FairMQPollerNN::CheckOutput(int index) bool FairMQPollerNN::CheckOutput(int index)
{ {
if (items[index].revents & NN_POLLOUT) if (items[index].revents & NN_POLLOUT)
{
return true; return true;
}
return false; return false;
} }
@ -56,5 +64,7 @@ bool FairMQPollerNN::CheckOutput(int index)
FairMQPollerNN::~FairMQPollerNN() FairMQPollerNN::~FairMQPollerNN()
{ {
if (items != NULL) if (items != NULL)
{
delete[] items; delete[] items;
}
} }

View File

@ -18,12 +18,12 @@
#include <vector> #include <vector>
#include "FairMQPoller.h" #include "FairMQPoller.h"
#include "FairMQSocket.h" #include "FairMQChannel.h"
class FairMQPollerNN : public FairMQPoller class FairMQPollerNN : public FairMQPoller
{ {
public: public:
FairMQPollerNN(const std::vector<FairMQSocket*>& inputs); FairMQPollerNN(const std::vector<FairMQChannel>& channels);
virtual void Poll(int timeout); virtual void Poll(int timeout);
virtual bool CheckInput(int index); virtual bool CheckInput(int index);

View File

@ -20,7 +20,7 @@
using namespace std; using namespace std;
FairMQSocketNN::FairMQSocketNN(const string& type, int num, int numIoThreads) FairMQSocketNN::FairMQSocketNN(const string& type, const std::string& name, int numIoThreads)
: FairMQSocket(0, 0, NN_DONTWAIT) : FairMQSocket(0, 0, NN_DONTWAIT)
, fSocket() , fSocket()
, fId() , fId()
@ -29,9 +29,7 @@ FairMQSocketNN::FairMQSocketNN(const string& type, int num, int numIoThreads)
, fMessagesTx(0) , fMessagesTx(0)
, fMessagesRx(0) , fMessagesRx(0)
{ {
stringstream id; fId = name + "." + type;
id << type << "." << num;
fId = id.str();
if (numIoThreads > 1) if (numIoThreads > 1)
{ {
@ -46,7 +44,7 @@ FairMQSocketNN::FairMQSocketNN(const string& type, int num, int numIoThreads)
fSocket = nn_socket(AF_SP_RAW, GetConstant(type)); fSocket = nn_socket(AF_SP_RAW, GetConstant(type));
if (fSocket == -1) if (fSocket == -1)
{ {
LOG(ERROR) << "failed creating socket #" << fId << ", reason: " << nn_strerror(errno); LOG(ERROR) << "failed creating socket " << fId << ", reason: " << nn_strerror(errno);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -55,7 +53,7 @@ FairMQSocketNN::FairMQSocketNN(const string& type, int num, int numIoThreads)
fSocket = nn_socket(AF_SP, GetConstant(type)); fSocket = nn_socket(AF_SP, GetConstant(type));
if (fSocket == -1) if (fSocket == -1)
{ {
LOG(ERROR) << "failed creating socket #" << fId << ", reason: " << nn_strerror(errno); LOG(ERROR) << "failed creating socket " << fId << ", reason: " << nn_strerror(errno);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (type == "sub") if (type == "sub")
@ -64,7 +62,7 @@ FairMQSocketNN::FairMQSocketNN(const string& type, int num, int numIoThreads)
} }
} }
LOG(INFO) << "created socket #" << fId; LOG(INFO) << "created socket " << fId;
} }
string FairMQSocketNN::GetId() string FairMQSocketNN::GetId()
@ -74,12 +72,12 @@ string FairMQSocketNN::GetId()
bool FairMQSocketNN::Bind(const string& address) bool FairMQSocketNN::Bind(const string& address)
{ {
LOG(INFO) << "bind socket #" << fId << " on " << address; LOG(INFO) << "bind socket " << fId << " on " << address;
int eid = nn_bind(fSocket, address.c_str()); int eid = nn_bind(fSocket, address.c_str());
if (eid < 0) if (eid < 0)
{ {
LOG(ERROR) << "failed binding socket #" << fId << ", reason: " << nn_strerror(errno); LOG(ERROR) << "failed binding socket " << fId << ", reason: " << nn_strerror(errno);
return false; return false;
} }
return true; return true;
@ -87,12 +85,12 @@ bool FairMQSocketNN::Bind(const string& address)
void FairMQSocketNN::Connect(const string& address) void FairMQSocketNN::Connect(const string& address)
{ {
LOG(INFO) << "connect socket #" << fId << " to " << address; LOG(INFO) << "connect socket " << fId << " to " << address;
int eid = nn_connect(fSocket, address.c_str()); int eid = nn_connect(fSocket, address.c_str());
if (eid < 0) if (eid < 0)
{ {
LOG(ERROR) << "failed connecting socket #" << fId << ", reason: " << nn_strerror(errno); LOG(ERROR) << "failed connecting socket " << fId << ", reason: " << nn_strerror(errno);
} }
} }
@ -102,7 +100,7 @@ int FairMQSocketNN::Send(FairMQMessage* msg, const string& flag)
int rc = nn_send(fSocket, &ptr, NN_MSG, 0); int rc = nn_send(fSocket, &ptr, NN_MSG, 0);
if (rc < 0) if (rc < 0)
{ {
LOG(ERROR) << "failed sending on socket #" << fId << ", reason: " << nn_strerror(errno); LOG(ERROR) << "failed sending on socket " << fId << ", reason: " << nn_strerror(errno);
} }
else else
{ {
@ -120,7 +118,7 @@ int FairMQSocketNN::Send(FairMQMessage* msg, const int flags)
int rc = nn_send(fSocket, &ptr, NN_MSG, flags); int rc = nn_send(fSocket, &ptr, NN_MSG, flags);
if (rc < 0) if (rc < 0)
{ {
LOG(ERROR) << "failed sending on socket #" << fId << ", reason: " << nn_strerror(errno); LOG(ERROR) << "failed sending on socket " << fId << ", reason: " << nn_strerror(errno);
} }
else else
{ {
@ -138,7 +136,7 @@ int FairMQSocketNN::Receive(FairMQMessage* msg, const string& flag)
int rc = nn_recv(fSocket, &ptr, NN_MSG, 0); int rc = nn_recv(fSocket, &ptr, NN_MSG, 0);
if (rc < 0) if (rc < 0)
{ {
LOG(ERROR) << "failed receiving on socket #" << fId << ", reason: " << nn_strerror(errno); LOG(ERROR) << "failed receiving on socket " << fId << ", reason: " << nn_strerror(errno);
} }
else else
{ {
@ -157,7 +155,7 @@ int FairMQSocketNN::Receive(FairMQMessage* msg, const int flags)
int rc = nn_recv(fSocket, &ptr, NN_MSG, flags); int rc = nn_recv(fSocket, &ptr, NN_MSG, flags);
if (rc < 0) if (rc < 0)
{ {
LOG(ERROR) << "failed receiving on socket #" << fId << ", reason: " << nn_strerror(errno); LOG(ERROR) << "failed receiving on socket " << fId << ", reason: " << nn_strerror(errno);
} }
else else
{ {

View File

@ -26,17 +26,17 @@
class FairMQSocketNN : public FairMQSocket class FairMQSocketNN : public FairMQSocket
{ {
public: public:
FairMQSocketNN(const std::string& type, int num, int numIoThreads); // numIoThreads is not used in nanomsg. FairMQSocketNN(const std::string& type, const std::string& name, int numIoThreads); // numIoThreads is not used in nanomsg.
virtual std::string GetId(); virtual std::string GetId();
virtual bool Bind(const std::string& address); virtual bool Bind(const std::string& address);
virtual void Connect(const std::string& address); virtual void Connect(const std::string& address);
virtual int Send(FairMQMessage* msg, const std::string& flag=""); virtual int Send(FairMQMessage* msg, const std::string& flag = "");
virtual int Send(FairMQMessage* msg, const int flags); virtual int Send(FairMQMessage* msg, const int flags = 0);
virtual int Receive(FairMQMessage* msg, const std::string& flag=""); virtual int Receive(FairMQMessage* msg, const std::string& flag = "");
virtual int Receive(FairMQMessage* msg, const int flags); virtual int Receive(FairMQMessage* msg, const int flags = 0);
virtual void* GetSocket(); virtual void* GetSocket();
virtual int GetSocket(int nothing); virtual int GetSocket(int nothing);

View File

@ -36,12 +36,12 @@ FairMQMessage* FairMQTransportFactoryNN::CreateMessage(void* data, size_t size,
return new FairMQMessageNN(data, size, ffn, hint); return new FairMQMessageNN(data, size, ffn, hint);
} }
FairMQSocket* FairMQTransportFactoryNN::CreateSocket(const string& type, int num, int numIoThreads) FairMQSocket* FairMQTransportFactoryNN::CreateSocket(const string& type, const std::string& name, int numIoThreads)
{ {
return new FairMQSocketNN(type, num, numIoThreads); return new FairMQSocketNN(type, name, numIoThreads);
} }
FairMQPoller* FairMQTransportFactoryNN::CreatePoller(const vector<FairMQSocket*>& inputs) FairMQPoller* FairMQTransportFactoryNN::CreatePoller(const vector<FairMQChannel>& channels)
{ {
return new FairMQPollerNN(inputs); return new FairMQPollerNN(channels);
} }

View File

@ -30,8 +30,8 @@ class FairMQTransportFactoryNN : public FairMQTransportFactory
virtual FairMQMessage* CreateMessage(); virtual FairMQMessage* CreateMessage();
virtual FairMQMessage* CreateMessage(size_t size); virtual FairMQMessage* CreateMessage(size_t size);
virtual FairMQMessage* CreateMessage(void* data, size_t size, fairmq_free_fn *ffn = NULL, void* hint = NULL); virtual FairMQMessage* CreateMessage(void* data, size_t size, fairmq_free_fn *ffn = NULL, void* hint = NULL);
virtual FairMQSocket* CreateSocket(const std::string& type, int num, int numIoThreads); virtual FairMQSocket* CreateSocket(const std::string& type, const std::string& name, int numIoThreads);
virtual FairMQPoller* CreatePoller(const std::vector<FairMQSocket*>& inputs); virtual FairMQPoller* CreatePoller(const std::vector<FairMQChannel>& channels);
virtual ~FairMQTransportFactoryNN() {}; virtual ~FairMQTransportFactoryNN() {};
}; };

View File

@ -35,7 +35,6 @@ FairMQBinSampler::~FairMQBinSampler()
void FairMQBinSampler::Init() void FairMQBinSampler::Init()
{ {
FairMQDevice::Init();
} }
void FairMQBinSampler::Run() void FairMQBinSampler::Run()

View File

@ -37,7 +37,6 @@ FairMQProtoSampler::~FairMQProtoSampler()
void FairMQProtoSampler::Init() void FairMQProtoSampler::Init()
{ {
FairMQDevice::Init();
} }
void FairMQProtoSampler::Run() void FairMQProtoSampler::Run()

View File

@ -9,7 +9,7 @@
* runBenchmarkSampler.cxx * runBenchmarkSampler.cxx
* *
* @since 2013-04-23 * @since 2013-04-23
* @author D. Klein, A. Rybalchenko * @author: D. Klein, A. Rybalchenko
*/ */
#include <iostream> #include <iostream>
@ -32,12 +32,11 @@ FairMQBenchmarkSampler sampler;
static void s_signal_handler(int signal) static void s_signal_handler(int signal)
{ {
cout << endl << "Caught signal " << signal << endl; LOG(INFO) << "Caught signal " << signal;
sampler.ChangeState(FairMQBenchmarkSampler::STOP);
sampler.ChangeState(FairMQBenchmarkSampler::END); sampler.ChangeState(FairMQBenchmarkSampler::END);
cout << "Shutdown complete. Bye!" << endl; LOG(INFO) << "Shutdown complete";
exit(1); exit(1);
} }
@ -55,7 +54,8 @@ typedef struct DeviceOptions
{ {
DeviceOptions() : DeviceOptions() :
id(), eventSize(0), eventRate(0), ioThreads(0), id(), eventSize(0), eventRate(0), ioThreads(0),
outputSocketType(), outputBufSize(0), outputMethod(), outputAddress() {} outputSocketType(), outputBufSize(0), outputMethod(), outputAddress()
{}
string id; string id;
int eventSize; int eventSize;
@ -80,7 +80,7 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options)
("event-rate", bpo::value<int>()->default_value(0), "Event rate limit in maximum number of events per second") ("event-rate", bpo::value<int>()->default_value(0), "Event rate limit in maximum number of events per second")
("io-threads", bpo::value<int>()->default_value(1), "Number of I/O threads") ("io-threads", bpo::value<int>()->default_value(1), "Number of I/O threads")
("output-socket-type", bpo::value<string>()->required(), "Output socket type: pub/push") ("output-socket-type", bpo::value<string>()->required(), "Output socket type: pub/push")
("output-buff-size", bpo::value<int>()->required(), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("output-buff-size", bpo::value<int>()->default_value(1000), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)")
("output-method", bpo::value<string>()->required(), "Output method: bind/connect") ("output-method", bpo::value<string>()->required(), "Output method: bind/connect")
("output-address", bpo::value<string>()->required(), "Output address, e.g.: \"tcp://*:5555\"") ("output-address", bpo::value<string>()->required(), "Output address, e.g.: \"tcp://*:5555\"")
("help", "Print help messages"); ("help", "Print help messages");
@ -88,7 +88,7 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options)
bpo::variables_map vm; bpo::variables_map vm;
bpo::store(bpo::parse_command_line(_argc, _argv, desc), vm); bpo::store(bpo::parse_command_line(_argc, _argv, desc), vm);
if ( vm.count("help") ) if (vm.count("help"))
{ {
LOG(INFO) << "FairMQ Benchmark Sampler" << endl << desc; LOG(INFO) << "FairMQ Benchmark Sampler" << endl << desc;
return false; return false;
@ -96,28 +96,28 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options)
bpo::notify(vm); bpo::notify(vm);
if ( vm.count("id") ) if (vm.count("id"))
_options->id = vm["id"].as<string>(); _options->id = vm["id"].as<string>();
if ( vm.count("event-size") ) if (vm.count("event-size"))
_options->eventSize = vm["event-size"].as<int>(); _options->eventSize = vm["event-size"].as<int>();
if ( vm.count("event-rate") ) if (vm.count("event-rate"))
_options->eventRate = vm["event-rate"].as<int>(); _options->eventRate = vm["event-rate"].as<int>();
if ( vm.count("io-threads") ) if (vm.count("io-threads"))
_options->ioThreads = vm["io-threads"].as<int>(); _options->ioThreads = vm["io-threads"].as<int>();
if ( vm.count("output-socket-type") ) if (vm.count("output-socket-type"))
_options->outputSocketType = vm["output-socket-type"].as<string>(); _options->outputSocketType = vm["output-socket-type"].as<string>();
if ( vm.count("output-buff-size") ) if (vm.count("output-buff-size"))
_options->outputBufSize = vm["output-buff-size"].as<int>(); _options->outputBufSize = vm["output-buff-size"].as<int>();
if ( vm.count("output-method") ) if (vm.count("output-method"))
_options->outputMethod = vm["output-method"].as<string>(); _options->outputMethod = vm["output-method"].as<string>();
if ( vm.count("output-address") ) if (vm.count("output-address"))
_options->outputAddress = vm["output-address"].as<string>(); _options->outputAddress = vm["output-address"].as<string>();
return true; return true;
@ -151,35 +151,35 @@ int main(int argc, char** argv)
sampler.SetTransport(transportFactory); sampler.SetTransport(transportFactory);
FairMQChannel channel(options.outputSocketType, options.outputMethod, options.outputAddress);
channel.fSndBufSize = options.outputBufSize;
channel.fRcvBufSize = options.outputBufSize;
channel.fRateLogging = 1;
sampler.fChannels["data-out"].push_back(channel);
sampler.SetProperty(FairMQBenchmarkSampler::Id, options.id); sampler.SetProperty(FairMQBenchmarkSampler::Id, options.id);
sampler.SetProperty(FairMQBenchmarkSampler::EventSize, options.eventSize); sampler.SetProperty(FairMQBenchmarkSampler::EventSize, options.eventSize);
sampler.SetProperty(FairMQBenchmarkSampler::EventRate, options.eventRate); sampler.SetProperty(FairMQBenchmarkSampler::EventRate, options.eventRate);
sampler.SetProperty(FairMQBenchmarkSampler::NumIoThreads, options.ioThreads); sampler.SetProperty(FairMQBenchmarkSampler::NumIoThreads, options.ioThreads);
sampler.SetProperty(FairMQBenchmarkSampler::NumInputs, 0); sampler.ChangeState(FairMQBenchmarkSampler::INIT_DEVICE);
sampler.SetProperty(FairMQBenchmarkSampler::NumOutputs, 1); sampler.WaitForEndOfState(FairMQBenchmarkSampler::INIT_DEVICE);
sampler.ChangeState(FairMQBenchmarkSampler::INIT); sampler.ChangeState(FairMQBenchmarkSampler::INIT_TASK);
sampler.WaitForEndOfState(FairMQBenchmarkSampler::INIT_TASK);
sampler.SetProperty(FairMQBenchmarkSampler::OutputSocketType, options.outputSocketType);
sampler.SetProperty(FairMQBenchmarkSampler::OutputSndBufSize, options.outputBufSize);
sampler.SetProperty(FairMQBenchmarkSampler::OutputMethod, options.outputMethod);
sampler.SetProperty(FairMQBenchmarkSampler::OutputAddress, options.outputAddress);
sampler.ChangeState(FairMQBenchmarkSampler::SETOUTPUT);
sampler.ChangeState(FairMQBenchmarkSampler::SETINPUT);
sampler.ChangeState(FairMQBenchmarkSampler::BIND);
sampler.ChangeState(FairMQBenchmarkSampler::CONNECT);
sampler.ChangeState(FairMQBenchmarkSampler::RUN); sampler.ChangeState(FairMQBenchmarkSampler::RUN);
sampler.WaitForEndOfState(FairMQBenchmarkSampler::RUN);
// wait until the running thread has finished processing.
boost::unique_lock<boost::mutex> lock(sampler.fRunningMutex);
while (!sampler.fRunningFinished)
{
sampler.fRunningCondition.wait(lock);
}
sampler.ChangeState(FairMQBenchmarkSampler::STOP); sampler.ChangeState(FairMQBenchmarkSampler::STOP);
sampler.ChangeState(FairMQBenchmarkSampler::RESET_TASK);
sampler.WaitForEndOfState(FairMQBenchmarkSampler::RESET_TASK);
sampler.ChangeState(FairMQBenchmarkSampler::RESET_DEVICE);
sampler.WaitForEndOfState(FairMQBenchmarkSampler::RESET_DEVICE);
sampler.ChangeState(FairMQBenchmarkSampler::END); sampler.ChangeState(FairMQBenchmarkSampler::END);
return 0; return 0;

View File

@ -32,12 +32,11 @@ FairMQBuffer buffer;
static void s_signal_handler(int signal) static void s_signal_handler(int signal)
{ {
cout << endl << "Caught signal " << signal << endl; LOG(INFO) << "Caught signal " << signal;
buffer.ChangeState(FairMQBuffer::STOP);
buffer.ChangeState(FairMQBuffer::END); buffer.ChangeState(FairMQBuffer::END);
cout << "Shutdown complete. Bye!" << endl; LOG(INFO) << "Shutdown complete";
exit(1); exit(1);
} }
@ -81,11 +80,11 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options)
("id", bpo::value<string>(), "Device ID") ("id", bpo::value<string>(), "Device ID")
("io-threads", bpo::value<int>()->default_value(1), "Number of I/O threads") ("io-threads", bpo::value<int>()->default_value(1), "Number of I/O threads")
("input-socket-type", bpo::value<string>()->required(), "Input socket type: sub/pull") ("input-socket-type", bpo::value<string>()->required(), "Input socket type: sub/pull")
("input-buff-size", bpo::value<int>()->required(), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("input-buff-size", bpo::value<int>()->default_value(1000), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)")
("input-method", bpo::value<string>()->required(), "Input method: bind/connect") ("input-method", bpo::value<string>()->required(), "Input method: bind/connect")
("input-address", bpo::value<string>()->required(), "Input address, e.g.: \"tcp://localhost:5555\"") ("input-address", bpo::value<string>()->required(), "Input address, e.g.: \"tcp://localhost:5555\"")
("output-socket-type", bpo::value<string>()->required(), "Output socket type: pub/push") ("output-socket-type", bpo::value<string>()->required(), "Output socket type: pub/push")
("output-buff-size", bpo::value<int>()->required(), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("output-buff-size", bpo::value<int>()->default_value(1000), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)")
("output-method", bpo::value<string>()->required(), "Output method: bind/connect") ("output-method", bpo::value<string>()->required(), "Output method: bind/connect")
("output-address", bpo::value<string>()->required(), "Output address, e.g.: \"tcp://localhost:5555\"") ("output-address", bpo::value<string>()->required(), "Output address, e.g.: \"tcp://localhost:5555\"")
("help", "Print help messages"); ("help", "Print help messages");
@ -160,38 +159,40 @@ int main(int argc, char** argv)
buffer.SetTransport(transportFactory); buffer.SetTransport(transportFactory);
FairMQChannel inputChannel(options.inputSocketType, options.inputMethod, options.inputAddress);
inputChannel.fSndBufSize = options.inputBufSize;
inputChannel.fRcvBufSize = options.inputBufSize;
inputChannel.fRateLogging = 1;
buffer.fChannels["data-in"].push_back(inputChannel);
FairMQChannel outputChannel(options.outputSocketType, options.outputMethod, options.outputAddress);
outputChannel.fSndBufSize = options.outputBufSize;
outputChannel.fRcvBufSize = options.outputBufSize;
outputChannel.fRateLogging = 1;
buffer.fChannels["data-out"].push_back(outputChannel);
buffer.SetProperty(FairMQBuffer::Id, options.id); buffer.SetProperty(FairMQBuffer::Id, options.id);
buffer.SetProperty(FairMQBuffer::NumIoThreads, options.ioThreads); buffer.SetProperty(FairMQBuffer::NumIoThreads, options.ioThreads);
buffer.SetProperty(FairMQBuffer::NumInputs, 1); buffer.ChangeState(FairMQBuffer::INIT_DEVICE);
buffer.SetProperty(FairMQBuffer::NumOutputs, 1); buffer.WaitForEndOfState(FairMQBuffer::INIT_DEVICE);
buffer.ChangeState(FairMQBuffer::INIT); buffer.ChangeState(FairMQBuffer::INIT_TASK);
buffer.WaitForEndOfState(FairMQBuffer::INIT_TASK);
buffer.SetProperty(FairMQBuffer::InputSocketType, options.inputSocketType);
buffer.SetProperty(FairMQBuffer::InputRcvBufSize, options.inputBufSize);
buffer.SetProperty(FairMQBuffer::InputMethod, options.inputMethod);
buffer.SetProperty(FairMQBuffer::InputAddress, options.inputAddress);
buffer.SetProperty(FairMQBuffer::OutputSocketType, options.outputSocketType);
buffer.SetProperty(FairMQBuffer::OutputSndBufSize, options.outputBufSize);
buffer.SetProperty(FairMQBuffer::OutputMethod, options.outputMethod);
buffer.SetProperty(FairMQBuffer::OutputAddress, options.outputAddress);
buffer.ChangeState(FairMQBuffer::SETOUTPUT);
buffer.ChangeState(FairMQBuffer::SETINPUT);
buffer.ChangeState(FairMQBuffer::BIND);
buffer.ChangeState(FairMQBuffer::CONNECT);
buffer.ChangeState(FairMQBuffer::RUN); buffer.ChangeState(FairMQBuffer::RUN);
buffer.WaitForEndOfState(FairMQBuffer::RUN);
// wait until the running thread has finished processing.
boost::unique_lock<boost::mutex> lock(buffer.fRunningMutex);
while (!buffer.fRunningFinished)
{
buffer.fRunningCondition.wait(lock);
}
buffer.ChangeState(FairMQBuffer::STOP); buffer.ChangeState(FairMQBuffer::STOP);
buffer.ChangeState(FairMQBuffer::RESET_TASK);
buffer.WaitForEndOfState(FairMQBuffer::RESET_TASK);
buffer.ChangeState(FairMQBuffer::RESET_DEVICE);
buffer.WaitForEndOfState(FairMQBuffer::RESET_DEVICE);
buffer.ChangeState(FairMQBuffer::END); buffer.ChangeState(FairMQBuffer::END);
return 0; return 0;

View File

@ -32,12 +32,11 @@ FairMQMerger merger;
static void s_signal_handler(int signal) static void s_signal_handler(int signal)
{ {
cout << endl << "Caught signal " << signal << endl; LOG(INFO) << "Caught signal " << signal;
merger.ChangeState(FairMQMerger::STOP);
merger.ChangeState(FairMQMerger::END); merger.ChangeState(FairMQMerger::END);
cout << "Shutdown complete. Bye!" << endl; LOG(INFO) << "Shutdown complete.";
exit(1); exit(1);
} }
@ -165,41 +164,43 @@ int main(int argc, char** argv)
merger.SetTransport(transportFactory); merger.SetTransport(transportFactory);
for (int i = 0; i < options.inputAddress.size(); ++i)
{
FairMQChannel inputChannel(options.inputSocketType.at(i), options.inputMethod.at(i), options.inputAddress.at(i));
inputChannel.fSndBufSize = options.inputBufSize.at(i);
inputChannel.fRcvBufSize = options.inputBufSize.at(i);
inputChannel.fRateLogging = 1;
merger.fChannels["data-in"].push_back(inputChannel);
}
FairMQChannel outputChannel(options.outputSocketType, options.outputMethod, options.outputAddress);
outputChannel.fSndBufSize = options.outputBufSize;
outputChannel.fRcvBufSize = options.outputBufSize;
outputChannel.fRateLogging = 1;
merger.fChannels["data-out"].push_back(outputChannel);
merger.SetProperty(FairMQMerger::Id, options.id); merger.SetProperty(FairMQMerger::Id, options.id);
merger.SetProperty(FairMQMerger::NumIoThreads, options.ioThreads); merger.SetProperty(FairMQMerger::NumIoThreads, options.ioThreads);
merger.SetProperty(FairMQMerger::NumInputs, options.numInputs); merger.ChangeState(FairMQMerger::INIT_DEVICE);
merger.SetProperty(FairMQMerger::NumOutputs, 1); merger.WaitForEndOfState(FairMQMerger::INIT_DEVICE);
merger.ChangeState(FairMQMerger::INIT); merger.ChangeState(FairMQMerger::INIT_TASK);
merger.WaitForEndOfState(FairMQMerger::INIT_TASK);
for (int i = 0; i < options.numInputs; ++i)
{
merger.SetProperty(FairMQMerger::InputSocketType, options.inputSocketType.at(i), i);
merger.SetProperty(FairMQMerger::InputRcvBufSize, options.inputBufSize.at(i), i);
merger.SetProperty(FairMQMerger::InputMethod, options.inputMethod.at(i), i);
merger.SetProperty(FairMQMerger::InputAddress, options.inputAddress.at(i), i);
}
merger.SetProperty(FairMQMerger::OutputSocketType, options.outputSocketType);
merger.SetProperty(FairMQMerger::OutputSndBufSize, options.outputBufSize);
merger.SetProperty(FairMQMerger::OutputMethod, options.outputMethod);
merger.SetProperty(FairMQMerger::OutputAddress, options.outputAddress);
merger.ChangeState(FairMQMerger::SETOUTPUT);
merger.ChangeState(FairMQMerger::SETINPUT);
merger.ChangeState(FairMQMerger::BIND);
merger.ChangeState(FairMQMerger::CONNECT);
merger.ChangeState(FairMQMerger::RUN); merger.ChangeState(FairMQMerger::RUN);
merger.WaitForEndOfState(FairMQMerger::RUN);
// wait until the running thread has finished processing.
boost::unique_lock<boost::mutex> lock(merger.fRunningMutex);
while (!merger.fRunningFinished)
{
merger.fRunningCondition.wait(lock);
}
merger.ChangeState(FairMQMerger::STOP); merger.ChangeState(FairMQMerger::STOP);
merger.ChangeState(FairMQMerger::RESET_TASK);
merger.WaitForEndOfState(FairMQMerger::RESET_TASK);
merger.ChangeState(FairMQMerger::RESET_DEVICE);
merger.WaitForEndOfState(FairMQMerger::RESET_DEVICE);
merger.ChangeState(FairMQMerger::END); merger.ChangeState(FairMQMerger::END);
return 0; return 0;

View File

@ -32,12 +32,11 @@ FairMQProxy proxy;
static void s_signal_handler(int signal) static void s_signal_handler(int signal)
{ {
cout << endl << "Caught signal " << signal << endl; LOG(INFO) << "Caught signal " << signal;
proxy.ChangeState(FairMQProxy::STOP);
proxy.ChangeState(FairMQProxy::END); proxy.ChangeState(FairMQProxy::END);
cout << "Shutdown complete. Bye!" << endl; LOG(INFO) << "Shutdown complete.";
exit(1); exit(1);
} }
@ -81,11 +80,11 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options)
("id", bpo::value<string>()->required(), "Device ID") ("id", bpo::value<string>()->required(), "Device ID")
("io-threads", bpo::value<int>()->default_value(1), "Number of I/O threads") ("io-threads", bpo::value<int>()->default_value(1), "Number of I/O threads")
("input-socket-type", bpo::value<string>()->required(), "Input socket type: sub/pull") ("input-socket-type", bpo::value<string>()->required(), "Input socket type: sub/pull")
("input-buff-size", bpo::value<int>()->required(), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("input-buff-size", bpo::value<int>()->default_value(1000), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)")
("input-method", bpo::value<string>()->required(), "Input method: bind/connect") ("input-method", bpo::value<string>()->required(), "Input method: bind/connect")
("input-address", bpo::value<string>()->required(), "Input address, e.g.: \"tcp://localhost:5555\"") ("input-address", bpo::value<string>()->required(), "Input address, e.g.: \"tcp://localhost:5555\"")
("output-socket-type", bpo::value<string>()->required(), "Output socket type: pub/push") ("output-socket-type", bpo::value<string>()->required(), "Output socket type: pub/push")
("output-buff-size", bpo::value<int>()->required(), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("output-buff-size", bpo::value<int>()->default_value(1000), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)")
("output-method", bpo::value<string>()->required(), "Output method: bind/connect") ("output-method", bpo::value<string>()->required(), "Output method: bind/connect")
("output-address", bpo::value<string>()->required(), "Output address, e.g.: \"tcp://localhost:5555\"") ("output-address", bpo::value<string>()->required(), "Output address, e.g.: \"tcp://localhost:5555\"")
("help", "Print help messages"); ("help", "Print help messages");
@ -160,38 +159,40 @@ int main(int argc, char** argv)
proxy.SetTransport(transportFactory); proxy.SetTransport(transportFactory);
FairMQChannel inputChannel(options.inputSocketType, options.inputMethod, options.inputAddress);
inputChannel.fSndBufSize = options.inputBufSize;
inputChannel.fRcvBufSize = options.inputBufSize;
inputChannel.fRateLogging = 1;
proxy.fChannels["data-in"].push_back(inputChannel);
FairMQChannel outputChannel(options.outputSocketType, options.outputMethod, options.outputAddress);
outputChannel.fSndBufSize = options.outputBufSize;
outputChannel.fRcvBufSize = options.outputBufSize;
outputChannel.fRateLogging = 1;
proxy.fChannels["data-out"].push_back(outputChannel);
proxy.SetProperty(FairMQProxy::Id, options.id); proxy.SetProperty(FairMQProxy::Id, options.id);
proxy.SetProperty(FairMQProxy::NumIoThreads, options.ioThreads); proxy.SetProperty(FairMQProxy::NumIoThreads, options.ioThreads);
proxy.SetProperty(FairMQProxy::NumInputs, 1); proxy.ChangeState(FairMQProxy::INIT_DEVICE);
proxy.SetProperty(FairMQProxy::NumOutputs, 1); proxy.WaitForEndOfState(FairMQProxy::INIT_DEVICE);
proxy.ChangeState(FairMQProxy::INIT); proxy.ChangeState(FairMQProxy::INIT_TASK);
proxy.WaitForEndOfState(FairMQProxy::INIT_TASK);
proxy.SetProperty(FairMQProxy::InputSocketType, options.inputSocketType);
proxy.SetProperty(FairMQProxy::InputRcvBufSize, options.inputBufSize);
proxy.SetProperty(FairMQProxy::InputMethod, options.inputMethod);
proxy.SetProperty(FairMQProxy::InputAddress, options.inputAddress);
proxy.SetProperty(FairMQProxy::OutputSocketType, options.outputSocketType);
proxy.SetProperty(FairMQProxy::OutputSndBufSize, options.outputBufSize);
proxy.SetProperty(FairMQProxy::OutputMethod, options.outputMethod);
proxy.SetProperty(FairMQProxy::OutputAddress, options.outputAddress);
proxy.ChangeState(FairMQProxy::SETOUTPUT);
proxy.ChangeState(FairMQProxy::SETINPUT);
proxy.ChangeState(FairMQProxy::BIND);
proxy.ChangeState(FairMQProxy::CONNECT);
proxy.ChangeState(FairMQProxy::RUN); proxy.ChangeState(FairMQProxy::RUN);
proxy.WaitForEndOfState(FairMQProxy::RUN);
// wait until the running thread has finished processing.
boost::unique_lock<boost::mutex> lock(proxy.fRunningMutex);
while (!proxy.fRunningFinished)
{
proxy.fRunningCondition.wait(lock);
}
proxy.ChangeState(FairMQProxy::STOP); proxy.ChangeState(FairMQProxy::STOP);
proxy.ChangeState(FairMQProxy::RESET_TASK);
proxy.WaitForEndOfState(FairMQProxy::RESET_TASK);
proxy.ChangeState(FairMQProxy::RESET_DEVICE);
proxy.WaitForEndOfState(FairMQProxy::RESET_DEVICE);
proxy.ChangeState(FairMQProxy::END); proxy.ChangeState(FairMQProxy::END);
return 0; return 0;

View File

@ -32,12 +32,11 @@ FairMQSink sink;
static void s_signal_handler(int signal) static void s_signal_handler(int signal)
{ {
cout << endl << "Caught signal " << signal << endl; LOG(INFO) << "Caught signal " << signal;
sink.ChangeState(FairMQSink::STOP);
sink.ChangeState(FairMQSink::END); sink.ChangeState(FairMQSink::END);
cout << "Shutdown complete. Bye!" << endl; LOG(INFO) << "Shutdown complete.";
exit(1); exit(1);
} }
@ -55,7 +54,8 @@ typedef struct DeviceOptions
{ {
DeviceOptions() : DeviceOptions() :
id(), ioThreads(0), id(), ioThreads(0),
inputSocketType(), inputBufSize(0), inputMethod(), inputAddress() {} inputSocketType(), inputBufSize(0), inputMethod(), inputAddress()
{}
string id; string id;
int ioThreads; int ioThreads;
@ -76,7 +76,7 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options)
("id", bpo::value<string>()->required(), "Device ID") ("id", bpo::value<string>()->required(), "Device ID")
("io-threads", bpo::value<int>()->default_value(1), "Number of I/O threads") ("io-threads", bpo::value<int>()->default_value(1), "Number of I/O threads")
("input-socket-type", bpo::value<string>()->required(), "Input socket type: sub/pull") ("input-socket-type", bpo::value<string>()->required(), "Input socket type: sub/pull")
("input-buff-size", bpo::value<int>()->required(), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("input-buff-size", bpo::value<int>()->default_value(1000), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)")
("input-method", bpo::value<string>()->required(), "Input method: bind/connect") ("input-method", bpo::value<string>()->required(), "Input method: bind/connect")
("input-address", bpo::value<string>()->required(), "Input address, e.g.: \"tcp://*:5555\"") ("input-address", bpo::value<string>()->required(), "Input address, e.g.: \"tcp://*:5555\"")
("help", "Print help messages"); ("help", "Print help messages");
@ -139,33 +139,33 @@ int main(int argc, char** argv)
sink.SetTransport(transportFactory); sink.SetTransport(transportFactory);
FairMQChannel channel(options.inputSocketType, options.inputMethod, options.inputAddress);
channel.fSndBufSize = options.inputBufSize;
channel.fRcvBufSize = options.inputBufSize;
channel.fRateLogging = 1;
sink.fChannels["data-in"].push_back(channel);
sink.SetProperty(FairMQSink::Id, options.id); sink.SetProperty(FairMQSink::Id, options.id);
sink.SetProperty(FairMQSink::NumIoThreads, options.ioThreads); sink.SetProperty(FairMQSink::NumIoThreads, options.ioThreads);
sink.SetProperty(FairMQSink::NumInputs, 1); sink.ChangeState(FairMQSink::INIT_DEVICE);
sink.SetProperty(FairMQSink::NumOutputs, 0); sink.WaitForEndOfState(FairMQSink::INIT_DEVICE);
sink.ChangeState(FairMQSink::INIT); sink.ChangeState(FairMQSink::INIT_TASK);
sink.WaitForEndOfState(FairMQSink::INIT_TASK);
sink.SetProperty(FairMQSink::InputSocketType, options.inputSocketType);
sink.SetProperty(FairMQSink::InputRcvBufSize, options.inputBufSize);
sink.SetProperty(FairMQSink::InputMethod, options.inputMethod);
sink.SetProperty(FairMQSink::InputAddress, options.inputAddress);
sink.ChangeState(FairMQSink::SETOUTPUT);
sink.ChangeState(FairMQSink::SETINPUT);
sink.ChangeState(FairMQSink::BIND);
sink.ChangeState(FairMQSink::CONNECT);
sink.ChangeState(FairMQSink::RUN); sink.ChangeState(FairMQSink::RUN);
sink.WaitForEndOfState(FairMQSink::RUN);
// wait until the running thread has finished processing.
boost::unique_lock<boost::mutex> lock(sink.fRunningMutex);
while (!sink.fRunningFinished)
{
sink.fRunningCondition.wait(lock);
}
sink.ChangeState(FairMQSink::STOP); sink.ChangeState(FairMQSink::STOP);
sink.ChangeState(FairMQSink::RESET_TASK);
sink.WaitForEndOfState(FairMQSink::RESET_TASK);
sink.ChangeState(FairMQSink::RESET_DEVICE);
sink.WaitForEndOfState(FairMQSink::RESET_DEVICE);
sink.ChangeState(FairMQSink::END); sink.ChangeState(FairMQSink::END);
return 0; return 0;

View File

@ -32,12 +32,11 @@ FairMQSplitter splitter;
static void s_signal_handler(int signal) static void s_signal_handler(int signal)
{ {
cout << endl << "Caught signal " << signal << endl; LOG(INFO) << "Caught signal " << signal;
splitter.ChangeState(FairMQSplitter::STOP);
splitter.ChangeState(FairMQSplitter::END); splitter.ChangeState(FairMQSplitter::END);
cout << "Shutdown complete. Bye!" << endl; LOG(INFO) << "Shutdown complete.";
exit(1); exit(1);
} }
@ -56,7 +55,8 @@ typedef struct DeviceOptions
DeviceOptions() : DeviceOptions() :
id(), ioThreads(0), numOutputs(0), id(), ioThreads(0), numOutputs(0),
inputSocketType(), inputBufSize(0), inputMethod(), inputAddress(), inputSocketType(), inputBufSize(0), inputMethod(), inputAddress(),
outputSocketType(), outputBufSize(), outputMethod(), outputAddress() {} outputSocketType(), outputBufSize(), outputMethod(), outputAddress()
{}
string id; string id;
int ioThreads; int ioThreads;
@ -83,11 +83,11 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options)
("io-threads", bpo::value<int>()->default_value(1), "Number of I/O threads") ("io-threads", bpo::value<int>()->default_value(1), "Number of I/O threads")
("num-outputs", bpo::value<int>()->required(), "Number of Splitter output sockets") ("num-outputs", bpo::value<int>()->required(), "Number of Splitter output sockets")
("input-socket-type", bpo::value<string>()->required(), "Input socket type: sub/pull") ("input-socket-type", bpo::value<string>()->required(), "Input socket type: sub/pull")
("input-buff-size", bpo::value<int>()->required(), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("input-buff-size", bpo::value<int>(), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)")
("input-method", bpo::value<string>()->required(), "Input method: bind/connect") ("input-method", bpo::value<string>()->required(), "Input method: bind/connect")
("input-address", bpo::value<string>()->required(), "Input address, e.g.: \"tcp://localhost:5555\"") ("input-address", bpo::value<string>()->required(), "Input address, e.g.: \"tcp://localhost:5555\"")
("output-socket-type", bpo::value< vector<string> >()->required(), "Output socket type: pub/push") ("output-socket-type", bpo::value< vector<string> >()->required(), "Output socket type: pub/push")
("output-buff-size", bpo::value< vector<int> >()->required(), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("output-buff-size", bpo::value< vector<int> >(), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)")
("output-method", bpo::value< vector<string> >()->required(), "Output method: bind/connect") ("output-method", bpo::value< vector<string> >()->required(), "Output method: bind/connect")
("output-address", bpo::value< vector<string> >()->required(), "Output address, e.g.: \"tcp://localhost:5555\"") ("output-address", bpo::value< vector<string> >()->required(), "Output address, e.g.: \"tcp://localhost:5555\"")
("help", "Print help messages"); ("help", "Print help messages");
@ -165,41 +165,43 @@ int main(int argc, char** argv)
splitter.SetTransport(transportFactory); splitter.SetTransport(transportFactory);
FairMQChannel inputChannel(options.inputSocketType, options.inputMethod, options.inputAddress);
inputChannel.fSndBufSize = options.inputBufSize;
inputChannel.fRcvBufSize = options.inputBufSize;
inputChannel.fRateLogging = 1;
splitter.fChannels["data-in"].push_back(inputChannel);
for (int i = 0; i < options.outputAddress.size(); ++i)
{
FairMQChannel outputChannel(options.outputSocketType.at(i), options.outputMethod.at(i), options.outputAddress.at(i));
outputChannel.fSndBufSize = options.outputBufSize.at(i);
outputChannel.fRcvBufSize = options.outputBufSize.at(i);
outputChannel.fRateLogging = 1;
splitter.fChannels["data-out"].push_back(outputChannel);
}
splitter.SetProperty(FairMQSplitter::Id, options.id); splitter.SetProperty(FairMQSplitter::Id, options.id);
splitter.SetProperty(FairMQSplitter::NumIoThreads, options.ioThreads); splitter.SetProperty(FairMQSplitter::NumIoThreads, options.ioThreads);
splitter.SetProperty(FairMQSplitter::NumInputs, 1); splitter.ChangeState(FairMQSplitter::INIT_DEVICE);
splitter.SetProperty(FairMQSplitter::NumOutputs, options.numOutputs); splitter.WaitForEndOfState(FairMQSplitter::INIT_DEVICE);
splitter.ChangeState(FairMQSplitter::INIT); splitter.ChangeState(FairMQSplitter::INIT_TASK);
splitter.WaitForEndOfState(FairMQSplitter::INIT_TASK);
splitter.SetProperty(FairMQSplitter::InputSocketType, options.inputSocketType);
splitter.SetProperty(FairMQSplitter::InputRcvBufSize, options.inputBufSize);
splitter.SetProperty(FairMQSplitter::InputMethod, options.inputMethod);
splitter.SetProperty(FairMQSplitter::InputAddress, options.inputAddress);
for (int i = 0; i < options.numOutputs; ++i)
{
splitter.SetProperty(FairMQSplitter::OutputSocketType, options.outputSocketType.at(i), i);
splitter.SetProperty(FairMQSplitter::OutputSndBufSize, options.outputBufSize.at(i), i);
splitter.SetProperty(FairMQSplitter::OutputMethod, options.outputMethod.at(i), i);
splitter.SetProperty(FairMQSplitter::OutputAddress, options.outputAddress.at(i), i);
}
splitter.ChangeState(FairMQSplitter::SETOUTPUT);
splitter.ChangeState(FairMQSplitter::SETINPUT);
splitter.ChangeState(FairMQSplitter::BIND);
splitter.ChangeState(FairMQSplitter::CONNECT);
splitter.ChangeState(FairMQSplitter::RUN); splitter.ChangeState(FairMQSplitter::RUN);
splitter.WaitForEndOfState(FairMQSplitter::RUN);
// wait until the running thread has finished processing.
boost::unique_lock<boost::mutex> lock(splitter.fRunningMutex);
while (!splitter.fRunningFinished)
{
splitter.fRunningCondition.wait(lock);
}
splitter.ChangeState(FairMQSplitter::STOP); splitter.ChangeState(FairMQSplitter::STOP);
splitter.ChangeState(FairMQSplitter::RESET_TASK);
splitter.WaitForEndOfState(FairMQSplitter::RESET_TASK);
splitter.ChangeState(FairMQSplitter::RESET_DEVICE);
splitter.WaitForEndOfState(FairMQSplitter::RESET_DEVICE);
splitter.ChangeState(FairMQSplitter::END); splitter.ChangeState(FairMQSplitter::END);
return 0; return 0;

View File

@ -27,15 +27,13 @@ FairMQContextZMQ::FairMQContextZMQ(int numIoThreads)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int rc = zmq_ctx_set(fContext, ZMQ_IO_THREADS, numIoThreads); if (zmq_ctx_set(fContext, ZMQ_IO_THREADS, numIoThreads) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno);
} }
// Set the maximum number of allowed sockets on the context. // Set the maximum number of allowed sockets on the context.
rc = zmq_ctx_set(fContext, ZMQ_MAX_SOCKETS, 10000); if (zmq_ctx_set(fContext, ZMQ_MAX_SOCKETS, 10000) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno);
} }
@ -58,8 +56,7 @@ void FairMQContextZMQ::Close()
return; return;
} }
int rc = zmq_ctx_destroy(fContext); if (zmq_ctx_destroy(fContext) != 0)
if (rc != 0)
{ {
if (errno == EINTR) { if (errno == EINTR) {
LOG(ERROR) << " failed closing context, reason: " << zmq_strerror(errno); LOG(ERROR) << " failed closing context, reason: " << zmq_strerror(errno);

View File

@ -23,8 +23,7 @@ using namespace std;
FairMQMessageZMQ::FairMQMessageZMQ() FairMQMessageZMQ::FairMQMessageZMQ()
: fMessage() : fMessage()
{ {
int rc = zmq_msg_init(&fMessage); if (zmq_msg_init(&fMessage) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed initializing message, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed initializing message, reason: " << zmq_strerror(errno);
} }
@ -33,8 +32,7 @@ FairMQMessageZMQ::FairMQMessageZMQ()
FairMQMessageZMQ::FairMQMessageZMQ(size_t size) FairMQMessageZMQ::FairMQMessageZMQ(size_t size)
: fMessage() : fMessage()
{ {
int rc = zmq_msg_init_size(&fMessage, size); if (zmq_msg_init_size(&fMessage, size) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed initializing message with size, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed initializing message with size, reason: " << zmq_strerror(errno);
} }
@ -43,8 +41,7 @@ FairMQMessageZMQ::FairMQMessageZMQ(size_t size)
FairMQMessageZMQ::FairMQMessageZMQ(void* data, size_t size, fairmq_free_fn *ffn, void* hint) FairMQMessageZMQ::FairMQMessageZMQ(void* data, size_t size, fairmq_free_fn *ffn, void* hint)
: fMessage() : fMessage()
{ {
int rc = zmq_msg_init_data(&fMessage, data, size, ffn ? ffn : &CleanUp, hint); if (zmq_msg_init_data(&fMessage, data, size, ffn ? ffn : &CleanUp, hint) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed initializing message with data, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed initializing message with data, reason: " << zmq_strerror(errno);
} }
@ -53,8 +50,7 @@ FairMQMessageZMQ::FairMQMessageZMQ(void* data, size_t size, fairmq_free_fn *ffn,
void FairMQMessageZMQ::Rebuild() void FairMQMessageZMQ::Rebuild()
{ {
CloseMessage(); CloseMessage();
int rc = zmq_msg_init(&fMessage); if (zmq_msg_init(&fMessage) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed initializing message, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed initializing message, reason: " << zmq_strerror(errno);
} }
@ -63,8 +59,7 @@ void FairMQMessageZMQ::Rebuild()
void FairMQMessageZMQ::Rebuild(size_t size) void FairMQMessageZMQ::Rebuild(size_t size)
{ {
CloseMessage(); CloseMessage();
int rc = zmq_msg_init_size(&fMessage, size); if (zmq_msg_init_size(&fMessage, size) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed initializing message with size, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed initializing message with size, reason: " << zmq_strerror(errno);
} }
@ -73,8 +68,7 @@ void FairMQMessageZMQ::Rebuild(size_t size)
void FairMQMessageZMQ::Rebuild(void* data, size_t size, fairmq_free_fn *ffn, void* hint) void FairMQMessageZMQ::Rebuild(void* data, size_t size, fairmq_free_fn *ffn, void* hint)
{ {
CloseMessage(); CloseMessage();
int rc = zmq_msg_init_data(&fMessage, data, size, ffn ? ffn : &CleanUp, hint); if (zmq_msg_init_data(&fMessage, data, size, ffn ? ffn : &CleanUp, hint) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed initializing message with data, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed initializing message with data, reason: " << zmq_strerror(errno);
} }
@ -103,8 +97,7 @@ void FairMQMessageZMQ::SetMessage(void* data, size_t size)
void FairMQMessageZMQ::Copy(FairMQMessage* msg) void FairMQMessageZMQ::Copy(FairMQMessage* msg)
{ {
// Shares the message buffer between msg and this fMessage. // Shares the message buffer between msg and this fMessage.
int rc = zmq_msg_copy(&fMessage, (zmq_msg_t*)msg->GetMessage()); if (zmq_msg_copy(&fMessage, (zmq_msg_t*)msg->GetMessage()) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed copying message, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed copying message, reason: " << zmq_strerror(errno);
} }
@ -119,8 +112,7 @@ void FairMQMessageZMQ::Copy(FairMQMessage* msg)
inline void FairMQMessageZMQ::CloseMessage() inline void FairMQMessageZMQ::CloseMessage()
{ {
int rc = zmq_msg_close(&fMessage); if (zmq_msg_close(&fMessage) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed closing message, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed closing message, reason: " << zmq_strerror(errno);
} }
@ -133,8 +125,7 @@ void FairMQMessageZMQ::CleanUp(void* data, void* hint)
FairMQMessageZMQ::~FairMQMessageZMQ() FairMQMessageZMQ::~FairMQMessageZMQ()
{ {
int rc = zmq_msg_close(&fMessage); if (zmq_msg_close(&fMessage) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed closing message with data, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed closing message with data, reason: " << zmq_strerror(errno);
} }

View File

@ -19,16 +19,16 @@
using namespace std; using namespace std;
FairMQPollerZMQ::FairMQPollerZMQ(const vector<FairMQSocket*>& inputs) FairMQPollerZMQ::FairMQPollerZMQ(const vector<FairMQChannel>& channels)
: items() : items()
, fNumItems() , fNumItems()
{ {
fNumItems = inputs.size(); fNumItems = channels.size();
items = new zmq_pollitem_t[fNumItems]; items = new zmq_pollitem_t[fNumItems];
for (int i = 0; i < fNumItems; i++) for (int i = 0; i < fNumItems; ++i)
{ {
items[i].socket = inputs.at(i)->GetSocket(); items[i].socket = channels.at(i).fSocket->GetSocket();
items[i].fd = 0; items[i].fd = 0;
items[i].events = ZMQ_POLLIN; items[i].events = ZMQ_POLLIN;
items[i].revents = 0; items[i].revents = 0;
@ -37,8 +37,7 @@ FairMQPollerZMQ::FairMQPollerZMQ(const vector<FairMQSocket*>& inputs)
void FairMQPollerZMQ::Poll(int timeout) void FairMQPollerZMQ::Poll(int timeout)
{ {
int rc = zmq_poll(items, fNumItems, timeout); if (zmq_poll(items, fNumItems, timeout) < 0)
if (rc < 0)
{ {
LOG(ERROR) << "polling failed, reason: " << zmq_strerror(errno); LOG(ERROR) << "polling failed, reason: " << zmq_strerror(errno);
} }
@ -47,7 +46,9 @@ void FairMQPollerZMQ::Poll(int timeout)
bool FairMQPollerZMQ::CheckInput(int index) bool FairMQPollerZMQ::CheckInput(int index)
{ {
if (items[index].revents & ZMQ_POLLIN) if (items[index].revents & ZMQ_POLLIN)
{
return true; return true;
}
return false; return false;
} }
@ -55,7 +56,9 @@ bool FairMQPollerZMQ::CheckInput(int index)
bool FairMQPollerZMQ::CheckOutput(int index) bool FairMQPollerZMQ::CheckOutput(int index)
{ {
if (items[index].revents & ZMQ_POLLOUT) if (items[index].revents & ZMQ_POLLOUT)
{
return true; return true;
}
return false; return false;
} }
@ -63,5 +66,7 @@ bool FairMQPollerZMQ::CheckOutput(int index)
FairMQPollerZMQ::~FairMQPollerZMQ() FairMQPollerZMQ::~FairMQPollerZMQ()
{ {
if (items != NULL) if (items != NULL)
{
delete[] items; delete[] items;
}
} }

View File

@ -18,12 +18,12 @@
#include <vector> #include <vector>
#include "FairMQPoller.h" #include "FairMQPoller.h"
#include "FairMQSocket.h" #include "FairMQChannel.h"
class FairMQPollerZMQ : public FairMQPoller class FairMQPollerZMQ : public FairMQPoller
{ {
public: public:
FairMQPollerZMQ(const std::vector<FairMQSocket*>& inputs); FairMQPollerZMQ(const std::vector<FairMQChannel>& channels);
virtual void Poll(int timeout); virtual void Poll(int timeout);
virtual bool CheckInput(int index); virtual bool CheckInput(int index);

View File

@ -19,9 +19,10 @@
using namespace std; using namespace std;
// Context to hold the ZeroMQ sockets
boost::shared_ptr<FairMQContextZMQ> FairMQSocketZMQ::fContext = boost::shared_ptr<FairMQContextZMQ>(new FairMQContextZMQ(1)); boost::shared_ptr<FairMQContextZMQ> FairMQSocketZMQ::fContext = boost::shared_ptr<FairMQContextZMQ>(new FairMQContextZMQ(1));
FairMQSocketZMQ::FairMQSocketZMQ(const string& type, int num, int numIoThreads) FairMQSocketZMQ::FairMQSocketZMQ(const string& type, const string& name, int numIoThreads)
: FairMQSocket(ZMQ_SNDMORE, ZMQ_RCVMORE, ZMQ_DONTWAIT) : FairMQSocket(ZMQ_SNDMORE, ZMQ_RCVMORE, ZMQ_DONTWAIT)
, fSocket(NULL) , fSocket(NULL)
, fId() , fId()
@ -30,12 +31,9 @@ FairMQSocketZMQ::FairMQSocketZMQ(const string& type, int num, int numIoThreads)
, fMessagesTx(0) , fMessagesTx(0)
, fMessagesRx(0) , fMessagesRx(0)
{ {
stringstream id; fId = name + "." + type;
id << type << "." << num;
fId = id.str();
int rc = zmq_ctx_set(fContext->GetContext(), ZMQ_IO_THREADS, numIoThreads); if (zmq_ctx_set(fContext->GetContext(), ZMQ_IO_THREADS, numIoThreads) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno);
} }
@ -44,12 +42,11 @@ FairMQSocketZMQ::FairMQSocketZMQ(const string& type, int num, int numIoThreads)
if (fSocket == NULL) if (fSocket == NULL)
{ {
LOG(ERROR) << "failed creating socket #" << fId << ", reason: " << zmq_strerror(errno); LOG(ERROR) << "failed creating socket " << fId << ", reason: " << zmq_strerror(errno);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
rc = zmq_setsockopt(fSocket, ZMQ_IDENTITY, &fId, fId.length()); if (zmq_setsockopt(fSocket, ZMQ_IDENTITY, &fId, fId.length()) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed setting ZMQ_IDENTITY socket option, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed setting ZMQ_IDENTITY socket option, reason: " << zmq_strerror(errno);
} }
@ -57,22 +54,20 @@ FairMQSocketZMQ::FairMQSocketZMQ(const string& type, int num, int numIoThreads)
// Tell socket to try and send/receive outstanding messages for <linger> milliseconds before terminating. // Tell socket to try and send/receive outstanding messages for <linger> milliseconds before terminating.
// Default value for ZeroMQ is -1, which is to wait forever. // Default value for ZeroMQ is -1, which is to wait forever.
int linger = 500; int linger = 500;
rc = zmq_setsockopt(fSocket, ZMQ_LINGER, &linger, sizeof(linger)); if (zmq_setsockopt(fSocket, ZMQ_LINGER, &linger, sizeof(linger)) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed setting ZMQ_LINGER socket option, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed setting ZMQ_LINGER socket option, reason: " << zmq_strerror(errno);
} }
if (type == "sub") if (type == "sub")
{ {
rc = zmq_setsockopt(fSocket, ZMQ_SUBSCRIBE, NULL, 0); if (zmq_setsockopt(fSocket, ZMQ_SUBSCRIBE, NULL, 0) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed setting ZMQ_SUBSCRIBE socket option, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed setting ZMQ_SUBSCRIBE socket option, reason: " << zmq_strerror(errno);
} }
} }
LOG(INFO) << "created socket #" << fId; // LOG(INFO) << "created socket " << fId;
} }
string FairMQSocketZMQ::GetId() string FairMQSocketZMQ::GetId()
@ -82,16 +77,15 @@ string FairMQSocketZMQ::GetId()
bool FairMQSocketZMQ::Bind(const string& address) bool FairMQSocketZMQ::Bind(const string& address)
{ {
LOG(INFO) << "bind socket #" << fId << " on " << address; // LOG(INFO) << "bind socket " << fId << " on " << address;
int rc = zmq_bind(fSocket, address.c_str()); if (zmq_bind(fSocket, address.c_str()) != 0)
if (rc != 0)
{ {
if (errno == EADDRINUSE) { if (errno == EADDRINUSE) {
// do not print error in this case, this is handled by FairMQDevice in case no connection could be established after trying a number of random ports from a range. // do not print error in this case, this is handled by FairMQDevice in case no connection could be established after trying a number of random ports from a range.
return false; return false;
} }
LOG(ERROR) << "failed binding socket #" << fId << ", reason: " << zmq_strerror(errno); LOG(ERROR) << "failed binding socket " << fId << ", reason: " << zmq_strerror(errno);
return false; return false;
} }
return true; return true;
@ -99,12 +93,11 @@ bool FairMQSocketZMQ::Bind(const string& address)
void FairMQSocketZMQ::Connect(const string& address) void FairMQSocketZMQ::Connect(const string& address)
{ {
LOG(INFO) << "connect socket #" << fId << " on " << address; // LOG(INFO) << "connect socket " << fId << " on " << address;
int rc = zmq_connect(fSocket, address.c_str()); if (zmq_connect(fSocket, address.c_str()) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed connecting socket #" << fId << ", reason: " << zmq_strerror(errno); LOG(ERROR) << "failed connecting socket " << fId << ", reason: " << zmq_strerror(errno);
} }
} }
@ -123,10 +116,10 @@ int FairMQSocketZMQ::Send(FairMQMessage* msg, const string& flag)
} }
if (zmq_errno() == ETERM) if (zmq_errno() == ETERM)
{ {
LOG(INFO) << "terminating socket #" << fId; LOG(INFO) << "terminating socket " << fId;
return -1; return -1;
} }
LOG(ERROR) << "failed sending on socket #" << fId << ", reason: " << zmq_strerror(errno); LOG(ERROR) << "failed sending on socket " << fId << ", reason: " << zmq_strerror(errno);
return nbytes; return nbytes;
} }
@ -145,10 +138,10 @@ int FairMQSocketZMQ::Send(FairMQMessage* msg, const int flags)
} }
if (zmq_errno() == ETERM) if (zmq_errno() == ETERM)
{ {
LOG(INFO) << "terminating socket #" << fId; LOG(INFO) << "terminating socket " << fId;
return -1; return -1;
} }
LOG(ERROR) << "failed sending on socket #" << fId << ", reason: " << zmq_strerror(errno); LOG(ERROR) << "failed sending on socket " << fId << ", reason: " << zmq_strerror(errno);
return nbytes; return nbytes;
} }
@ -167,10 +160,10 @@ int FairMQSocketZMQ::Receive(FairMQMessage* msg, const string& flag)
} }
if (zmq_errno() == ETERM) if (zmq_errno() == ETERM)
{ {
LOG(INFO) << "terminating socket #" << fId; LOG(INFO) << "terminating socket " << fId;
return -1; return -1;
} }
LOG(ERROR) << "failed receiving on socket #" << fId << ", reason: " << zmq_strerror(errno); LOG(ERROR) << "failed receiving on socket " << fId << ", reason: " << zmq_strerror(errno);
return nbytes; return nbytes;
} }
@ -189,24 +182,25 @@ int FairMQSocketZMQ::Receive(FairMQMessage* msg, const int flags)
} }
if (zmq_errno() == ETERM) if (zmq_errno() == ETERM)
{ {
LOG(INFO) << "terminating socket #" << fId; LOG(INFO) << "terminating socket " << fId;
return -1; return -1;
} }
LOG(ERROR) << "failed receiving on socket #" << fId << ", reason: " << zmq_strerror(errno); LOG(ERROR) << "failed receiving on socket " << fId << ", reason: " << zmq_strerror(errno);
return nbytes; return nbytes;
} }
void FairMQSocketZMQ::Close() void FairMQSocketZMQ::Close()
{ {
// LOG(DEBUG) << "Closing socket " << fId;
if (fSocket == NULL) if (fSocket == NULL)
{ {
return; return;
} }
int rc = zmq_close(fSocket); if (zmq_close(fSocket) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed closing socket, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed closing socket " << fId << ", reason: " << zmq_strerror(errno);
} }
fSocket = NULL; fSocket = NULL;
@ -214,8 +208,7 @@ void FairMQSocketZMQ::Close()
void FairMQSocketZMQ::Terminate() void FairMQSocketZMQ::Terminate()
{ {
int rc = zmq_ctx_destroy(fContext->GetContext()); if (zmq_ctx_destroy(fContext->GetContext()) != 0)
if (rc != 0)
{ {
LOG(ERROR) << "failed terminating context, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed terminating context, reason: " << zmq_strerror(errno);
} }
@ -234,8 +227,7 @@ int FairMQSocketZMQ::GetSocket(int nothing)
void FairMQSocketZMQ::SetOption(const string& option, const void* value, size_t valueSize) void FairMQSocketZMQ::SetOption(const string& option, const void* value, size_t valueSize)
{ {
int rc = zmq_setsockopt(fSocket, GetConstant(option), value, valueSize); if (zmq_setsockopt(fSocket, GetConstant(option), value, valueSize) < 0)
if (rc < 0)
{ {
LOG(ERROR) << "failed setting socket option, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed setting socket option, reason: " << zmq_strerror(errno);
} }
@ -243,8 +235,8 @@ void FairMQSocketZMQ::SetOption(const string& option, const void* value, size_t
void FairMQSocketZMQ::GetOption(const string& option, void* value, size_t* valueSize) void FairMQSocketZMQ::GetOption(const string& option, void* value, size_t* valueSize)
{ {
int rc = zmq_getsockopt(fSocket, GetConstant(option), value, valueSize); if (zmq_getsockopt(fSocket, GetConstant(option), value, valueSize) < 0)
if (rc < 0) { {
LOG(ERROR) << "failed getting socket option, reason: " << zmq_strerror(errno); LOG(ERROR) << "failed getting socket option, reason: " << zmq_strerror(errno);
} }
} }

View File

@ -25,17 +25,17 @@
class FairMQSocketZMQ : public FairMQSocket class FairMQSocketZMQ : public FairMQSocket
{ {
public: public:
FairMQSocketZMQ(const std::string& type, int num, int numIoThreads); FairMQSocketZMQ(const std::string& type, const std::string& name, int numIoThreads);
virtual std::string GetId(); virtual std::string GetId();
virtual bool Bind(const std::string& address); virtual bool Bind(const std::string& address);
virtual void Connect(const std::string& address); virtual void Connect(const std::string& address);
virtual int Send(FairMQMessage* msg, const std::string& flag=""); virtual int Send(FairMQMessage* msg, const std::string& flag = "");
virtual int Send(FairMQMessage* msg, const int flags); virtual int Send(FairMQMessage* msg, const int flags = 0);
virtual int Receive(FairMQMessage* msg, const std::string& flag=""); virtual int Receive(FairMQMessage* msg, const std::string& flag = "");
virtual int Receive(FairMQMessage* msg, const int flags); virtual int Receive(FairMQMessage* msg, const int flags = 0);
virtual void* GetSocket(); virtual void* GetSocket();
virtual int GetSocket(int nothing); virtual int GetSocket(int nothing);

View File

@ -22,7 +22,7 @@ FairMQTransportFactoryZMQ::FairMQTransportFactoryZMQ()
{ {
int major, minor, patch; int major, minor, patch;
zmq_version(&major, &minor, &patch); zmq_version(&major, &minor, &patch);
LOG(INFO) << "Using ZeroMQ library, version: " << major << "." << minor << "." << patch; LOG(DEBUG) << "Using ZeroMQ library, version: " << major << "." << minor << "." << patch;
} }
FairMQMessage* FairMQTransportFactoryZMQ::CreateMessage() FairMQMessage* FairMQTransportFactoryZMQ::CreateMessage()
@ -40,12 +40,12 @@ FairMQMessage* FairMQTransportFactoryZMQ::CreateMessage(void* data, size_t size,
return new FairMQMessageZMQ(data, size, ffn, hint); return new FairMQMessageZMQ(data, size, ffn, hint);
} }
FairMQSocket* FairMQTransportFactoryZMQ::CreateSocket(const string& type, int num, int numIoThreads) FairMQSocket* FairMQTransportFactoryZMQ::CreateSocket(const string& type, const std::string& name, int numIoThreads)
{ {
return new FairMQSocketZMQ(type, num, numIoThreads); return new FairMQSocketZMQ(type, name, numIoThreads);
} }
FairMQPoller* FairMQTransportFactoryZMQ::CreatePoller(const vector<FairMQSocket*>& inputs) FairMQPoller* FairMQTransportFactoryZMQ::CreatePoller(const vector<FairMQChannel>& channels)
{ {
return new FairMQPollerZMQ(inputs); return new FairMQPollerZMQ(channels);
} }

View File

@ -31,8 +31,8 @@ class FairMQTransportFactoryZMQ : public FairMQTransportFactory
virtual FairMQMessage* CreateMessage(); virtual FairMQMessage* CreateMessage();
virtual FairMQMessage* CreateMessage(size_t size); virtual FairMQMessage* CreateMessage(size_t size);
virtual FairMQMessage* CreateMessage(void* data, size_t size, fairmq_free_fn *ffn = NULL, void* hint = NULL); virtual FairMQMessage* CreateMessage(void* data, size_t size, fairmq_free_fn *ffn = NULL, void* hint = NULL);
virtual FairMQSocket* CreateSocket(const std::string& type, int num, int numIoThreads); virtual FairMQSocket* CreateSocket(const std::string& type, const std::string& name, int numIoThreads);
virtual FairMQPoller* CreatePoller(const std::vector<FairMQSocket*>& inputs); virtual FairMQPoller* CreatePoller(const std::vector<FairMQChannel>& channels);
virtual ~FairMQTransportFactoryZMQ() {}; virtual ~FairMQTransportFactoryZMQ() {};
}; };