-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMDP.h
290 lines (245 loc) · 7.28 KB
/
MDP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#pragma once
// https://rfc.zeromq.org/spec:7/MDP/
#include <zmqpp/zmqpp.hpp>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "Ensure.h"
#include "ZMQIdentity.h"
namespace MDP {
struct EmptyFrame {};
using Message = zmqpp::message;
using MessageHandle = std::unique_ptr<Message>;
template <typename ...T_n>
MessageHandle makeMessageHandle(T_n &&...tail)
{
return std::make_unique<Message>(std::forward<T_n>(tail)...);
}
inline
void append(Message &) {}
template <typename T, typename ...T_n>
void append(Message &msg, const T &value, const T_n &...tail);
template <typename ...T_n>
void append(Message &msg, const EmptyFrame &, const T_n &...tail);
template <typename ...T_n>
void append(Message &msg, const ZMQIdentity &identity, const T_n &...tail);
template <typename ...T_n>
void append(Message &msg, const std::vector<std::string> &seq, const T_n &...tail);
template <typename T, typename ...T_n>
void append(Message &msg, const T &value, const T_n &...tail)
{
msg.add(value);
append(msg, tail...);
}
template <typename ...T_n>
void append(Message &msg, const EmptyFrame &, const T_n &...tail)
{
msg.raw_new_msg();
append(msg, tail...);
}
template <typename ...T_n>
void append(Message &msg, const ZMQIdentity &identity, const T_n &...tail)
{
msg.add_raw(identity.data(), identity.size());
append(msg, tail...);
}
template <typename ...T_n>
void append(Message &msg, const std::vector<std::string> &seq, const T_n &...tail)
{
for(const auto &str : seq) msg.add(str);
append(msg, tail...);
}
template <typename ...T_n>
Message makeMessage(const T_n &...tail)
{
Message msg;
append(msg, tail...);
return msg;
}
namespace Client {
namespace Signature {
constexpr auto self = "MDPC01";
} /* Signature */
/* Client REQUEST:
* Frame 0: Empty (zero bytes, invisible to REQ application)
* Frame 1: "MDPC01" (six bytes, representing MDP/Client v0.1)
* Frame 2: Service name (printable string)
* Frames 3+: Request body (opaque binary) */
template <typename ...T_n>
Message makeReq(const std::string &service, const T_n & ...body)
{
return makeMessage(EmptyFrame{}, Signature::self, service, body...);
}
} /* Client */
namespace Worker {
namespace Signature {
constexpr auto self = "MDPW01";
constexpr auto ready = "\x1";
constexpr auto request = "\x2";
constexpr auto reply = "\x3";
constexpr auto heartbeat = "\x4";
constexpr auto disconnect = "\x5";
} /* Signature */
/* Worker READY
* Frame 0: Empty frame
* Frame 1: "MDPW01" (six bytes, representing MDP/Worker v0.1)
* Frame 2: 0x01 (one byte, representing READY)
* Frame 3: Service name (printable string) */
inline
Message makeReady(const std::string &service)
{
return makeMessage(EmptyFrame{}, Signature::self, Signature::ready, service);
}
/* Worker REPLY
* Frame 0: Empty frame
* Frame 1: "MDPW01" (six bytes, representing MDP/Worker v0.1)
* Frame 2: 0x03 (one byte, representing REPLY)
* Frame 3: Client address (envelope stack)
* Frame 4: Empty (zero bytes, envelope delimiter)
* Frames 5+: Reply body (opaque binary) */
template <typename ...T_n>
Message makeRep(const ZMQIdentity &identity, const T_n & ...body)
{
return makeMessage(EmptyFrame{}, Signature::self, Signature::reply, identity, EmptyFrame{}, body...);
}
/* Worker HEARTBEAT
* Frame 0: Empty frame
* Frame 1: "MDPW01" (six bytes, representing MDP/Worker v0.1)
* Frame 2: 0x04 (one byte, representing HEARTBEAT) */
inline
Message makeHeartbeat()
{
return makeMessage(EmptyFrame{}, Signature::self, Signature::heartbeat);
}
/* Worker DISCONNECT
* Frame 0: Empty frame
* Frame 1: "MDPW01" (six bytes, representing MDP/Worker v0.1)
* Frame 2: 0x05 (one byte, representing DISCONNECT) */
inline
Message makeDisconnect()
{
return makeMessage(EmptyFrame{}, Signature::self, Signature::disconnect);
}
} /* Worker */
namespace Broker {
namespace Signature {
constexpr auto serviceUndefined = "error: service undefined";
constexpr auto serviceUnsupported = "service unsupported";
constexpr auto serviceBusy = "service busy";
constexpr auto serviceRegistered = "service registered";
constexpr auto serviceFailure = "service failure";
constexpr auto statusSucess = "success";
constexpr auto statusFailure = "failure";
}
/* Client REPLY:
* Frame 0: Identity
* Frame 1: Empty (zero bytes, invisible to REQ application)
* Frame 2: "MDPC01" (six bytes, representing MDP/Client v0.1)
* Frame 3: Service name (printable string)
* Frame 4: status (success | failure)
* Frames 5+: Reply body (opaque binary) */
template <typename ...T_n>
Message makeSucessClientRep(
const ZMQIdentity &identity,
const std::string &service,
const T_n & ...body)
{
return makeMessage(
identity,
EmptyFrame{},
Client::Signature::self,
service,
Broker::Signature::statusSucess,
body...);
}
template <typename ...T_n>
Message makeFailureClientRep(
const ZMQIdentity &identity,
const std::string &service,
const T_n & ...body)
{
return makeMessage(
identity,
EmptyFrame{},
Client::Signature::self,
service,
Broker::Signature::statusFailure,
body...);
}
/* Worker REQUEST
* Frame 0: Identity
* Frame 1: Empty frame
* Frame 2: "MDPW01" (six bytes, representing MDP/Worker v0.1)
* Frame 3: 0x02 (one byte, representing REQUEST)
* Frame 4: Client address (envelope stack)
* Frame 5: Empty (zero bytes, envelope delimiter)
* Frames 6+: Request body (opaque binary) */
template <typename ...T_n>
Message makeWorkerReq(const ZMQIdentity &workerIdentity, const ZMQIdentity &clientIdentity, const T_n & ...body)
{
return makeMessage(workerIdentity, EmptyFrame{}, Worker::Signature::self, Worker::Signature::request, clientIdentity, EmptyFrame{}, body...);
}
/* Worker HEARTBEAT
* Frame 0: Identity
* Frame 1: Empty frame
* Frame 2: "MDPW01" (six bytes, representing MDP/Worker v0.1)
* Frame 3: 0x04 (one byte, representing HEARTBEAT) */
inline
Message makeHeartbeat(const ZMQIdentity &identity)
{
return makeMessage(identity, EmptyFrame{}, Worker::Signature::self, Worker::Signature::heartbeat);
}
/* Worker DISCONNECT
* Frame 0: Identity
* Frame 1: Empty frame
* Frame 2: "MDPW01" (six bytes, representing MDP/Worker v0.1)
* Frame 3: 0x05 (one byte, representing DISCONNECT) */
inline
Message makeDisconnect(const ZMQIdentity &identity)
{
return makeMessage(identity, EmptyFrame{}, Worker::Signature::self, Worker::Signature::disconnect);
}
} /* Broker */
} /* MDP */
/* ADL for zmqpp::message */
namespace zmqpp {
template <typename T>
void traceSerializeImpl(std::ostream &os, T value)
{
os << value;
}
inline
void traceSerializeImpl(std::ostream &os, char value)
{
os << int(value);
}
inline
void traceSerializeImpl(std::ostream &os, unsigned char value)
{
os << int(value);
}
inline
void traceSerializeImpl(std::ostream &os, signed char value)
{
os << int(value);
}
inline
void traceSerializeImpl(std::ostream &os, const MDP::Message &message)
{
os << '{';
for(auto i = 0u; i < message.parts(); ++i)
{
os << '[' << i << ']' << '(';
traceSerializeImpl(os, message.get(i));
os << ')';
}
os << '}';
}
inline
void traceSerializeImpl(std::ostream &os, const MDP::MessageHandle &handle)
{
if(!handle) return;
traceSerializeImpl(os, *handle);
}
}