Convert factory methods to return smart ptrs

- Convert factory methods to return smart ptrs.
 - Refactor state machine to use same thread for user states.
 - Remove unused includes and dependencies, use std.
This commit is contained in:
Alexey Rybalchenko 2016-11-16 16:27:21 +01:00
parent 12f04c7237
commit b166cedb63
19 changed files with 406 additions and 480 deletions

View File

@ -120,14 +120,10 @@ Set(DEPENDENCIES
${Boost_THREAD_LIBRARY} ${Boost_THREAD_LIBRARY}
dl dl
fairmq_logger fairmq_logger
${Boost_TIMER_LIBRARY}
${Boost_SYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}
${Boost_FILESYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_RANDOM_LIBRARY}
${Boost_CHRONO_LIBRARY}
${Boost_REGEX_LIBRARY} ${Boost_REGEX_LIBRARY}
${Boost_ATOMIC_LIBRARY}
${Boost_DATE_TIME_LIBRARY} ${Boost_DATE_TIME_LIBRARY}
) )

View File

@ -14,16 +14,14 @@
#include <set> #include <set>
#include <boost/exception/all.hpp>
#include "FairMQChannel.h" #include "FairMQChannel.h"
#include "FairMQLogger.h" #include "FairMQLogger.h"
using namespace std; using namespace std;
boost::mutex FairMQChannel::fChannelMutex; mutex FairMQChannel::fChannelMutex;
std::atomic<bool> FairMQChannel::fInterrupted(false); atomic<bool> FairMQChannel::fInterrupted(false);
FairMQChannel::FairMQChannel() FairMQChannel::FairMQChannel()
: fSocket(nullptr) : fSocket(nullptr)
@ -121,12 +119,12 @@ string FairMQChannel::GetType() const
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
return fType; return fType;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::GetType: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::GetType: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -135,12 +133,12 @@ string FairMQChannel::GetMethod() const
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
return fMethod; return fMethod;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::GetMethod: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::GetMethod: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -149,12 +147,12 @@ string FairMQChannel::GetAddress() const
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
return fAddress; return fAddress;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::GetAddress: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::GetAddress: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -163,12 +161,12 @@ int FairMQChannel::GetSndBufSize() const
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
return fSndBufSize; return fSndBufSize;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::GetSndBufSize: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::GetSndBufSize: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -177,12 +175,12 @@ int FairMQChannel::GetRcvBufSize() const
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
return fRcvBufSize; return fRcvBufSize;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::GetRcvBufSize: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::GetRcvBufSize: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -191,12 +189,12 @@ int FairMQChannel::GetSndKernelSize() const
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
return fSndKernelSize; return fSndKernelSize;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::GetSndKernelSize: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::GetSndKernelSize: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -205,12 +203,12 @@ int FairMQChannel::GetRcvKernelSize() const
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
return fRcvKernelSize; return fRcvKernelSize;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::GetRcvKernelSize: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::GetRcvKernelSize: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -219,12 +217,12 @@ int FairMQChannel::GetRateLogging() const
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
return fRateLogging; return fRateLogging;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::GetRateLogging: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::GetRateLogging: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -233,13 +231,13 @@ void FairMQChannel::UpdateType(const string& type)
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
fIsValid = false; fIsValid = false;
fType = type; fType = type;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::UpdateType: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::UpdateType: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -248,13 +246,13 @@ void FairMQChannel::UpdateMethod(const string& method)
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
fIsValid = false; fIsValid = false;
fMethod = method; fMethod = method;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::UpdateMethod: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::UpdateMethod: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -263,13 +261,13 @@ void FairMQChannel::UpdateAddress(const string& address)
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
fIsValid = false; fIsValid = false;
fAddress = address; fAddress = address;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::UpdateAddress: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::UpdateAddress: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -278,13 +276,13 @@ void FairMQChannel::UpdateSndBufSize(const int sndBufSize)
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
fIsValid = false; fIsValid = false;
fSndBufSize = sndBufSize; fSndBufSize = sndBufSize;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::UpdateSndBufSize: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::UpdateSndBufSize: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -293,13 +291,13 @@ void FairMQChannel::UpdateRcvBufSize(const int rcvBufSize)
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
fIsValid = false; fIsValid = false;
fRcvBufSize = rcvBufSize; fRcvBufSize = rcvBufSize;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::UpdateRcvBufSize: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::UpdateRcvBufSize: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -308,13 +306,13 @@ void FairMQChannel::UpdateSndKernelSize(const int sndKernelSize)
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
fIsValid = false; fIsValid = false;
fSndKernelSize = sndKernelSize; fSndKernelSize = sndKernelSize;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::UpdateSndKernelSize: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::UpdateSndKernelSize: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -323,13 +321,13 @@ void FairMQChannel::UpdateRcvKernelSize(const int rcvKernelSize)
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
fIsValid = false; fIsValid = false;
fRcvKernelSize = rcvKernelSize; fRcvKernelSize = rcvKernelSize;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::UpdateRcvKernelSize: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::UpdateRcvKernelSize: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -338,13 +336,13 @@ void FairMQChannel::UpdateRateLogging(const int rateLogging)
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
fIsValid = false; fIsValid = false;
fRateLogging = rateLogging; fRateLogging = rateLogging;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::UpdateRateLogging: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::UpdateRateLogging: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -353,12 +351,12 @@ bool FairMQChannel::IsValid() const
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
return fIsValid; return fIsValid;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::IsValid: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::IsValid: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -367,7 +365,7 @@ bool FairMQChannel::ValidateChannel()
{ {
try try
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
stringstream ss; stringstream ss;
ss << "Validating channel \"" << fChannelName << "\"... "; ss << "Validating channel \"" << fChannelName << "\"... ";
@ -402,14 +400,17 @@ bool FairMQChannel::ValidateChannel()
{ {
//TODO: maybe cache fEndpoints as a class member? not really needed as tokenizing is //TODO: maybe cache fEndpoints as a class member? not really needed as tokenizing is
//fast, and only happens during (re-)configure //fast, and only happens during (re-)configure
std::vector<std::string> fEndpoints; vector<string> fEndpoints;
Tokenize(fEndpoints, fAddress); Tokenize(fEndpoints, fAddress);
for (const auto endpoint : fEndpoints) for (const auto endpoint : fEndpoints)
{ {
std::string address; string address;
if (endpoint[0]=='@'||endpoint[0]=='+'||endpoint[0]=='>') { if (endpoint[0] == '@' || endpoint[0] == '+' || endpoint[0] == '>')
address = endpoint.substr(1); {
} else { address = endpoint.substr(1);
}
else
{
// we don't have a method modifier, check if the default method is set // we don't have a method modifier, check if the default method is set
const string socketMethodNames[] = { "bind", "connect" }; const string socketMethodNames[] = { "bind", "connect" };
const set<string> socketMethods(socketMethodNames, socketMethodNames + sizeof(socketMethodNames) / sizeof(string)); const set<string> socketMethods(socketMethodNames, socketMethodNames + sizeof(socketMethodNames) / sizeof(string));
@ -508,14 +509,14 @@ bool FairMQChannel::ValidateChannel()
LOG(DEBUG) << ss.str(); LOG(DEBUG) << ss.str();
return true; return true;
} }
catch (boost::exception& e) catch (exception& e)
{ {
LOG(ERROR) << "Exception caught in FairMQChannel::ValidateChannel: " << boost::diagnostic_information(e); LOG(ERROR) << "Exception caught in FairMQChannel::ValidateChannel: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
bool FairMQChannel::InitCommandInterface(FairMQTransportFactory* factory, int numIoThreads) bool FairMQChannel::InitCommandInterface(shared_ptr<FairMQTransportFactory> factory, int numIoThreads)
{ {
fTransportFactory = factory; fTransportFactory = factory;
@ -539,7 +540,7 @@ bool FairMQChannel::InitCommandInterface(FairMQTransportFactory* factory, int nu
void FairMQChannel::ResetChannel() void FairMQChannel::ResetChannel()
{ {
boost::unique_lock<boost::mutex> scoped_lock(fChannelMutex); unique_lock<mutex> lock(fChannelMutex);
fIsValid = false; fIsValid = false;
// TODO: implement channel resetting // TODO: implement channel resetting
} }
@ -586,7 +587,7 @@ int FairMQChannel::Receive(const unique_ptr<FairMQMessage>& msg, int rcvTimeoutI
return -2; return -2;
} }
int64_t FairMQChannel::Send(const std::vector<std::unique_ptr<FairMQMessage>>& msgVec, int sndTimeoutInMs) const int64_t FairMQChannel::Send(const vector<unique_ptr<FairMQMessage>>& msgVec, int sndTimeoutInMs) const
{ {
fPoller->Poll(sndTimeoutInMs); fPoller->Poll(sndTimeoutInMs);
@ -607,7 +608,7 @@ int64_t FairMQChannel::Send(const std::vector<std::unique_ptr<FairMQMessage>>& m
return -2; return -2;
} }
int64_t FairMQChannel::Receive(std::vector<std::unique_ptr<FairMQMessage>>& msgVec, int rcvTimeoutInMs) const int64_t FairMQChannel::Receive(vector<unique_ptr<FairMQMessage>>& msgVec, int rcvTimeoutInMs) const
{ {
fPoller->Poll(rcvTimeoutInMs); fPoller->Poll(rcvTimeoutInMs);
@ -769,34 +770,27 @@ bool FairMQChannel::ExpectsAnotherPart() const
inline bool FairMQChannel::HandleUnblock() const inline bool FairMQChannel::HandleUnblock() const
{ {
FairMQMessage* cmd = fTransportFactory->CreateMessage(); FairMQMessagePtr cmd(fTransportFactory->CreateMessage());
if (fCmdSocket->Receive(cmd, 0) >= 0) if (fCmdSocket->Receive(cmd.get(), 0) >= 0)
{ {
// LOG(DEBUG) << "unblocked"; // LOG(DEBUG) << "unblocked";
} }
delete cmd;
return true; return true;
} }
FairMQChannel::~FairMQChannel() FairMQChannel::~FairMQChannel()
{ {
delete fCmdSocket;
delete fPoller;
} }
void FairMQChannel::Tokenize(std::vector<std::string>& output, void FairMQChannel::Tokenize(vector<string>& output, const string& input, const string delimiters)
const std::string& input,
const std::string delimiters)
{ {
using namespace std; size_t start = 0;
size_t start = 0; size_t end = input.find_first_of(delimiters);
size_t end = input.find_first_of(delimiters); while (end != string::npos)
while (end != string::npos) {
{ output.push_back(input.substr(start, end-start));
output.push_back(input.substr(start, end-start)); start = ++end;
start = ++end; end = input.find_first_of(delimiters, start);
end = input.find_first_of(delimiters, start); }
} output.push_back(input.substr(start));
output.push_back(input.substr(start));
} }

View File

@ -18,8 +18,7 @@
#include <string> #include <string>
#include <memory> // unique_ptr #include <memory> // unique_ptr
#include <atomic> #include <atomic>
#include <mutex>
#include <boost/thread/mutex.hpp>
#include "FairMQTransportFactory.h" #include "FairMQTransportFactory.h"
#include "FairMQSocket.h" #include "FairMQSocket.h"
@ -134,7 +133,7 @@ class FairMQChannel
/// Resets the channel (requires validation to be used again). /// Resets the channel (requires validation to be used again).
void ResetChannel(); void ResetChannel();
FairMQSocket* fSocket; std::unique_ptr<FairMQSocket> fSocket;
/// Sends a message to the socket queue. /// Sends a message to the socket queue.
/// @details Send method attempts to send a message by /// @details Send method attempts to send a message by
@ -249,9 +248,7 @@ class FairMQChannel
int Receive(FairMQMessage* msg, const int flags, int rcvTimeoutInMs = -1) const; int Receive(FairMQMessage* msg, const int flags, int rcvTimeoutInMs = -1) const;
// TODO: this might go to some base utility library // TODO: this might go to some base utility library
static void Tokenize(std::vector<std::string>& output, static void Tokenize(std::vector<std::string>& output, const std::string& input, const std::string delimiters = ",");
const std::string& input,
const std::string delimiters = ",");
private: private:
std::string fType; std::string fType;
@ -266,15 +263,15 @@ class FairMQChannel
std::string fChannelName; std::string fChannelName;
std::atomic<bool> fIsValid; std::atomic<bool> fIsValid;
FairMQPoller* fPoller; FairMQPollerPtr fPoller;
FairMQSocket* fCmdSocket; FairMQSocketPtr fCmdSocket;
FairMQTransportFactory* fTransportFactory; std::shared_ptr<FairMQTransportFactory> fTransportFactory;
int fNoBlockFlag; int fNoBlockFlag;
int fSndMoreFlag; int fSndMoreFlag;
bool InitCommandInterface(FairMQTransportFactory* factory, int numIoThreads); bool InitCommandInterface(std::shared_ptr<FairMQTransportFactory> factory, int numIoThreads);
bool HandleUnblock() const; bool HandleUnblock() const;
@ -282,7 +279,7 @@ class FairMQChannel
// implication: same mutex is used for all instances of the class // implication: same mutex is used for all instances of the class
// this does not hurt much, because mutex is used only during initialization with very low contention // this does not hurt much, because mutex is used only during initialization with very low contention
// possible TODO: improve this // possible TODO: improve this
static boost::mutex fChannelMutex; static std::mutex fChannelMutex;
static std::atomic<bool> fInterrupted; static std::atomic<bool> fInterrupted;
}; };

View File

@ -19,12 +19,13 @@
#include <stdexcept> #include <stdexcept>
#include <random> #include <random>
#include <chrono> #include <chrono>
#include <mutex>
#include <thread>
#include <functional>
#include <termios.h> // for the InteractiveStateLoop #include <termios.h> // for the InteractiveStateLoop
#include <poll.h> #include <poll.h>
#include <boost/thread.hpp>
#include "FairMQSocket.h" #include "FairMQSocket.h"
#include "FairMQDevice.h" #include "FairMQDevice.h"
#include "FairMQLogger.h" #include "FairMQLogger.h"
@ -38,8 +39,8 @@
using namespace std; using namespace std;
// boost::function and a wrapper to catch the signals // std::function and a wrapper to catch the signals
boost::function<void(int)> sigHandler; std::function<void(int)> sigHandler;
static void CallSignalHandler(int signal) static void CallSignalHandler(int signal)
{ {
sigHandler(signal); sigHandler(signal);
@ -249,18 +250,13 @@ void FairMQDevice::InitWrapper()
if (numAttempts != 0) if (numAttempts != 0)
{ {
boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); std::this_thread::sleep_for(std::chrono::milliseconds(1000));
} }
} }
Init(); Init();
ChangeState(internal_DEVICE_READY); ChangeState(internal_DEVICE_READY);
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fStateMutex);
fStateFinished = true;
fStateCondition.notify_one();
} }
void FairMQDevice::WaitForInitialValidation() void FairMQDevice::WaitForInitialValidation()
@ -399,11 +395,6 @@ void FairMQDevice::InitTaskWrapper()
InitTask(); InitTask();
ChangeState(internal_READY); ChangeState(internal_READY);
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fStateMutex);
fStateFinished = true;
fStateCondition.notify_one();
} }
void FairMQDevice::InitTask() void FairMQDevice::InitTask()
@ -475,7 +466,7 @@ void FairMQDevice::RunWrapper()
{ {
LOG(INFO) << "DEVICE: Running..."; LOG(INFO) << "DEVICE: Running...";
boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this)); std::thread rateLogger(&FairMQDevice::LogSocketRates, this);
FairMQChannel::fInterrupted = false; FairMQChannel::fInterrupted = false;
@ -497,7 +488,7 @@ void FairMQDevice::RunWrapper()
inputChannelKeys.push_back(i.first); inputChannelKeys.push_back(i.first);
} }
unique_ptr<FairMQPoller> poller(fTransportFactory->CreatePoller(fChannels, inputChannelKeys)); FairMQPollerPtr poller(fTransportFactory->CreatePoller(fChannels, inputChannelKeys));
while (CheckCurrentState(RUNNING) && !exitingRunningCallback) while (CheckCurrentState(RUNNING) && !exitingRunningCallback)
{ {
@ -580,26 +571,20 @@ void FairMQDevice::RunWrapper()
ChangeState(ERROR); ChangeState(ERROR);
} }
try
{
rateLogger.interrupt();
rateLogger.join();
}
catch (const boost::thread_resource_error& e)
{
LOG(ERROR) << e.what();
exit(EXIT_FAILURE);
}
if (CheckCurrentState(RUNNING)) if (CheckCurrentState(RUNNING))
{ {
ChangeState(internal_READY); ChangeState(internal_READY);
} }
// notify parent thread about end of processing. try
boost::lock_guard<boost::mutex> lock(fStateMutex); {
fStateFinished = true; rateLogger.join();
fStateCondition.notify_one(); }
catch (exception& e)
{
LOG(ERROR) << "Exception cought during Run(): " << e.what();
exit(EXIT_FAILURE);
}
} }
void FairMQDevice::Run() void FairMQDevice::Run()
@ -621,19 +606,12 @@ void FairMQDevice::PostRun()
void FairMQDevice::Pause() void FairMQDevice::Pause()
{ {
while (true) while (CheckCurrentState(PAUSED))
{ {
try std::this_thread::sleep_for(std::chrono::milliseconds(500));
{ LOG(DEBUG) << "paused...";
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
LOG(DEBUG) << "paused...";
}
catch (const boost::thread_interrupted&)
{
LOG(INFO) << "FairMQDevice::Pause() interrupted";
break;
}
} }
LOG(DEBUG) << "Unpausing";
} }
// Method for setting properties represented as a string. // Method for setting properties represented as a string.
@ -748,19 +726,19 @@ int FairMQDevice::GetProperty(const int key, const int default_ /*= 0*/)
void FairMQDevice::SetTransport(FairMQTransportFactory* factory) void FairMQDevice::SetTransport(FairMQTransportFactory* factory)
{ {
fTransportFactory = factory; fTransportFactory = unique_ptr<FairMQTransportFactory>(factory);
} }
void FairMQDevice::SetTransport(const string& transport) void FairMQDevice::SetTransport(const string& transport)
{ {
if (transport == "zeromq") if (transport == "zeromq")
{ {
fTransportFactory = new FairMQTransportFactoryZMQ(); fTransportFactory = unique_ptr<FairMQTransportFactory>(new FairMQTransportFactoryZMQ());
} }
#ifdef NANOMSG_FOUND #ifdef NANOMSG_FOUND
else if (transport == "nanomsg") else if (transport == "nanomsg")
{ {
fTransportFactory = new FairMQTransportFactoryNN(); fTransportFactory = unique_ptr<FairMQTransportFactory>(new FairMQTransportFactoryNN());
} }
#endif #endif
else else
@ -809,7 +787,7 @@ void FairMQDevice::LogSocketRates()
{ {
if (vi->fRateLogging > 0) if (vi->fRateLogging > 0)
{ {
filteredSockets.push_back(vi->fSocket); filteredSockets.push_back(vi->fSocket.get());
logIntervals.push_back(vi->fRateLogging); logIntervals.push_back(vi->fRateLogging);
intervalCounters.push_back(0); intervalCounters.push_back(0);
stringstream ss; stringstream ss;
@ -847,56 +825,48 @@ void FairMQDevice::LogSocketRates()
t0 = get_timestamp(); t0 = get_timestamp();
while (true) while (CheckCurrentState(RUNNING))
{ {
try t1 = get_timestamp();
msSinceLastLog = (t1 - t0) / 1000.0L;
i = 0;
for (const auto& vi : filteredSockets)
{ {
t1 = get_timestamp(); intervalCounters.at(i)++;
msSinceLastLog = (t1 - t0) / 1000.0L; if (intervalCounters.at(i) == logIntervals.at(i))
i = 0;
for (const auto& vi : filteredSockets)
{ {
intervalCounters.at(i)++; intervalCounters.at(i) = 0;
if (intervalCounters.at(i) == logIntervals.at(i)) bytesInNew.at(i) = vi->GetBytesRx();
{ mbPerSecIn.at(i) = (static_cast<double>(bytesInNew.at(i) - bytesIn.at(i)) / (1024. * 1024.)) / static_cast<double>(msSinceLastLog) * 1000.;
intervalCounters.at(i) = 0; bytesIn.at(i) = bytesInNew.at(i);
bytesInNew.at(i) = vi->GetBytesRx(); msgInNew.at(i) = vi->GetMessagesRx();
mbPerSecIn.at(i) = (static_cast<double>(bytesInNew.at(i) - bytesIn.at(i)) / (1024. * 1024.)) / static_cast<double>(msSinceLastLog) * 1000.; msgPerSecIn.at(i) = static_cast<double>(msgInNew.at(i) - msgIn.at(i)) / static_cast<double>(msSinceLastLog) * 1000.;
bytesIn.at(i) = bytesInNew.at(i); msgIn.at(i) = msgInNew.at(i);
msgInNew.at(i) = vi->GetMessagesRx(); bytesOutNew.at(i) = vi->GetBytesTx();
msgPerSecIn.at(i) = static_cast<double>(msgInNew.at(i) - msgIn.at(i)) / static_cast<double>(msSinceLastLog) * 1000.; mbPerSecOut.at(i) = (static_cast<double>(bytesOutNew.at(i) - bytesOut.at(i)) / (1024. * 1024.)) / static_cast<double>(msSinceLastLog) * 1000.;
msgIn.at(i) = msgInNew.at(i); bytesOut.at(i) = bytesOutNew.at(i);
bytesOutNew.at(i) = vi->GetBytesTx(); msgOutNew.at(i) = vi->GetMessagesTx();
mbPerSecOut.at(i) = (static_cast<double>(bytesOutNew.at(i) - bytesOut.at(i)) / (1024. * 1024.)) / static_cast<double>(msSinceLastLog) * 1000.; msgPerSecOut.at(i) = static_cast<double>(msgOutNew.at(i) - msgOut.at(i)) / static_cast<double>(msSinceLastLog) * 1000.;
bytesOut.at(i) = bytesOutNew.at(i); msgOut.at(i) = msgOutNew.at(i);
msgOutNew.at(i) = vi->GetMessagesTx(); LOG(DEBUG) << filteredChannelNames.at(i) << ": "
msgPerSecOut.at(i) = static_cast<double>(msgOutNew.at(i) - msgOut.at(i)) / static_cast<double>(msSinceLastLog) * 1000.; << "in: " << msgPerSecIn.at(i) << " msg (" << mbPerSecIn.at(i) << " MB), "
msgOut.at(i) = msgOutNew.at(i); << "out: " << msgPerSecOut.at(i) << " msg (" << mbPerSecOut.at(i) << " MB)";
LOG(DEBUG) << filteredChannelNames.at(i) << ": "
<< "in: " << msgPerSecIn.at(i) << " msg (" << mbPerSecIn.at(i) << " MB), "
<< "out: " << msgPerSecOut.at(i) << " msg (" << mbPerSecOut.at(i) << " MB)";
}
++i;
} }
t0 = t1; ++i;
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
catch (boost::thread_interrupted&)
{
// LOG(DEBUG) << "FairMQDevice::LogSocketRates() interrupted";
break;
} }
t0 = t1;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
} }
// LOG(DEBUG) << "FairMQDevice::LogSocketRates() stopping"; // LOG(DEBUG) << "FairMQDevice::LogSocketRates() stopping";
@ -979,7 +949,7 @@ void FairMQDevice::InteractiveStateLoop()
ChangeState(END); ChangeState(END);
if (CheckCurrentState("EXITING")) if (CheckCurrentState(EXITING))
{ {
fInteractiveRunning = false; fInteractiveRunning = false;
} }
@ -1002,9 +972,8 @@ void FairMQDevice::InteractiveStateLoop()
void FairMQDevice::Unblock() void FairMQDevice::Unblock()
{ {
FairMQChannel::fInterrupted = true; FairMQChannel::fInterrupted = true;
FairMQMessage* cmd = fTransportFactory->CreateMessage(); FairMQMessagePtr cmd(fTransportFactory->CreateMessage());
fCmdSocket->Send(cmd, 0); fCmdSocket->Send(cmd.get(), 0);
delete cmd;
} }
void FairMQDevice::ResetTaskWrapper() void FairMQDevice::ResetTaskWrapper()
@ -1012,11 +981,6 @@ void FairMQDevice::ResetTaskWrapper()
ResetTask(); ResetTask();
ChangeState(internal_DEVICE_READY); ChangeState(internal_DEVICE_READY);
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fStateMutex);
fStateFinished = true;
fStateCondition.notify_one();
} }
void FairMQDevice::ResetTask() void FairMQDevice::ResetTask()
@ -1028,11 +992,6 @@ void FairMQDevice::ResetWrapper()
Reset(); Reset();
ChangeState(internal_IDLE); ChangeState(internal_IDLE);
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fStateMutex);
fStateFinished = true;
fStateCondition.notify_one();
} }
void FairMQDevice::Reset() void FairMQDevice::Reset()
@ -1044,14 +1003,11 @@ void FairMQDevice::Reset()
for (auto& vi : mi.second) for (auto& vi : mi.second)
{ {
vi.fSocket->Close(); vi.fSocket->Close();
delete vi.fSocket;
vi.fSocket = nullptr; vi.fSocket = nullptr;
delete vi.fPoller;
vi.fPoller = nullptr; vi.fPoller = nullptr;
vi.fCmdSocket->Close(); vi.fCmdSocket->Close();
delete vi.fCmdSocket;
vi.fCmdSocket = nullptr; vi.fCmdSocket = nullptr;
} }
} }
@ -1102,32 +1058,5 @@ void FairMQDevice::Shutdown()
FairMQDevice::~FairMQDevice() FairMQDevice::~FairMQDevice()
{ {
// iterate over the channels map
for (auto& mi : fChannels)
{
// iterate over the channels vector
for (auto& vi : mi.second)
{
if (vi.fSocket)
{
delete vi.fSocket;
vi.fSocket = nullptr;
}
if (vi.fPoller)
{
delete vi.fPoller;
vi.fPoller = nullptr;
}
}
}
if (fCmdSocket)
{
delete fCmdSocket;
fCmdSocket = nullptr;
}
delete fTransportFactory;
LOG(DEBUG) << "Device destroyed"; LOG(DEBUG) << "Device destroyed";
} }

