Belle II Software development
ZMQParent Class Reference

A helper class for creating ZMQ sockets keeping track of the ZMQ context and terminating it if needed. More...

#include <ZMQParent.h>

Public Member Functions

 ~ZMQParent ()
 Destroy the parent by terminating the ZMQ context.
 
void terminate ()
 Terminate the parent manually (before calling its destructor). You probably do not need to do this.
 
template<int AZMQType>
std::unique_ptr< zmq::socket_t > createSocket (const std::string &socketAddress, bool bind)
 Create a socket of the given type with the given address and bind or not bind it.
 
template<int AZMQType>
std::unique_ptr< zmq::socket_t > createSocket (const std::string &socketAddress)
 Create a socket of the given type while deciding on the bind behaviour via the address.
 
void reset ()
 Expert function: Reset the parent without context closing. ATTENTION: which will not clean up properly! Do only use after forks.
 

Static Public Member Functions

template<class AZMQMessage >
static void send (std::unique_ptr< zmq::socket_t > &socket, AZMQMessage message)
 Send a given message over the given created socket. You need to move in the message for zero-copy.
 
static unsigned int poll (const std::vector< zmq::socket_t * > &socketList, int timeout)
 Poll function.
 
static std::string createIdentity (unsigned int pid=0)
 Create a unique ZMQ identity in the form <hostname>_<pid> (if pid is 0, use the current processes PID).
 

Private Member Functions

void initialize ()
 Initialize the parent by creating the context.
 

Private Attributes

std::unique_ptr< zmq::context_t > m_context
 ZMQ context.
 

Detailed Description

A helper class for creating ZMQ sockets keeping track of the ZMQ context and terminating it if needed.

Basically just a convenience wrapper around a zmq::context_t.

In your application, you should have only a single instance of a ZMQParent, for example a std::shared_ptr in your main class. Use it to create sockets and let the automatic destruction at the end call the cleanup functions properly. After the parent is closed, all its sockets are also invalid. The parent gets initialized on first socket creation.

Only in very rare cases you need to not terminate this parent, e.g. when you create a parent and then fork (because you do not want to close the parent twice). This however is an expert function.

Definition at line 39 of file ZMQParent.h.

Constructor & Destructor Documentation

◆ ~ZMQParent()

~ZMQParent ( )

Destroy the parent by terminating the ZMQ context.

Definition at line 14 of file ZMQParent.cc.

15{
16 terminate();
17}
void terminate()
Terminate the parent manually (before calling its destructor). You probably do not need to do this.
Definition: ZMQParent.cc:19

Member Function Documentation

◆ createIdentity()

std::string createIdentity ( unsigned int  pid = 0)
static

Create a unique ZMQ identity in the form <hostname>_<pid> (if pid is 0, use the current processes PID).

Definition at line 32 of file ZMQParent.cc.

33{
34 char hostname[HOST_NAME_MAX];
35 gethostname(hostname, HOST_NAME_MAX);
36
37 if (pid == 0) {
38 pid = getpid();
39 }
40
41 return std::string(hostname) + "_" + std::to_string(pid);
42}

◆ initialize()

void initialize ( )
private

Initialize the parent by creating the context.

Definition at line 44 of file ZMQParent.cc.

45{
46 if (m_context) {
47 return;
48 }
49 m_context = std::make_unique<zmq::context_t>(1);
50}
std::unique_ptr< zmq::context_t > m_context
ZMQ context.
Definition: ZMQParent.h:98

◆ poll()

unsigned int poll ( const std::vector< zmq::socket_t * > &  socketList,
int  timeout 
)
static

Poll function.

Poll on the given socket pointers and return a bitmask indicating which sockets received a message. If the poll is interrupted (e.g. by a system interrupt), return 0.

If the timeout is positive, wait maximal timeout milliseconds before returning (even if there is no message in the sockets). If the timeout is 0, only check once and return immediately. If the timeout is -1, wait infinitely.

Definition at line 56 of file ZMQParent.cc.

57{
58 B2ASSERT("Only allow to poll on maximal 8 sockets at the same time!", socketList.size() <= 8);
59 std::bitset<8> return_bitmask;
60 std::vector<zmq::pollitem_t> items(socketList.size());
61
62 for (unsigned int i = 0; i < socketList.size(); i++) {
63 items[i].socket = static_cast<void*>(*socketList[i]);
64 items[i].events = ZMQ_POLLIN;
65 items[i].revents = 0;
66 }
67
68 try {
69 zmq::poll(items.data(), socketList.size(), timeout);
70
71 for (unsigned int i = 0; i < socketList.size(); i++) {
72 return_bitmask[i] = static_cast<bool>(items[i].revents & ZMQ_POLLIN);
73 }
74 return return_bitmask.to_ulong();
75 } catch (zmq::error_t& error) {
76 if (error.num() == EINTR) {
77 return 0;
78 } else {
79 // cannot handle, rethrow exception
80 throw;
81 }
82 }
83}

◆ reset()

void reset ( )

Expert function: Reset the parent without context closing. ATTENTION: which will not clean up properly! Do only use after forks.

Definition at line 27 of file ZMQParent.cc.

28{
29 m_context.release();
30}

◆ terminate()

void terminate ( )

Terminate the parent manually (before calling its destructor). You probably do not need to do this.

Definition at line 19 of file ZMQParent.cc.

20{
21 if (m_context) {
22 m_context->close();
23 m_context.reset();
24 }
25}

Member Data Documentation

◆ m_context

std::unique_ptr<zmq::context_t> m_context
private

ZMQ context.

Definition at line 98 of file ZMQParent.h.


The documentation for this class was generated from the following files: