Belle II Software light-2406-ragdoll
ZMQMessageFactory.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/logging/Logger.h>
11#include <framework/pcore/zmq/messages/ZMQNoIdMessage.h>
12#include <framework/pcore/zmq/messages/ZMQIdMessage.h>
13#include <framework/pcore/zmq/messages/ZMQDefinitions.h>
14#include <memory>
15#include <string>
16#include <zmq.hpp>
17
18namespace Belle2 {
25 public:
27 static auto createMessage(const std::string& msgIdentity,
28 const EMessageTypes msgType,
29 const std::unique_ptr<EvtMessage>& eventMessage)
30 {
31 return createMessage<ZMQIdMessage>(msgIdentity, msgType, eventMessage);
32 }
33
35 static auto createMessage(const std::string& msgIdentity,
36 const EMessageTypes msgType,
37 const std::string& msgData = "")
38 {
39 return createMessage<ZMQIdMessage>(msgIdentity, msgType, msgData);
40 }
41
43 static auto createMessage(const std::string& msgIdentity,
44 const EMessageTypes msgType,
45 zmq::message_t msgData)
46 {
47 return createMessage<ZMQIdMessage>(msgIdentity, msgType, std::move(msgData));
48 }
49
51 static auto createMessage(const std::string& msgIdentity,
52 std::unique_ptr<ZMQNoIdMessage> shortMessage)
53 {
54 return createMessage<ZMQIdMessage>(msgIdentity, std::move(shortMessage->getMessagePart<0>()),
55 std::move(shortMessage->getMessagePart<1>()));
56 }
57
59 static auto createMessage(const EMessageTypes msgType,
60 const std::string& msgData = "")
61 {
62 return createMessage<ZMQNoIdMessage>(msgType, msgData);
63 }
64
66 static auto createMessage(const EMessageTypes msgType,
67 int msgData)
68 {
69 return createMessage<ZMQNoIdMessage>(msgType, msgData);
70 }
71
73 static auto createMessage(const EMessageTypes msgType,
74 const StoreObjPtr<EventMetaData>& evtMetaData)
75 {
76 return createMessage<ZMQNoIdMessage>(msgType, evtMetaData);
77 }
78
80 static auto createMessage(const EMessageTypes msgType,
81 zmq::message_t msgData)
82 {
83 return createMessage<ZMQNoIdMessage>(msgType, std::move(msgData));
84 }
85
87 static auto createMessage(const EMessageTypes msgType,
88 zmq::message_t msgData,
89 zmq::message_t additionalData)
90 {
91 return createMessage<ZMQNoIdMessage>(msgType, std::move(msgData), std::move(additionalData));
92 }
93
95 static auto createMessage(zmq::message_t msgType,
96 zmq::message_t msgData)
97 {
98 return createMessage<ZMQNoIdMessage>(std::move(msgType), std::move(msgData));
99 }
100
102 static auto createMessage(const EMessageTypes msgType,
103 const std::unique_ptr<EvtMessage>& eventMessage)
104 {
105 return createMessage<ZMQNoIdMessage>(msgType, eventMessage);
106 }
107
109 static auto createMessage(const EMessageTypes msgType,
110 const std::unique_ptr<EvtMessage>& eventMessage,
111 zmq::message_t additionalData)
112 {
113 return createMessage<ZMQNoIdMessage>(msgType, eventMessage, std::move(additionalData));
114 }
115
117 static auto stripIdentity(std::unique_ptr<ZMQIdMessage> message)
118 {
119 return createMessage<ZMQNoIdMessage>(std::move(message->getMessagePart<1>()),
120 std::move(message->getMessagePart<2>()),
121 std::move(message->getMessagePart<3>()));
122 }
123
125 template <class AMessage>
126 static std::unique_ptr<AMessage> fromSocket(const std::unique_ptr<zmq::socket_t>& socket)
127 {
128 auto newMessage = std::unique_ptr<AMessage>(new AMessage());
129 auto& messageParts = newMessage->getMessageParts();
130 for (unsigned int i = 0; i < AMessage::c_messageParts; i++) {
131 B2ASSERT("The next part does not belong to the same message",
132 socket->get(zmq::sockopt::rcvmore) == 1 or i == 0);
133 auto received = socket->recv(messageParts[i], zmq::recv_flags::none);
134 B2ASSERT("No message received", received);
135 }
136 B2ASSERT("There should not be more than the retrieved parts", socket->get(zmq::sockopt::rcvmore) == 0);
137 return newMessage;
138 }
139
140 private:
142 template <class T, class... Args>
143 static std::unique_ptr<T> createMessage(Args&& ... args)
144 {
145 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
146 }
147 };
149}
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
Helper class for creating new ID/No-ID messages.
static auto createMessage(const EMessageTypes msgType, const std::string &msgData="")
Create a No-ID Message out of an identity, the type and a string.
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.
static auto createMessage(const EMessageTypes msgType, const std::unique_ptr< EvtMessage > &eventMessage)
Create a No-ID Message out of an identity, the type and an event message.
static auto createMessage(const EMessageTypes msgType, const StoreObjPtr< EventMetaData > &evtMetaData)
Create a No-ID Message out of an identity, the type and an event meta data.
static std::unique_ptr< T > createMessage(Args &&... args)
Small helper constructor to create a unique_ptr out of some arguments, as std::make_unique is not wor...
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.
static auto stripIdentity(std::unique_ptr< ZMQIdMessage > message)
Create a No-ID Message out of an ID message.
static auto createMessage(const EMessageTypes msgType, zmq::message_t msgData)
Create a No-ID Message out of the type and another zmq message.
static auto createMessage(zmq::message_t msgType, zmq::message_t msgData)
Create a No-ID Message out of an identity, the type and another zmq message.
static auto createMessage(const EMessageTypes msgType, int msgData)
Create a No-ID Message out of an identity, the type and an int.
static auto createMessage(const EMessageTypes msgType, const std::unique_ptr< EvtMessage > &eventMessage, zmq::message_t additionalData)
Create a No-ID Message out of an identity, the type, an event message, and and additional message.
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.
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.
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.
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.
EMessageTypes
Type the messages can have.
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24