View File

@ -36,7 +36,7 @@
typedef std::unordered_map<std::string, std::vector<FairMQChannel>> FairMQChannelMap; typedef std::unordered_map<std::string, std::vector<FairMQChannel>> FairMQChannelMap;
typedef std::function<bool(std::unique_ptr<FairMQMessage>&, int)> InputMsgCallback; typedef std::function<bool(FairMQMessagePtr&, int)> InputMsgCallback;
typedef std::function<bool(FairMQParts&, int)> InputMultipartCallback; typedef std::function<bool(FairMQParts&, int)> InputMultipartCallback;
class FairMQProgOptions; class FairMQProgOptions;
@ -106,7 +106,7 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
/// @param i channel index /// @param i channel index
/// @return Number of bytes that have been queued. -2 If queueing was not possible or timed out. /// @return Number of bytes that have been queued. -2 If queueing was not possible or timed out.
/// In case of errors, returns -1. /// In case of errors, returns -1.
inline int Send(const std::unique_ptr<FairMQMessage>& msg, const std::string& chan, const int i = 0, int sndTimeoutInMs = -1) const inline int Send(const FairMQMessagePtr& msg, const std::string& chan, const int i = 0, int sndTimeoutInMs = -1) const
{ {
return fChannels.at(chan).at(i).Send(msg, sndTimeoutInMs); return fChannels.at(chan).at(i).Send(msg, sndTimeoutInMs);
} }
@ -117,7 +117,7 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
/// @param i channel index /// @param i channel index
/// @return Number of bytes that have been queued. -2 If queueing was not possible or timed out. /// @return Number of bytes that have been queued. -2 If queueing was not possible or timed out.
/// In case of errors, returns -1. /// In case of errors, returns -1.
inline int SendAsync(const std::unique_ptr<FairMQMessage>& msg, const std::string& chan, const int i = 0) const inline int SendAsync(const FairMQMessagePtr& msg, const std::string& chan, const int i = 0) const
{ {
return fChannels.at(chan).at(i).SendAsync(msg); return fChannels.at(chan).at(i).SendAsync(msg);
} }
@ -150,7 +150,7 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
/// @param i channel index /// @param i channel index
/// @return Number of bytes that have been received. -2 If reading from the queue was not possible or timed out. /// @return Number of bytes that have been received. -2 If reading from the queue was not possible or timed out.
/// In case of errors, returns -1. /// In case of errors, returns -1.
inline int Receive(const std::unique_ptr<FairMQMessage>& msg, const std::string& chan, const int i = 0, int rcvTimeoutInMs = -1) const inline int Receive(const FairMQMessagePtr& msg, const std::string& chan, const int i = 0, int rcvTimeoutInMs = -1) const
{ {
return fChannels.at(chan).at(i).Receive(msg, rcvTimeoutInMs); return fChannels.at(chan).at(i).Receive(msg, rcvTimeoutInMs);
} }
@ -161,7 +161,7 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
/// @param i channel index /// @param i channel index
/// @return Number of bytes that have been received. -2 If reading from the queue was not possible or timed out. /// @return Number of bytes that have been received. -2 If reading from the queue was not possible or timed out.
/// In case of errors, returns -1. /// In case of errors, returns -1.
inline int ReceiveAsync(const std::unique_ptr<FairMQMessage>& msg, const std::string& chan, const int i = 0) const inline int ReceiveAsync(const FairMQMessagePtr& msg, const std::string& chan, const int i = 0) const
{ {
return fChannels.at(chan).at(i).ReceiveAsync(msg); return fChannels.at(chan).at(i).ReceiveAsync(msg);
} }
@ -188,16 +188,9 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
return fChannels.at(chan).at(i).ReceiveAsync(parts.fParts); return fChannels.at(chan).at(i).ReceiveAsync(parts.fParts);
} }
/// @brief Create FairMQPoller
/// @return pointer to FairMQPoller
inline FairMQPoller* NewPoller(const std::initializer_list<std::string> channelList) const
{
return fTransportFactory->CreatePoller(fChannels, channelList);
}
/// @brief Create empty FairMQMessage /// @brief Create empty FairMQMessage
/// @return pointer to FairMQMessage /// @return pointer to FairMQMessage
inline FairMQMessage* NewMessage() const inline FairMQMessagePtr NewMessage() const
{ {
return fTransportFactory->CreateMessage(); return fTransportFactory->CreateMessage();
} }
@ -205,7 +198,7 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
/// @brief Create new FairMQMessage of specified size /// @brief Create new FairMQMessage of specified size
/// @param size message size /// @param size message size
/// @return pointer to FairMQMessage /// @return pointer to FairMQMessage
inline FairMQMessage* NewMessage(int size) const inline FairMQMessagePtr NewMessage(int size) const
{ {
return fTransportFactory->CreateMessage(size); return fTransportFactory->CreateMessage(size);
} }
@ -216,26 +209,26 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
/// @param ffn optional callback, called when the message is transfered (and can be deleted) /// @param ffn optional callback, called when the message is transfered (and can be deleted)
/// @param hint optional helper pointer that can be used in the callback /// @param hint optional helper pointer that can be used in the callback
/// @return pointer to FairMQMessage /// @return pointer to FairMQMessage
inline FairMQMessage* NewMessage(void* data, int size, fairmq_free_fn* ffn, void* hint = NULL) const inline FairMQMessagePtr NewMessage(void* data, int size, fairmq_free_fn* ffn, void* hint = NULL) const
{ {
return fTransportFactory->CreateMessage(data, size, ffn, hint); return fTransportFactory->CreateMessage(data, size, ffn, hint);
} }
template<typename T> template<typename T>
inline FairMQMessage* NewSimpleMessage(const T& data) const inline FairMQMessagePtr NewSimpleMessage(const T& data) const
{ {
T* dataCopy = new T(data); T* dataCopy = new T(data);
return fTransportFactory->CreateMessage(dataCopy, sizeof(T), FairMQSimpleMsgCleanup<T>, dataCopy); return fTransportFactory->CreateMessage(dataCopy, sizeof(T), FairMQSimpleMsgCleanup<T>, dataCopy);
} }
template<std::size_t N> template<std::size_t N>
inline FairMQMessage* NewSimpleMessage(const char(&data)[N]) const inline FairMQMessagePtr NewSimpleMessage(const char(&data)[N]) const
{ {
std::string* msgStr = new std::string(data); std::string* msgStr = new std::string(data);
return fTransportFactory->CreateMessage(const_cast<char*>(msgStr->c_str()), msgStr->length(), FairMQSimpleMsgCleanup<std::string>, msgStr); return fTransportFactory->CreateMessage(const_cast<char*>(msgStr->c_str()), msgStr->length(), FairMQSimpleMsgCleanup<std::string>, msgStr);
} }
inline FairMQMessage* NewSimpleMessage(const std::string& str) const inline FairMQMessagePtr NewSimpleMessage(const std::string& str) const
{ {
std::string* msgStr = new std::string(str); std::string* msgStr = new std::string(str);
return fTransportFactory->CreateMessage(const_cast<char*>(msgStr->c_str()), msgStr->length(), FairMQSimpleMsgCleanup<std::string>, msgStr); return fTransportFactory->CreateMessage(const_cast<char*>(msgStr->c_str()), msgStr->length(), FairMQSimpleMsgCleanup<std::string>, msgStr);
@ -298,10 +291,10 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
FairMQProgOptions* fConfig; ///< Program options configuration FairMQProgOptions* fConfig; ///< Program options configuration
template<class T> template<class T>
void OnData(const std::string& channelName, bool (T::* memberFunction)(std::unique_ptr<FairMQMessage>& msg, int index)) void OnData(const std::string& channelName, bool (T::* memberFunction)(FairMQMessagePtr& msg, int index))
{ {
fDataCallbacks = true; fDataCallbacks = true;
fMsgInputs.insert(std::make_pair(channelName, [this, memberFunction](std::unique_ptr<FairMQMessage>& msg, int index) fMsgInputs.insert(std::make_pair(channelName, [this, memberFunction](FairMQMessagePtr& msg, int index)
{ {
return (static_cast<T*>(this)->*memberFunction)(msg, index); return (static_cast<T*>(this)->*memberFunction)(msg, index);
})); }));
@ -336,9 +329,9 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
int fLogIntervalInMs; ///< Interval for logging the socket transfer rates int fLogIntervalInMs; ///< Interval for logging the socket transfer rates
FairMQSocket* fCmdSocket; ///< Socket used for the internal unblocking mechanism FairMQSocketPtr fCmdSocket; ///< Socket used for the internal unblocking mechanism
FairMQTransportFactory* fTransportFactory; ///< Transport factory std::shared_ptr<FairMQTransportFactory> fTransportFactory; ///< Transport factory
/// Additional user initialization (can be overloaded in child classes). Prefer to use InitTask(). /// Additional user initialization (can be overloaded in child classes). Prefer to use InitTask().
virtual void Init(); virtual void Init();

View File

@ -16,6 +16,7 @@
#define FAIRMQPOLLER_H_ #define FAIRMQPOLLER_H_
#include <string> #include <string>
#include <memory>
class FairMQPoller class FairMQPoller
{ {
@ -29,4 +30,6 @@ class FairMQPoller
virtual ~FairMQPoller() {}; virtual ~FairMQPoller() {};
}; };
#endif /* FAIRMQPOLLER_H_ */ using FairMQPollerPtr = std::unique_ptr<FairMQPoller>;
#endif /* FAIRMQPOLLER_H_ */

View File

@ -17,6 +17,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
#include "FairMQMessage.h" #include "FairMQMessage.h"
@ -68,4 +69,6 @@ class FairMQSocket
virtual ~FairMQSocket() {}; virtual ~FairMQSocket() {};
}; };
using FairMQSocketPtr = std::unique_ptr<FairMQSocket>;
#endif /* FAIRMQSOCKET_H_ */ #endif /* FAIRMQSOCKET_H_ */

View File

@ -12,8 +12,7 @@
* @author D. Klein, A. Rybalchenko * @author D. Klein, A. Rybalchenko
*/ */
#include <boost/exception/all.hpp> #include <chrono> // WaitForEndOfStateForMs()
#include <boost/chrono.hpp> // for WaitForEndOfStateForMs()
#include "FairMQStateMachine.h" #include "FairMQStateMachine.h"
#include "FairMQLogger.h" #include "FairMQLogger.h"
@ -97,13 +96,9 @@ bool FairMQStateMachine::ChangeState(int event)
return false; return false;
} }
} }
catch (boost::thread_interrupted& e) catch (std::exception& e)
{ {
LOG(ERROR) << boost::diagnostic_information(e); LOG(ERROR) << "Exception in FairMQStateMachine::ChangeState(): " << e.what();
}
catch (boost::exception& e)
{
LOG(ERROR) << boost::diagnostic_information(e);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return false; return false;
@ -121,35 +116,28 @@ void FairMQStateMachine::WaitForEndOfState(int event)
switch (event) switch (event)
{ {
case INIT_DEVICE: case INIT_DEVICE:
case INIT_TASK:
case RUN: case RUN:
case RESET_TASK: case RESET_TASK:
case RESET_DEVICE: case RESET_DEVICE:
{ {
try std::unique_lock<std::mutex> lock(fWorkMutex);
while (fWorkActive || fWorkAvailable)
{ {
boost::unique_lock<boost::mutex> lock(fStateMutex); fWorkDoneCondition.wait(lock);
while (!fStateFinished)
{
fStateCondition.wait(lock);
}
fStateThread.join();
}
catch (boost::exception& e)
{
LOG(ERROR) << boost::diagnostic_information(e);
exit(EXIT_FAILURE);
} }
break; break;
} }
case INIT_TASK:
break; // InitTask is synchronous, until ROOT workaround is no longer needed.
default: default:
LOG(ERROR) << "Requested state is either synchronous or does not exist."; LOG(ERROR) << "Requested state is either synchronous or does not exist.";
break; break;
} }
} }
catch (boost::thread_interrupted& e) catch (std::exception& e)
{ {
LOG(ERROR) << boost::diagnostic_information(e); LOG(ERROR) << "Exception in FairMQStateMachine::WaitForEndOfState(): " << e.what();
} }
} }
@ -165,16 +153,15 @@ bool FairMQStateMachine::WaitForEndOfStateForMs(int event, int durationInMs)
switch (event) switch (event)
{ {
case INIT_DEVICE: case INIT_DEVICE:
case INIT_TASK:
case RUN: case RUN:
case RESET_TASK: case RESET_TASK:
case RESET_DEVICE: case RESET_DEVICE:
{ {
boost::unique_lock<boost::mutex> lock(fStateMutex); std::unique_lock<std::mutex> lock(fWorkMutex);
while (!fStateFinished) while (fWorkActive || fWorkAvailable)
{ {
fStateCondition.wait_until(lock, boost::chrono::system_clock::now() + boost::chrono::milliseconds(durationInMs)); fWorkDoneCondition.wait_for(lock, std::chrono::milliseconds(durationInMs));
if (!fStateFinished) if (fWorkActive)
{ {
return false; return false;
} }
@ -182,14 +169,16 @@ bool FairMQStateMachine::WaitForEndOfStateForMs(int event, int durationInMs)
return true; return true;
break; break;
} }
case INIT_TASK:
break; // InitTask is synchronous, until ROOT workaround is no longer needed.
default: default:
LOG(ERROR) << "Requested state is either synchronous or does not exist."; LOG(ERROR) << "Requested state is either synchronous or does not exist.";
return false; return false;
} }
} }
catch (boost::thread_interrupted& e) catch (std::exception& e)
{ {
LOG(ERROR) << boost::diagnostic_information(e); LOG(ERROR) << "Exception in FairMQStateMachine::WaitForEndOfStateForMs(): " << e.what();
} }
return false; return false;
} }

