From 7fda980710d340ec5a1e99a877464ad7d23b16e0 Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Wed, 29 Apr 2015 13:25:42 +0200 Subject: [PATCH] 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. --- fairmq/CMakeLists.txt | 7 +- fairmq/FairMQChannel.cxx | 122 ++++ fairmq/FairMQChannel.h | 52 ++ fairmq/FairMQConfigurable.cxx | 8 +- fairmq/FairMQConfigurable.h | 8 +- fairmq/FairMQDevice.cxx | 643 +++++++++--------- fairmq/FairMQDevice.h | 85 +-- fairmq/FairMQLogger.cxx | 9 +- fairmq/FairMQLogger.h | 12 +- fairmq/FairMQSocket.h | 9 +- fairmq/FairMQStateMachine.cxx | 195 +++++- fairmq/FairMQStateMachine.h | 512 +++++++++----- fairmq/FairMQTransportFactory.h | 5 +- fairmq/devices/FairMQBenchmarkSampler.cxx | 80 +-- fairmq/devices/FairMQBenchmarkSampler.h | 14 +- fairmq/devices/FairMQBuffer.cxx | 28 +- fairmq/devices/FairMQMerger.cxx | 36 +- fairmq/devices/FairMQProxy.cxx | 28 +- fairmq/devices/FairMQSink.cxx | 24 +- fairmq/devices/FairMQSplitter.cxx | 30 +- fairmq/devices/GenericFileSink.h | 74 +- fairmq/devices/GenericFileSink.tpl | 41 +- fairmq/devices/GenericMerger.h | 65 +- fairmq/devices/GenericProcessor.h | 108 ++- fairmq/devices/GenericSampler.h | 58 +- fairmq/devices/GenericSampler.tpl | 251 +++---- .../examples/req-rep/FairMQExampleClient.cxx | 33 +- fairmq/examples/req-rep/FairMQExampleClient.h | 10 +- .../examples/req-rep/FairMQExampleServer.cxx | 17 +- fairmq/examples/req-rep/runExampleClient.cxx | 43 +- fairmq/examples/req-rep/runExampleServer.cxx | 42 +- fairmq/nanomsg/FairMQMessageNN.cxx | 8 +- fairmq/nanomsg/FairMQPollerNN.cxx | 20 +- fairmq/nanomsg/FairMQPollerNN.h | 4 +- fairmq/nanomsg/FairMQSocketNN.cxx | 28 +- fairmq/nanomsg/FairMQSocketNN.h | 10 +- fairmq/nanomsg/FairMQTransportFactoryNN.cxx | 8 +- fairmq/nanomsg/FairMQTransportFactoryNN.h | 4 +- fairmq/prototest/FairMQBinSampler.cxx | 1 - fairmq/prototest/FairMQProtoSampler.cxx | 1 - fairmq/run/runBenchmarkSampler.cxx | 68 +- fairmq/run/runBuffer.cxx | 59 +- fairmq/run/runMerger.cxx | 61 +- fairmq/run/runProxy.cxx | 59 +- fairmq/run/runSink.cxx | 48 +- fairmq/run/runSplitter.cxx | 68 +- fairmq/zeromq/FairMQContextZMQ.cxx | 9 +- fairmq/zeromq/FairMQMessageZMQ.cxx | 27 +- fairmq/zeromq/FairMQPollerZMQ.cxx | 17 +- fairmq/zeromq/FairMQPollerZMQ.h | 4 +- fairmq/zeromq/FairMQSocketZMQ.cxx | 70 +- fairmq/zeromq/FairMQSocketZMQ.h | 10 +- fairmq/zeromq/FairMQTransportFactoryZMQ.cxx | 10 +- fairmq/zeromq/FairMQTransportFactoryZMQ.h | 4 +- 54 files changed, 1674 insertions(+), 1573 deletions(-) create mode 100644 fairmq/FairMQChannel.cxx create mode 100644 fairmq/FairMQChannel.h diff --git a/fairmq/CMakeLists.txt b/fairmq/CMakeLists.txt index 239ff5ac..1f63620f 100644 --- a/fairmq/CMakeLists.txt +++ b/fairmq/CMakeLists.txt @@ -69,7 +69,9 @@ set(SRCS "FairMQTransportFactory.cxx" "FairMQMessage.cxx" "FairMQSocket.cxx" + "FairMQChannel.cxx" "FairMQDevice.cxx" + "FairMQPoller.cxx" "devices/FairMQBenchmarkSampler.cxx" "devices/FairMQSink.cxx" "devices/FairMQBuffer.cxx" @@ -79,7 +81,6 @@ set(SRCS "options/FairProgOptions.cxx" "options/FairMQProgOptions.cxx" "options/FairMQParser.cxx" - "FairMQPoller.cxx" "examples/req-rep/FairMQExampleClient.cxx" "examples/req-rep/FairMQExampleServer.cxx" ) @@ -150,10 +151,10 @@ GENERATE_LIBRARY() set(Exe_Names bsampler + sink buffer splitter merger - sink proxy example_client example_server @@ -172,10 +173,10 @@ set(Exe_Names set(Exe_Source run/runBenchmarkSampler.cxx + run/runSink.cxx run/runBuffer.cxx run/runSplitter.cxx run/runMerger.cxx - run/runSink.cxx run/runProxy.cxx examples/req-rep/runExampleClient.cxx examples/req-rep/runExampleServer.cxx diff --git a/fairmq/FairMQChannel.cxx b/fairmq/FairMQChannel.cxx new file mode 100644 index 00000000..7344931b --- /dev/null +++ b/fairmq/FairMQChannel.cxx @@ -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 + +#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 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 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() +{ +} diff --git a/fairmq/FairMQChannel.h b/fairmq/FairMQChannel.h new file mode 100644 index 00000000..c1e788da --- /dev/null +++ b/fairmq/FairMQChannel.h @@ -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 + +#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_ */ \ No newline at end of file diff --git a/fairmq/FairMQConfigurable.cxx b/fairmq/FairMQConfigurable.cxx index 73619c74..9178142e 100644 --- a/fairmq/FairMQConfigurable.cxx +++ b/fairmq/FairMQConfigurable.cxx @@ -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."; 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."; 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."; 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."; return default_; diff --git a/fairmq/FairMQConfigurable.h b/fairmq/FairMQConfigurable.h index bb9a6da8..1c88e9cd 100644 --- a/fairmq/FairMQConfigurable.h +++ b/fairmq/FairMQConfigurable.h @@ -25,10 +25,10 @@ class FairMQConfigurable Last = 1 }; FairMQConfigurable(); - 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 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 ~FairMQConfigurable(); }; diff --git a/fairmq/FairMQDevice.cxx b/fairmq/FairMQDevice.cxx index 5c21f360..ec362582 100644 --- a/fairmq/FairMQDevice.cxx +++ b/fairmq/FairMQDevice.cxx @@ -12,6 +12,9 @@ * @author D. Klein, A. Rybalchenko */ +#include +#include // for std::sort() + #include #include // for choosing random port in range #include // for choosing random port in range @@ -23,233 +26,310 @@ using namespace std; FairMQDevice::FairMQDevice() - : fId() + : fChannels() + , fId() + , fMaxInitializationTime(120) , fNumIoThreads(1) - , fNumInputs(0) - , fNumOutputs(0) , fPortRangeMin(22000) , fPortRangeMax(32000) - , fInputAddress() - , fInputMethod() - , fInputSocketType() - , fInputSndBufSize() - , fInputRcvBufSize() - , fInputRateLogging() - , fOutputAddress() - , fOutputMethod() - , fOutputSocketType() - , fOutputSndBufSize() - , fOutputRcvBufSize() - , fOutputRateLogging() - , fPayloadInputs(new vector()) - , fPayloadOutputs(new vector()) , fLogIntervalInMs(1000) + , fCommandSocket() , 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 uninitializedChannels; + for (map< string,vector >::iterator mi = fChannels.begin(); mi != fChannels.end(); ++mi) + { + for (vector::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::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 lock(fInitializingMutex); + fInitializingFinished = true; + fInitializingCondition.notify_one(); + + ChangeState(DEVICE_READY); +} + 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))); - socket->SetOption("rcv-hwm", &fInputRcvBufSize.at(i), sizeof(fInputRcvBufSize.at(i))); + // initialize random generator + 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() -{ - 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") + // try to bind to the saved port. In case of failure, try random one. + if (!ch.fSocket->Bind(ch.fAddress)) { - 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 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::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"; - LOG(INFO) << "port range: " << fPortRangeMin << " - " << fPortRangeMax; - do { - ++numAttempts; - - 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))); + // set channel name: name + vector index + stringstream ss; + ss << name << "[" << vi - fChannels[name].begin() << "]"; + vi->fChannelName = ss.str(); } } } - - numAttempts = 0; - - for (int i = 0; i < fNumInputs; ++i) + else { - if (fInputMethod.at(i) == "bind") - { - 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))); - } - } + LOG(ERROR) << "Sorting failed: no channel with the name \"" << name << "\"."; } } -void FairMQDevice::Connect() +void FairMQDevice::PrintChannel(const string& name) { - LOG(INFO) << ">>>>>>> connecting <<<<<<<"; - - for (int i = 0; i < fNumOutputs; ++i) + if (fChannels.find(name) != fChannels.end()) { - if (fOutputMethod.at(i) == "connect") + for (vector::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; } } - - for (int i = 0; i < fNumInputs; ++i) + else { - if (fInputMethod.at(i) == "connect") - { - fPayloadInputs->at(i)->Connect(fInputAddress.at(i)); - } + LOG(ERROR) << "Printing failed: no channel with the name \"" << name << "\"."; } } +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 lock(fRunningMutex); + fRunningFinished = true; + fRunningCondition.notify_one(); +} + void FairMQDevice::Run() { } 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 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 lock(fResetMutex); + fResetFinished = true; + fResetCondition.notify_one(); + + ChangeState(IDLE); +} + +void FairMQDevice::Reset() { } // 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) { case Id: fId = value; 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: - FairMQConfigurable::SetProperty(key, value, slot); + FairMQConfigurable::SetProperty(key, value); break; } } // 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) { case NumIoThreads: fNumIoThreads = value; break; - case NumInputs: - fNumInputs = value; - break; - case NumOutputs: - fNumOutputs = value; + case MaxInitializationTime: + fMaxInitializationTime = value; break; case PortRangeMin: fPortRangeMin = value; @@ -260,95 +340,41 @@ void FairMQDevice::SetProperty(const int key, const int value, const int slot /* case LogIntervalInMs: fLogIntervalInMs = value; 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: - FairMQConfigurable::SetProperty(key, value, slot); + FairMQConfigurable::SetProperty(key, value); break; } } // 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) { case Id: 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: - return FairMQConfigurable::GetProperty(key, default_, slot); + return FairMQConfigurable::GetProperty(key, default_); } } // 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) { case NumIoThreads: return fNumIoThreads; - case NumInputs: - return fNumInputs; - case NumOutputs: - return fNumOutputs; + case MaxInitializationTime: + return fMaxInitializationTime; case PortRangeMin: return fPortRangeMin; case PortRangeMax: return fPortRangeMax; case LogIntervalInMs: 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: - return FairMQConfigurable::GetProperty(key, default_, slot); + return FairMQConfigurable::GetProperty(key, default_); } } @@ -364,67 +390,55 @@ void FairMQDevice::LogSocketRates() timestamp_t msSinceLastLog; - int numFilteredInputs = 0; - int numFilteredOutputs = 0; - vector filteredInputs; - vector filteredOutputs; + int numFilteredSockets = 0; + vector filteredSockets; + vector filteredChannelNames; + + // iterate over the channels map + for (map< string,vector >::iterator mi = fChannels.begin(); mi != fChannels.end(); ++mi) + { + // iterate over the channels vector + for (vector::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 bytesIn(numFilteredSockets); + vector msgIn(numFilteredSockets); + vector bytesOut(numFilteredSockets); + vector msgOut(numFilteredSockets); + + vector bytesInNew(numFilteredSockets); + vector msgInNew(numFilteredSockets); + vector bytesOutNew(numFilteredSockets); + vector msgOutNew(numFilteredSockets); + + vector mbPerSecIn(numFilteredSockets); + vector msgPerSecIn(numFilteredSockets); + vector mbPerSecOut(numFilteredSockets); + vector msgPerSecOut(numFilteredSockets); int i = 0; - for (vector::iterator itr = fPayloadInputs->begin(); itr != fPayloadInputs->end(); itr++) - { - if (fInputRateLogging.at(i) > 0) - { - filteredInputs.push_back((*itr)); - ++numFilteredInputs; - } - ++i; - } - - i = 0; - for (vector::iterator itr = fPayloadOutputs->begin(); itr != fPayloadOutputs->end(); itr++) - { - if (fOutputRateLogging.at(i) > 0) - { - filteredOutputs.push_back((*itr)); - ++numFilteredOutputs; - } - ++i; - } - - vector bytesIn(numFilteredInputs); - vector msgIn(numFilteredInputs); - vector bytesOut(numFilteredOutputs); - vector msgOut(numFilteredOutputs); - - vector bytesInNew(numFilteredInputs); - vector msgInNew(numFilteredInputs); - vector bytesOutNew(numFilteredOutputs); - vector msgOutNew(numFilteredOutputs); - - vector mbPerSecIn(numFilteredInputs); - vector msgPerSecIn(numFilteredInputs); - vector mbPerSecOut(numFilteredOutputs); - vector msgPerSecOut(numFilteredOutputs); - - i = 0; - for (vector::iterator itr = filteredInputs.begin(); itr != filteredInputs.end(); itr++) + for (vector::iterator itr = filteredSockets.begin(); itr != filteredSockets.end(); ++itr) { bytesIn.at(i) = (*itr)->GetBytesRx(); - msgIn.at(i) = (*itr)->GetMessagesRx(); - ++i; - } - - i = 0; - for (vector::iterator itr = filteredOutputs.begin(); itr != filteredOutputs.end(); itr++) - { bytesOut.at(i) = (*itr)->GetBytesTx(); + msgIn.at(i) = (*itr)->GetMessagesRx(); msgOut.at(i) = (*itr)->GetMessagesTx(); ++i; } t0 = get_timestamp(); - while (fState == RUNNING) + while (GetCurrentState() == RUNNING) { try { @@ -434,32 +448,27 @@ void FairMQDevice::LogSocketRates() i = 0; - for (vector::iterator itr = filteredInputs.begin(); itr != filteredInputs.end(); itr++) + for (vector::iterator itr = filteredSockets.begin(); itr != filteredSockets.end(); itr++) { bytesInNew.at(i) = (*itr)->GetBytesRx(); mbPerSecIn.at(i) = ((double)(bytesInNew.at(i) - bytesIn.at(i)) / (1024. * 1024.)) / (double)msSinceLastLog * 1000.; bytesIn.at(i) = bytesInNew.at(i); + msgInNew.at(i) = (*itr)->GetMessagesRx(); msgPerSecIn.at(i) = (double)(msgInNew.at(i) - msgIn.at(i)) / (double)msSinceLastLog * 1000.; 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::iterator itr = filteredOutputs.begin(); itr != filteredOutputs.end(); itr++) - { bytesOutNew.at(i) = (*itr)->GetBytesTx(); mbPerSecOut.at(i) = ((double)(bytesOutNew.at(i) - bytesOut.at(i)) / (1024. * 1024.)) / (double)msSinceLastLog * 1000.; bytesOut.at(i) = bytesOutNew.at(i); + msgOutNew.at(i) = (*itr)->GetMessagesTx(); msgPerSecOut.at(i) = (double)(msgOutNew.at(i) - msgOut.at(i)) / (double)msSinceLastLog * 1000.; 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; } @@ -469,59 +478,57 @@ void FairMQDevice::LogSocketRates() } catch (boost::thread_interrupted&) { - LOG(INFO) << "FairMQDevice::LogSocketRates() interrupted"; + // LOG(DEBUG) << "FairMQDevice::LogSocketRates() interrupted"; 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() { - LOG(INFO) << ">>>>>>> closing inputs <<<<<<<"; - for (vector::iterator itr = fPayloadInputs->begin(); itr != fPayloadInputs->end(); itr++) + LOG(DEBUG) << "Closing sockets..."; + + // iterate over the channels map + for (map< string,vector >::iterator mi = fChannels.begin(); mi != fChannels.end(); ++mi) { - (*itr)->Close(); + // iterate over the channels vector + for (vector::iterator vi = (mi->second).begin(); vi != (mi->second).end(); ++vi) + { + vi->fSocket->Close(); + } } - LOG(INFO) << ">>>>>>> closing outputs <<<<<<<"; - for (vector::iterator itr = fPayloadOutputs->begin(); itr != fPayloadOutputs->end(); itr++) - { - (*itr)->Close(); - } + fCommandSocket->Close(); + + LOG(DEBUG) << "Closed all sockets!"; } void FairMQDevice::Terminate() { // Termination signal has to be sent only once to any socket. - // Find available socket and send termination signal to it. - 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."; - } + fCommandSocket->Terminate(); } FairMQDevice::~FairMQDevice() { - for (vector::iterator itr = fPayloadInputs->begin(); itr != fPayloadInputs->end(); itr++) + // iterate over the channels map + for (map< string,vector >::iterator mi = fChannels.begin(); mi != fChannels.end(); ++mi) { - delete (*itr); + // iterate over the channels vector + for (vector::iterator vi = (mi->second).begin(); vi != (mi->second).end(); ++vi) + { + delete vi->fSocket; + } } - for (vector::iterator itr = fPayloadOutputs->begin(); itr != fPayloadOutputs->end(); itr++) - { - delete (*itr); - } - - delete fPayloadInputs; - delete fPayloadOutputs; + delete fCommandSocket; } diff --git a/fairmq/FairMQDevice.h b/fairmq/FairMQDevice.h index 0d884804..5e82544b 100644 --- a/fairmq/FairMQDevice.h +++ b/fairmq/FairMQDevice.h @@ -18,11 +18,13 @@ #include #include #include +#include #include "FairMQConfigurable.h" #include "FairMQStateMachine.h" #include "FairMQTransportFactory.h" #include "FairMQSocket.h" +#include "FairMQChannel.h" class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable { @@ -30,25 +32,10 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable enum { Id = FairMQConfigurable::Last, + MaxInitializationTime, NumIoThreads, - NumInputs, - NumOutputs, PortRangeMin, 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, Last }; @@ -57,57 +44,59 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable virtual void LogSocketRates(); - 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 int value, const int slot = 0); - virtual int GetProperty(const int key, const int default_ = 0, const int slot = 0); + void SortChannel(const std::string& name, const bool reindex = true); + void PrintChannel(const std::string& name); + + 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 ~FairMQDevice(); + std::map< std::string,std::vector > fChannels; + protected: std::string fId; - int fNumIoThreads; + int fMaxInitializationTime; - int fNumInputs; - int fNumOutputs; + int fNumIoThreads; int fPortRangeMin; int fPortRangeMax; - std::vector fInputAddress; - std::vector fInputMethod; - std::vector fInputSocketType; - std::vector fInputSndBufSize; - std::vector fInputRcvBufSize; - std::vector fInputRateLogging; - - std::vector fOutputAddress; - std::vector fOutputMethod; - std::vector fOutputSocketType; - std::vector fOutputSndBufSize; - std::vector fOutputRcvBufSize; - std::vector fOutputRateLogging; - - std::vector* fPayloadInputs; - std::vector* fPayloadOutputs; - int fLogIntervalInMs; + FairMQSocket* fCommandSocket; + FairMQTransportFactory* fTransportFactory; + void InitWrapper(); 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: /// Copy Constructor diff --git a/fairmq/FairMQLogger.cxx b/fairmq/FairMQLogger.cxx index aad0becd..522c3562 100644 --- a/fairmq/FairMQLogger.cxx +++ b/fairmq/FairMQLogger.cxx @@ -18,20 +18,23 @@ int FairMQLogger::fMinLogLevel = FairMQLogger::DEBUG; FairMQLogger::FairMQLogger() - : os(), fLogLevel(DEBUG) + : os() + , fLogLevel(DEBUG) { } FairMQLogger::~FairMQLogger() { - if(fLogLevel>=FairMQLogger::fMinLogLevel && fLogLevel= FairMQLogger::fMinLogLevel && fLogLevel < FairMQLogger::NOLOG) + { std::cout << os.str() << std::endl; + } } std::ostringstream& FairMQLogger::Log(int type) { std::string type_str; - fLogLevel=type; + fLogLevel = type; switch (type) { case DEBUG : diff --git a/fairmq/FairMQLogger.h b/fairmq/FairMQLogger.h index cb250f3e..1b7dedb0 100644 --- a/fairmq/FairMQLogger.h +++ b/fairmq/FairMQLogger.h @@ -25,7 +25,7 @@ class FairMQLogger { - public: + public: enum Level { DEBUG, @@ -35,16 +35,16 @@ class FairMQLogger STATE, NOLOG }; - + FairMQLogger(); virtual ~FairMQLogger(); std::ostringstream& Log(int type); - static void SetLogLevel(int loglevel) - { - FairMQLogger::fMinLogLevel = loglevel; + static void SetLogLevel(int logLevel) + { + FairMQLogger::fMinLogLevel = logLevel; } - + private: std::ostringstream os; int fLogLevel; diff --git a/fairmq/FairMQSocket.h b/fairmq/FairMQSocket.h index da970aad..dafff06f 100644 --- a/fairmq/FairMQSocket.h +++ b/fairmq/FairMQSocket.h @@ -16,6 +16,7 @@ #define FAIRMQSOCKET_H_ #include + #include "FairMQMessage.h" class FairMQSocket @@ -36,10 +37,10 @@ class FairMQSocket virtual bool Bind(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 int flags) = 0; - virtual int Receive(FairMQMessage* msg, const std::string& flag="") = 0; - virtual int Receive(FairMQMessage* msg, const int flags) = 0; + virtual int Send(FairMQMessage* msg, const std::string& flag = "") = 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 int flags = 0) = 0; virtual void* GetSocket() = 0; virtual int GetSocket(int nothing) = 0; diff --git a/fairmq/FairMQStateMachine.cxx b/fairmq/FairMQStateMachine.cxx index 39e466a4..edf13147 100644 --- a/fairmq/FairMQStateMachine.cxx +++ b/fairmq/FairMQStateMachine.cxx @@ -16,7 +16,13 @@ #include "FairMQLogger.h" FairMQStateMachine::FairMQStateMachine() - : fRunningFinished(false) + : fInitializingFinished(false) + , fInitializingCondition() + , fInitializingMutex() + , fInitializingTaskFinished(false) + , fInitializingTaskCondition() + , fInitializingTaskMutex() + , fRunningFinished(false) , fRunningCondition() , fRunningMutex() { @@ -39,20 +45,17 @@ bool FairMQStateMachine::ChangeState(int event) { switch (event) { - case INIT: - process_event(FairMQFSM::INIT()); + case INIT_DEVICE: + process_event(FairMQFSM::INIT_DEVICE()); return true; - case SETOUTPUT: - process_event(FairMQFSM::SETOUTPUT()); + case DEVICE_READY: + process_event(FairMQFSM::DEVICE_READY()); return true; - case SETINPUT: - process_event(FairMQFSM::SETINPUT()); + case INIT_TASK: + process_event(FairMQFSM::INIT_TASK()); return true; - case BIND: - process_event(FairMQFSM::BIND()); - return true; - case CONNECT: - process_event(FairMQFSM::CONNECT()); + case READY: + process_event(FairMQFSM::READY()); return true; case RUN: process_event(FairMQFSM::RUN()); @@ -60,15 +63,27 @@ bool FairMQStateMachine::ChangeState(int event) case PAUSE: process_event(FairMQFSM::PAUSE()); return true; + case RESUME: + process_event(FairMQFSM::RESUME()); + return true; case STOP: process_event(FairMQFSM::STOP()); 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: process_event(FairMQFSM::END()); return true; default: - LOG(ERROR) << "Requested unsupported state: " << event; - LOG(ERROR) << "Supported are: INIT, SETOUTPUT, SETINPUT, BIND, CONNECT, RUN, PAUSE, STOP, END"; + LOG(ERROR) << "Requested unsupported state: " << event << std::endl + << "Supported are: INIT_DEVICE, INIT_TASK, RUN, PAUSE, RESUME, STOP, RESET_TASK, RESET_DEVICE, END"; return false; } } @@ -80,25 +95,21 @@ bool FairMQStateMachine::ChangeState(int 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); - } - else if (event == "CONNECT") - { - return ChangeState(CONNECT); + return ChangeState(READY); } else if (event == "RUN") { @@ -108,18 +119,146 @@ bool FairMQStateMachine::ChangeState(std::string event) { return ChangeState(PAUSE); } + else if (event == "RESUME") + { + return ChangeState(RESUME); + } else if (event == "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") { return ChangeState(END); } else { - LOG(ERROR) << "Requested unsupported state: " << event; - LOG(ERROR) << "Supported are: INIT, SETOUTPUT, SETINPUT, BIND, CONNECT, RUN, PAUSE, STOP, END"; + LOG(ERROR) << "Requested unsupported state: " << event << std::endl + << "Supported are: INIT_DEVICE, INIT_TASK, RUN, PAUSE, RESUME, STOP, RESET_TASK, RESET_DEVICE, END"; return false; } } + +void FairMQStateMachine::WaitForEndOfState(int event) +{ + switch (event) + { + case INIT_DEVICE: + { + boost::unique_lock lock(fInitializingMutex); + while (!fInitializingFinished) + { + fInitializingCondition.wait(lock); + } + break; + } + case INIT_TASK: + { + boost::unique_lock initTaskLock(fInitializingTaskMutex); + while (!fInitializingTaskFinished) + { + fInitializingTaskCondition.wait(initTaskLock); + } + break; + } + case RUN: + { + boost::unique_lock runLock(fRunningMutex); + while (!fRunningFinished) + { + fRunningCondition.wait(runLock); + } + break; + } + case RESET_TASK: + { + boost::unique_lock runLock(fResetTaskMutex); + while (!fResetTaskFinished) + { + fResetTaskCondition.wait(runLock); + } + break; + } + case RESET_DEVICE: + { + boost::unique_lock 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"; + } +} \ No newline at end of file diff --git a/fairmq/FairMQStateMachine.h b/fairmq/FairMQStateMachine.h index 2f17da2d..85f92cd7 100644 --- a/fairmq/FairMQStateMachine.h +++ b/fairmq/FairMQStateMachine.h @@ -15,7 +15,7 @@ #ifndef FAIRMQSTATEMACHINE_H_ #define FAIRMQSTATEMACHINE_H_ -#define FAIRMQ_INTERFACE_VERSION 1 +#define FAIRMQ_INTERFACE_VERSION 2 #include @@ -28,8 +28,6 @@ #include #include #include -#include -#include #include "FairMQLogger.h" @@ -39,199 +37,328 @@ namespace msmf = boost::msm::front; 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 - struct FairMQFSM_ : public msm::front::state_machine_def +// defining events for the boost MSM state machine +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_() + : fState() + , fStateThread() + , fTerminateStateThread() + {} + + // Destructor + virtual ~FairMQFSM_() {}; + + template + void on_entry(Event const&, FSM&) { - FairMQFSM_() - : fState() - , fRunningStateThread() - {} + LOG(STATE) << "Entering FairMQ state machine"; + fState = IDLE; + } - // Destructor - virtual ~FairMQFSM_() {}; + template + void on_exit(Event const&, FSM&) + { + LOG(STATE) << "Exiting FairMQ state machine"; + } - template - void on_entry(Event const&, FSM&) + // The list of FSM states + 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 + void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) { - LOG(STATE) << "Entering FairMQ state machine"; - fState = IDLE; + fsm.fState = IDLE; } - template - 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 - 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 - void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) - { - fsm.fState = INITIALIZING; - fsm.Init(); - } - }; - struct SetOutputFct - { - template - void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) - { - fsm.fState = SETTINGOUTPUT; - fsm.InitOutput(); - } - }; - struct SetInputFct - { - template - void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) - { - fsm.fState = SETTINGINPUT; - fsm.InitInput(); - } - }; - struct BindFct - { - template - void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) - { - fsm.fState = BINDING; - fsm.Bind(); - } - }; - struct ConnectFct - { - template - void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) - { - fsm.fState = CONNECTING; - fsm.Connect(); - } - }; - struct RunFct - { - template - void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) - { - fsm.fState = RUNNING; - fsm.fRunningStateThread = boost::thread(boost::bind(&FairMQFSM_::Run, &fsm)); - } - }; - struct StopFct - { - template - void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) - { - fsm.fState = IDLE; - fsm.Terminate(); - fsm.fRunningStateThread.join(); - } - }; - struct PauseFct - { - template - 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, - msmf::Row, // this is an invalid transition... - msmf::Row, - msmf::Row, - msmf::Row, - msmf::Row, - msmf::Row, - msmf::Row, - msmf::Row, - msmf::Row, - msmf::Row, - msmf::Row > - { - }; - // Replaces the default no-transition response. - template - 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; -} + + struct InitDeviceFct + { + template + 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 + void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) + { + fsm.fState = DEVICE_READY; + } + }; + + struct InitTaskFct + { + template + 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 + void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) + { + fsm.fState = READY; + } + }; + + struct RunFct + { + template + 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 + 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 + 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 + void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) + { + fsm.fState = IDLE; + // fsm.SendCommand("stop"); + fsm.fStateThread.join(); + } + }; + + struct ResetTaskFct + { + template + 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 + 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 + 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 + 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, + msmf::Row, + msmf::Row, + msmf::Row, + msmf::Row, + msmf::Row, + msmf::Row, + msmf::Row, + msmf::Row, + msmf::Row, + msmf::Row, + msmf::Row, + msmf::Row, // temporary + msmf::Row > + {}; + + // Replaces the default no-transition response. + template + 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; +} // namespace FairMQFSM class FairMQStateMachine : public FairMQFSM::FairMQFSM { public: enum Event { - INIT, - SETOUTPUT, - SETINPUT, - BIND, - CONNECT, - PAUSE, + INIT_DEVICE, + DEVICE_READY, + INIT_TASK, + READY, RUN, + PAUSE, + RESUME, STOP, + RESET_TASK, + RESET_DEVICE, + IDLE, END }; FairMQStateMachine(); @@ -242,10 +369,29 @@ class FairMQStateMachine : public FairMQFSM::FairMQFSM bool ChangeState(int 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::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_ */ diff --git a/fairmq/FairMQTransportFactory.h b/fairmq/FairMQTransportFactory.h index 2767648b..f398fbb6 100644 --- a/fairmq/FairMQTransportFactory.h +++ b/fairmq/FairMQTransportFactory.h @@ -18,6 +18,7 @@ #include #include "FairMQMessage.h" +#include "FairMQChannel.h" #include "FairMQSocket.h" #include "FairMQPoller.h" #include "FairMQLogger.h" @@ -28,8 +29,8 @@ class FairMQTransportFactory virtual FairMQMessage* CreateMessage() = 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 FairMQSocket* CreateSocket(const std::string& type, int num, int numIoThreads) = 0; - virtual FairMQPoller* CreatePoller(const std::vector& inputs) = 0; + virtual FairMQSocket* CreateSocket(const std::string& type, const std::string& name, int numIoThreads) = 0; + virtual FairMQPoller* CreatePoller(const std::vector& channels) = 0; virtual ~FairMQTransportFactory() {}; }; diff --git a/fairmq/devices/FairMQBenchmarkSampler.cxx b/fairmq/devices/FairMQBenchmarkSampler.cxx index c9f844d3..30a8ea9e 100644 --- a/fairmq/devices/FairMQBenchmarkSampler.cxx +++ b/fairmq/devices/FairMQBenchmarkSampler.cxx @@ -33,27 +33,19 @@ FairMQBenchmarkSampler::~FairMQBenchmarkSampler() { } -void FairMQBenchmarkSampler::Init() -{ - FairMQDevice::Init(); -} - void FairMQBenchmarkSampler::Run() { - LOG(INFO) << ">>>>>>> Run <<<<<<<"; - - boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); boost::thread resetEventCounter(boost::bind(&FairMQBenchmarkSampler::ResetEventCounter, this)); 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(); - msg->Copy(base_msg); + msg->Copy(baseMsg); - fPayloadOutputs->at(0)->Send(msg); + fChannels["data-out"].at(0).Send(msg); --fEventCounter; @@ -65,28 +57,19 @@ void FairMQBenchmarkSampler::Run() delete msg; } - delete base_msg; + delete baseMsg; try { - rateLogger.interrupt(); - rateLogger.join(); resetEventCounter.interrupt(); resetEventCounter.join(); } catch(boost::thread_resource_error& e) { LOG(ERROR) << e.what(); } - - FairMQDevice::Shutdown(); - - // notify parent thread about end of processing. - boost::lock_guard lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); } void FairMQBenchmarkSampler::ResetEventCounter() { - while (true) + while (GetCurrentState() == RUNNING) { try { @@ -100,61 +83,26 @@ void FairMQBenchmarkSampler::ResetEventCounter() } } -void FairMQBenchmarkSampler::Log(int intervalInMs) -{ - 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*/) +void FairMQBenchmarkSampler::SetProperty(const int key, const string& value) { switch (key) { default: - FairMQDevice::SetProperty(key, value, slot); + FairMQDevice::SetProperty(key, value); 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) { 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) { @@ -165,12 +113,12 @@ void FairMQBenchmarkSampler::SetProperty(const int key, const int value, const i fEventRate = value; break; default: - FairMQDevice::SetProperty(key, value, slot); + FairMQDevice::SetProperty(key, value); 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) { @@ -179,6 +127,6 @@ int FairMQBenchmarkSampler::GetProperty(const int key, const int default_ /*= 0* case EventRate: return fEventRate; default: - return FairMQDevice::GetProperty(key, default_, slot); + return FairMQDevice::GetProperty(key, default_); } } diff --git a/fairmq/devices/FairMQBenchmarkSampler.h b/fairmq/devices/FairMQBenchmarkSampler.h index 429ed2c7..63a87e5b 100644 --- a/fairmq/devices/FairMQBenchmarkSampler.h +++ b/fairmq/devices/FairMQBenchmarkSampler.h @@ -29,25 +29,27 @@ class FairMQBenchmarkSampler : public FairMQDevice enum { InputFile = FairMQDevice::Last, - EventRate, EventSize, + EventRate, Last }; + FairMQBenchmarkSampler(); virtual ~FairMQBenchmarkSampler(); + void Log(int intervalInMs); 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 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); protected: int fEventSize; int fEventRate; int fEventCounter; - virtual void Init(); virtual void Run(); }; diff --git a/fairmq/devices/FairMQBuffer.cxx b/fairmq/devices/FairMQBuffer.cxx index 276970ed..5284a22d 100644 --- a/fairmq/devices/FairMQBuffer.cxx +++ b/fairmq/devices/FairMQBuffer.cxx @@ -26,39 +26,17 @@ FairMQBuffer::FairMQBuffer() void FairMQBuffer::Run() { - LOG(INFO) << ">>>>>>> Run <<<<<<<"; - - boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); - - int received = 0; - while (fState == RUNNING) + while (GetCurrentState() == RUNNING) { FairMQMessage* msg = fTransportFactory->CreateMessage(); - received = fPayloadInputs->at(0)->Receive(msg); - - if (received > 0) + if (fChannels["data-in"].at(0).Receive(msg) > 0) { - fPayloadOutputs->at(0)->Send(msg); - received = 0; + fChannels["data-out"].at(0).Send(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 lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); } FairMQBuffer::~FairMQBuffer() diff --git a/fairmq/devices/FairMQMerger.cxx b/fairmq/devices/FairMQMerger.cxx index cfd29a8b..a274055d 100644 --- a/fairmq/devices/FairMQMerger.cxx +++ b/fairmq/devices/FairMQMerger.cxx @@ -29,30 +29,22 @@ FairMQMerger::~FairMQMerger() void FairMQMerger::Run() { - LOG(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; - - while (fState == RUNNING) + while (GetCurrentState() == RUNNING) { FairMQMessage* msg = fTransportFactory->CreateMessage(); poller->Poll(100); - for (int i = 0; i < fNumInputs; i++) + for (int i = 0; i < fChannels["data-in"].size(); ++i) { if (poller->CheckInput(i)) { - received = fPayloadInputs->at(i)->Receive(msg); - } - if (received > 0) - { - fPayloadOutputs->at(0)->Send(msg); - received = 0; + if (fChannels["data-in"].at(i).Receive(msg)) + { + fChannels["data-out"].at(0).Send(msg); + } } } @@ -60,18 +52,4 @@ void FairMQMerger::Run() } 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 lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); } diff --git a/fairmq/devices/FairMQProxy.cxx b/fairmq/devices/FairMQProxy.cxx index 1f961af4..f6249168 100644 --- a/fairmq/devices/FairMQProxy.cxx +++ b/fairmq/devices/FairMQProxy.cxx @@ -28,37 +28,15 @@ FairMQProxy::~FairMQProxy() void FairMQProxy::Run() { - LOG(INFO) << ">>>>>>> Run <<<<<<<"; - - boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); - FairMQMessage* msg = fTransportFactory->CreateMessage(); - int received = 0; - - while (fState == RUNNING) + while (GetCurrentState() == RUNNING) { - received = fPayloadInputs->at(0)->Receive(msg); - if (received > 0) + if (fChannels["data-in"].at(0).Receive(msg) > 0) { - fPayloadOutputs->at(0)->Send(msg); - received = 0; + fChannels["data-out"].at(0).Send(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 lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); } diff --git a/fairmq/devices/FairMQSink.cxx b/fairmq/devices/FairMQSink.cxx index ae6bf9e6..ba50e24b 100644 --- a/fairmq/devices/FairMQSink.cxx +++ b/fairmq/devices/FairMQSink.cxx @@ -24,34 +24,14 @@ FairMQSink::FairMQSink() void FairMQSink::Run() { - LOG(INFO) << ">>>>>>> Run <<<<<<<"; - - boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); - - int received = 0; - - while (fState == RUNNING) + while (GetCurrentState() == RUNNING) { FairMQMessage* msg = fTransportFactory->CreateMessage(); - received = fPayloadInputs->at(0)->Receive(msg); + fChannels["data-in"].at(0).Receive(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 lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); } FairMQSink::~FairMQSink() diff --git a/fairmq/devices/FairMQSplitter.cxx b/fairmq/devices/FairMQSplitter.cxx index 93b41e20..50385ea1 100644 --- a/fairmq/devices/FairMQSplitter.cxx +++ b/fairmq/devices/FairMQSplitter.cxx @@ -28,44 +28,22 @@ FairMQSplitter::~FairMQSplitter() void FairMQSplitter::Run() { - LOG(INFO) << ">>>>>>> Run <<<<<<<"; - - boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); - - int received = 0; int direction = 0; - while (fState == RUNNING) + while (GetCurrentState() == RUNNING) { FairMQMessage* msg = fTransportFactory->CreateMessage(); - received = fPayloadInputs->at(0)->Receive(msg); - - if (received > 0) + if (fChannels["data-in"].at(0).Receive(msg) > 0) { - fPayloadOutputs->at(direction)->Send(msg); + fChannels["data-out"].at(direction).Send(msg); direction++; - if (direction >= fNumOutputs) + if (direction >= fChannels["data-out"].size()) { direction = 0; } - received = 0; } 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 lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); } diff --git a/fairmq/devices/GenericFileSink.h b/fairmq/devices/GenericFileSink.h index d1f48269..817a2314 100644 --- a/fairmq/devices/GenericFileSink.h +++ b/fairmq/devices/GenericFileSink.h @@ -21,8 +21,6 @@ #include #include "FairMQLogger.h" - - /********************************************************************* * -------------- NOTES ----------------------- * All policies must have a default constructor @@ -38,87 +36,57 @@ * OutputPolicy::InitOutputFile() **********************************************************************/ - - #include "FairMQDevice.h" -template < typename InputPolicy, - typename OutputPolicy> -class GenericFileSink : public FairMQDevice, - public InputPolicy, - public OutputPolicy -{ -public: - GenericFileSink(): - InputPolicy(), - OutputPolicy() +template +class GenericFileSink : public FairMQDevice, public InputPolicy, public OutputPolicy +{ + public: + GenericFileSink() + : InputPolicy() + , OutputPolicy() {} - + virtual ~GenericFileSink() {} - - + void SetTransport(FairMQTransportFactory* transport) { FairMQDevice::SetTransport(transport); } template - void InitInputContainer(Args... args) - { - InputPolicy::InitContainer(std::forward(args)...); - } -protected: - - virtual void Init() + void InitInputContainer(Args... args) { - FairMQDevice::Init(); - OutputPolicy::InitOutputFile(); + InputPolicy::InitContainer(std::forward(args)...); } protected: + virtual void InitTask() + { + OutputPolicy::InitOutputFile(); + } + virtual void Run() { - MQLOG(INFO) << ">>>>>>> Run <<<<<<<"; - - boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); - - int received = 0; int receivedMsg = 0; - while (fState == RUNNING) + while (GetCurrentState() == RUNNING) { 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)); receivedMsg++; } + delete msg; } 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 lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); } - - }; -#endif /* GENERICFILESINK_H */ +#endif /* GENERICFILESINK_H */ diff --git a/fairmq/devices/GenericFileSink.tpl b/fairmq/devices/GenericFileSink.tpl index 4a28a32d..48e11f13 100644 --- a/fairmq/devices/GenericFileSink.tpl +++ b/fairmq/devices/GenericFileSink.tpl @@ -6,9 +6,9 @@ */ template -GenericFileSink::GenericFileSink() : - InputPolicy(), - OutputPolicy() +GenericFileSink::GenericFileSink() + : InputPolicy() + , OutputPolicy() { } @@ -21,17 +21,16 @@ template void GenericFileSink::SetTransport(FairMQTransportFactory* transport) { FairMQDevice::SetTransport(transport); - //InputPolicy::SetTransport(transport); + // InputPolicy::SetTransport(transport); } template -void GenericFileSink::Init() +void GenericFileSink::InitTask() { - FairMQDevice::Init(); InitOutputFile(); - //InputPolicy::Init(); - //OutputPolicy::Init(); + // InputPolicy::Init(); + // OutputPolicy::Init(); } template @@ -43,18 +42,12 @@ void GenericFileSink::InitOutputFile() template void GenericFileSink::Run() { - MQLOG(INFO) << ">>>>>>> Run <<<<<<<"; - - boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); - - int received = 0; int receivedMsg = 0; - while (fState == RUNNING) + while (GetCurrentState() == RUNNING) { 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)); receivedMsg++; @@ -63,20 +56,4 @@ void GenericFileSink::Run() } 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 lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); } diff --git a/fairmq/devices/GenericMerger.h b/fairmq/devices/GenericMerger.h index 6ac036df..5dd2849b 100644 --- a/fairmq/devices/GenericMerger.h +++ b/fairmq/devices/GenericMerger.h @@ -17,19 +17,14 @@ #include "FairMQPoller.h" -template < typename MergerPolicy, - typename InputPolicy, - typename OutputPolicy - > -class GenericMerger : public FairMQDevice, - public MergerPolicy, - public InputPolicy, - public OutputPolicy +template +class GenericMerger : public FairMQDevice, public MergerPolicy, public InputPolicy, public OutputPolicy { public: - GenericMerger() : fBlockingTime(100) + GenericMerger() + : fBlockingTime(100) {} - + virtual ~GenericMerger() {} @@ -37,44 +32,35 @@ class GenericMerger : public FairMQDevice, { FairMQDevice::SetTransport(transport); } - - - - + protected: - int fBlockingTime; - - + virtual void Run() { - MQLOG(INFO) << ">>>>>>> Run <<<<<<<"; - - boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); - - FairMQPoller* poller = fTransportFactory->CreatePoller(*fPayloadInputs); + FairMQPoller* poller = fTransportFactory->CreatePoller(fChannels["data-in"]); int received = 0; - while (fState == RUNNING) + while (GetCurrentState() == RUNNING) { FairMQMessage* msg = fTransportFactory->CreateMessage(); - //MergerPolicy:: + // MergerPolicy:: poller->Poll(fBlockingTime); - for (int i = 0; i < fNumInputs; i++) + for (int i = 0; i < fChannels["datain"].size(); i++) { if (poller->CheckInput(i)) { - received = fPayloadInputs->at(i)->Receive(msg); + received = fChannels["data-in"].at(i).Receive(msg) MergerPolicy::Merge(InputPolicy::DeSerializeMsg(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; } } @@ -83,25 +69,8 @@ class GenericMerger : public FairMQDevice, } 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 lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); } }; -#endif /* GENERICMERGER_H */ +#endif /* GENERICMERGER_H */ diff --git a/fairmq/devices/GenericProcessor.h b/fairmq/devices/GenericProcessor.h index 0cd67276..746076d1 100644 --- a/fairmq/devices/GenericProcessor.h +++ b/fairmq/devices/GenericProcessor.h @@ -13,11 +13,10 @@ */ #ifndef GENERICPROCESSOR_H -#define GENERICPROCESSOR_H +#define GENERICPROCESSOR_H #include "FairMQDevice.h" - /********************************************************************* * -------------- NOTES ----------------------- * All policies must have a default constructor @@ -41,73 +40,67 @@ * **********************************************************************/ - -template < typename InputPolicy, - typename OutputPolicy, - typename TaskPolicy> -class GenericProcessor: public FairMQDevice, - public InputPolicy, - public OutputPolicy, - public TaskPolicy +template +class GenericProcessor : public FairMQDevice, public InputPolicy, public OutputPolicy, public TaskPolicy { public: - GenericProcessor() : InputPolicy(), OutputPolicy(), TaskPolicy() + GenericProcessor() + : InputPolicy() + , OutputPolicy() + , TaskPolicy() {} virtual ~GenericProcessor() {} - + // the four following methods ensure // that the correct policy method is called - + void SetTransport(FairMQTransportFactory* transport) { FairMQDevice::SetTransport(transport); } - - template - void InitTask(Args... args) - { - TaskPolicy::InitTask(std::forward(args)...); - } template - void InitInputContainer(Args... args) - { - InputPolicy::InitContainer(std::forward(args)...); - } + void InitTask(Args... args) + { + TaskPolicy::InitTask(std::forward(args)...); + } template - void InitOutputContainer(Args... args) - { - OutputPolicy::InitContainer(std::forward(args)...); - } + void InitInputContainer(Args... args) + { + InputPolicy::InitContainer(std::forward(args)...); + } - + template + void InitOutputContainer(Args... args) + { + OutputPolicy::InitContainer(std::forward(args)...); + } - /* * // *********************** TODO: implement multipart features 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(); } - //void SendPart(); - //bool ReceivePart(); + // void SendPart(); + // bool ReceivePart(); bool ReceivePart() { int64_t more = 0; size_t more_size = sizeof(more); - fPayloadInputs->at(0)->GetOption("rcv-more", &more, &more_size); - if(more) + fChannels["data-in"].at(0).fSocket->GetOption("rcv-more", &more, &more_size); + if (more) { InputPolicy::CloseMessage(); - //fProcessorTask->GetPayload()->CloseMessage(); + // fProcessorTask->GetPayload()->CloseMessage(); fProcessorTask->SetPayload(fTransportFactory->CreateMessage()); - return fPayloadInputs->at(0)->Receive(fProcessorTask->GetPayload()); + return fChannels["data-in"].at(0).Receive(fProcessorTask->GetPayload()); } else { @@ -117,29 +110,23 @@ class GenericProcessor: public FairMQDevice, */ protected: - virtual void Init() + virtual void InitTask() { - FairMQDevice::Init(); // TODO: implement multipart features - //fProcessorTask->InitTask(); - //fProcessorTask->SetSendPart(boost::bind(&FairMQProcessor::SendPart, this)); - //fProcessorTask->SetReceivePart(boost::bind(&FairMQProcessor::ReceivePart, this)); + // fProcessorTask->InitTask(); + // fProcessorTask->SetSendPart(boost::bind(&FairMQProcessor::SendPart, this)); + // fProcessorTask->SetReceivePart(boost::bind(&FairMQProcessor::ReceivePart, this)); } virtual void Run() { - MQLOG(INFO) << ">>>>>>> Run <<<<<<<"; - - boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); int receivedMsgs = 0; int sentMsgs = 0; - int received = 0; - while ( fState == RUNNING ) + while (GetCurrentState() == RUNNING) { FairMQMessage* msg = fTransportFactory->CreateMessage(); - received = fPayloadInputs->at(0)->Receive(msg); receivedMsgs++; // InputPolicy::DeSerializeMsg(msg) --> deserialize data of msg and fill output container @@ -148,37 +135,22 @@ class GenericProcessor: public FairMQDevice, // OutputPolicy::fMessage point to msg OutputPolicy::SetMessage(msg); - - if (received > 0) + + if (fChannels["data-in"].at(0).Receive(msg) > 0) { // TaskPolicy::GetOutputData() --> Get processed output container // 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++; - received = 0; } - if(msg) + if (msg) + { msg->CloseMessage(); + } } 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 lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); } }; diff --git a/fairmq/devices/GenericSampler.h b/fairmq/devices/GenericSampler.h index 97c2c332..89eeda76 100644 --- a/fairmq/devices/GenericSampler.h +++ b/fairmq/devices/GenericSampler.h @@ -12,7 +12,6 @@ * Created on November 24, 2014, 3:30 PM */ - #ifndef GENERICSAMPLER_H #define GENERICSAMPLER_H @@ -45,38 +44,34 @@ * **********************************************************************/ -template -class GenericSampler: public FairMQDevice, - public SamplerPolicy, - public OutputPolicy -{ +template +class GenericSampler : public FairMQDevice, public SamplerPolicy, public OutputPolicy +{ public: - - enum + enum { InputFile = FairMQDevice::Last, Branch, ParFile, EventRate }; - + GenericSampler(); virtual ~GenericSampler(); + virtual void SetTransport(FairMQTransportFactory* factory); void ResetEventCounter(); - virtual void ListenToCommands(); template - void SetFileProperties(Args&... args) - { - SamplerPolicy::SetFileProperties(args...); - } + void SetFileProperties(Args&... args) + { + SamplerPolicy::SetFileProperties(args...); + } - 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 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); /** * 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 //void SendPart(); - void SetContinuous(bool flag) { fContinuous = flag; } + void SetContinuous(bool flag); -protected: - virtual void Init(); - virtual void Run(); + protected: + virtual void InitTask(); + virtual void Run(); -protected: - std::string fInputFile; // Filename of a root file containing the simulated digis. - std::string fParFile; - std::string fBranch; // The name of the sub-detector branch to stream the digis from. - int64_t fNumEvents; - int fEventRate; - int fEventCounter; - bool fContinuous; + int64_t fNumEvents; + int fEventRate; + int fEventCounter; + bool fContinuous; + std::string fInputFile; // Filename of a root file containing the simulated digis. + std::string fParFile; + std::string fBranch; // The name of the sub-detector branch to stream the digis from. }; #include "GenericSampler.tpl" -#endif /* GENERICSAMPLER_H */ +#endif /* GENERICSAMPLER_H */ diff --git a/fairmq/devices/GenericSampler.tpl b/fairmq/devices/GenericSampler.tpl index 75e4dd47..42790c51 100644 --- a/fairmq/devices/GenericSampler.tpl +++ b/fairmq/devices/GenericSampler.tpl @@ -6,11 +6,14 @@ */ template -GenericSampler::GenericSampler() : - fNumEvents(0), - fEventRate(1), - fEventCounter(0), - fContinuous(false) +GenericSampler::GenericSampler() + : fNumEvents(0) + , fEventRate(1) + , fEventCounter(0) + , fContinuous(false) + , fInputFile() + , fParFile() + , fBranch() { } @@ -23,202 +26,152 @@ template void GenericSampler::SetTransport(FairMQTransportFactory* factory) { FairMQDevice::SetTransport(factory); - //OutputPolicy::SetTransport(factory); + // OutputPolicy::SetTransport(factory); } template -void GenericSampler::Init() +void GenericSampler::InitTask() { - FairMQDevice::Init(); - SamplerPolicy::InitSampler(); - fNumEvents=SamplerPolicy::GetNumberOfEvent(); + SamplerPolicy::InitSampler(); + fNumEvents = SamplerPolicy::GetNumberOfEvent(); } template void GenericSampler::Run() { - LOG(INFO) << ">>>>>>> Run <<<<<<<"; + // boost::thread resetEventCounter(boost::bind(&GenericSampler::ResetEventCounter, this)); - boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); - // boost::thread resetEventCounter(boost::bind(&GenericSampler::ResetEventCounter, this)); - // boost::thread commandListener(boost::bind(&GenericSampler::ListenToCommands, this)); + int sentMsgs = 0; - 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; - -// while ( fState == RUNNING ) { - - do - { - for ( int64_t eventNr = 0 ; eventNr < fNumEvents; ++eventNr ) + do { + for (int64_t eventNr = 0; eventNr < fNumEvents; ++eventNr) { //fSamplerTask->SetEventIndex(eventNr); FairMQMessage* msg = fTransportFactory->CreateMessage(); OutputPolicy::SetMessage(msg); - fPayloadOutputs->at(0)->Send(OutputPolicy::SerializeMsg( - SamplerPolicy::GetDataBranch(eventNr))); + fChannels["data-out"].at(0).Send(OutputPolicy::SerializeMsg(SamplerPolicy::GetDataBranch(eventNr))); ++sentMsgs; - if(msg) + if (msg) + { msg->CloseMessage(); - + } + // Optional event rate limiting // --fEventCounter; // while (fEventCounter == 0) { // 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); - 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 lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); + boost::timer::cpu_times const elapsed_time(timer.elapsed()); + LOG(INFO) << "Sent everything in:\n" << boost::timer::format(elapsed_time, 2); + LOG(INFO) << "Sent " << sentMsgs << " messages!"; } /* template void GenericSampler::SendPart() { - fPayloadOutputs->at(0)->Send(OutputPolicy::GetMessage(), "snd-more"); - OutputPolicy::CloseMessage(); + fChannels["data-out"].at(0).Send(OutputPolicy::GetMessage(), "snd-more"); + OutputPolicy::CloseMessage(); } */ +template +void GenericSampler::SetContinuous(bool flag) +{ + fContinuous = flag; +} + template void GenericSampler::ResetEventCounter() { - while (true) - { - try + while (GetCurrentState() == RUNNING) { - fEventCounter = fEventRate / 100; - boost::this_thread::sleep(boost::posix_time::milliseconds(10)); + try + { + 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 +void GenericSampler::SetProperty(const int key, const std::string& value) +{ + switch (key) { - LOG(DEBUG) << "resetEventCounter interrupted"; - break; + case InputFile: + fInputFile = value; + break; + case ParFile: + fParFile = value; + break; + case Branch: + fBranch = value; + break; + default: + FairMQDevice::SetProperty(key, value); + break; } - } - LOG(DEBUG) << ">>>>>>> stopping resetEventCounter <<<<<<<"; } template -void GenericSampler::ListenToCommands() +std::string GenericSampler::GetProperty(const int key, const std::string& default_/*= ""*/) { - LOG(INFO) << ">>>>>>> ListenToCommands <<<<<<<"; - - int received = 0; - - while (true) { - try { - FairMQMessage *msg = fTransportFactory->CreateMessage(); - - received = fPayloadInputs->at(0)->Receive(msg); - - if (received > 0) { - // command handling goes here. - LOG(INFO) << "> received command <"; - received = 0; - } - - delete msg; - - boost::this_thread::interruption_point(); + switch (key) + { + case InputFile: + return fInputFile; + case ParFile: + return fParFile; + case Branch: + return fBranch; + default: + return FairMQDevice::GetProperty(key, default_); } - catch (boost::thread_interrupted &) { - LOG(DEBUG) << "commandListener interrupted"; - break; +} + +template +void GenericSampler::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 -void GenericSampler::SetProperty(const int key, const std::string& value, const int slot/*= 0*/) +int GenericSampler::GetProperty(const int key, const int default_/*= 0*/) { - switch (key) - { - case InputFile: - fInputFile = value; - break; - case ParFile: - fParFile = value; - break; - case Branch: - fBranch = value; - break; - default: - FairMQDevice::SetProperty(key, value, slot); - break; - } -} - -template -std::string GenericSampler::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 -void GenericSampler::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 -int GenericSampler::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); - } + switch (key) + { + case EventRate: + return fEventRate; + default: + return FairMQDevice::GetProperty(key, default_); + } } diff --git a/fairmq/examples/req-rep/FairMQExampleClient.cxx b/fairmq/examples/req-rep/FairMQExampleClient.cxx index d7978279..8f24e0cd 100644 --- a/fairmq/examples/req-rep/FairMQExampleClient.cxx +++ b/fairmq/examples/req-rep/FairMQExampleClient.cxx @@ -36,9 +36,7 @@ void FairMQExampleClient::CustomCleanup(void *data, void *hint) void FairMQExampleClient::Run() { - // boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); - - while (fState == RUNNING) + while (GetCurrentState() == RUNNING) { boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); @@ -49,26 +47,17 @@ void FairMQExampleClient::Run() LOG(INFO) << "Sending \"" << fText << "\" to server."; - fPayloadOutputs->at(0)->Send(request); - fPayloadOutputs->at(0)->Receive(reply); + fChannels["data"].at(0).Send(request); + fChannels["data"].at(0).Receive(reply); LOG(INFO) << "Received reply from server: \"" << string(static_cast(reply->GetData()), reply->GetSize()) << "\""; delete reply; } - - // rateLogger.interrupt(); - // rateLogger.join(); - - FairMQDevice::Shutdown(); - - boost::lock_guard 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) { @@ -76,12 +65,12 @@ void FairMQExampleClient::SetProperty(const int key, const string& value, const fText = value; break; default: - FairMQDevice::SetProperty(key, value, slot); + FairMQDevice::SetProperty(key, value); 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) { @@ -89,25 +78,25 @@ string FairMQExampleClient::GetProperty(const int key, const string& default_ /* return fText; break; 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) { default: - FairMQDevice::SetProperty(key, value, slot); + FairMQDevice::SetProperty(key, value); 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) { default: - return FairMQDevice::GetProperty(key, default_, slot); + return FairMQDevice::GetProperty(key, default_); } } diff --git a/fairmq/examples/req-rep/FairMQExampleClient.h b/fairmq/examples/req-rep/FairMQExampleClient.h index 63ba1f6a..633557ac 100644 --- a/fairmq/examples/req-rep/FairMQExampleClient.h +++ b/fairmq/examples/req-rep/FairMQExampleClient.h @@ -30,12 +30,12 @@ class FairMQExampleClient : public FairMQDevice 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 std::string GetProperty(const int key, const std::string& default_ = "", const int slot = 0); - 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); protected: std::string fText; diff --git a/fairmq/examples/req-rep/FairMQExampleServer.cxx b/fairmq/examples/req-rep/FairMQExampleServer.cxx index 549bb2e4..4a740e34 100644 --- a/fairmq/examples/req-rep/FairMQExampleServer.cxx +++ b/fairmq/examples/req-rep/FairMQExampleServer.cxx @@ -31,15 +31,13 @@ void FairMQExampleServer::CustomCleanup(void *data, void *hint) void FairMQExampleServer::Run() { - // boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); - - while (fState == RUNNING) + while (GetCurrentState() == RUNNING) { boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); FairMQMessage* request = fTransportFactory->CreateMessage(); - fPayloadInputs->at(0)->Receive(request); + fChannels["data"].at(0).Receive(request); LOG(INFO) << "Received request from client: \"" << string(static_cast(request->GetData()), request->GetSize()) << "\""; @@ -51,17 +49,8 @@ void FairMQExampleServer::Run() FairMQMessage* reply = fTransportFactory->CreateMessage(const_cast(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 lock(fRunningMutex); - fRunningFinished = true; - fRunningCondition.notify_one(); } FairMQExampleServer::~FairMQExampleServer() diff --git a/fairmq/examples/req-rep/runExampleClient.cxx b/fairmq/examples/req-rep/runExampleClient.cxx index bd110453..5eda0879 100644 --- a/fairmq/examples/req-rep/runExampleClient.cxx +++ b/fairmq/examples/req-rep/runExampleClient.cxx @@ -32,12 +32,11 @@ FairMQExampleClient client; 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); - cout << "Shutdown complete. Bye!" << endl; + LOG(INFO) << "Shutdown complete."; exit(1); } @@ -115,33 +114,31 @@ int main(int argc, char** argv) client.SetProperty(FairMQExampleClient::Id, "client"); 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.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.fChannels["data"].push_back(requestChannel); - 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); - - // wait until the running thread has finished processing. - boost::unique_lock lock(client.fRunningMutex); - while (!client.fRunningFinished) - { - client.fRunningCondition.wait(lock); - } + client.WaitForEndOfState(FairMQExampleClient::RUN); 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); return 0; diff --git a/fairmq/examples/req-rep/runExampleServer.cxx b/fairmq/examples/req-rep/runExampleServer.cxx index f1324100..6801e007 100644 --- a/fairmq/examples/req-rep/runExampleServer.cxx +++ b/fairmq/examples/req-rep/runExampleServer.cxx @@ -30,12 +30,11 @@ FairMQExampleServer server; 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); - cout << "Shutdown complete. Bye!" << endl; + LOG(INFO) << "Caught signal " << signal; exit(1); } @@ -65,34 +64,31 @@ int main(int argc, char** argv) server.SetProperty(FairMQExampleServer::Id, "server"); 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.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.fChannels["data"].push_back(replyChannel); - server.ChangeState(FairMQExampleServer::SETOUTPUT); - server.ChangeState(FairMQExampleServer::SETINPUT); - server.ChangeState(FairMQExampleServer::BIND); - server.ChangeState(FairMQExampleServer::CONNECT); + server.ChangeState(FairMQExampleServer::INIT_DEVICE); + server.WaitForEndOfState(FairMQExampleServer::INIT_DEVICE); - LOG(INFO) << "Listening for requests!"; + server.ChangeState(FairMQExampleServer::INIT_TASK); + server.WaitForEndOfState(FairMQExampleServer::INIT_TASK); server.ChangeState(FairMQExampleServer::RUN); - - // wait until the running thread has finished processing. - boost::unique_lock lock(server.fRunningMutex); - while (!server.fRunningFinished) - { - server.fRunningCondition.wait(lock); - } + server.WaitForEndOfState(FairMQExampleServer::RUN); 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); return 0; diff --git a/fairmq/nanomsg/FairMQMessageNN.cxx b/fairmq/nanomsg/FairMQMessageNN.cxx index 76b53a64..7f865ff9 100644 --- a/fairmq/nanomsg/FairMQMessageNN.cxx +++ b/fairmq/nanomsg/FairMQMessageNN.cxx @@ -63,7 +63,7 @@ FairMQMessageNN::FairMQMessageNN(void* data, size_t size, fairmq_free_fn *ffn, v fSize = size; fReceiving = false; - if(ffn) + if (ffn) { ffn(data, hint); } @@ -140,8 +140,7 @@ void FairMQMessageNN::Copy(FairMQMessage* msg) { if (fMessage) { - int rc = nn_freemsg(fMessage); - if (rc < 0) + if (nn_freemsg(fMessage) < 0) { LOG(ERROR) << "failed freeing message, reason: " << nn_strerror(errno); } @@ -160,8 +159,7 @@ void FairMQMessageNN::Copy(FairMQMessage* msg) inline void FairMQMessageNN::Clear() { - int rc = nn_freemsg(fMessage); - if (rc < 0) + if (nn_freemsg(fMessage) < 0) { LOG(ERROR) << "failed freeing message, reason: " << nn_strerror(errno); } diff --git a/fairmq/nanomsg/FairMQPollerNN.cxx b/fairmq/nanomsg/FairMQPollerNN.cxx index 3f1d19d5..e6659438 100644 --- a/fairmq/nanomsg/FairMQPollerNN.cxx +++ b/fairmq/nanomsg/FairMQPollerNN.cxx @@ -15,32 +15,38 @@ #include #include "FairMQPollerNN.h" +#include "FairMQLogger.h" using namespace std; -FairMQPollerNN::FairMQPollerNN(const vector& inputs) +FairMQPollerNN::FairMQPollerNN(const vector& channels) : items() , fNumItems() { - fNumItems = inputs.size(); + fNumItems = channels.size(); 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; } } 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) { if (items[index].revents & NN_POLLIN) + { return true; + } return false; } @@ -48,7 +54,9 @@ bool FairMQPollerNN::CheckInput(int index) bool FairMQPollerNN::CheckOutput(int index) { if (items[index].revents & NN_POLLOUT) + { return true; + } return false; } @@ -56,5 +64,7 @@ bool FairMQPollerNN::CheckOutput(int index) FairMQPollerNN::~FairMQPollerNN() { if (items != NULL) + { delete[] items; + } } diff --git a/fairmq/nanomsg/FairMQPollerNN.h b/fairmq/nanomsg/FairMQPollerNN.h index 95aff3e6..ba146b10 100644 --- a/fairmq/nanomsg/FairMQPollerNN.h +++ b/fairmq/nanomsg/FairMQPollerNN.h @@ -18,12 +18,12 @@ #include #include "FairMQPoller.h" -#include "FairMQSocket.h" +#include "FairMQChannel.h" class FairMQPollerNN : public FairMQPoller { public: - FairMQPollerNN(const std::vector& inputs); + FairMQPollerNN(const std::vector& channels); virtual void Poll(int timeout); virtual bool CheckInput(int index); diff --git a/fairmq/nanomsg/FairMQSocketNN.cxx b/fairmq/nanomsg/FairMQSocketNN.cxx index 05f7aa6b..d5f0a591 100644 --- a/fairmq/nanomsg/FairMQSocketNN.cxx +++ b/fairmq/nanomsg/FairMQSocketNN.cxx @@ -20,7 +20,7 @@ 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) , fSocket() , fId() @@ -29,9 +29,7 @@ FairMQSocketNN::FairMQSocketNN(const string& type, int num, int numIoThreads) , fMessagesTx(0) , fMessagesRx(0) { - stringstream id; - id << type << "." << num; - fId = id.str(); + fId = name + "." + type; if (numIoThreads > 1) { @@ -46,7 +44,7 @@ FairMQSocketNN::FairMQSocketNN(const string& type, int num, int numIoThreads) fSocket = nn_socket(AF_SP_RAW, GetConstant(type)); 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); } } @@ -55,7 +53,7 @@ FairMQSocketNN::FairMQSocketNN(const string& type, int num, int numIoThreads) fSocket = nn_socket(AF_SP, GetConstant(type)); 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); } 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() @@ -74,12 +72,12 @@ string FairMQSocketNN::GetId() 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()); 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 true; @@ -87,12 +85,12 @@ bool FairMQSocketNN::Bind(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()); 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); 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 { @@ -120,7 +118,7 @@ int FairMQSocketNN::Send(FairMQMessage* msg, const int flags) int rc = nn_send(fSocket, &ptr, NN_MSG, flags); 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 { @@ -138,7 +136,7 @@ int FairMQSocketNN::Receive(FairMQMessage* msg, const string& flag) int rc = nn_recv(fSocket, &ptr, NN_MSG, 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 { @@ -157,7 +155,7 @@ int FairMQSocketNN::Receive(FairMQMessage* msg, const int flags) int rc = nn_recv(fSocket, &ptr, NN_MSG, flags); 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 { diff --git a/fairmq/nanomsg/FairMQSocketNN.h b/fairmq/nanomsg/FairMQSocketNN.h index 39b2b217..4317fe78 100644 --- a/fairmq/nanomsg/FairMQSocketNN.h +++ b/fairmq/nanomsg/FairMQSocketNN.h @@ -26,17 +26,17 @@ class FairMQSocketNN : public FairMQSocket { 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 bool Bind(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 int flags); - virtual int Receive(FairMQMessage* msg, const std::string& flag=""); - virtual int Receive(FairMQMessage* msg, const int flags); + virtual int Send(FairMQMessage* msg, const std::string& flag = ""); + virtual int Send(FairMQMessage* msg, const int flags = 0); + virtual int Receive(FairMQMessage* msg, const std::string& flag = ""); + virtual int Receive(FairMQMessage* msg, const int flags = 0); virtual void* GetSocket(); virtual int GetSocket(int nothing); diff --git a/fairmq/nanomsg/FairMQTransportFactoryNN.cxx b/fairmq/nanomsg/FairMQTransportFactoryNN.cxx index 1dac556f..63ff874b 100644 --- a/fairmq/nanomsg/FairMQTransportFactoryNN.cxx +++ b/fairmq/nanomsg/FairMQTransportFactoryNN.cxx @@ -36,12 +36,12 @@ FairMQMessage* FairMQTransportFactoryNN::CreateMessage(void* data, size_t size, 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& inputs) +FairMQPoller* FairMQTransportFactoryNN::CreatePoller(const vector& channels) { - return new FairMQPollerNN(inputs); + return new FairMQPollerNN(channels); } diff --git a/fairmq/nanomsg/FairMQTransportFactoryNN.h b/fairmq/nanomsg/FairMQTransportFactoryNN.h index 8c12878c..973a9831 100644 --- a/fairmq/nanomsg/FairMQTransportFactoryNN.h +++ b/fairmq/nanomsg/FairMQTransportFactoryNN.h @@ -30,8 +30,8 @@ class FairMQTransportFactoryNN : public FairMQTransportFactory virtual FairMQMessage* CreateMessage(); virtual FairMQMessage* CreateMessage(size_t size); 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 FairMQPoller* CreatePoller(const std::vector& inputs); + virtual FairMQSocket* CreateSocket(const std::string& type, const std::string& name, int numIoThreads); + virtual FairMQPoller* CreatePoller(const std::vector& channels); virtual ~FairMQTransportFactoryNN() {}; }; diff --git a/fairmq/prototest/FairMQBinSampler.cxx b/fairmq/prototest/FairMQBinSampler.cxx index d09102e9..1a2507f5 100644 --- a/fairmq/prototest/FairMQBinSampler.cxx +++ b/fairmq/prototest/FairMQBinSampler.cxx @@ -35,7 +35,6 @@ FairMQBinSampler::~FairMQBinSampler() void FairMQBinSampler::Init() { - FairMQDevice::Init(); } void FairMQBinSampler::Run() diff --git a/fairmq/prototest/FairMQProtoSampler.cxx b/fairmq/prototest/FairMQProtoSampler.cxx index d15fd4fe..7677384e 100644 --- a/fairmq/prototest/FairMQProtoSampler.cxx +++ b/fairmq/prototest/FairMQProtoSampler.cxx @@ -37,7 +37,6 @@ FairMQProtoSampler::~FairMQProtoSampler() void FairMQProtoSampler::Init() { - FairMQDevice::Init(); } void FairMQProtoSampler::Run() diff --git a/fairmq/run/runBenchmarkSampler.cxx b/fairmq/run/runBenchmarkSampler.cxx index a1c24d0e..337d73ac 100644 --- a/fairmq/run/runBenchmarkSampler.cxx +++ b/fairmq/run/runBenchmarkSampler.cxx @@ -9,7 +9,7 @@ * runBenchmarkSampler.cxx * * @since 2013-04-23 - * @author D. Klein, A. Rybalchenko + * @author: D. Klein, A. Rybalchenko */ #include @@ -32,12 +32,11 @@ FairMQBenchmarkSampler sampler; 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); - cout << "Shutdown complete. Bye!" << endl; + LOG(INFO) << "Shutdown complete"; exit(1); } @@ -55,7 +54,8 @@ typedef struct DeviceOptions { DeviceOptions() : id(), eventSize(0), eventRate(0), ioThreads(0), - outputSocketType(), outputBufSize(0), outputMethod(), outputAddress() {} + outputSocketType(), outputBufSize(0), outputMethod(), outputAddress() + {} string id; int eventSize; @@ -80,7 +80,7 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options) ("event-rate", bpo::value()->default_value(0), "Event rate limit in maximum number of events per second") ("io-threads", bpo::value()->default_value(1), "Number of I/O threads") ("output-socket-type", bpo::value()->required(), "Output socket type: pub/push") - ("output-buff-size", bpo::value()->required(), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") + ("output-buff-size", bpo::value()->default_value(1000), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("output-method", bpo::value()->required(), "Output method: bind/connect") ("output-address", bpo::value()->required(), "Output address, e.g.: \"tcp://*:5555\"") ("help", "Print help messages"); @@ -88,7 +88,7 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options) bpo::variables_map 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; return false; @@ -96,28 +96,28 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options) bpo::notify(vm); - if ( vm.count("id") ) + if (vm.count("id")) _options->id = vm["id"].as(); - if ( vm.count("event-size") ) + if (vm.count("event-size")) _options->eventSize = vm["event-size"].as(); - if ( vm.count("event-rate") ) + if (vm.count("event-rate")) _options->eventRate = vm["event-rate"].as(); - if ( vm.count("io-threads") ) + if (vm.count("io-threads")) _options->ioThreads = vm["io-threads"].as(); - if ( vm.count("output-socket-type") ) + if (vm.count("output-socket-type")) _options->outputSocketType = vm["output-socket-type"].as(); - if ( vm.count("output-buff-size") ) + if (vm.count("output-buff-size")) _options->outputBufSize = vm["output-buff-size"].as(); - if ( vm.count("output-method") ) + if (vm.count("output-method")) _options->outputMethod = vm["output-method"].as(); - if ( vm.count("output-address") ) + if (vm.count("output-address")) _options->outputAddress = vm["output-address"].as(); return true; @@ -151,35 +151,35 @@ int main(int argc, char** argv) 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::EventSize, options.eventSize); sampler.SetProperty(FairMQBenchmarkSampler::EventRate, options.eventRate); sampler.SetProperty(FairMQBenchmarkSampler::NumIoThreads, options.ioThreads); - sampler.SetProperty(FairMQBenchmarkSampler::NumInputs, 0); - sampler.SetProperty(FairMQBenchmarkSampler::NumOutputs, 1); + sampler.ChangeState(FairMQBenchmarkSampler::INIT_DEVICE); + 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); - - // wait until the running thread has finished processing. - boost::unique_lock lock(sampler.fRunningMutex); - while (!sampler.fRunningFinished) - { - sampler.fRunningCondition.wait(lock); - } + sampler.WaitForEndOfState(FairMQBenchmarkSampler::RUN); 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); return 0; diff --git a/fairmq/run/runBuffer.cxx b/fairmq/run/runBuffer.cxx index cb60cfb3..45994373 100644 --- a/fairmq/run/runBuffer.cxx +++ b/fairmq/run/runBuffer.cxx @@ -32,12 +32,11 @@ FairMQBuffer buffer; 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); - cout << "Shutdown complete. Bye!" << endl; + LOG(INFO) << "Shutdown complete"; exit(1); } @@ -81,11 +80,11 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options) ("id", bpo::value(), "Device ID") ("io-threads", bpo::value()->default_value(1), "Number of I/O threads") ("input-socket-type", bpo::value()->required(), "Input socket type: sub/pull") - ("input-buff-size", bpo::value()->required(), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") + ("input-buff-size", bpo::value()->default_value(1000), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("input-method", bpo::value()->required(), "Input method: bind/connect") ("input-address", bpo::value()->required(), "Input address, e.g.: \"tcp://localhost:5555\"") ("output-socket-type", bpo::value()->required(), "Output socket type: pub/push") - ("output-buff-size", bpo::value()->required(), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") + ("output-buff-size", bpo::value()->default_value(1000), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("output-method", bpo::value()->required(), "Output method: bind/connect") ("output-address", bpo::value()->required(), "Output address, e.g.: \"tcp://localhost:5555\"") ("help", "Print help messages"); @@ -160,38 +159,40 @@ int main(int argc, char** argv) 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::NumIoThreads, options.ioThreads); - buffer.SetProperty(FairMQBuffer::NumInputs, 1); - buffer.SetProperty(FairMQBuffer::NumOutputs, 1); + buffer.ChangeState(FairMQBuffer::INIT_DEVICE); + 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); - - // wait until the running thread has finished processing. - boost::unique_lock lock(buffer.fRunningMutex); - while (!buffer.fRunningFinished) - { - buffer.fRunningCondition.wait(lock); - } + buffer.WaitForEndOfState(FairMQBuffer::RUN); 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); return 0; diff --git a/fairmq/run/runMerger.cxx b/fairmq/run/runMerger.cxx index a45cc42c..5178a513 100644 --- a/fairmq/run/runMerger.cxx +++ b/fairmq/run/runMerger.cxx @@ -32,12 +32,11 @@ FairMQMerger merger; 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); - cout << "Shutdown complete. Bye!" << endl; + LOG(INFO) << "Shutdown complete."; exit(1); } @@ -165,41 +164,43 @@ int main(int argc, char** argv) 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::NumIoThreads, options.ioThreads); - merger.SetProperty(FairMQMerger::NumInputs, options.numInputs); - merger.SetProperty(FairMQMerger::NumOutputs, 1); + merger.ChangeState(FairMQMerger::INIT_DEVICE); + 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); - - // wait until the running thread has finished processing. - boost::unique_lock lock(merger.fRunningMutex); - while (!merger.fRunningFinished) - { - merger.fRunningCondition.wait(lock); - } + merger.WaitForEndOfState(FairMQMerger::RUN); 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); return 0; diff --git a/fairmq/run/runProxy.cxx b/fairmq/run/runProxy.cxx index 7720fc79..25864045 100644 --- a/fairmq/run/runProxy.cxx +++ b/fairmq/run/runProxy.cxx @@ -32,12 +32,11 @@ FairMQProxy proxy; 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); - cout << "Shutdown complete. Bye!" << endl; + LOG(INFO) << "Shutdown complete."; exit(1); } @@ -81,11 +80,11 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options) ("id", bpo::value()->required(), "Device ID") ("io-threads", bpo::value()->default_value(1), "Number of I/O threads") ("input-socket-type", bpo::value()->required(), "Input socket type: sub/pull") - ("input-buff-size", bpo::value()->required(), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") + ("input-buff-size", bpo::value()->default_value(1000), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("input-method", bpo::value()->required(), "Input method: bind/connect") ("input-address", bpo::value()->required(), "Input address, e.g.: \"tcp://localhost:5555\"") ("output-socket-type", bpo::value()->required(), "Output socket type: pub/push") - ("output-buff-size", bpo::value()->required(), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") + ("output-buff-size", bpo::value()->default_value(1000), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("output-method", bpo::value()->required(), "Output method: bind/connect") ("output-address", bpo::value()->required(), "Output address, e.g.: \"tcp://localhost:5555\"") ("help", "Print help messages"); @@ -160,38 +159,40 @@ int main(int argc, char** argv) 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::NumIoThreads, options.ioThreads); - proxy.SetProperty(FairMQProxy::NumInputs, 1); - proxy.SetProperty(FairMQProxy::NumOutputs, 1); + proxy.ChangeState(FairMQProxy::INIT_DEVICE); + 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); - - // wait until the running thread has finished processing. - boost::unique_lock lock(proxy.fRunningMutex); - while (!proxy.fRunningFinished) - { - proxy.fRunningCondition.wait(lock); - } + proxy.WaitForEndOfState(FairMQProxy::RUN); 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); return 0; diff --git a/fairmq/run/runSink.cxx b/fairmq/run/runSink.cxx index 86f83def..ec8b99e4 100644 --- a/fairmq/run/runSink.cxx +++ b/fairmq/run/runSink.cxx @@ -32,12 +32,11 @@ FairMQSink sink; 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); - cout << "Shutdown complete. Bye!" << endl; + LOG(INFO) << "Shutdown complete."; exit(1); } @@ -55,7 +54,8 @@ typedef struct DeviceOptions { DeviceOptions() : id(), ioThreads(0), - inputSocketType(), inputBufSize(0), inputMethod(), inputAddress() {} + inputSocketType(), inputBufSize(0), inputMethod(), inputAddress() + {} string id; int ioThreads; @@ -76,7 +76,7 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options) ("id", bpo::value()->required(), "Device ID") ("io-threads", bpo::value()->default_value(1), "Number of I/O threads") ("input-socket-type", bpo::value()->required(), "Input socket type: sub/pull") - ("input-buff-size", bpo::value()->required(), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") + ("input-buff-size", bpo::value()->default_value(1000), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("input-method", bpo::value()->required(), "Input method: bind/connect") ("input-address", bpo::value()->required(), "Input address, e.g.: \"tcp://*:5555\"") ("help", "Print help messages"); @@ -139,33 +139,33 @@ int main(int argc, char** argv) 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::NumIoThreads, options.ioThreads); - sink.SetProperty(FairMQSink::NumInputs, 1); - sink.SetProperty(FairMQSink::NumOutputs, 0); + sink.ChangeState(FairMQSink::INIT_DEVICE); + 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); - - // wait until the running thread has finished processing. - boost::unique_lock lock(sink.fRunningMutex); - while (!sink.fRunningFinished) - { - sink.fRunningCondition.wait(lock); - } + sink.WaitForEndOfState(FairMQSink::RUN); 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); return 0; diff --git a/fairmq/run/runSplitter.cxx b/fairmq/run/runSplitter.cxx index 748da93d..bbea6d01 100644 --- a/fairmq/run/runSplitter.cxx +++ b/fairmq/run/runSplitter.cxx @@ -32,12 +32,11 @@ FairMQSplitter splitter; 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); - cout << "Shutdown complete. Bye!" << endl; + LOG(INFO) << "Shutdown complete."; exit(1); } @@ -56,7 +55,8 @@ typedef struct DeviceOptions DeviceOptions() : id(), ioThreads(0), numOutputs(0), inputSocketType(), inputBufSize(0), inputMethod(), inputAddress(), - outputSocketType(), outputBufSize(), outputMethod(), outputAddress() {} + outputSocketType(), outputBufSize(), outputMethod(), outputAddress() + {} string id; int ioThreads; @@ -83,11 +83,11 @@ inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options) ("io-threads", bpo::value()->default_value(1), "Number of I/O threads") ("num-outputs", bpo::value()->required(), "Number of Splitter output sockets") ("input-socket-type", bpo::value()->required(), "Input socket type: sub/pull") - ("input-buff-size", bpo::value()->required(), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") + ("input-buff-size", bpo::value(), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("input-method", bpo::value()->required(), "Input method: bind/connect") ("input-address", bpo::value()->required(), "Input address, e.g.: \"tcp://localhost:5555\"") ("output-socket-type", bpo::value< vector >()->required(), "Output socket type: pub/push") - ("output-buff-size", bpo::value< vector >()->required(), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") + ("output-buff-size", bpo::value< vector >(), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)") ("output-method", bpo::value< vector >()->required(), "Output method: bind/connect") ("output-address", bpo::value< vector >()->required(), "Output address, e.g.: \"tcp://localhost:5555\"") ("help", "Print help messages"); @@ -165,41 +165,43 @@ int main(int argc, char** argv) 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::NumIoThreads, options.ioThreads); - splitter.SetProperty(FairMQSplitter::NumInputs, 1); - splitter.SetProperty(FairMQSplitter::NumOutputs, options.numOutputs); + splitter.ChangeState(FairMQSplitter::INIT_DEVICE); + 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); - - // wait until the running thread has finished processing. - boost::unique_lock lock(splitter.fRunningMutex); - while (!splitter.fRunningFinished) - { - splitter.fRunningCondition.wait(lock); - } + splitter.WaitForEndOfState(FairMQSplitter::RUN); 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); return 0; diff --git a/fairmq/zeromq/FairMQContextZMQ.cxx b/fairmq/zeromq/FairMQContextZMQ.cxx index 7095433b..ed8fe795 100644 --- a/fairmq/zeromq/FairMQContextZMQ.cxx +++ b/fairmq/zeromq/FairMQContextZMQ.cxx @@ -27,15 +27,13 @@ FairMQContextZMQ::FairMQContextZMQ(int numIoThreads) exit(EXIT_FAILURE); } - int rc = zmq_ctx_set(fContext, ZMQ_IO_THREADS, numIoThreads); - if (rc != 0) + if (zmq_ctx_set(fContext, ZMQ_IO_THREADS, numIoThreads) != 0) { LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno); } // Set the maximum number of allowed sockets on the context. - rc = zmq_ctx_set(fContext, ZMQ_MAX_SOCKETS, 10000); - if (rc != 0) + if (zmq_ctx_set(fContext, ZMQ_MAX_SOCKETS, 10000) != 0) { LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno); } @@ -58,8 +56,7 @@ void FairMQContextZMQ::Close() return; } - int rc = zmq_ctx_destroy(fContext); - if (rc != 0) + if (zmq_ctx_destroy(fContext) != 0) { if (errno == EINTR) { LOG(ERROR) << " failed closing context, reason: " << zmq_strerror(errno); diff --git a/fairmq/zeromq/FairMQMessageZMQ.cxx b/fairmq/zeromq/FairMQMessageZMQ.cxx index 63abdd58..907f3ed0 100644 --- a/fairmq/zeromq/FairMQMessageZMQ.cxx +++ b/fairmq/zeromq/FairMQMessageZMQ.cxx @@ -23,8 +23,7 @@ using namespace std; FairMQMessageZMQ::FairMQMessageZMQ() : fMessage() { - int rc = zmq_msg_init(&fMessage); - if (rc != 0) + if (zmq_msg_init(&fMessage) != 0) { LOG(ERROR) << "failed initializing message, reason: " << zmq_strerror(errno); } @@ -33,8 +32,7 @@ FairMQMessageZMQ::FairMQMessageZMQ() FairMQMessageZMQ::FairMQMessageZMQ(size_t size) : fMessage() { - int rc = zmq_msg_init_size(&fMessage, size); - if (rc != 0) + if (zmq_msg_init_size(&fMessage, size) != 0) { 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) : fMessage() { - int rc = zmq_msg_init_data(&fMessage, data, size, ffn ? ffn : &CleanUp, hint); - if (rc != 0) + if (zmq_msg_init_data(&fMessage, data, size, ffn ? ffn : &CleanUp, hint) != 0) { 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() { CloseMessage(); - int rc = zmq_msg_init(&fMessage); - if (rc != 0) + if (zmq_msg_init(&fMessage) != 0) { LOG(ERROR) << "failed initializing message, reason: " << zmq_strerror(errno); } @@ -63,8 +59,7 @@ void FairMQMessageZMQ::Rebuild() void FairMQMessageZMQ::Rebuild(size_t size) { CloseMessage(); - int rc = zmq_msg_init_size(&fMessage, size); - if (rc != 0) + if (zmq_msg_init_size(&fMessage, size) != 0) { 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) { CloseMessage(); - int rc = zmq_msg_init_data(&fMessage, data, size, ffn ? ffn : &CleanUp, hint); - if (rc != 0) + if (zmq_msg_init_data(&fMessage, data, size, ffn ? ffn : &CleanUp, hint) != 0) { 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) { // Shares the message buffer between msg and this fMessage. - int rc = zmq_msg_copy(&fMessage, (zmq_msg_t*)msg->GetMessage()); - if (rc != 0) + if (zmq_msg_copy(&fMessage, (zmq_msg_t*)msg->GetMessage()) != 0) { LOG(ERROR) << "failed copying message, reason: " << zmq_strerror(errno); } @@ -119,8 +112,7 @@ void FairMQMessageZMQ::Copy(FairMQMessage* msg) inline void FairMQMessageZMQ::CloseMessage() { - int rc = zmq_msg_close(&fMessage); - if (rc != 0) + if (zmq_msg_close(&fMessage) != 0) { LOG(ERROR) << "failed closing message, reason: " << zmq_strerror(errno); } @@ -133,8 +125,7 @@ void FairMQMessageZMQ::CleanUp(void* data, void* hint) FairMQMessageZMQ::~FairMQMessageZMQ() { - int rc = zmq_msg_close(&fMessage); - if (rc != 0) + if (zmq_msg_close(&fMessage) != 0) { LOG(ERROR) << "failed closing message with data, reason: " << zmq_strerror(errno); } diff --git a/fairmq/zeromq/FairMQPollerZMQ.cxx b/fairmq/zeromq/FairMQPollerZMQ.cxx index 6819a728..8f979c71 100644 --- a/fairmq/zeromq/FairMQPollerZMQ.cxx +++ b/fairmq/zeromq/FairMQPollerZMQ.cxx @@ -19,16 +19,16 @@ using namespace std; -FairMQPollerZMQ::FairMQPollerZMQ(const vector& inputs) +FairMQPollerZMQ::FairMQPollerZMQ(const vector& channels) : items() , fNumItems() { - fNumItems = inputs.size(); + fNumItems = channels.size(); 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].events = ZMQ_POLLIN; items[i].revents = 0; @@ -37,8 +37,7 @@ FairMQPollerZMQ::FairMQPollerZMQ(const vector& inputs) void FairMQPollerZMQ::Poll(int timeout) { - int rc = zmq_poll(items, fNumItems, timeout); - if (rc < 0) + if (zmq_poll(items, fNumItems, timeout) < 0) { LOG(ERROR) << "polling failed, reason: " << zmq_strerror(errno); } @@ -47,7 +46,9 @@ void FairMQPollerZMQ::Poll(int timeout) bool FairMQPollerZMQ::CheckInput(int index) { if (items[index].revents & ZMQ_POLLIN) + { return true; + } return false; } @@ -55,7 +56,9 @@ bool FairMQPollerZMQ::CheckInput(int index) bool FairMQPollerZMQ::CheckOutput(int index) { if (items[index].revents & ZMQ_POLLOUT) + { return true; + } return false; } @@ -63,5 +66,7 @@ bool FairMQPollerZMQ::CheckOutput(int index) FairMQPollerZMQ::~FairMQPollerZMQ() { if (items != NULL) + { delete[] items; + } } diff --git a/fairmq/zeromq/FairMQPollerZMQ.h b/fairmq/zeromq/FairMQPollerZMQ.h index 0ea6d50b..9c21beee 100644 --- a/fairmq/zeromq/FairMQPollerZMQ.h +++ b/fairmq/zeromq/FairMQPollerZMQ.h @@ -18,12 +18,12 @@ #include #include "FairMQPoller.h" -#include "FairMQSocket.h" +#include "FairMQChannel.h" class FairMQPollerZMQ : public FairMQPoller { public: - FairMQPollerZMQ(const std::vector& inputs); + FairMQPollerZMQ(const std::vector& channels); virtual void Poll(int timeout); virtual bool CheckInput(int index); diff --git a/fairmq/zeromq/FairMQSocketZMQ.cxx b/fairmq/zeromq/FairMQSocketZMQ.cxx index 481e9c7f..1a588590 100644 --- a/fairmq/zeromq/FairMQSocketZMQ.cxx +++ b/fairmq/zeromq/FairMQSocketZMQ.cxx @@ -19,9 +19,10 @@ using namespace std; +// Context to hold the ZeroMQ sockets boost::shared_ptr FairMQSocketZMQ::fContext = boost::shared_ptr(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) , fSocket(NULL) , fId() @@ -30,12 +31,9 @@ FairMQSocketZMQ::FairMQSocketZMQ(const string& type, int num, int numIoThreads) , fMessagesTx(0) , fMessagesRx(0) { - stringstream id; - id << type << "." << num; - fId = id.str(); + fId = name + "." + type; - int rc = zmq_ctx_set(fContext->GetContext(), ZMQ_IO_THREADS, numIoThreads); - if (rc != 0) + if (zmq_ctx_set(fContext->GetContext(), ZMQ_IO_THREADS, numIoThreads) != 0) { 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) { - LOG(ERROR) << "failed creating socket #" << fId << ", reason: " << zmq_strerror(errno); + LOG(ERROR) << "failed creating socket " << fId << ", reason: " << zmq_strerror(errno); exit(EXIT_FAILURE); } - rc = zmq_setsockopt(fSocket, ZMQ_IDENTITY, &fId, fId.length()); - if (rc != 0) + if (zmq_setsockopt(fSocket, ZMQ_IDENTITY, &fId, fId.length()) != 0) { 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 milliseconds before terminating. // Default value for ZeroMQ is -1, which is to wait forever. int linger = 500; - rc = zmq_setsockopt(fSocket, ZMQ_LINGER, &linger, sizeof(linger)); - if (rc != 0) + if (zmq_setsockopt(fSocket, ZMQ_LINGER, &linger, sizeof(linger)) != 0) { LOG(ERROR) << "failed setting ZMQ_LINGER socket option, reason: " << zmq_strerror(errno); } if (type == "sub") { - rc = zmq_setsockopt(fSocket, ZMQ_SUBSCRIBE, NULL, 0); - if (rc != 0) + if (zmq_setsockopt(fSocket, ZMQ_SUBSCRIBE, NULL, 0) != 0) { 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() @@ -82,16 +77,15 @@ string FairMQSocketZMQ::GetId() 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 (rc != 0) + if (zmq_bind(fSocket, address.c_str()) != 0) { 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. 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 true; @@ -99,12 +93,11 @@ bool FairMQSocketZMQ::Bind(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 (rc != 0) + if (zmq_connect(fSocket, address.c_str()) != 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) { - LOG(INFO) << "terminating socket #" << fId; + LOG(INFO) << "terminating socket " << fId; 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; } @@ -145,10 +138,10 @@ int FairMQSocketZMQ::Send(FairMQMessage* msg, const int flags) } if (zmq_errno() == ETERM) { - LOG(INFO) << "terminating socket #" << fId; + LOG(INFO) << "terminating socket " << fId; 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; } @@ -167,10 +160,10 @@ int FairMQSocketZMQ::Receive(FairMQMessage* msg, const string& flag) } if (zmq_errno() == ETERM) { - LOG(INFO) << "terminating socket #" << fId; + LOG(INFO) << "terminating socket " << fId; 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; } @@ -189,24 +182,25 @@ int FairMQSocketZMQ::Receive(FairMQMessage* msg, const int flags) } if (zmq_errno() == ETERM) { - LOG(INFO) << "terminating socket #" << fId; + LOG(INFO) << "terminating socket " << fId; 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; } void FairMQSocketZMQ::Close() { + // LOG(DEBUG) << "Closing socket " << fId; + if (fSocket == NULL) { return; } - int rc = zmq_close(fSocket); - if (rc != 0) + if (zmq_close(fSocket) != 0) { - LOG(ERROR) << "failed closing socket, reason: " << zmq_strerror(errno); + LOG(ERROR) << "failed closing socket " << fId << ", reason: " << zmq_strerror(errno); } fSocket = NULL; @@ -214,8 +208,7 @@ void FairMQSocketZMQ::Close() void FairMQSocketZMQ::Terminate() { - int rc = zmq_ctx_destroy(fContext->GetContext()); - if (rc != 0) + if (zmq_ctx_destroy(fContext->GetContext()) != 0) { 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) { - int rc = zmq_setsockopt(fSocket, GetConstant(option), value, valueSize); - if (rc < 0) + if (zmq_setsockopt(fSocket, GetConstant(option), value, valueSize) < 0) { 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) { - int rc = zmq_getsockopt(fSocket, GetConstant(option), value, valueSize); - if (rc < 0) { + if (zmq_getsockopt(fSocket, GetConstant(option), value, valueSize) < 0) + { LOG(ERROR) << "failed getting socket option, reason: " << zmq_strerror(errno); } } diff --git a/fairmq/zeromq/FairMQSocketZMQ.h b/fairmq/zeromq/FairMQSocketZMQ.h index 5b067e80..45c187ef 100644 --- a/fairmq/zeromq/FairMQSocketZMQ.h +++ b/fairmq/zeromq/FairMQSocketZMQ.h @@ -25,17 +25,17 @@ class FairMQSocketZMQ : public FairMQSocket { 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 bool Bind(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 int flags); - virtual int Receive(FairMQMessage* msg, const std::string& flag=""); - virtual int Receive(FairMQMessage* msg, const int flags); + virtual int Send(FairMQMessage* msg, const std::string& flag = ""); + virtual int Send(FairMQMessage* msg, const int flags = 0); + virtual int Receive(FairMQMessage* msg, const std::string& flag = ""); + virtual int Receive(FairMQMessage* msg, const int flags = 0); virtual void* GetSocket(); virtual int GetSocket(int nothing); diff --git a/fairmq/zeromq/FairMQTransportFactoryZMQ.cxx b/fairmq/zeromq/FairMQTransportFactoryZMQ.cxx index 52d999ee..9b01dcdd 100644 --- a/fairmq/zeromq/FairMQTransportFactoryZMQ.cxx +++ b/fairmq/zeromq/FairMQTransportFactoryZMQ.cxx @@ -22,7 +22,7 @@ FairMQTransportFactoryZMQ::FairMQTransportFactoryZMQ() { int 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() @@ -40,12 +40,12 @@ FairMQMessage* FairMQTransportFactoryZMQ::CreateMessage(void* data, size_t size, 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& inputs) +FairMQPoller* FairMQTransportFactoryZMQ::CreatePoller(const vector& channels) { - return new FairMQPollerZMQ(inputs); + return new FairMQPollerZMQ(channels); } diff --git a/fairmq/zeromq/FairMQTransportFactoryZMQ.h b/fairmq/zeromq/FairMQTransportFactoryZMQ.h index b257cef2..ca9205b8 100644 --- a/fairmq/zeromq/FairMQTransportFactoryZMQ.h +++ b/fairmq/zeromq/FairMQTransportFactoryZMQ.h @@ -31,8 +31,8 @@ class FairMQTransportFactoryZMQ : public FairMQTransportFactory virtual FairMQMessage* CreateMessage(); virtual FairMQMessage* CreateMessage(size_t size); 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 FairMQPoller* CreatePoller(const std::vector& inputs); + virtual FairMQSocket* CreateSocket(const std::string& type, const std::string& name, int numIoThreads); + virtual FairMQPoller* CreatePoller(const std::vector& channels); virtual ~FairMQTransportFactoryZMQ() {}; };