Belle II Software  release-05-01-25
ZMQConnection.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <framework/pcore/zmq/utils/ZMQLogger.h>
13 #include <framework/pcore/zmq/utils/ZMQParent.h>
14 
15 #include <zmq.hpp>
16 
17 #include <memory>
18 
19 namespace Belle2 {
32  class ZMQConnection : public ZMQLogger {
33  public:
35  using ReactorFunction = std::function<void(void)>;
36 
46  static bool poll(const std::map<const ZMQConnection*, ReactorFunction>& connectionList, int timeout);
47 
49  static bool hasMessage(const ZMQConnection* connection);
50 
52  virtual ~ZMQConnection() = default;
53 
55  virtual bool isReady() const;
57  virtual std::vector<zmq::socket_t*> getSockets() const = 0;
58  };
59 
64  class ZMQConnectionOverSocket : public ZMQConnection {
65  public:
67  explicit ZMQConnectionOverSocket(const std::shared_ptr<ZMQParent>& parent);
68 
70  std::vector<zmq::socket_t*> getSockets() const final;
71 
72  protected:
74  std::shared_ptr<ZMQParent> m_parent;
76  std::unique_ptr<zmq::socket_t> m_socket;
77  };
79 }
Belle2::ZMQConnectionOverSocket::m_parent
std::shared_ptr< ZMQParent > m_parent
The shared ZMQParent instance.
Definition: ZMQConnection.h:82
Belle2::ZMQConnection::ReactorFunction
std::function< void(void)> ReactorFunction
Typedef of a function which will be called if a connection has a message.
Definition: ZMQConnection.h:43
Belle2::ZMQConnection::~ZMQConnection
virtual ~ZMQConnection()=default
Virtual default destructor.
Belle2::ZMQConnection::isReady
virtual bool isReady() const
Return true of this connection is able to send messages right now. Can be overloaded in derived class...
Definition: ZMQConnection.cc:17
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ZMQConnection::getSockets
virtual std::vector< zmq::socket_t * > getSockets() const =0
Return the list of sockets which should be polled on to check for incoming messages....
Belle2::ZMQConnection::poll
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.
Definition: ZMQConnection.cc:29
Belle2::ZMQParent
A helper class for creating ZMQ sockets keeping track of the ZMQ context and terminating it if needed...
Definition: ZMQParent.h:49
Belle2::ZMQConnectionOverSocket::ZMQConnectionOverSocket
ZMQConnectionOverSocket(const std::shared_ptr< ZMQParent > &parent)
Create a new instance passing the shared ZMQParent.
Definition: ZMQConnection.cc:80
Belle2::ZMQConnection::hasMessage
static bool hasMessage(const ZMQConnection *connection)
Check if the given connection as an incoming message (right now, no waiting).
Definition: ZMQConnection.cc:22
Belle2::ZMQConnectionOverSocket::m_socket
std::unique_ptr< zmq::socket_t > m_socket
The memory of the socket. Needs to be initialized in a derived class.
Definition: ZMQConnection.h:84
Belle2::ZMQConnectionOverSocket::getSockets
std::vector< zmq::socket_t * > getSockets() const final
The socket used for polling is just the stored socket.
Definition: ZMQConnection.cc:85