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& socketAddress,
30 void initialize(
const std::string& pubSocketAddress,
const std::string& subSocketAddress);
42 template <
class AZMQMessage>
43 void send(AZMQMessage message)
const
45 AZMQMessage::element_type::toSocket(std::move(message),
m_socket);
49 void send(zmq::message_t& message)
const;
52 template <
class AZMQMessage>
55 AZMQMessage::element_type::toSocket(std::move(message),
m_pubSocket);
75 template <
class AMulticastAnswer,
class ASocketAnswer>
76 int poll(
unsigned int timeout, AMulticastAnswer multicastAnswer, ASocketAnswer socketAnswer)
const;
83 template <
class ASocketAnswer>
84 int pollSocket(
unsigned int timeout, ASocketAnswer socketAnswer)
const;
91 template <
class AMulticastAnswer>
92 int pollMulticast(
unsigned int timeout, AMulticastAnswer multicastAnswer)
const;
96 static int pollSocketVector(
const std::vector<zmq::socket_t*>& socketList,
int timeout);
112 template <
class AMulticastAnswer,
class ASocketAnswer>
113 int ZMQClient::poll(
unsigned int timeout, AMulticastAnswer multicastAnswer, ASocketAnswer socketAnswer)
const
120 if (pollResult & 1) {
123 std::vector<zmq::socket_t*> vector = {
m_subSocket.get()};
129 if (pollResult & 2) {
131 std::vector<zmq::socket_t*> vector = {
m_socket.get()};
136 }
while (repeat and pollResult);
141 template <
class ASocketAnswer>
144 B2ASSERT(
"Can only run this on started clients",
m_socket);
145 std::vector<zmq::socket_t*> vector = {
m_socket.get()};
156 }
while (repeat and pollResult);
161 template <
class AMulticastAnswer>
164 B2ASSERT(
"Can only run this on started clients",
m_subSocket);
165 std::vector<zmq::socket_t*> vector = {
m_subSocket.get()};
176 }
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.
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.
void initialize(const std::string &pubSocketAddress, const std::string &subSocketAddress, const std::string &socketAddress, bool bind)
Initialize the multicast and a data socket of the given type.
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.