View File

@ -19,13 +19,10 @@
#include <string> #include <string>
#include <atomic> #include <atomic>
#include <mutex>
#include <boost/thread.hpp> #include <condition_variable>
#include <boost/thread/mutex.hpp> #include <thread>
#include <boost/thread/condition_variable.hpp> #include <functional>
#include <boost/thread/locks.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
// Increase maximum number of boost::msm states (default is 10) // Increase maximum number of boost::msm states (default is 10)
// This #define has to be before any msm header includes // This #define has to be before any msm header includes
@ -71,15 +68,19 @@ _Pragma("GCC diagnostic ignored \"-Weffc++\"")
#endif #endif
// defining the boost MSM state machine // defining the boost MSM state machine
struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_> struct FairMQFSM_ : public msmf::state_machine_def<FairMQFSM_>
{ {
public: public:
FairMQFSM_() FairMQFSM_()
: fStateThread() : fWorkerThread()
, fTerminateStateThread() , fTerminateStateThread()
, fStateFinished(false) , fWork()
, fStateCondition() , fWorkAvailableCondition()
, fStateMutex() , fWorkDoneCondition()
, fWorkMutex()
, fWorkerTerminated(false)
, fWorkActive(false)
, fWorkAvailable(false)
, fState() , fState()
{} {}
@ -87,32 +88,38 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
virtual ~FairMQFSM_() {}; virtual ~FairMQFSM_() {};
template <class Event, class FSM> template <class Event, class FSM>
void on_entry(Event const&, FSM&) void on_entry(Event const&, FSM& fsm)
{ {
LOG(STATE) << "Starting FairMQ state machine"; LOG(STATE) << "Starting FairMQ state machine";
fState = IDLE; fState = IDLE;
// start a worker thread to execute user states in.
fsm.fWorkerThread = std::thread(&FairMQFSM_::Worker, &fsm);
} }
template <class Event, class FSM> template <class Event, class FSM>
void on_exit(Event const&, FSM&) void on_exit(Event const&, FSM& fsm)
{ {
// join the worker thread (executing user states)
fsm.fWorkerThread.join();
LOG(STATE) << "Exiting FairMQ state machine"; LOG(STATE) << "Exiting FairMQ state machine";
} }
// The list of FSM states // The list of FSM states
struct OK_FSM : public msm::front::state<> {}; struct OK_FSM : public msmf::state<> {};
struct ERROR_FSM : public msm::front::terminate_state<> {}; struct ERROR_FSM : public msmf::terminate_state<> {};
struct IDLE_FSM : public msm::front::state<> {}; struct IDLE_FSM : public msmf::state<> {};
struct INITIALIZING_DEVICE_FSM : public msm::front::state<> {}; struct INITIALIZING_DEVICE_FSM : public msmf::state<> {};
struct DEVICE_READY_FSM : public msm::front::state<> {}; struct DEVICE_READY_FSM : public msmf::state<> {};
struct INITIALIZING_TASK_FSM : public msm::front::state<> {}; struct INITIALIZING_TASK_FSM : public msmf::state<> {};
struct READY_FSM : public msm::front::state<> {}; struct READY_FSM : public msmf::state<> {};
struct RUNNING_FSM : public msm::front::state<> {}; struct RUNNING_FSM : public msmf::state<> {};
struct PAUSED_FSM : public msm::front::state<> {}; struct PAUSED_FSM : public msmf::state<> {};
struct RESETTING_TASK_FSM : public msm::front::state<> {}; struct RESETTING_TASK_FSM : public msmf::state<> {};
struct RESETTING_DEVICE_FSM : public msm::front::state<> {}; struct RESETTING_DEVICE_FSM : public msmf::state<> {};
struct EXITING_FSM : public msm::front::state<> {}; struct EXITING_FSM : public msmf::state<> {};
// Define initial states // Define initial states
typedef mpl::vector<IDLE_FSM, OK_FSM> initial_state; typedef mpl::vector<IDLE_FSM, OK_FSM> initial_state;
@ -124,7 +131,6 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering IDLE state"; LOG(STATE) << "Entering IDLE state";
fsm.fState = IDLE; fsm.fState = IDLE;
} }
}; };
@ -135,12 +141,16 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering INITIALIZING DEVICE state"; LOG(STATE) << "Entering INITIALIZING DEVICE state";
fsm.fStateFinished = false;
fsm.fState = INITIALIZING_DEVICE; fsm.fState = INITIALIZING_DEVICE;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::InitWrapper, &fsm)); std::unique_lock<std::mutex> lock(fsm.fWorkMutex);
while (fsm.fWorkActive)
{
fsm.fWorkDoneCondition.wait(lock);
}
fsm.fWorkAvailable = true;
fsm.fWork = std::bind(&FairMQFSM_::InitWrapper, &fsm);
fsm.fWorkAvailableCondition.notify_one();
} }
}; };
@ -150,7 +160,6 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering DEVICE READY state"; LOG(STATE) << "Entering DEVICE READY state";
fsm.fState = DEVICE_READY; fsm.fState = DEVICE_READY;
} }
}; };
@ -161,13 +170,10 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering INITIALIZING TASK state"; LOG(STATE) << "Entering INITIALIZING TASK state";
fsm.fStateFinished = false;
fsm.fState = INITIALIZING_TASK; fsm.fState = INITIALIZING_TASK;
fsm.InitTaskWrapper(); fsm.InitTaskWrapper();
// fsm.fInitializingTaskThread = boost::thread(boost::bind(&FairMQFSM_::InitTaskWrapper, &fsm)); // fsm.fInitializingTaskThread = std::thread(&FairMQFSM_::InitTaskWrapper, &fsm);
} }
}; };
@ -177,7 +183,6 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering READY state"; LOG(STATE) << "Entering READY state";
fsm.fState = READY; fsm.fState = READY;
} }
}; };
@ -188,12 +193,16 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering RUNNING state"; LOG(STATE) << "Entering RUNNING state";
fsm.fStateFinished = false;
fsm.fState = RUNNING; fsm.fState = RUNNING;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::RunWrapper, &fsm)); std::unique_lock<std::mutex> lock(fsm.fWorkMutex);
while (fsm.fWorkActive)
{
fsm.fWorkDoneCondition.wait(lock);
}
fsm.fWorkAvailable = true;
fsm.fWork = std::bind(&FairMQFSM_::RunWrapper, &fsm);
fsm.fWorkAvailableCondition.notify_one();
} }
}; };
@ -202,15 +211,19 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class EVT, class FSM, class SourceState, class TargetState> template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering PAUSED state";
fsm.fState = PAUSED; fsm.fState = PAUSED;
fsm.fStateFinished = false;
fsm.Unblock(); fsm.Unblock();
fsm.fStateThread.join();
LOG(STATE) << "Entering PAUSED state"; std::unique_lock<std::mutex> lock(fsm.fWorkMutex);
while (fsm.fWorkActive)
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::Pause, &fsm)); {
fsm.fWorkDoneCondition.wait(lock);
}
fsm.fWorkAvailable = true;
fsm.fWork = std::bind(&FairMQFSM_::Pause, &fsm);
fsm.fWorkAvailableCondition.notify_one();
} }
}; };
@ -219,15 +232,17 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class EVT, class FSM, class SourceState, class TargetState> template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering RUNNING state";
fsm.fState = RUNNING; fsm.fState = RUNNING;
fsm.fStateThread.interrupt(); std::unique_lock<std::mutex> lock(fsm.fWorkMutex);
fsm.fStateThread.join(); while (fsm.fWorkActive)
fsm.fStateFinished = false; {
fsm.fWorkDoneCondition.wait(lock);
LOG(STATE) << "Entering RUNNING state"; }
fsm.fWorkAvailable = true;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::RunWrapper, &fsm)); fsm.fWork = std::bind(&FairMQFSM_::RunWrapper, &fsm);
fsm.fWorkAvailableCondition.notify_one();
} }
}; };
@ -237,11 +252,14 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering READY state"; LOG(STATE) << "Entering READY state";
fsm.fState = READY; fsm.fState = READY;
fsm.Unblock(); fsm.Unblock();
fsm.fStateThread.join(); std::unique_lock<std::mutex> lock(fsm.fWorkMutex);
while (fsm.fWorkActive)
{
fsm.fWorkDoneCondition.wait(lock);
}
} }
}; };
@ -250,8 +268,7 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class EVT, class FSM, class SourceState, class TargetState> template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "RUNNING state finished without an external event"; LOG(STATE) << "RUNNING state finished without an external event, entering READY state";
LOG(STATE) << "Entering READY state";
fsm.fState = READY; fsm.fState = READY;
} }
}; };
@ -262,12 +279,16 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering RESETTING TASK state"; LOG(STATE) << "Entering RESETTING TASK state";
fsm.fStateFinished = false;
fsm.fState = RESETTING_TASK; fsm.fState = RESETTING_TASK;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::ResetTaskWrapper, &fsm)); std::unique_lock<std::mutex> lock(fsm.fWorkMutex);
while (fsm.fWorkActive)
{
fsm.fWorkDoneCondition.wait(lock);
}
fsm.fWorkAvailable = true;
fsm.fWork = std::bind(&FairMQFSM_::ResetTaskWrapper, &fsm);
fsm.fWorkAvailableCondition.notify_one();
} }
}; };
@ -277,12 +298,16 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering RESETTING DEVICE state"; LOG(STATE) << "Entering RESETTING DEVICE state";
fsm.fStateFinished = false;
fsm.fState = RESETTING_DEVICE; fsm.fState = RESETTING_DEVICE;
fsm.ResetWrapper(); std::unique_lock<std::mutex> lock(fsm.fWorkMutex);
while (fsm.fWorkActive)
{
fsm.fWorkDoneCondition.wait(lock);
}
fsm.fWorkAvailable = true;
fsm.fWork = std::bind(&FairMQFSM_::ResetWrapper, &fsm);
fsm.fWorkAvailableCondition.notify_one();
} }
}; };
@ -292,10 +317,14 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Entering EXITING state"; LOG(STATE) << "Entering EXITING state";
fsm.fState = EXITING; fsm.fState = EXITING;
fsm.fTerminateStateThread = boost::thread(boost::bind(&FairMQFSM_::Terminate, &fsm)); // terminate worker thread
std::lock_guard<std::mutex> lock(fsm.fWorkMutex);
fsm.fWorkerTerminated = true;
fsm.fWorkAvailableCondition.notify_one();
fsm.fTerminateStateThread = std::thread(&FairMQFSM_::Terminate, &fsm);
fsm.Shutdown(); fsm.Shutdown();
fsm.fTerminateStateThread.join(); fsm.fTerminateStateThread.join();
} }
@ -310,10 +339,14 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
fsm.fState = EXITING; fsm.fState = EXITING;
fsm.Unblock(); fsm.Unblock(); // Unblock potential blocking transfer calls
fsm.fStateThread.join();
fsm.fTerminateStateThread = boost::thread(boost::bind(&FairMQFSM_::Terminate, &fsm)); // terminate worker thread
std::lock_guard<std::mutex> lock(fsm.fWorkMutex);
fsm.fWorkerTerminated = true;
fsm.fWorkAvailableCondition.notify_one();
fsm.fTerminateStateThread = std::thread(&FairMQFSM_::Terminate, &fsm);
fsm.Shutdown(); fsm.Shutdown();
fsm.fTerminateStateThread.join(); fsm.fTerminateStateThread.join();
} }
@ -346,6 +379,35 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
virtual void Terminate() {} // Termination method called during StopFct action. virtual void Terminate() {} // Termination method called during StopFct action.
virtual void Unblock() {} // Method to send commands. virtual void Unblock() {} // Method to send commands.
void Worker()
{
while (true)
{
{
std::unique_lock<std::mutex> lock(fWorkMutex);
// Wait for work to be done.
while (!fWorkAvailable && !fWorkerTerminated)
{
fWorkAvailableCondition.wait(lock);
}
if (fWorkerTerminated)
{
break;
}
fWorkActive = true;
}
fWork();
std::lock_guard<std::mutex> lock(fWorkMutex);
fWorkActive = false;
fWorkAvailable = false;
fWorkDoneCondition.notify_one();
}
}
// Transition table for FairMQFSM // Transition table for FairMQFSM
struct transition_table : mpl::vector< struct transition_table : mpl::vector<
// Start Event Next Action Guard // Start Event Next Action Guard
@ -372,15 +434,18 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class FSM, class Event> template <class FSM, class Event>
void no_transition(Event const& e, FSM&, int state) void no_transition(Event const& e, FSM&, int state)
{ {
typedef typename boost::msm::back::recursive_get_transition_table<FSM>::type recursive_stt; typedef typename msm::back::recursive_get_transition_table<FSM>::type recursive_stt;
typedef typename boost::msm::back::generate_state_set<recursive_stt>::type all_states; typedef typename msm::back::generate_state_set<recursive_stt>::type all_states;
std::string stateName; std::string stateName;
boost::mpl::for_each<all_states,boost::msm::wrap<boost::mpl::placeholders::_1> >(boost::msm::back::get_state_name<recursive_stt>(stateName, state));
mpl::for_each<all_states, msm::wrap<mpl::placeholders::_1>>(msm::back::get_state_name<recursive_stt>(stateName, state));
stateName = stateName.substr(24); stateName = stateName.substr(24);
std::size_t pos = stateName.find("_FSME"); std::size_t pos = stateName.find("_FSME");
stateName.erase(pos); stateName.erase(pos);
if (stateName == "1RUNNING" || stateName == "6DEVICE_READY" || stateName == "0PAUSED" || stateName == "8RESETTING_TASK") if (stateName == "1RUNNING" || stateName == "6DEVICE_READY" || stateName == "0PAUSED" || stateName == "8RESETTING_TASK" || stateName == "0RESETTING_DEVICE")
{ {
stateName = stateName.substr(1); stateName = stateName.substr(1);
} }
@ -478,13 +543,17 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
} }
// this is to run certain functions in a separate thread // this is to run certain functions in a separate thread
boost::thread fStateThread; std::thread fWorkerThread;
boost::thread fTerminateStateThread; std::thread fTerminateStateThread;
// condition variable to notify parent thread about end of state. // function to execute user states in a worker thread
std::atomic<bool> fStateFinished; std::function<void(void)> fWork;
boost::condition_variable fStateCondition; std::condition_variable fWorkAvailableCondition;
boost::mutex fStateMutex; std::condition_variable fWorkDoneCondition;
std::mutex fWorkMutex;
bool fWorkerTerminated;
bool fWorkActive;
bool fWorkAvailable;
protected: protected:
std::atomic<State> fState; std::atomic<State> fState;

