Belle II Software  release-05-02-19
ZMQModuleMessage.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun, Anselm Baur *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <framework/pcore/zmq/messages/ZMQMessageHelper.h>
13 
14 #include <zmq.hpp>
15 #include <memory>
16 #include <array>
17 
18 namespace Belle2 {
24  template <unsigned int AMessageFrameNumber>
26  class ZMQModuleMessage {
27  friend class ZMQMessageFactory;
28 
29  public:
31  static constexpr unsigned int c_messageParts = AMessageFrameNumber;
32 
34  using MessageParts = std::array<zmq::message_t, ZMQModuleMessage::c_messageParts>;
35 
37  static void toSocket(std::unique_ptr<ZMQModuleMessage> message, const std::unique_ptr<zmq::socket_t>& socket)
38  {
39  for (unsigned int i = 0; i < c_messageParts - 1; i++) {
40  socket->send(message->m_messageParts[i], ZMQ_SNDMORE);
41  }
42  socket->send(message->m_messageParts[c_messageParts - 1]);
43  }
44 
46  ZMQModuleMessage(const ZMQModuleMessage&) = delete;
48  void operator=(const ZMQModuleMessage&) = delete;
49 
52  {
53  return m_messageParts;
54  };
55 
57  const MessageParts& getMessageParts() const
58  {
60  };
61 
63  template <unsigned int index>
64  const zmq::message_t& getMessagePart() const
65  {
66  return m_messageParts[index];
67  }
68 
70  template <unsigned int index>
71  const char* getMessagePartAsCharArray() const
72  {
73  const auto& messagePart = getMessagePart<index>();
74  return static_cast<const char*>(messagePart.data());
75  }
76 
78  template <unsigned int index>
79  std::string getMessagePartAsString() const
80  {
81  const auto& messagePart = getMessagePart<index>();
82  return std::string(static_cast<const char*>(messagePart.data()), messagePart.size());
83  }
84 
86  template <unsigned int index>
87  zmq::message_t& getMessagePart()
88  {
89  return m_messageParts[index];
90  }
91 
93  template <unsigned int index>
95  {
96  auto& messagePart = getMessagePart<index>();
97  return static_cast<char*>(messagePart.data());
98  }
99 
100  protected:
102  ZMQModuleMessage() = default;
103 
105  template <class ...T>
106  explicit ZMQModuleMessage(T&& ... arguments) :
107  m_messageParts( {ZMQMessageHelper::createZMQMessage(std::forward<T>(arguments)) ... })
108  {
109  }
110 
111  private:
114  };
116 }
Belle2::ZMQModuleMessage::c_messageParts
static constexpr unsigned int c_messageParts
The number of message parts this message carries.
Definition: ZMQModuleMessage.h:39
Belle2::ZMQModuleMessage::getMessagePartAsCharArray
const char * getMessagePartAsCharArray() const
Get the message part with the given index as char* (const version)
Definition: ZMQModuleMessage.h:79
Belle2::ZMQModuleMessage::getMessageParts
MessageParts & getMessageParts()
Get a reference to the message parts.
Definition: ZMQModuleMessage.h:59
Belle2::ZMQMessageHelper::createZMQMessage
static zmq::message_t createZMQMessage(zmq::message_t message)
Just pass a zmq message.
Definition: ZMQMessageHelper.h:39
Belle2::ZMQModuleMessage::ZMQModuleMessage
ZMQModuleMessage()=default
Do not allow to create a new message from scratch publicly.
Belle2::ZMQModuleMessage::getMessagePartAsString
std::string getMessagePartAsString() const
Get the message part with the given index as string (const version)
Definition: ZMQModuleMessage.h:87
Belle2::ZMQModuleMessage::toSocket
static void toSocket(std::unique_ptr< ZMQModuleMessage > message, const std::unique_ptr< zmq::socket_t > &socket)
Send the message to the given socket. As the message is nullified, you have to move it in here.
Definition: ZMQModuleMessage.h:45
Belle2::ZMQModuleMessage
A general message with as many parts as given as template argument.
Definition: ZMQModuleMessage.h:34
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ZMQModuleMessage::getMessagePart
const zmq::message_t & getMessagePart() const
Get the message part with the given index (const version)
Definition: ZMQModuleMessage.h:72
Belle2::ZMQModuleMessage::MessageParts
std::array< zmq::message_t, ZMQModuleMessage::c_messageParts > MessageParts
The base class of the message parts.
Definition: ZMQModuleMessage.h:42
Belle2::ZMQModuleMessage::operator=
void operator=(const ZMQModuleMessage &)=delete
Do not allow to copy a message.
Belle2::ZMQModuleMessage::m_messageParts
MessageParts m_messageParts
The content of this message as an array of zmq messages. Will be set during constructor or when comin...
Definition: ZMQModuleMessage.h:121