Belle II Software  release-05-02-19
ZMQMessageFactory.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/logging/Logger.h>
13 #include <framework/pcore/zmq/messages/ZMQNoIdMessage.h>
14 #include <framework/pcore/zmq/messages/ZMQIdMessage.h>
15 #include <framework/pcore/zmq/messages/ZMQDefinitions.h>
16 #include <memory>
17 #include <string>
18 #include <zmq.hpp>
19 
20 namespace Belle2 {
25  class ZMQMessageFactory {
27  public:
29  static auto createMessage(const std::string& msgIdentity,
30  const EMessageTypes msgType,
31  const std::unique_ptr<EvtMessage>& eventMessage)
32  {
33  return createMessage<ZMQIdMessage>(msgIdentity, msgType, eventMessage);
34  }
35 
37  static auto createMessage(const std::string& msgIdentity,
38  const EMessageTypes msgType,
39  const std::string& msgData = "")
40  {
41  return createMessage<ZMQIdMessage>(msgIdentity, msgType, msgData);
42  }
43 
45  static auto createMessage(const std::string& msgIdentity,
46  const EMessageTypes msgType,
47  zmq::message_t msgData)
48  {
49  return createMessage<ZMQIdMessage>(msgIdentity, msgType, std::move(msgData));
50  }
51 
53  static auto createMessage(const std::string& msgIdentity,
54  std::unique_ptr<ZMQNoIdMessage> shortMessage)
55  {
56  return createMessage<ZMQIdMessage>(msgIdentity, std::move(shortMessage->getMessagePart<0>()),
57  std::move(shortMessage->getMessagePart<1>()));
58  }
59 
61  static auto createMessage(const EMessageTypes msgType,
62  const std::string& msgData = "")
63  {
64  return createMessage<ZMQNoIdMessage>(msgType, msgData);
65  }
66 
68  static auto createMessage(const EMessageTypes msgType,
69  int msgData)
70  {
71  return createMessage<ZMQNoIdMessage>(msgType, msgData);
72  }
73 
75  static auto createMessage(const EMessageTypes msgType,
76  const StoreObjPtr<EventMetaData>& evtMetaData)
77  {
78  return createMessage<ZMQNoIdMessage>(msgType, evtMetaData);
79  }
80 
82  static auto createMessage(const EMessageTypes msgType,
83  zmq::message_t msgData)
84  {
85  return createMessage<ZMQNoIdMessage>(msgType, std::move(msgData));
86  }
87 
89  static auto createMessage(const EMessageTypes msgType,
90  zmq::message_t msgData,
91  zmq::message_t additionalData)
92  {
93  return createMessage<ZMQNoIdMessage>(msgType, std::move(msgData), std::move(additionalData));
94  }
95 
97  static auto createMessage(zmq::message_t msgType,
98  zmq::message_t msgData)
99  {
100  return createMessage<ZMQNoIdMessage>(std::move(msgType), std::move(msgData));
101  }
102 
104  static auto createMessage(const EMessageTypes msgType,
105  const std::unique_ptr<EvtMessage>& eventMessage)
106  {
107  return createMessage<ZMQNoIdMessage>(msgType, eventMessage);
108  }
109 
111  static auto createMessage(const EMessageTypes msgType,
112  const std::unique_ptr<EvtMessage>& eventMessage,
113  zmq::message_t additionalData)
114  {
115  return createMessage<ZMQNoIdMessage>(msgType, eventMessage, std::move(additionalData));
116  }
117 
119  static auto stripIdentity(std::unique_ptr<ZMQIdMessage> message)
120  {
121  return createMessage<ZMQNoIdMessage>(std::move(message->getMessagePart<1>()),
122  std::move(message->getMessagePart<2>()),
123  std::move(message->getMessagePart<3>()));
124  }
125 
127  template <class AMessage>
128  static std::unique_ptr<AMessage> fromSocket(const std::unique_ptr<zmq::socket_t>& socket)
129  {
130  auto newMessage = std::unique_ptr<AMessage>(new AMessage());
131  auto& messageParts = newMessage->getMessageParts();
132  for (unsigned int i = 0; i < AMessage::c_messageParts; i++) {
133  B2ASSERT("The next part does not belong to the same message",
134  socket->getsockopt<int>(ZMQ_RCVMORE) == 1 or i == 0);
135  socket->recv(&messageParts[i]);
136  }
137  B2ASSERT("There should not be more than the retrieved parts", socket->getsockopt<int>(ZMQ_RCVMORE) == 0);
138  return newMessage;
139  }
140 
141  private:
143  template <class T, class... Args>
144  static std::unique_ptr<T> createMessage(Args&& ... args)
145  {
146  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
147  }
148  };
150 }
Belle2::ZMQMessageFactory::createMessage
static auto createMessage(const EMessageTypes msgType, zmq::message_t msgData, zmq::message_t additionalData)
Create a No-ID Message out of the type, another zmq message and an additional message.
Definition: ZMQMessageFactory.h:97
Belle2::EMessageTypes
EMessageTypes
Type the messages can have.
Definition: ZMQDefinitions.h:26
Belle2::ZMQMessageFactory::createMessage
static auto createMessage(const std::string &msgIdentity, const EMessageTypes msgType, const std::string &msgData="")
Create an ID Message out of an identity, the type and a string.
Definition: ZMQMessageFactory.h:45
Belle2::ZMQMessageFactory::createMessage
static auto createMessage(const std::string &msgIdentity, std::unique_ptr< ZMQNoIdMessage > shortMessage)
Create an ID Message out of an identity and an already received message.
Definition: ZMQMessageFactory.h:61
Belle2::ZMQMessageFactory::stripIdentity
static auto stripIdentity(std::unique_ptr< ZMQIdMessage > message)
Create a No-ID Message out of an ID message.
Definition: ZMQMessageFactory.h:127
Belle2::ZMQMessageFactory::createMessage
static auto createMessage(const std::string &msgIdentity, const EMessageTypes msgType, zmq::message_t msgData)
Create an ID Message out of an identity, the type and another zmq message.
Definition: ZMQMessageFactory.h:53
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::ZMQMessageFactory::createMessage
static auto createMessage(const std::string &msgIdentity, const EMessageTypes msgType, const std::unique_ptr< EvtMessage > &eventMessage)
Create an ID Message out of an identity, the type and an event message.
Definition: ZMQMessageFactory.h:37
Belle2::ZMQMessageFactory::fromSocket
static std::unique_ptr< AMessage > fromSocket(const std::unique_ptr< zmq::socket_t > &socket)
Create a message of the given type by receiving a message from the socket.
Definition: ZMQMessageFactory.h:136