View File

@ -16,6 +16,7 @@
#define FAIRMQTRANSPORTFACTORY_H_ #define FAIRMQTRANSPORTFACTORY_H_
#include <string> #include <string>
#include <memory>
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
@ -30,15 +31,15 @@ class FairMQChannel;
class FairMQTransportFactory class FairMQTransportFactory
{ {
public: public:
virtual FairMQMessage* CreateMessage() const = 0; virtual FairMQMessagePtr CreateMessage() const = 0;
virtual FairMQMessage* CreateMessage(const size_t size) const = 0; virtual FairMQMessagePtr CreateMessage(const size_t size) const = 0;
virtual FairMQMessage* CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint = NULL) const = 0; virtual FairMQMessagePtr CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint = NULL) const = 0;
virtual FairMQSocket* CreateSocket(const std::string& type, const std::string& name, const int numIoThreads, const std::string& id = "") const = 0; virtual FairMQSocketPtr CreateSocket(const std::string& type, const std::string& name, const int numIoThreads, const std::string& id = "") const = 0;
virtual FairMQPoller* CreatePoller(const std::vector<FairMQChannel>& channels) const = 0; virtual FairMQPollerPtr CreatePoller(const std::vector<FairMQChannel>& channels) const = 0;
virtual FairMQPoller* CreatePoller(const std::unordered_map<std::string, std::vector<FairMQChannel>>& channelsMap, const std::vector<std::string>& channelList) const = 0; virtual FairMQPollerPtr CreatePoller(const std::unordered_map<std::string, std::vector<FairMQChannel>>& channelsMap, const std::vector<std::string>& channelList) const = 0;
virtual FairMQPoller* CreatePoller(const FairMQSocket& cmdSocket, const FairMQSocket& dataSocket) const = 0; virtual FairMQPollerPtr CreatePoller(const FairMQSocket& cmdSocket, const FairMQSocket& dataSocket) const = 0;
virtual ~FairMQTransportFactory() {}; virtual ~FairMQTransportFactory() {};
}; };

