Belle II Software development
ZMQConnection.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <framework/pcore/zmq/utils/ZMQLogger.h>
11#include <framework/pcore/zmq/utils/ZMQParent.h>
12
13#include <zmq.hpp>
14
15#include <memory>
16#include <functional>
17
18namespace Belle2 {
31 class ZMQConnection : public ZMQLogger {
32 public:
34 using ReactorFunction = std::function<void(void)>;
35
45 static bool poll(const std::map<const ZMQConnection*, ReactorFunction>& connectionList, int timeout);
46
48 static bool hasMessage(const ZMQConnection* connection);
49
51 virtual ~ZMQConnection() = default;
52
54 virtual bool isReady() const;
56 virtual std::vector<zmq::socket_t*> getSockets() const = 0;
57 };
58
64 public:
66 explicit ZMQConnectionOverSocket(const std::shared_ptr<ZMQParent>& parent);
67
69 std::vector<zmq::socket_t*> getSockets() const final;
70
72 std::string getEndPoint() const;
73 protected:
75 std::shared_ptr<ZMQParent> m_parent;
77 std::unique_ptr<zmq::socket_t> m_socket;
78 };
80}
Specialized connection over a ZMQ socket.
Definition: ZMQConnection.h:63
std::string getEndPoint() const
Return the connection string for this socket.
std::shared_ptr< ZMQParent > m_parent
The shared ZMQParent instance.
Definition: ZMQConnection.h:75
std::unique_ptr< zmq::socket_t > m_socket
The memory of the socket. Needs to be initialized in a derived class.
Definition: ZMQConnection.h:77
std::vector< zmq::socket_t * > getSockets() const final
The socket used for polling is just the stored socket.
Base class for every connection with virtual functions to be implemented:
Definition: ZMQConnection.h:31
virtual bool isReady() const
Return true of this connection is able to send messages right now. Can be overloaded in derived class...
virtual ~ZMQConnection()=default
Virtual default destructor.
std::function< void(void)> ReactorFunction
Typedef of a function which will be called if a connection has a message.
Definition: ZMQConnection.h:34
static bool hasMessage(const ZMQConnection *connection)
Check if the given connection as an incoming message (right now, no waiting).
virtual std::vector< zmq::socket_t * > getSockets() const =0
Return the list of sockets which should be polled on to check for incoming messages....
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.
Base class for the ZMQ connection to help monitor and log certain values.
Definition: ZMQLogger.h:35
Abstract base class for different kinds of events.