8 #ifndef FAIR_MQ_SHMEM_POLLER_H_
9 #define FAIR_MQ_SHMEM_POLLER_H_
12 #include <fairmq/tools/Strings.h>
13 #include <FairMQChannel.h>
14 #include <FairMQLogger.h>
15 #include <FairMQPoller.h>
19 #include <unordered_map>
30 Poller(
const std::vector<FairMQChannel>& channels)
35 fNumItems = channels.size();
36 fItems =
new zmq_pollitem_t[fNumItems];
38 for (
int i = 0; i < fNumItems; ++i) {
39 fItems[i].socket =
static_cast<const Socket*
>(&(channels.at(i).GetSocket()))->GetSocket();
41 fItems[i].revents = 0;
44 size_t size =
sizeof(type);
45 zmq_getsockopt(
static_cast<const Socket*
>(&(channels.at(i).GetSocket()))->GetSocket(), ZMQ_TYPE, &type, &size);
47 SetItemEvents(fItems[i], type);
51 Poller(
const std::vector<FairMQChannel*>& channels)
56 fNumItems = channels.size();
57 fItems =
new zmq_pollitem_t[fNumItems];
59 for (
int i = 0; i < fNumItems; ++i) {
60 fItems[i].socket =
static_cast<const Socket*
>(&(channels.at(i)->GetSocket()))->GetSocket();
62 fItems[i].revents = 0;
65 size_t size =
sizeof(type);
66 zmq_getsockopt(
static_cast<const Socket*
>(&(channels.at(i)->GetSocket()))->GetSocket(), ZMQ_TYPE, &type, &size);
68 SetItemEvents(fItems[i], type);
72 Poller(
const std::unordered_map<std::string, std::vector<FairMQChannel>>& channelsMap,
const std::vector<std::string>& channelList)
80 for (std::string channel : channelList) {
81 fOffsetMap[channel] = offset;
82 offset += channelsMap.at(channel).size();
83 fNumItems += channelsMap.at(channel).size();
86 fItems =
new zmq_pollitem_t[fNumItems];
89 for (std::string channel : channelList) {
90 for (
unsigned int i = 0; i < channelsMap.at(channel).size(); ++i) {
91 index = fOffsetMap[channel] + i;
93 fItems[index].socket =
static_cast<const Socket*
>(&(channelsMap.at(channel).at(i).GetSocket()))->GetSocket();
95 fItems[index].revents = 0;
98 size_t size =
sizeof(type);
99 zmq_getsockopt(
static_cast<const Socket*
>(&(channelsMap.at(channel).at(i).GetSocket()))->GetSocket(), ZMQ_TYPE, &type, &size);
101 SetItemEvents(fItems[index], type);
104 }
catch (
const std::out_of_range& oor) {
105 LOG(error) <<
"At least one of the provided channel keys for poller initialization is invalid." <<
" Out of range error: " << oor.what();
106 throw fair::mq::PollerError(fair::mq::tools::ToString(
"At least one of the provided channel keys for poller initialization is invalid. ",
"Out of range error: ", oor.what()));
113 void SetItemEvents(zmq_pollitem_t& item,
const int type)
115 if (type == ZMQ_REQ || type == ZMQ_REP || type == ZMQ_PAIR || type == ZMQ_DEALER || type == ZMQ_ROUTER) {
116 item.events = ZMQ_POLLIN | ZMQ_POLLOUT;
117 }
else if (type == ZMQ_PUSH || type == ZMQ_PUB || type == ZMQ_XPUB) {
118 item.events = ZMQ_POLLOUT;
119 }
else if (type == ZMQ_PULL || type == ZMQ_SUB || type == ZMQ_XSUB) {
120 item.events = ZMQ_POLLIN;
122 LOG(error) <<
"invalid poller configuration, exiting.";
127 void Poll(
const int timeout)
override
130 if (zmq_poll(fItems, fNumItems, timeout) < 0) {
131 if (errno == ETERM) {
132 LOG(debug) <<
"polling exited, reason: " << zmq_strerror(errno);
134 }
else if (errno == EINTR) {
135 LOG(debug) <<
"polling interrupted by system call";
138 LOG(error) <<
"polling failed, reason: " << zmq_strerror(errno);
139 throw fair::mq::PollerError(fair::mq::tools::ToString(
"Polling failed, reason: ", zmq_strerror(errno)));
146 bool CheckInput(
const int index)
override
148 if (fItems[index].revents & ZMQ_POLLIN) {
155 bool CheckOutput(
const int index)
override
157 if (fItems[index].revents & ZMQ_POLLOUT) {
164 bool CheckInput(
const std::string& channelKey,
const int index)
override
167 if (fItems[fOffsetMap.at(channelKey) + index].revents & ZMQ_POLLIN) {
172 }
catch (
const std::out_of_range& oor) {
173 LOG(error) <<
"invalid channel key: '" << channelKey <<
"'";
174 LOG(error) <<
"out of range error: " << oor.what();
175 throw fair::mq::PollerError(fair::mq::tools::ToString(
"Invalid channel key '", channelKey,
"'. Out of range error: ", oor.what()));
179 bool CheckOutput(
const std::string& channelKey,
const int index)
override
182 if (fItems[fOffsetMap.at(channelKey) + index].revents & ZMQ_POLLOUT) {
187 }
catch (
const std::out_of_range& oor) {
188 LOG(error) <<
"invalid channel key: '" << channelKey <<
"'";
189 LOG(error) <<
"out of range error: " << oor.what();
190 throw fair::mq::PollerError(fair::mq::tools::ToString(
"Invalid channel key '", channelKey,
"'. Out of range error: ", oor.what()));
194 ~
Poller()
override {
delete[] fItems; }
197 zmq_pollitem_t* fItems;
200 std::unordered_map<std::string, int> fOffsetMap;