-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathZMsg.hx
456 lines (422 loc) · 12.1 KB
/
ZMsg.hx
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/**
* (c) 2011 Richard J Smith
*
* This file is part of hxzmq
*
* hxzmq is free software; you can redistribute it and/or modify it under
* the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* hxzmq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.zeromq;
import haxe.io.Bytes;
import neko.io.FileInput;
import neko.io.FileOutput;
import org.zeromq.ZMQ;
import org.zeromq.ZFrame;
/**
* <p>The ZMsg class provides methods to send and receive multipart messages
* across 0MQ sockets. This class provides a list-like container interface,
* with methods to work with the overall container. ZMsg messages are
* composed of zero or more ZFrame objects.
* </p>
* <p>
* <pre>
* // Send a simple single-frame string message on a ZMQSocket "output" socket object
* ZMsg.newStringMsg("Hello").send(output);
*
* // Add several frames into one message
* var msg:ZMsg = new ZMsg();
* for (i in 0 ... 10) {
* msg.addString("Frame" + i);
* }
* msg.send(output);
*
* // Receive message from ZMQSocket "input" socket object and iterate over frames
* var receivedMessage = ZMsg.recvMsg(input);
* for (f in receivedMessage) {
* // Do something with frame f (of type ZFrame)
* }
* </pre>
* </p>
* <p>
* Based on <a href="http://github.com/zeromq/czmq/blob/master/src/zmsg.c">zmsg.c</a> in czmq
* </p>
*/
class ZMsg
{
// Hold internal list of ZFrame objects
private var frames:List<ZFrame>;
/**
* Constructor
*/
public function new() {
frames = new List<ZFrame>();
}
/**
* Destructor.
* Destroys all ZFrames stored in ZMsg
*/
public function destroy() {
if (frames == null) // Handle usecase if destroy() is called repeatedly on same ZMsg object
return;
while (frames.length > 0) {
var f:ZFrame = frames.pop();
f.destroy();
}
frames = null;
}
/** Return number of frames in message */
public function size():Int {
if (frames != null) {
return frames.length;
} else {
return 0;
}
}
/**
* Return number of bytes contained in all the frames in this message
* @return
*/
public function contentSize():Int {
var size:Int = 0;
if (frames != null) {
for (f in frames) {
size += f.size();
}
}
return size;
}
/**
* Add a ZFrame to end of list
* @param frame ZFrame to add to list
* @throws ZMQException if frame is null
*/
public function add(frame:ZFrame) {
if (frame == null) {
throw new ZMQException(EINVAL);
}
frames.add(frame);
}
/**
* Push frame plus empty frame to front of message, before first frame.
* Message takes ownership of frame, will destroy it when message is sent.
*/
public function wrap(frame:ZFrame) {
if (frame != null) {
push(new ZFrame(Bytes.ofString("")));
push(frame);
}
}
/**
* Pop frame off front of message, caller now owns frame.
* If next frame is empty, pops and destroys that empty frame
* (e.g. useful when unwrapping ROUTER socket envelopes)
* @return Unwrapped frame
*/
public function unwrap():ZFrame {
if (size() == 0) {
return null;
} else {
var f = pop();
var empty:ZFrame = first();
if (empty.hasData() && empty.size() == 0) {
empty = pop();
empty.destroy();
}
return f;
}
}
/**
* Removes an existing frame from the message.
* Does not destroy the removed frame.
* @param frame ZFrame to remove
* @return true if frame is found and removed, else false
*/
public function remove(frame:ZFrame):Bool {
if (frame == null) {
throw new ZMQException(EINVAL);
}
return frames.remove(frame);
}
/**
* Returns an iterator over the ZFrame list within the ZMsg
* @return
*/
public function iterator():Iterator<ZFrame> {
if (frames != null) {
return frames.iterator();
} else
return null;
}
/**
* Returns a new ZMsg object containing only those ZFrames from this message
* where f(x) is true
* @param f
* @return Filtered ZMsg object, else null if this message contains no frame list (ie has been destroyed)
*/
public function filter(f: ZFrame -> Bool):ZMsg {
if (frames != null) {
var filteredFrames:List<ZFrame> = frames.filter(f);
var filteredMsg:ZMsg = new ZMsg();
for (frame in filteredFrames) {
filteredMsg.add(frame);
}
return filteredMsg;
} else
return null;
}
/**
* Returns last frame in message, else null if frame list is empty or invalid (destroyed)
* @return
*/
public function first():ZFrame {
if (frames != null) {
return frames.first();
} else
return null;
}
/**
* returns True if the ZMsg has no frames, else false
* @return
*/
public function isEmpty():Bool {
return (frames == null || size() == 0);
}
/**
* Returns last frame in message, else null if frame list is empty or invalid (destroyed)
* @return
*/
public function last():ZFrame {
if (frames != null) {
return frames.last();
} else
return null;
}
/**
* Removes first frame from message, if any. Returns frame or null.
* Caller now owns frame and must destroy() it when finished with it.
* @return
*/
public function pop():ZFrame {
if (frames != null) {
return frames.pop();
} else
return null;
}
/**
* Push frame to the front of the message, before all the other frames.
* ZMsg object takes ownership of the frame, will destroy it when message is sent.
* @param frame
*/
public function push(frame:ZFrame) {
if (frame == null) {
throw new ZMQException(EINVAL);
}
if (frames != null) {
frames.push(frame);
}
}
/**
* Pushes string as new ZFrame at front of ZMsg frame list
* @param str
*/
public function pushString(str:String) {
if (frames == null) {
frames = new List<ZFrame>();
}
frames.push(new ZFrame(Bytes.ofString(str)));
}
/**
* Adds string as new ZFrame at end of ZMsg frame list
* @param str
*/
public function addString(str:String) {
if (frames == null) {
frames = new List<ZFrame>();
}
frames.add(new ZFrame(Bytes.ofString(str)));
}
/**
* Pop frame off top of message, returns as a String, else null if
* no frames in message, or if popped frame has no data.
* @return
*/
public function popString():String {
if (frames != null) {
var f:ZFrame = frames.pop();
if (f != null && f.hasData()) {
var s = f.data.toString();
f.destroy();
return s;
}
}
return null;
}
/**
* Creates copy of this message, also copying all contained frames & their data
* @return Copied ZMsg object, or null if this message contains an invalid (destroyed) frame list.
*/
public function duplicate():ZMsg {
if (frames != null) {
var msg:ZMsg = new ZMsg();
for (f in frames) {
msg.add(f.duplicate());
}
return msg;
} else
return null;
}
/**
* Send message to socket, destroys after sending. If the message has no
* frames, sends nothing but destroys the message anyhow.
* @param socket
*/
public function send(socket:ZMQSocket):Void {
if (socket == null) {
throw new ZMQException(EINVAL);
return;
}
if (frames == null) {
return;
}
var iter:Iterator<ZFrame> = iterator();
var f:ZFrame = iter.next();
while (f != null) {
f.send(socket, { if (iter.hasNext()) ZFrame.ZFRAME_MORE else null; });
f = iter.next();
}
destroy();
return;
}
/**
* Returns msg contents as a readable string
* @return
*/
public function toString():String {
var buf:StringBuf = new StringBuf();
if (isEmpty()) {
buf.add("empty");
} else {
buf.add("#frames:" + size());
var frame_nbr = 0;
for (f in frames) {
buf.add(",#" + ++frame_nbr + ":[");
buf.add(f.toString());
buf.add("]");
}
}
return buf.toString();
}
/**
* Receives message from socket, returns ZMsg object or null if the
* recv was interrupted. Does a blocking recv, if you want not to block then use
* the ZLoop class or ZMQPoller to check for socket input before receiving.
* @param socket
* @return
*/
public static function recvMsg(socket:ZMQSocket):ZMsg {
if (socket == null) {
throw new ZMQException(EINVAL);
return null;
}
var msg:ZMsg = null;
while (true) {
var f:ZFrame = ZFrame.recvFrame(socket);
if (f == null) {
// If receive failed or was interrupted
if (msg != null) msg.destroy();
break;
}
if (msg == null) msg = new ZMsg();
msg.add(f);
if (!f.more) {
break;
}
}
return msg;
}
/**
* Simple method that adds a single supplied string to a new ZMsg, and returns the message object
* @param data
* @return
*/
public static function newStringMsg(data:String):ZMsg {
var msg:ZMsg = new ZMsg();
msg.addString(data);
return msg;
}
/**
* Save message to an open file, return true if OK, else false
*
* Data saved as:
* 4 bytes: number of frames
* For every frame:
* 4 bytes: byte size of frame
* + bytes: frame data bytes
*
* @param msg
* @param file
* @return
*/
public static function save(msg:ZMsg, file:FileOutput):Bool {
if (file == null || msg == null) {
throw new ZMQException(EINVAL);
return false;
}
try {
// Write number of frames
file.writeInt31(msg.size());
if (msg.size() > 0) {
for (f in msg.frames) {
// Write byte size of frame
file.writeInt31(f.size());
// Write frame byte data
file.prepare(f.size());
file.writeBytes(f.data, 0, f.size());
}
}
return true;
} catch (e:Dynamic) {
return false;
}
}
/**
* Load / append a ZMsg from an open file.
* Create a new message if null message provided.
*
* @param file
* @param ?msg
* @return
*/
public static function load(file:FileInput, ?msg:ZMsg):ZMsg {
if (file == null) {
throw new ZMQException(EINVAL);
return null;
}
var rcvMsg:ZMsg = {
if (msg == null)
new ZMsg()
else
msg;
}
var msgSize = file.readInt31();
if (msgSize > 0) {
var msg_nbr = 0;
while (++msg_nbr <= msgSize) {
var frameSize = file.readInt31();
var f:ZFrame = new ZFrame(file.read(frameSize));
rcvMsg.add(f);
}
}
return rcvMsg;
}
}