13 #include <framework/pcore/zmq/messages/ZMQDefinitions.h>
14 #include <framework/logging/Logger.h>
25 template <
int AZMQType>
26 void initialize(
const std::string& pubSocketAddress,
const std::string& subSocketAddress,
const std::string& socketName,
bool bind);
29 void initialize(
const std::string& pubSocketAddress,
const std::string& subSocketAddress);
41 template <
class AZMQMessage>
42 void send(AZMQMessage message)
const
44 AZMQMessage::element_type::toSocket(std::move(message),
m_socket);
48 void send(zmq::message_t& message)
const;
51 template <
class AZMQMessage>
54 AZMQMessage::element_type::toSocket(std::move(message),
m_pubSocket);
74 template <
class AMulticastAnswer,
class ASocketAnswer>
75 int poll(
unsigned int timeout, AMulticastAnswer multicastAnswer, ASocketAnswer socketAnswer)
const;
82 template <
class ASocketAnswer>
83 int pollSocket(
unsigned int timeout, ASocketAnswer socketAnswer)
const;
90 template <
class AMulticastAnswer>
91 int pollMulticast(
unsigned int timeout, AMulticastAnswer multicastAnswer)
const;
95 static int pollSocketVector(
const std::vector<zmq::socket_t*>& socketList,
int timeout);
111 template <
class AMulticastAnswer,
class ASocketAnswer>
112 int ZMQClient::poll(
unsigned int timeout, AMulticastAnswer multicastAnswer, ASocketAnswer socketAnswer)
const
119 if (pollResult & 1) {
122 std::vector<zmq::socket_t*> vector = {
m_subSocket.get()};
128 if (pollResult & 2) {
130 std::vector<zmq::socket_t*> vector = {
m_socket.get()};
135 }
while (repeat and pollResult);
140 template <
class ASocketAnswer>
143 B2ASSERT(
"Can only run this on started clients",
m_socket);
144 std::vector<zmq::socket_t*> vector = {
m_socket.get()};
155 }
while (repeat and pollResult);
160 template <
class AMulticastAnswer>
163 B2ASSERT(
"Can only run this on started clients",
m_subSocket);
164 std::vector<zmq::socket_t*> vector = {
m_subSocket.get()};
175 }
while (repeat and pollResult);
A helper class for communicating over ZMQ. Includes a multicast and (if needed) also a data socket.
std::unique_ptr< zmq::socket_t > m_subSocket
ZMQ sub socket.
void initialize(const std::string &pubSocketAddress, const std::string &subSocketAddress, const std::string &socketName, bool bind)
Initialize the multicast and a data socket of the given type.
std::unique_ptr< zmq::socket_t > m_pubSocket
ZMQ Pub socket.
static int pollSocketVector(const std::vector< zmq::socket_t * > &socketList, int timeout)
Internal poll function.
bool isOnline() const
Check if the client was initialized and not terminated.
void publish(AZMQMessage message) const
Publish the message to the multicast.
std::unique_ptr< zmq::context_t > m_context
ZMQ context.
std::vector< zmq::socket_t * > m_pollSocketPtrList
Will use this vector for polling.
void terminate(bool sendGoodbye=true)
Terminate the sockets properly.
std::unique_ptr< zmq::socket_t > m_socket
ZMQ socket.
void reset()
Reset the sockets. ATTENTION: this does not close the sockets! Use only after forks to not clean up t...
void send(AZMQMessage message) const
Send a message over the data socket.
void subscribe(EMessageTypes messageType)
Subscribe to the given multicast message type.
int poll(unsigned int timeout, AMulticastAnswer multicastAnswer, ASocketAnswer socketAnswer) const
Poll both the multicast and the data socket until, either:
EMessageTypes
Type the messages can have.
int pollMulticast(unsigned int timeout, AMulticastAnswer multicastAnswer) const
Poll method to only the multicast socket.
int pollSocket(unsigned int timeout, ASocketAnswer socketAnswer) const
Poll method to only the data socket.
Abstract base class for different kinds of events.