View File

@ -13,10 +13,8 @@
*/ */
#include <vector> #include <vector>
#include <chrono>
#include <boost/thread.hpp> #include <thread>
#include <boost/bind.hpp>
#include <boost/timer/timer.hpp>
#include "FairMQBenchmarkSampler.h" #include "FairMQBenchmarkSampler.h"
#include "FairMQLogger.h" #include "FairMQLogger.h"
@ -47,7 +45,7 @@ void FairMQBenchmarkSampler::InitTask()
void FairMQBenchmarkSampler::Run() void FairMQBenchmarkSampler::Run()
{ {
// boost::thread resetMsgCounter(boost::bind(&FairMQBenchmarkSampler::ResetMsgCounter, this)); // std::thread resetMsgCounter(&FairMQBenchmarkSampler::ResetMsgCounter, this);
int numSentMsgs = 0; int numSentMsgs = 0;
@ -57,7 +55,7 @@ void FairMQBenchmarkSampler::Run()
const FairMQChannel& dataOutChannel = fChannels.at(fOutChannelName).at(0); const FairMQChannel& dataOutChannel = fChannels.at(fOutChannelName).at(0);
LOG(INFO) << "Starting the benchmark with message size of " << fMsgSize << " and number of messages " << fNumMsgs << "."; LOG(INFO) << "Starting the benchmark with message size of " << fMsgSize << " and number of messages " << fNumMsgs << ".";
boost::timer::auto_cpu_timer timer; auto tStart = chrono::high_resolution_clock::now();
while (CheckCurrentState(RUNNING)) while (CheckCurrentState(RUNNING))
{ {
@ -78,38 +76,23 @@ void FairMQBenchmarkSampler::Run()
// --fMsgCounter; // --fMsgCounter;
// while (fMsgCounter == 0) { // while (fMsgCounter == 0) {
// boost::this_thread::sleep(boost::posix_time::milliseconds(1)); // this_thread::sleep_for(chrono::milliseconds(1));
// } // }
} }
LOG(INFO) << "Sent " << numSentMsgs << " messages, leaving RUNNING state."; auto tEnd = chrono::high_resolution_clock::now();
LOG(INFO) << "Sending time: ";
// try LOG(INFO) << "Sent " << numSentMsgs << " messages, leaving RUNNING state.";
// { LOG(INFO) << "Sending time: " << chrono::duration<double, milli>(tEnd - tStart).count() << " ms";
// resetMsgCounter.interrupt();
// resetMsgCounter.join(); // resetMsgCounter.join();
// }
// catch(boost::thread_resource_error& e)
// {
// LOG(ERROR) << e.what();
// exit(EXIT_FAILURE);
// }
} }
void FairMQBenchmarkSampler::ResetMsgCounter() void FairMQBenchmarkSampler::ResetMsgCounter()
{ {
while (true) while (CheckCurrentState(RUNNING))
{ {
try fMsgCounter = fMsgRate / 100;
{ this_thread::sleep_for(chrono::milliseconds(10));
fMsgCounter = fMsgRate / 100;
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
}
catch (boost::thread_interrupted&)
{
LOG(DEBUG) << "Event rate limiter thread interrupted";
break;
}
} }
} }

