mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-16 01:51:45 +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
@@ -13,6 +13,10 @@
|
||||
*/
|
||||
|
||||
#include <nanomsg/nn.h>
|
||||
#include <nanomsg/pipeline.h>
|
||||
#include <nanomsg/pubsub.h>
|
||||
#include <nanomsg/reqrep.h>
|
||||
#include <nanomsg/pair.h>
|
||||
|
||||
#include "FairMQPollerNN.h"
|
||||
#include "FairMQLogger.h"
|
||||
@@ -21,7 +25,8 @@ using namespace std;
|
||||
|
||||
FairMQPollerNN::FairMQPollerNN(const vector<FairMQChannel>& channels)
|
||||
: items()
|
||||
, fNumItems()
|
||||
, fNumItems(0)
|
||||
, fOffsetMap()
|
||||
{
|
||||
fNumItems = channels.size();
|
||||
items = new nn_pollfd[fNumItems];
|
||||
@@ -33,15 +38,95 @@ FairMQPollerNN::FairMQPollerNN(const vector<FairMQChannel>& channels)
|
||||
}
|
||||
}
|
||||
|
||||
FairMQPollerNN::FairMQPollerNN(map< string,vector<FairMQChannel> >& channelsMap, initializer_list<string> channelList)
|
||||
: items()
|
||||
, fNumItems(0)
|
||||
, fOffsetMap()
|
||||
{
|
||||
int offset = 0;
|
||||
|
||||
try
|
||||
{
|
||||
// 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 nn_pollfd[fNumItems];
|
||||
|
||||
int index = 0;
|
||||
for (string channel : channelList)
|
||||
{
|
||||
for (int i = 0; i < channelsMap.at(channel).size(); ++i)
|
||||
{
|
||||
index = fOffsetMap[channel] + i;
|
||||
items[index].fd = channelsMap.at(channel).at(i).fSocket->GetSocket(1);
|
||||
items[index].events = NN_POLLIN;
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
FairMQPollerNN::FairMQPollerNN(FairMQSocket& dataSocket, FairMQSocket& cmdSocket)
|
||||
: items()
|
||||
, fNumItems(2)
|
||||
, fOffsetMap()
|
||||
{
|
||||
items = new nn_pollfd[fNumItems];
|
||||
|
||||
items[0].fd = cmdSocket.GetSocket(1);
|
||||
items[0].events = NN_POLLIN;
|
||||
|
||||
int type = 0;
|
||||
size_t sz = sizeof(type);
|
||||
nn_getsockopt(dataSocket.GetSocket(1), NN_SOL_SOCKET, NN_PROTOCOL, &type, &sz);
|
||||
|
||||
items[1].fd = dataSocket.GetSocket(1);
|
||||
|
||||
if (type == NN_REQ || type == NN_REP || type == NN_PAIR)
|
||||
{
|
||||
items[1].events = NN_POLLIN|NN_POLLOUT;
|
||||
}
|
||||
else if (type == NN_PUSH || type == NN_PUB)
|
||||
{
|
||||
items[1].events = NN_POLLOUT;
|
||||
}
|
||||
else if (type == NN_PULL || type == NN_SUB)
|
||||
{
|
||||
items[1].events = NN_POLLIN;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << "invalid poller configuration, exiting.";
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQPollerNN::Poll(int timeout)
|
||||
{
|
||||
if (nn_poll(items, fNumItems, timeout) < 0)
|
||||
{
|
||||
LOG(ERROR) << "polling failed, reason: " << nn_strerror(errno);
|
||||
if (errno == ETERM)
|
||||
{
|
||||
LOG(DEBUG) << "polling exited, reason: " << nn_strerror(errno);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << "polling failed, reason: " << nn_strerror(errno);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool FairMQPollerNN::CheckInput(int index)
|
||||
bool FairMQPollerNN::CheckInput(const int index)
|
||||
{
|
||||
if (items[index].revents & NN_POLLIN)
|
||||
{
|
||||
@@ -51,7 +136,7 @@ bool FairMQPollerNN::CheckInput(int index)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FairMQPollerNN::CheckOutput(int index)
|
||||
bool FairMQPollerNN::CheckOutput(const int index)
|
||||
{
|
||||
if (items[index].revents & NN_POLLOUT)
|
||||
{
|
||||
@@ -61,6 +146,44 @@ bool FairMQPollerNN::CheckOutput(int index)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FairMQPollerNN::CheckInput(const string channelKey, const int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (items[fOffsetMap.at(channelKey) + index].revents & NN_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 FairMQPollerNN::CheckOutput(const string channelKey, const int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (items[fOffsetMap.at(channelKey) + index].revents & NN_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);
|
||||
}
|
||||
}
|
||||
|
||||
FairMQPollerNN::~FairMQPollerNN()
|
||||
{
|
||||
if (items != NULL)
|
||||
|
@@ -16,25 +16,40 @@
|
||||
#define FAIRMQPOLLERNN_H_
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <initializer_list>
|
||||
|
||||
#include "FairMQPoller.h"
|
||||
#include "FairMQChannel.h"
|
||||
#include "FairMQTransportFactoryNN.h"
|
||||
|
||||
class FairMQChannel;
|
||||
|
||||
class FairMQPollerNN : public FairMQPoller
|
||||
{
|
||||
friend class FairMQChannel;
|
||||
friend class FairMQTransportFactoryNN;
|
||||
|
||||
public:
|
||||
FairMQPollerNN(const std::vector<FairMQChannel>& channels);
|
||||
FairMQPollerNN(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 ~FairMQPollerNN();
|
||||
|
||||
private:
|
||||
FairMQPollerNN(FairMQSocket& dataSocket, FairMQSocket& cmdSocket);
|
||||
|
||||
nn_pollfd* items;
|
||||
int fNumItems;
|
||||
|
||||
std::unordered_map<std::string,int> fOffsetMap;
|
||||
|
||||
/// Copy Constructor
|
||||
FairMQPollerNN(const FairMQPollerNN&);
|
||||
FairMQPollerNN operator=(const FairMQPollerNN&);
|
||||
|
@@ -22,7 +22,7 @@ using namespace std;
|
||||
|
||||
FairMQSocketNN::FairMQSocketNN(const string& type, const std::string& name, int numIoThreads)
|
||||
: FairMQSocket(0, 0, NN_DONTWAIT)
|
||||
, fSocket()
|
||||
, fSocket(-1)
|
||||
, fId()
|
||||
, fBytesTx(0)
|
||||
, fBytesRx(0)
|
||||
|
@@ -45,3 +45,13 @@ FairMQPoller* FairMQTransportFactoryNN::CreatePoller(const vector<FairMQChannel>
|
||||
{
|
||||
return new FairMQPollerNN(channels);
|
||||
}
|
||||
|
||||
FairMQPoller* FairMQTransportFactoryNN::CreatePoller(std::map< std::string,std::vector<FairMQChannel> >& channelsMap, std::initializer_list<std::string> channelList)
|
||||
{
|
||||
return new FairMQPollerNN(channelsMap, channelList);
|
||||
}
|
||||
|
||||
FairMQPoller* FairMQTransportFactoryNN::CreatePoller(FairMQSocket& dataSocket, FairMQSocket& cmdSocket)
|
||||
{
|
||||
return new FairMQPollerNN(dataSocket, cmdSocket);
|
||||
}
|
||||
|
@@ -30,8 +30,12 @@ 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, 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 ~FairMQTransportFactoryNN() {};
|
||||
};
|
||||
|
Reference in New Issue
Block a user