Belle II Software  release-08-01-10
ZMQConfirmedOutput Class Reference

Output part of a confirmed connection. More...

#include <ZMQConfirmedConnection.h>

Inheritance diagram for ZMQConfirmedOutput:
Collaboration diagram for ZMQConfirmedOutput:

Public Types

using ReactorFunction = std::function< void(void)>
 Typedef of a function which will be called if a connection has a message.
 

Public Member Functions

 ZMQConfirmedOutput (const std::string &outputAddress, const std::shared_ptr< ZMQParent > &parent)
 Create a new confirmed output by connecting to the address.
 
void handleEvent (std::unique_ptr< ZMQNoIdMessage > message, bool requireConfirmation=true, int maximalWaitTime=10000)
 Send the message to the output (a message without a ID as there is only a single output). More...
 
void handleIncomingData ()
 Blocks until it can receive the (hopefully confirmation) message from the output. More...
 
std::vector< zmq::socket_t * > getSockets () const final
 The socket used for polling is just the stored socket.
 
std::string getEndPoint () const
 Return the connection string for this socket.
 
virtual bool isReady () const
 Return true of this connection is able to send messages right now. Can be overloaded in derived classes.
 
virtual std::string getMonitoringJSON () const
 Convert the stored monitoring values to a JSON string ready for sending out via a message.
 
template<class AClass >
void log (const std::string &key, const AClass &value)
 Store a value under a certain key. Different types of values can be stored, namely long, double or string. Mixtures are not allowed for a given key.
 
void increment (const std::string &key)
 Increment the value with the given key (only numerical values). If not present, set to 1.
 
void decrement (const std::string &key)
 Decrement the value with the given key (only numerical values). If not present, set to -1.
 
template<size_t MAX_SIZE = 100>
void average (const std::string &key, double value)
 Instead of storeing the double value directly under the given key, store the average of the last MAX_SIZE values.
 
template<size_t AVERAGE_SIZE = 2000>
void timeit (const std::string &key)
 Measure the rate of calls with the same key every AVERAGE_SIZE calls (and also display the last time AVERAGE_SIZE was reached under <key>_last_measurement)
 
void logTime (const std::string &key)
 Store the current time as a string under the given key.
 

Static Public Member Functions

static bool poll (const std::map< const ZMQConnection *, ReactorFunction > &connectionList, int timeout)
 Poll on the given connections and call the attached function if a messages comes in. More...
 
static bool hasMessage (const ZMQConnection *connection)
 Check if the given connection as an incoming message (right now, no waiting).
 

Protected Attributes

std::shared_ptr< ZMQParentm_parent
 The shared ZMQParent instance.
 
std::unique_ptr< zmq::socket_t > m_socket
 The memory of the socket. Needs to be initialized in a derived class.
 

Private Attributes

unsigned int m_waitingForConfirmation = 0
 On how many confirmation messages are we still waiting?
 
long m_timespanWaitingForConfirmation = 0
 Internal monitoring how long we were waiting for confirmation messages.
 
std::map< std::string, std::variant< long, double, std::string > > m_monitoring
 Internal storage of all stored values.
 
std::unordered_map< std::string, std::tuple< std::vector< double >, size_t > > m_averages
 Internal storage of the previous values when calculating averages.
 
std::unordered_map< std::string, std::tuple< unsigned long, std::chrono::system_clock::time_point > > m_timeCounters
 Internal storage how often the timeit function for a given key was called and when it has last reached MAX_SIZE.
 

Detailed Description

Output part of a confirmed connection.

For an introduction to confirmed connections please see the ZMQConfirmedInput.

The confirmed output basically just sends out messages and waits for confirmations. Two additions to that:

  • on startup, it sends a single hello message to the output to register itself. This message also needs to be confirmed.
  • Instead of waiting for a confirmation after sending the message, the blocking happens before sending the next message. This gives the output some time for reacting without slowing down the output part.

If no confirmation is received on time, either a FATAL is issued or (configurable) only a warning.

Internally, a ZMQ_DEALER socket is used in non-bind mode. The order of starting output and input does not play any role.

Definition at line 106 of file ZMQConfirmedConnection.h.

Member Function Documentation

◆ handleEvent()

void handleEvent ( std::unique_ptr< ZMQNoIdMessage message,
bool  requireConfirmation = true,
int  maximalWaitTime = 10000 
)

Send the message to the output (a message without a ID as there is only a single output).

If requireConfirmation is set to true and no confirmation (from the last sent event!) is received before sending the new event, a B2FATAL is issued (in other cases only a warning). Set the maximalWaitTime to -1 to always wait infinitely (not preferred).

Definition at line 228 of file ZMQConfirmedConnection.cc.

◆ handleIncomingData()

void handleIncomingData ( )

Blocks until it can receive the (hopefully confirmation) message from the output.

Can be called even before sending an event to receive the confirmation message ahead of time (e.g. when there is nothing else to do).

Definition at line 263 of file ZMQConfirmedConnection.cc.

◆ poll()

bool poll ( const std::map< const ZMQConnection *, ReactorFunction > &  connectionList,
int  timeout 
)
staticinherited

Poll on the given connections and call the attached function if a messages comes in.

If after timeout milliseconds still no message is received, return anyways. If timeout is 0, do not wait. If timeout is -1, wait infinitely.

Returns true if a message was received on any socket, false otherwise. Attention: in case of an interrupted system call (e.g. because a signal was received) the function might return anyways with a negative result even before the timeout!

Definition at line 27 of file ZMQConnection.cc.


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