View File

@ -12,7 +12,7 @@
* @author D. Klein, A. Rybalchenko * @author D. Klein, A. Rybalchenko
*/ */
#include <boost/timer/timer.hpp> #include <chrono>
#include "FairMQSink.h" #include "FairMQSink.h"
#include "FairMQLogger.h" #include "FairMQLogger.h"
@ -39,7 +39,7 @@ void FairMQSink::Run()
const FairMQChannel& dataInChannel = fChannels.at(fInChannelName).at(0); const FairMQChannel& dataInChannel = fChannels.at(fInChannelName).at(0);
LOG(INFO) << "Starting the benchmark and expecting to receive " << fNumMsgs << " messages."; LOG(INFO) << "Starting the benchmark and expecting to receive " << fNumMsgs << " messages.";
boost::timer::auto_cpu_timer timer; auto tStart = chrono::high_resolution_clock::now();
while (CheckCurrentState(RUNNING)) while (CheckCurrentState(RUNNING))
{ {
@ -58,8 +58,10 @@ void FairMQSink::Run()
} }
} }
auto tEnd = chrono::high_resolution_clock::now();
LOG(INFO) << "Received " << numReceivedMsgs << " messages, leaving RUNNING state."; LOG(INFO) << "Received " << numReceivedMsgs << " messages, leaving RUNNING state.";
LOG(INFO) << "Receiving time: "; LOG(INFO) << "Receiving time: " << chrono::duration<double, milli>(tEnd - tStart).count() << " ms";
} }
FairMQSink::~FairMQSink() FairMQSink::~FairMQSink()

