Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ZeroMQSvc.cpp
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * RA, Roel Aaij, NIKHEF
5 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl
6 *
7 * Copyright (c) 2021, CERN
8 *
9 * Redistribution and use in source and binary forms,
10 * with or without modification, are permitted according to the terms
11 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
12 */
13
15
16#include <functional> // std::ref
17
18/** \class ZeroMQSvc
19 * \brief Wrapper class for basic ZeroMQ context and socket management
20 *
21 * This singleton class wraps a couple of basic ZeroMQ tasks:
22 *
23 * 1. Creating, storing and eventually closing a ZeroMQ context.
24 * 2. Creating new sockets in the context.
25 * 3. Sending, receiving, encoding and decoding messages over sockets.
26 *
27 * For convenience, it offers a number of template overloads that automatically
28 * encode all kinds of data types in ZeroMQ message objects.
29 */
30
31/**
32 * \brief Get singleton object of this class
33 */
35{
36 static std::unique_ptr<ZeroMQSvc> svc;
37 if (!svc) {
38 svc = std::make_unique<ZeroMQSvc>();
39 }
40 return *svc;
41}
42
44{
45 return m_enc;
46}
47
48/**
49 * \brief Set encoding mode
50 *
51 * \param[in] e Encoding mode; either Text or Binary.
52 */
54{
55 m_enc = e;
56}
57
58/**
59 * \brief Get context
60 *
61 * Creates a context if it has not yet been created and returns a reference to it.
62 */
63zmq::context_t &ZeroMQSvc::context() const
64{
65 if (!m_context) {
66 try {
67 m_context = new zmq::context_t;
68 } catch (zmq::error_t &e) {
69 std::cerr << "ERROR: Creating ZeroMQ context failed. This only happens when PGM initialization failed or when "
70 "a nullptr was returned from zmq_ctx_new because the created context was invalid. Contact ZMQ "
71 "experts when this happens, because it shouldn't.\n";
72 throw;
73 }
74 }
75 return *m_context;
76}
77
78/**
79 * \brief Create and return a new socket
80 *
81 * \param[in] type Type of the socket. See http://api.zeromq.org/master:zmq-socket for possible values.
82 * \return The socket object.
83 */
84zmq::socket_t ZeroMQSvc::socket(zmq::socket_type type) const
85{
86 try {
87 // the actual work this function should do, all the rest is error handling:
88 return zmq::socket_t{context(), type};
89 } catch (zmq::error_t &e) {
90 // all zmq errors not recoverable from here, only at call site
91 std::cerr << "ERROR in ZeroMQSvc::socket: " << e.what() << " (errno: " << e.num() << ")\n";
92 throw;
93 }
94}
95
96/**
97 * \brief Create and return a new socket by pointer
98 *
99 * \param[in] type Type of the socket. See http://api.zeromq.org/master:zmq-socket for possible values.
100 * \return A raw pointer to the socket object. Note: the caller must take ownership!
101 */
102zmq::socket_t *ZeroMQSvc::socket_ptr(zmq::socket_type type) const
103{
104 try {
105 // the actual work this function should do, all the rest is error handling:
106 return new zmq::socket_t(context(), type);
107 } catch (zmq::error_t &e) {
108 // all zmq errors not recoverable from here, only at call site
109 std::cerr << "ERROR in ZeroMQSvc::socket_ptr: " << e.what() << " (errno: " << e.num() << ")\n";
110 throw;
111 }
112}
113
115{
116 if (m_context) {
117 delete m_context;
118 m_context = nullptr;
119 }
120}
121
122/**
123 * \fn zmq::message_t ZeroMQSvc::encode(const char *item) const
124 * \brief Encode string as a ZeroMQ message object
125 *
126 * \param[in] item String.
127 */
128zmq::message_t ZeroMQSvc::encode(const char *item) const
129{
130 std::function<size_t(const char &t)> fun = ZMQ::stringLength;
131 return encode(*item, fun);
132}
133
134/**
135 * \overload zmq::message_t ZeroMQSvc::encode(const std::string &item) const
136 */
137zmq::message_t ZeroMQSvc::encode(const std::string &item) const
138{
139 return encode(item.c_str());
140}
141
142/**
143 * \fn bool ZeroMQSvc::send(zmq::socket_t &socket, const char *item, zmq::send_flags flags) const
144 * \brief Send message over a socket
145 *
146 * \param[in] socket Socket.
147 * \param[in] item Message to send over.
148 * \param[in] flags Flags to send. See http://api.zeromq.org/master:zmq-send for possible flags and the cppzmq API for
149 * the type-safe equivalents in the zmq::send_flags enum class.
150 * \return An optional of type zmq::send_result_t that contains the number of bytes sent if successful, and is empty if
151 * EAGAIN was received, which probably means you should try again.
152 */
153zmq::send_result_t ZeroMQSvc::send(zmq::socket_t &socket, const char *item, zmq::send_flags flags) const
154{
155 return retry_send(socket, 2, encode(item), flags);
156}
157
158/**
159 * \overload zmq::send_result_t ZeroMQSvc::send(zmq::socket_t &socket, zmq::message_t &msg, zmq::send_flags flags) const
160 */
161zmq::send_result_t ZeroMQSvc::send(zmq::socket_t &socket, zmq::message_t &msg, zmq::send_flags flags) const
162{
163 return retry_send(socket, 2, std::ref(msg), flags);
164}
165
166/**
167 * \overload zmq::send_result_t ZeroMQSvc::send(zmq::socket_t &socket, zmq::message_t &&msg, zmq::send_flags flags)
168 * const
169 */
170zmq::send_result_t ZeroMQSvc::send(zmq::socket_t &socket, zmq::message_t &&msg, zmq::send_flags flags) const
171{
172 return retry_send(socket, 2, std::move(msg), flags);
173}
#define e(i)
Definition RSha256.hxx:103
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
ZeroMQSvc & zmqSvc()
Get singleton object of this class.
Definition ZeroMQSvc.cpp:34
auto retry_send(zmq::socket_t &socket, int max_tries, args_t... args) -> decltype(socket.send(args...))
Definition ZeroMQSvc.h:79
Wrapper class for basic ZeroMQ context and socket management.
Definition ZeroMQSvc.h:116
Encoding m_enc
Definition ZeroMQSvc.h:212
zmq::context_t * m_context
Definition ZeroMQSvc.h:213
zmq::message_t encode(const T &item, std::function< size_t(const T &t)> sizeFun=ZMQ::defaultSizeOf< T >) const
encode message to ZMQ
Definition ZeroMQSvc.h:186
zmq::send_result_t send(zmq::socket_t &socket, const T &item, zmq::send_flags flags=zmq::send_flags::none) const
Send message with ZMQ.
Definition ZeroMQSvc.h:199
zmq::socket_t * socket_ptr(zmq::socket_type type) const
Create and return a new socket by pointer.
Encoding encoding() const
Definition ZeroMQSvc.cpp:43
zmq::context_t & context() const
Get context.
Definition ZeroMQSvc.cpp:63
zmq::socket_t socket(zmq::socket_type type) const
Create and return a new socket.
Definition ZeroMQSvc.cpp:84
void close_context() const
void setEncoding(const Encoding &e)
Set encoding mode.
Definition ZeroMQSvc.cpp:53
std::size_t stringLength(const char &cs)
Definition functions.cpp:19