mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-16 10:01:47 +00:00
Several FairMQ fixes and improvements:
- FairMQ: add possibility to poll on multiple channels. - FairMQ: include command channel when polling on blocking calls (for unblocking without termination). - FairMQ: move signal handler inside of FairMQDevice class (call FairMQDevice::CatchSignals() in the main function). - FairMQ: add 'bool CheckCurrentState(statename)' (instead of 'GetCurrentState() == statename' that cannot be thread safe). - FairMQDevice: add 'InteractiveStateLoop()' method that can be used to change states from the command line. - FairMQDevice: add automatic transition to IDLE state if Run() exits without an external event. - FairMQDevice: implement device reset. - FairMQDevice: use unordered_map for device channels. - FairMQChannel: improve address validation for channels. - FairMQChannel: add ExpectsAnotherPart() method to check if another msg part is expected (old approach still works). - FairMQ: remove invalid transition from the run files. - FairMQFileSink: disable ROOT termination signal handler. - Tutorial3: spawn xterm windows from start scripts without overlapping for better visibility. - FairMQ Examples: update protobuf test and move its files to a common directory. - FairMQStateMachine: improve feedback on invalid transitions (more readable).
This commit is contained in:
committed by
Mohammad Al-Turany
parent
d1bba61939
commit
1302e77a16
@@ -21,7 +21,8 @@ using namespace std;
|
||||
|
||||
FairMQPollerZMQ::FairMQPollerZMQ(const vector<FairMQChannel>& channels)
|
||||
: items()
|
||||
, fNumItems()
|
||||
, fNumItems(0)
|
||||
, fOffsetMap()
|
||||
{
|
||||
fNumItems = channels.size();
|
||||
items = new zmq_pollitem_t[fNumItems];
|
||||
@@ -35,15 +36,101 @@ FairMQPollerZMQ::FairMQPollerZMQ(const vector<FairMQChannel>& channels)
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQPollerZMQ::Poll(int timeout)
|
||||
FairMQPollerZMQ::FairMQPollerZMQ(map< string,vector<FairMQChannel> >& channelsMap, initializer_list<string> channelList)
|
||||
: items()
|
||||
, fNumItems(0)
|
||||
, fOffsetMap()
|
||||
{
|
||||
if (zmq_poll(items, fNumItems, timeout) < 0)
|
||||
int offset = 0;
|
||||
|
||||
try
|
||||
{
|
||||
LOG(ERROR) << "polling failed, reason: " << zmq_strerror(errno);
|
||||
// calculate offsets and the total size of the poll item set
|
||||
for (string channel : channelList)
|
||||
{
|
||||
fOffsetMap[channel] = offset;
|
||||
offset += channelsMap.at(channel).size();
|
||||
fNumItems += channelsMap.at(channel).size();
|
||||
}
|
||||
|
||||
items = new zmq_pollitem_t[fNumItems];
|
||||
|
||||
int index = 0;
|
||||
for (string channel : channelList)
|
||||
{
|
||||
for (int i = 0; i < channelsMap.at(channel).size(); ++i)
|
||||
{
|
||||
index = fOffsetMap[channel] + i;
|
||||
items[index].socket = channelsMap.at(channel).at(i).fSocket->GetSocket();
|
||||
items[index].fd = 0;
|
||||
items[index].events = ZMQ_POLLIN;
|
||||
items[index].revents = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const std::out_of_range& oor)
|
||||
{
|
||||
LOG(ERROR) << "At least one of the provided channel keys for poller initialization is invalid";
|
||||
LOG(ERROR) << "Out of Range error: " << oor.what() << '\n';
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
bool FairMQPollerZMQ::CheckInput(int index)
|
||||
FairMQPollerZMQ::FairMQPollerZMQ(FairMQSocket& dataSocket, FairMQSocket& cmdSocket)
|
||||
: items()
|
||||
, fNumItems(2)
|
||||
, fOffsetMap()
|
||||
{
|
||||
items = new zmq_pollitem_t[fNumItems];
|
||||
|
||||
items[0].socket = cmdSocket.GetSocket();
|
||||
items[0].fd = 0;
|
||||
items[0].events = ZMQ_POLLIN;
|
||||
items[0].revents = 0;
|
||||
|
||||
items[1].socket = dataSocket.GetSocket();
|
||||
items[1].fd = 0;
|
||||
items[1].revents = 0;
|
||||
|
||||
int type = 0;
|
||||
size_t size = sizeof(type);
|
||||
zmq_getsockopt (dataSocket.GetSocket(), ZMQ_TYPE, &type, &size);
|
||||
|
||||
if (type == ZMQ_REQ || type == ZMQ_REP || type == ZMQ_PAIR || type == ZMQ_DEALER || type == ZMQ_ROUTER)
|
||||
{
|
||||
items[1].events = ZMQ_POLLIN|ZMQ_POLLOUT;
|
||||
}
|
||||
else if (type == ZMQ_PUSH || type == ZMQ_PUB || type == ZMQ_XPUB)
|
||||
{
|
||||
items[1].events = ZMQ_POLLOUT;
|
||||
}
|
||||
else if (type == ZMQ_PULL || type == ZMQ_SUB || type == ZMQ_XSUB)
|
||||
{
|
||||
items[1].events = ZMQ_POLLIN;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << "invalid poller configuration, exiting.";
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQPollerZMQ::Poll(const int timeout)
|
||||
{
|
||||
if (zmq_poll(items, fNumItems, timeout) < 0)
|
||||
{
|
||||
if (errno == ETERM)
|
||||
{
|
||||
LOG(DEBUG) << "polling exited, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << "polling failed, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool FairMQPollerZMQ::CheckInput(const int index)
|
||||
{
|
||||
if (items[index].revents & ZMQ_POLLIN)
|
||||
{
|
||||
@@ -53,7 +140,7 @@ bool FairMQPollerZMQ::CheckInput(int index)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FairMQPollerZMQ::CheckOutput(int index)
|
||||
bool FairMQPollerZMQ::CheckOutput(const int index)
|
||||
{
|
||||
if (items[index].revents & ZMQ_POLLOUT)
|
||||
{
|
||||
@@ -63,6 +150,44 @@ bool FairMQPollerZMQ::CheckOutput(int index)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FairMQPollerZMQ::CheckInput(const string channelKey, const int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (items[fOffsetMap.at(channelKey) + index].revents & ZMQ_POLLIN)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
catch (const std::out_of_range& oor)
|
||||
{
|
||||
LOG(ERROR) << "Invalid channel key: \"" << channelKey << "\"";
|
||||
LOG(ERROR) << "Out of Range error: " << oor.what() << '\n';
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
bool FairMQPollerZMQ::CheckOutput(const string channelKey, const int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (items[fOffsetMap.at(channelKey) + index].revents & ZMQ_POLLOUT)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
catch (const std::out_of_range& oor)
|
||||
{
|
||||
LOG(ERROR) << "Invalid channel key: \"" << channelKey << "\"";
|
||||
LOG(ERROR) << "Out of Range error: " << oor.what() << '\n';
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
FairMQPollerZMQ::~FairMQPollerZMQ()
|
||||
{
|
||||
if (items != NULL)
|
||||
|
@@ -16,25 +16,40 @@
|
||||
#define FAIRMQPOLLERZMQ_H_
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <initializer_list>
|
||||
|
||||
#include "FairMQPoller.h"
|
||||
#include "FairMQChannel.h"
|
||||
#include "FairMQTransportFactoryZMQ.h"
|
||||
|
||||
class FairMQChannel;
|
||||
|
||||
class FairMQPollerZMQ : public FairMQPoller
|
||||
{
|
||||
friend class FairMQChannel;
|
||||
friend class FairMQTransportFactoryZMQ;
|
||||
|
||||
public:
|
||||
FairMQPollerZMQ(const std::vector<FairMQChannel>& channels);
|
||||
FairMQPollerZMQ(std::map< std::string,std::vector<FairMQChannel> >& channelsMap, std::initializer_list<std::string> channelList);
|
||||
|
||||
virtual void Poll(int timeout);
|
||||
virtual bool CheckInput(int index);
|
||||
virtual bool CheckOutput(int index);
|
||||
virtual void Poll(const int timeout);
|
||||
virtual bool CheckInput(const int index);
|
||||
virtual bool CheckOutput(const int index);
|
||||
virtual bool CheckInput(const std::string channelKey, const int index);
|
||||
virtual bool CheckOutput(const std::string channelKey, const int index);
|
||||
|
||||
virtual ~FairMQPollerZMQ();
|
||||
|
||||
private:
|
||||
FairMQPollerZMQ(FairMQSocket& dataSocket, FairMQSocket& cmdSocket);
|
||||
|
||||
zmq_pollitem_t* items;
|
||||
int fNumItems;
|
||||
|
||||
std::unordered_map<std::string,int> fOffsetMap;
|
||||
|
||||
/// Copy Constructor
|
||||
FairMQPollerZMQ(const FairMQPollerZMQ&);
|
||||
FairMQPollerZMQ operator=(const FairMQPollerZMQ&);
|
||||
|
@@ -98,6 +98,8 @@ void FairMQSocketZMQ::Connect(const string& address)
|
||||
if (zmq_connect(fSocket, address.c_str()) != 0)
|
||||
{
|
||||
LOG(ERROR) << "failed connecting socket " << fId << ", reason: " << zmq_strerror(errno);
|
||||
// error here means incorrect configuration. exit if it happens.
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -49,3 +49,13 @@ FairMQPoller* FairMQTransportFactoryZMQ::CreatePoller(const vector<FairMQChannel
|
||||
{
|
||||
return new FairMQPollerZMQ(channels);
|
||||
}
|
||||
|
||||
FairMQPoller* FairMQTransportFactoryZMQ::CreatePoller(std::map< std::string,std::vector<FairMQChannel> >& channelsMap, std::initializer_list<std::string> channelList)
|
||||
{
|
||||
return new FairMQPollerZMQ(channelsMap, channelList);
|
||||
}
|
||||
|
||||
FairMQPoller* FairMQTransportFactoryZMQ::CreatePoller(FairMQSocket& dataSocket, FairMQSocket& cmdSocket)
|
||||
{
|
||||
return new FairMQPollerZMQ(dataSocket, cmdSocket);
|
||||
}
|
||||
|
@@ -31,8 +31,12 @@ 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, const std::string& name, int numIoThreads);
|
||||
|
||||
virtual FairMQPoller* CreatePoller(const std::vector<FairMQChannel>& channels);
|
||||
virtual FairMQPoller* CreatePoller(std::map< std::string,std::vector<FairMQChannel> >& channelsMap, std::initializer_list<std::string> channelList);
|
||||
virtual FairMQPoller* CreatePoller(FairMQSocket& dataSocket, FairMQSocket& cmdSocket);
|
||||
|
||||
virtual ~FairMQTransportFactoryZMQ() {};
|
||||
};
|
||||
|
Reference in New Issue
Block a user