View File

@ -21,37 +21,37 @@ FairMQTransportFactoryNN::FairMQTransportFactoryNN()
LOG(INFO) << "Using nanomsg library"; LOG(INFO) << "Using nanomsg library";
} }
FairMQMessage* FairMQTransportFactoryNN::CreateMessage() const FairMQMessagePtr FairMQTransportFactoryNN::CreateMessage() const
{ {
return new FairMQMessageNN(); return unique_ptr<FairMQMessage>(new FairMQMessageNN());
} }
FairMQMessage* FairMQTransportFactoryNN::CreateMessage(const size_t size) const FairMQMessagePtr FairMQTransportFactoryNN::CreateMessage(const size_t size) const
{ {
return new FairMQMessageNN(size); return unique_ptr<FairMQMessage>(new FairMQMessageNN(size));
} }
FairMQMessage* FairMQTransportFactoryNN::CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint) const FairMQMessagePtr FairMQTransportFactoryNN::CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint) const
{ {
return new FairMQMessageNN(data, size, ffn, hint); return unique_ptr<FairMQMessage>(new FairMQMessageNN(data, size, ffn, hint));
} }
FairMQSocket* FairMQTransportFactoryNN::CreateSocket(const string& type, const std::string& name, const int numIoThreads, const std::string& id /*= ""*/) const FairMQSocketPtr FairMQTransportFactoryNN::CreateSocket(const string& type, const string& name, const int numIoThreads, const string& id /*= ""*/) const
{ {
return new FairMQSocketNN(type, name, numIoThreads, id); return unique_ptr<FairMQSocket>(new FairMQSocketNN(type, name, numIoThreads, id));
} }
FairMQPoller* FairMQTransportFactoryNN::CreatePoller(const vector<FairMQChannel>& channels) const FairMQPollerPtr FairMQTransportFactoryNN::CreatePoller(const vector<FairMQChannel>& channels) const
{ {
return new FairMQPollerNN(channels); return unique_ptr<FairMQPoller>(new FairMQPollerNN(channels));
} }
FairMQPoller* FairMQTransportFactoryNN::CreatePoller(const std::unordered_map<std::string, std::vector<FairMQChannel>>& channelsMap, const std::vector<std::string>& channelList) const FairMQPollerPtr FairMQTransportFactoryNN::CreatePoller(const unordered_map<string, vector<FairMQChannel>>& channelsMap, const vector<string>& channelList) const
{ {
return new FairMQPollerNN(channelsMap, channelList); return unique_ptr<FairMQPoller>(new FairMQPollerNN(channelsMap, channelList));
} }
FairMQPoller* FairMQTransportFactoryNN::CreatePoller(const FairMQSocket& cmdSocket, const FairMQSocket& dataSocket) const FairMQPollerPtr FairMQTransportFactoryNN::CreatePoller(const FairMQSocket& cmdSocket, const FairMQSocket& dataSocket) const
{ {
return new FairMQPollerNN(cmdSocket, dataSocket); return unique_ptr<FairMQPoller>(new FairMQPollerNN(cmdSocket, dataSocket));
} }

View File

@ -27,15 +27,15 @@ class FairMQTransportFactoryNN : public FairMQTransportFactory
public: public:
FairMQTransportFactoryNN(); FairMQTransportFactoryNN();
virtual FairMQMessage* CreateMessage() const; virtual FairMQMessagePtr CreateMessage() const;
virtual FairMQMessage* CreateMessage(const size_t size) const; virtual FairMQMessagePtr CreateMessage(const size_t size) const;
virtual FairMQMessage* CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint = NULL) const; virtual FairMQMessagePtr CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint = NULL) const;
virtual FairMQSocket* CreateSocket(const std::string& type, const std::string& name, const int numIoThreads, const std::string& id = "") const; virtual FairMQSocketPtr CreateSocket(const std::string& type, const std::string& name, const int numIoThreads, const std::string& id = "") const;
virtual FairMQPoller* CreatePoller(const std::vector<FairMQChannel>& channels) const; virtual FairMQPollerPtr CreatePoller(const std::vector<FairMQChannel>& channels) const;
virtual FairMQPoller* CreatePoller(const std::unordered_map<std::string, std::vector<FairMQChannel>>& channelsMap, const std::vector<std::string>& channelList) const; virtual FairMQPollerPtr CreatePoller(const std::unordered_map<std::string, std::vector<FairMQChannel>>& channelsMap, const std::vector<std::string>& channelList) const;
virtual FairMQPoller* CreatePoller(const FairMQSocket& cmdSocket, const FairMQSocket& dataSocket) const; virtual FairMQPollerPtr CreatePoller(const FairMQSocket& cmdSocket, const FairMQSocket& dataSocket) const;
virtual ~FairMQTransportFactoryNN() {}; virtual ~FairMQTransportFactoryNN() {};
}; };

