-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
mdp_worker.c
371 lines (304 loc) · 9.42 KB
/
mdp_worker.c
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* =========================================================================
mdp_worker.c
Generated codec implementation for mdp_worker
=========================================================================
*/
#include <czmq.h>
#include "mdp_worker.h"
// Structure of our class
struct _mdp_worker_t {
int id; // mdp_worker message ID
zframe_t *address; // Sender address if any
char *service;
zframe_t *client;
zframe_t *body;
};
// --------------------------------------------------------------------------
// Create a new mdp_worker
mdp_worker_t *
mdp_worker_new (int id)
{
mdp_worker_t *self = (mdp_worker_t *) zmalloc (sizeof (mdp_worker_t));
self->id = id;
return self;
}
// --------------------------------------------------------------------------
// Destroy the mdp_worker
void
mdp_worker_destroy (mdp_worker_t **self_p)
{
assert (self_p);
if (*self_p) {
mdp_worker_t *self = *self_p;
// Free class properties
zframe_destroy (&self->address);
free (self->service);
zframe_destroy (&self->client);
zframe_destroy (&self->body);
// Free object itself
free (self);
*self_p = NULL;
}
}
// --------------------------------------------------------------------------
// Receive and parse a mdp_worker from the socket. Returns new object or
// NULL if error. Will block if there's no message waiting.
mdp_worker_t *
mdp_worker_recv (void *socket)
{
// Read all frames off socket
assert (socket);
zmsg_t *msg = zmsg_recv (socket);
if (!msg)
return NULL; // Interrupted
// Create and populate new mdp_worker instance
mdp_worker_t *self = mdp_worker_new (0);
// If we're reading from a ROUTER socket, get address
if (zsocket_type (socket) == ZMQ_ROUTER) {
self->address = zmsg_pop (msg);
if (!self->address)
goto empty; // Interrupted
}
// Read and check header
zframe_t *empty_frame = zmsg_pop (msg);
if (!empty_frame || !zframe_streq (empty_frame, ""))
goto malformed;
zframe_destroy (&empty_frame);
zframe_t *protocol_frame = zmsg_pop (msg);
if (!protocol_frame || !zframe_streq (protocol_frame, "MDPW01"))
goto malformed;
zframe_destroy (&protocol_frame);
zframe_t *id_frame = zmsg_pop (msg);
if (!id_frame)
goto malformed;
self->id = zframe_data (id_frame) [0];
zframe_destroy (&id_frame);
// Read and parse fields per-message
switch (self->id) {
case MDP_WORKER_READY:
self->service = zmsg_popstr (msg);
if (!self->service)
goto malformed;
break;
case MDP_WORKER_REQUEST:
self->client = zmsg_pop (msg);
if (!self->client)
goto malformed;
self->body = zmsg_pop (msg);
if (!self->body)
goto malformed;
break;
case MDP_WORKER_REPLY:
self->client = zmsg_pop (msg);
if (!self->client)
goto malformed;
self->body = zmsg_pop (msg);
if (!self->body)
goto malformed;
break;
case MDP_WORKER_HEARBEAT:
break;
case MDP_WORKER_DISCONNECT:
break;
default:
goto malformed;
}
// Successful return
zmsg_destroy (&msg);
return self;
// Error returns
malformed:
printf ("E: malformed message '%d'\n", self->id);
empty:
zmsg_destroy (&msg);
mdp_worker_destroy (&self);
return (NULL);
}
// --------------------------------------------------------------------------
// Send the mdp_worker to the socket, and destroy it
int
mdp_worker_send (mdp_worker_t **self_p, void *socket)
{
assert (socket);
assert (self_p);
assert (*self_p);
mdp_worker_t *self = *self_p;
// If we're sending to a ROUTER, we send the address first
zmsg_t *msg = zmsg_new ();
if (zsocket_type (socket) == ZMQ_ROUTER) {
assert (self->address);
zmsg_add (msg, self->address);
self->address = NULL; // Owned by msg now
}
// Send header fields
zmsg_addstr (msg, "");
zmsg_addstr (msg, "MDPW01");
zmsg_addmem (msg, &self->id, 1);
switch (self->id) {
case MDP_WORKER_READY:
zmsg_addstr (msg, self->service);
break;
case MDP_WORKER_REQUEST:
zmsg_add (msg, self->client);
self->client = NULL;
zmsg_add (msg, self->body);
self->body = NULL;
break;
case MDP_WORKER_REPLY:
zmsg_add (msg, self->client);
self->client = NULL;
zmsg_add (msg, self->body);
self->body = NULL;
break;
case MDP_WORKER_HEARBEAT:
break;
case MDP_WORKER_DISCONNECT:
break;
}
// Send the message and destroy mdp_worker object
int rc = zmsg_send (&msg, socket);
mdp_worker_destroy (self_p);
return rc;
}
// --------------------------------------------------------------------------
// Get/set the message address
zframe_t *
mdp_worker_address (mdp_worker_t *self)
{
assert (self);
return self->address;
}
void
mdp_worker_address_set (mdp_worker_t *self, zframe_t *address)
{
if (self->address)
zframe_destroy (&self->address);
self->address = zframe_dup (address);
}
// --------------------------------------------------------------------------
// Get/set the mdp_worker id
int
mdp_worker_id (mdp_worker_t *self)
{
assert (self);
return self->id;
}
void
mdp_worker_id_set (mdp_worker_t *self, int id)
{
self->id = id;
}
// --------------------------------------------------------------------------
// Get/set the service field
char *
mdp_worker_service (mdp_worker_t *self)
{
assert (self);
return self->service;
}
void
mdp_worker_service_set (mdp_worker_t *self, char *format, ...)
{
// Format into newly allocated string
assert (self);
va_list argptr;
va_start (argptr, format);
free (self->service);
self->service = (char *) malloc (255 + 1);
assert (self->service);
vsnprintf (self->service, 255, format, argptr);
va_end (argptr);
}
// --------------------------------------------------------------------------
// Get/set the client field
zframe_t *
mdp_worker_client (mdp_worker_t *self)
{
assert (self);
return self->client;
}
// Takes ownership of supplied frame
void
mdp_worker_client_set (mdp_worker_t *self, zframe_t *frame)
{
assert (self);
if (self->client)
zframe_destroy (&self->client);
self->client = frame;
}
// --------------------------------------------------------------------------
// Get/set the body field
zframe_t *
mdp_worker_body (mdp_worker_t *self)
{
assert (self);
return self->body;
}
// Takes ownership of supplied frame
void
mdp_worker_body_set (mdp_worker_t *self, zframe_t *frame)
{
assert (self);
if (self->body)
zframe_destroy (&self->body);
self->body = frame;
}
// --------------------------------------------------------------------------
// Selftest
int
mdp_worker_test (bool verbose)
{
printf (" * mdp_worker: ");
// Simple create/destroy test
mdp_worker_t *self = mdp_worker_new (0);
assert (self);
mdp_worker_destroy (&self);
// Create pair of sockets we can send through
zctx_t *ctx = zctx_new ();
assert (ctx);
void *output = zsocket_new (ctx, ZMQ_DEALER);
assert (output);
zsocket_bind (output, "inproc://selftest");
void *input = zsocket_new (ctx, ZMQ_ROUTER);
assert (input);
zsocket_connect (input, "inproc://selftest");
// Encode/send/decode and verify each message type
self = mdp_worker_new (MDP_WORKER_READY);
mdp_worker_service_set (self, "Life is short but Now lasts for ever");
mdp_worker_send (&self, output);
self = mdp_worker_recv (input);
assert (self);
assert (streq (mdp_worker_service (self), "Life is short but Now lasts for ever"));
mdp_worker_destroy (&self);
self = mdp_worker_new (MDP_WORKER_REQUEST);
mdp_worker_client_set (self, zframe_new ("Captcha Diem", 12));
mdp_worker_body_set (self, zframe_new ("Captcha Diem", 12));
mdp_worker_send (&self, output);
self = mdp_worker_recv (input);
assert (self);
assert (zframe_streq (mdp_worker_client (self), "Captcha Diem"));
assert (zframe_streq (mdp_worker_body (self), "Captcha Diem"));
mdp_worker_destroy (&self);
self = mdp_worker_new (MDP_WORKER_REPLY);
mdp_worker_client_set (self, zframe_new ("Captcha Diem", 12));
mdp_worker_body_set (self, zframe_new ("Captcha Diem", 12));
mdp_worker_send (&self, output);
self = mdp_worker_recv (input);
assert (self);
assert (zframe_streq (mdp_worker_client (self), "Captcha Diem"));
assert (zframe_streq (mdp_worker_body (self), "Captcha Diem"));
mdp_worker_destroy (&self);
self = mdp_worker_new (MDP_WORKER_HEARBEAT);
mdp_worker_send (&self, output);
self = mdp_worker_recv (input);
assert (self);
mdp_worker_destroy (&self);
self = mdp_worker_new (MDP_WORKER_DISCONNECT);
mdp_worker_send (&self, output);
self = mdp_worker_recv (input);
assert (self);
mdp_worker_destroy (&self);
zctx_destroy (&ctx);
printf ("OK\n");
return 0;
}