View File

@ -59,33 +59,6 @@ void MyCallBack(MyDevice& d, double val)
d.Print(); d.Print();
} }
void PrintMQParam(const FairMQMap& channels, const FairMQProgOptions& config)
{
for (const auto& p : channels)
{
int index = 0;
for (const auto& channel : p.second)
{
string typeKey = p.first + "." + to_string(index) + ".type";
string methodKey = p.first + "." + to_string(index) + ".method";
string addressKey = p.first + "." + to_string(index) + ".address";
string propertyKey = p.first + "." + to_string(index) + ".property";
string sndBufSizeKey = p.first + "." + to_string(index) + ".sndBufSize";
string rcvBufSizeKey = p.first + "." + to_string(index) + ".rcvBufSize";
string rateLoggingKey = p.first + "." + to_string(index) + ".rateLogging";
LOG(INFO) << "Channel name = " << p.first;
LOG(INFO) << "key = " << typeKey <<"\t value = " << config.GetValue<string>(typeKey);
LOG(INFO) << "key = " << methodKey <<"\t value = " << config.GetValue<string>(methodKey);
LOG(INFO) << "key = " << addressKey <<"\t value = " << config.GetValue<string>(addressKey);
LOG(INFO) << "key = " << propertyKey <<"\t value = " << config.GetValue<string>(propertyKey);
LOG(INFO) << "key = " << sndBufSizeKey << "\t value = " << config.GetValue<int>(sndBufSizeKey);
LOG(INFO) << "key = " << rcvBufSizeKey <<"\t value = " << config.GetValue<int>(rcvBufSizeKey);
LOG(INFO) << "key = " << rateLoggingKey <<"\t value = " << config.GetValue<int>(rateLoggingKey);
}
}
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
try try
@ -103,9 +76,6 @@ int main(int argc, char** argv)
// // get FairMQMap // // get FairMQMap
// auto map1 = config.GetFairMQMap(); // auto map1 = config.GetFairMQMap();
// // form keys from map1 and print the value stored in variable map
// PrintMQParam(map1, config);
// // update value in variable map, and propagate the update to the FairMQMap // // update value in variable map, and propagate the update to the FairMQMap
// config.UpdateValue<string>("data.0.address","tcp://localhost:1234"); // config.UpdateValue<string>("data.0.address","tcp://localhost:1234");
@ -118,9 +88,6 @@ int main(int argc, char** argv)
// // update the FairMQMap and propagate the change in variable map // // update the FairMQMap and propagate the change in variable map
// config.UpdateChannelMap(map2); // config.UpdateChannelMap(map2);
// // print values stored in variable map
// PrintMQParam(map2, config);
MyDevice device; MyDevice device;
device.CatchSignals(); device.CatchSignals();
device.SetConfig(config); device.SetConfig(config);

View File

@ -125,7 +125,7 @@ class FairMQConfigPluginDDS
} }
} }
void Run(FairMQDevice& device) void Run(FairMQDevice& /*device*/)
{ {
// start DDS intercom service // start DDS intercom service
fService.start(); fService.start();

View File

@ -31,7 +31,7 @@ int main(int argc, char* argv[])
}); });
// subscribe to receive messages from DDS // subscribe to receive messages from DDS
ddsCustomCmd.subscribe([](const string& msg, const string& condition, uint64_t senderId) ddsCustomCmd.subscribe([](const string& msg, const string& /*condition*/, uint64_t /*senderId*/)
{ {
cout << "Received: \"" << msg << "\"" << endl; cout << "Received: \"" << msg << "\"" << endl;
}); });

View File

@ -25,37 +25,37 @@ FairMQTransportFactoryZMQ::FairMQTransportFactoryZMQ()
LOG(DEBUG) << "Using ZeroMQ library, version: " << major << "." << minor << "." << patch; LOG(DEBUG) << "Using ZeroMQ library, version: " << major << "." << minor << "." << patch;
} }
FairMQMessage* FairMQTransportFactoryZMQ::CreateMessage() const FairMQMessagePtr FairMQTransportFactoryZMQ::CreateMessage() const
{ {
return new FairMQMessageZMQ(); return unique_ptr<FairMQMessage>(new FairMQMessageZMQ());
} }
FairMQMessage* FairMQTransportFactoryZMQ::CreateMessage(const size_t size) const FairMQMessagePtr FairMQTransportFactoryZMQ::CreateMessage(const size_t size) const
{ {
return new FairMQMessageZMQ(size); return unique_ptr<FairMQMessage>(new FairMQMessageZMQ(size));
} }
FairMQMessage* FairMQTransportFactoryZMQ::CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint) const FairMQMessagePtr FairMQTransportFactoryZMQ::CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint) const
{ {
return new FairMQMessageZMQ(data, size, ffn, hint); return unique_ptr<FairMQMessage>(new FairMQMessageZMQ(data, size, ffn, hint));
} }
FairMQSocket* FairMQTransportFactoryZMQ::CreateSocket(const string& type, const std::string& name, const int numIoThreads, const std::string& id /*= ""*/) const FairMQSocketPtr FairMQTransportFactoryZMQ::CreateSocket(const string& type, const string& name, const int numIoThreads, const string& id /*= ""*/) const
{ {
return new FairMQSocketZMQ(type, name, numIoThreads, id); return unique_ptr<FairMQSocket>(new FairMQSocketZMQ(type, name, numIoThreads, id));
} }
FairMQPoller* FairMQTransportFactoryZMQ::CreatePoller(const vector<FairMQChannel>& channels) const FairMQPollerPtr FairMQTransportFactoryZMQ::CreatePoller(const vector<FairMQChannel>& channels) const
{ {
return new FairMQPollerZMQ(channels); return unique_ptr<FairMQPoller>(new FairMQPollerZMQ(channels));
} }
FairMQPoller* FairMQTransportFactoryZMQ::CreatePoller(const std::unordered_map<std::string, std::vector<FairMQChannel>>& channelsMap, const std::vector<std::string>& channelList) const FairMQPollerPtr FairMQTransportFactoryZMQ::CreatePoller(const unordered_map<string, vector<FairMQChannel>>& channelsMap, const vector<string>& channelList) const
{ {
return new FairMQPollerZMQ(channelsMap, channelList); return unique_ptr<FairMQPoller>(new FairMQPollerZMQ(channelsMap, channelList));
} }
FairMQPoller* FairMQTransportFactoryZMQ::CreatePoller(const FairMQSocket& cmdSocket, const FairMQSocket& dataSocket) const FairMQPollerPtr FairMQTransportFactoryZMQ::CreatePoller(const FairMQSocket& cmdSocket, const FairMQSocket& dataSocket) const
{ {
return new FairMQPollerZMQ(cmdSocket, dataSocket); return unique_ptr<FairMQPoller>(new FairMQPollerZMQ(cmdSocket, dataSocket));
} }

View File

@ -28,15 +28,15 @@ class FairMQTransportFactoryZMQ : public FairMQTransportFactory
public: public:
FairMQTransportFactoryZMQ(); FairMQTransportFactoryZMQ();
virtual FairMQMessage* CreateMessage() const; virtual FairMQMessagePtr CreateMessage() const;
virtual FairMQMessage* CreateMessage(const size_t size) const; virtual FairMQMessagePtr CreateMessage(const size_t size) const;
virtual FairMQMessage* CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint = NULL) const; virtual FairMQMessagePtr CreateMessage(void* data, const size_t size, fairmq_free_fn* ffn, void* hint = NULL) const;
virtual FairMQSocket* CreateSocket(const std::string& type, const std::string& name, const int numIoThreads, const std::string& id = "") const; virtual FairMQSocketPtr CreateSocket(const std::string& type, const std::string& name, const int numIoThreads, const std::string& id = "") const;
virtual FairMQPoller* CreatePoller(const std::vector<FairMQChannel>& channels) const; virtual FairMQPollerPtr CreatePoller(const std::vector<FairMQChannel>& channels) const;
virtual FairMQPoller* CreatePoller(const std::unordered_map<std::string, std::vector<FairMQChannel>>& channelsMap, const std::vector<std::string>& channelList) const; virtual FairMQPollerPtr CreatePoller(const std::unordered_map<std::string, std::vector<FairMQChannel>>& channelsMap, const std::vector<std::string>& channelList) const;
virtual FairMQPoller* CreatePoller(const FairMQSocket& cmdSocket, const FairMQSocket& dataSocket) const; virtual FairMQPollerPtr CreatePoller(const FairMQSocket& cmdSocket, const FairMQSocket& dataSocket) const;
virtual ~FairMQTransportFactoryZMQ() {}; virtual ~FairMQTransportFactoryZMQ() {};
}; };