-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstomp.rpgle
669 lines (503 loc) · 16.6 KB
/
stomp.rpgle
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
**FREE
// The MIT License (MIT)
//
// Copyright (c) 2017 Mihael Schmidt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// TODO support for more than CCSID 819 (f. e. utf8) (umlaute?!)
// TODO support for optional charset
// TODO support for stomp spec 1.1
// TODO support for stomp spec 1.2
// TODO max receiving frame size is 65535
// TODO use set timeout for net communications (send/recv)
ctl-opt nomain;
//
// Prototypes
//
dcl-pr waitForReceipts extproc('waitForReceipts');
conn pointer const;
end-pr;
dcl-pr ackMessage;
conn pointer const;
frame pointer const;
end-pr;
/define SOCKET_UNIX98
/include 'stomp_h.rpgle'
/include 'stompframe_h.rpgle'
/include 'stomputil_h.rpgle'
/include 'stompparser_h.rpgle'
/include 'stompext_h.rpgle'
/include QLOG4RPG,PLOG4RPG
/include 'net_h.rpgle'
/include 'socket_h.rpgle'
/include 'message/message_h.rpgle'
/include 'libc_h.rpgle'
/include 'errno_h.rpgle'
/include 'libtree/libtree_h.rpgle'
/include 'llist/llist_h.rpgle'
//
// Templates
//
/include 'stomp_t.rpgle'
//
// Global Variables
//
dcl-s logger pointer;
//
// Procedures
//
dcl-proc stomp_create export;
dcl-pi *N pointer;
host varchar(255) const;
port int(10) const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(ptr);
dcl-s flags int(10);
dcl-s c_err int(10) based(errPtr);
ptr = %alloc(%size(stomp_header_t));
clear header;
header.host = host;
header.port = port;
header.connected = *off;
header.extension = *null;
header.useReceipts = *off;
header.options = tree_rb_create(%paddr('tree_rb_int_compare'));
header.openReceipts = list_create();
header.bufferedFrames = list_create();
header.socket = socket(AF_INET : SOCK_STREAM : PROTOCOL_DEFAULT);
if (header.socket = -1);
errPtr = errno();
message_sendEscapeMessageToCaller(
'Could not create socket. ' + %char(c_err) + ': ' + %str(strerror(c_err)));
endif;
if (logger = *null);
logger = Logger_getLogger('rpgnextgen.stomp');
endif;
Logger_info(logger : 'stomp client created');
return ptr;
end-proc;
dcl-proc stomp_open export;
dcl-pi *N;
conn pointer const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-ds socket_address_inet likeds(socket_address_inet_t);
dcl-s c_err int(10) based(errPtr);
dcl-ds node likeds(tree_node_int_t) based(nodePtr);
dcl-ds timeout likeds(timeout_t) based(node.value);
clear socket_address_inet;
socket_address_inet.family = AF_INET;
socket_address_inet.addr = inet_addr(%trim(header.host));
socket_address_inet.port = header.port;
socket_address_inet.zero = *allx'00';
if (connect(header.socket : %addr(socket_address_inet) :
%size(socket_address_inet)) = -1);
errPtr = errno();
message_sendEscapeMessageToCaller(
'Could not open socket connection. ' + %char(c_err) + ': ' + %str(strerror(c_err)));
endif;
// set timeout
if (tree_rb_int_containsKey(header.options : STOMP_OPTION_TIMEOUT));
nodePtr = tree_rb_int_get(header.options : STOMP_OPTION_TIMEOUT);
if (setsockopt(header.socket : SOL_SOCKET: SO_RCVTIMEO : %addr(timeout) : %size(timeout)) = -1);
errPtr = errno();
message_sendEscapeMessageToCaller(
'Could not set socket timeout. ' + %char(c_err) + ': ' + %str(strerror(c_err)));
endif;
endif;
header.connected = *on;
Logger_info(logger : 'socket connection established to ' +
%trimr(header.host) + ':' + %char(header.port));
end-proc;
dcl-proc stomp_close export;
dcl-pi *N;
conn pointer const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
if (header.connected);
callp close(header.socket);
header.connected = *off;
endif;
Logger_info(logger : 'socket connection closed');
end-proc;
dcl-proc stomp_finalize export;
dcl-pi *N;
conn pointer;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
if (header.connected);
// close socket
stomp_close(conn);
endif;
tree_rb_int_finalize(header.options);
if (header.extension <> *null);
stomp_ext_finalize(header.extension);
endif;
list_dispose(header.openReceipts);
list_dispose(header.bufferedFrames);
Logger_info(logger : 'stomp client disposed');
dealloc(n) conn;
end-proc;
dcl-proc stomp_setTimeout export;
dcl-pi *N;
conn pointer const;
mseconds int(10) const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-ds timeout likeds(timeout_t);
timeout.seconds = mseconds / 1000;
timeout.useconds = %rem(mseconds : 1000) * 1000;
tree_rb_int_put(header.options : STOMP_OPTION_TIMEOUT :
%addr(timeout) : %size(timeout));
Logger_debug(logger : 'set timeout: ' + %char(mseconds) + 'ms');
end-proc;
dcl-proc stomp_setVirtualHost export;
dcl-pi *N;
conn pointer const;
pVirtualHost varchar(100) const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-s virtualHost varchar(100);
virtualHost = pVirtualHost;
tree_rb_int_put(header.options : STOMP_OPTION_VIRTUAL_HOST :
%addr(virtualHost : *DATA) : %len(virtualHost));
Logger_debug(logger : 'set virtual host: ' + pVirtualHost);
end-proc;
dcl-proc stomp_setClientId export;
dcl-pi *N;
conn pointer const;
pClientId varchar(100) const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-s clientId varchar(100);
clientId = pClientId;
tree_rb_int_put(header.options : STOMP_OPTION_CLIENT_ID :
%addr(clientId : *DATA) : %len(clientId));
Logger_debug(logger : 'set client id: ' + pClientId);
end-proc;
dcl-proc stomp_setAckMode export;
dcl-pi *N;
conn pointer const;
pMode char(10) const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-s mode char(10);
mode = pMode;
tree_rb_int_put(header.options : STOMP_OPTION_ACK :
%addr(mode) : %len(%trimr(mode)));
Logger_debug(logger : 'set ack mode: ' + pMode);
end-proc;
dcl-proc stomp_setDurableSubscriberName export;
dcl-pi *N;
conn pointer const;
pName varchar(100) const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-s name varchar(100);
name = pName;
tree_rb_int_put(header.options : STOMP_OPTION_DURABLE_SUBSCRIBER :
%addr(name : *DATA) : %len(name));
Logger_debug(logger : 'set durable subscriber: ' + pName);
end-proc;
dcl-proc stomp_getSessionId export;
dcl-pi *N like(stomp_sessionid_t);
conn pointer const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
return header.sessionId;
end-proc;
dcl-proc stomp_setPersistMessages export;
dcl-pi *N;
conn pointer const;
persistent ind const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-s value varchar(6);
if (persistent);
value = 'true';
else;
value = 'false';
endif;
tree_rb_int_put(header.options : STOMP_OPTION_PERSISTENT :
%addr(value : *DATA) : %len(value));
Logger_debug(logger : 'set persist messages: ' + value);
end-proc;
dcl-proc stomp_sendFrame export;
dcl-pi *N;
conn pointer const;
frame pointer const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-s length int(10);
dcl-s ptr pointer;
dcl-s backupPtr pointer;
dcl-s rc int(10);
dcl-s receiptid like(stomp_receiptid_t);
Logger_info(logger : 'sending ' + stomp_frame_getCommand(frame) + ' frame');
// all frame (except the CONNECT frame) might get a receipt header
if (stomp_frame_getCommand(frame) <> 'CONNECT' and
stomp_isUsingReceipts(conn));
receiptid = %char(%timestamp() : *ISO);
stomp_frame_setHeader(frame : 'receipt' : receiptid);
stomp_addOpenReceipt(conn : receiptid);
endif;
ptr = stomp_frame_toString(frame);
backupPtr = ptr;
length = strlen(ptr);
translateToAscii(ptr : length);
ptr = backupPtr;
rc = send(header.socket : ptr : length + 1 : 0);
Logger_debug(logger : 'sent ' + %char(rc) + ' bytes');
if (stomp_isUsingReceipts(conn));
waitForReceipts(conn);
endif;
dealloc(n) ptr;
end-proc;
dcl-proc stomp_receiveFrame export;
dcl-pi *N pointer;
conn pointer const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-s length int(10);
dcl-s ptr pointer;
dcl-s backupPtr pointer;
dcl-s rc int(10);
dcl-s returnFrame pointer;
dcl-s receipt char(50);
dcl-s bufferedFrame pointer based(tmpPtr);
Logger_debug(logger : 'receiving frames ...');
// return next buffered frames
if (not list_isEmpty(header.bufferedFrames));
tmpPtr =list_getFirst(header.bufferedFrames);
list_removeFirst(header.bufferedFrames);
Logger_debug(logger : 'return buffered frame ' + stomp_frame_getCommand(bufferedFrame));
ackMessage(conn : bufferedFrame);
return bufferedFrame;
endif;
ptr = %alloc(65535);
length = 65535;
rc = recv(header.socket : ptr : length : 0);
if (rc = -1);
Logger_debug(logger : 'no data received from connection');
else;
backupPtr = ptr;
length = rc;
translateFromAscii(ptr : length);
ptr = backupPtr;
returnFrame = stomp_frame_parse(ptr);
Logger_debug(logger : 'received ' + stomp_frame_getCommand(returnFrame) + ' frame');
ackMessage(conn : returnFrame);
endif;
dealloc(n) backupPtr;
return returnFrame;
end-proc;
dcl-proc stomp_setExtension export;
dcl-pi *N;
conn pointer const;
extension pointer const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
if (header.extension <> *null);
Logger_debug(logger : 'replace extension: ' +
stomp_ext_getId(header.extension));
stomp_ext_finalize(header.extension);
endif;
header.extension = extension;
Logger_debug(logger : 'set extension: ' + stomp_ext_getId(extension));
end-proc;
dcl-proc stomp_setExtensionByName export;
dcl-pi *N;
conn pointer const;
extensionName char(10) const;
userdata pointer const options(*nopass : *string);
procedureName char(256) const options(*nopass);
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-s extension pointer;
if (%parms() = 2);
extension = stomp_ext_create(extensionName);
elseif (%parms() = 3);
extension = stomp_ext_create(extensionName : userdata);
elseif (%parms() = 4);
extension = stomp_ext_create(extensionName : userdata : procedureName);
endif;
stomp_setExtension(conn : extension);
end-proc;
dcl-proc stomp_getExtension export;
dcl-pi *N pointer;
conn pointer const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
return header.extension;
end-proc;
dcl-proc stomp_useReceipts export;
dcl-pi *N;
conn pointer const;
value ind const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
header.useReceipts = value;
Logger_debug(logger : 'set using receipts: ' + value);
end-proc;
dcl-proc stomp_isUsingReceipts export;
dcl-pi *N ind;
conn pointer const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
return header.useReceipts;
end-proc;
dcl-proc stomp_hasOption export;
dcl-pi *N ind;
conn pointer const;
option int(10) const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-ds node likeds(tree_node_int_t) based(nodePtr);
return tree_rb_int_containsKey(header.options : option);
end-proc;
dcl-proc stomp_getOptionValue export;
dcl-pi *N pointer;
conn pointer const;
option int(10) const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-ds node likeds(tree_node_int_t) based(nodePtr);
nodePtr = tree_rb_int_get(header.options : option);
return node.value;
end-proc;
dcl-proc stomp_setSessionId export;
dcl-pi *N;
conn pointer const;
session like(stomp_header_t.sessionId) const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
header.sessionId = session;
Logger_debug(logger : 'set session id: ' + session);
end-proc;
dcl-proc stomp_addOpenReceipt export;
dcl-pi *N;
conn pointer const;
pReceipt like(stomp_receiptid_t) const;
end-pi;
dcl-s receipt like(stomp_receiptid_t);
dcl-ds header likeds(stomp_header_t) based(conn);
receipt = pReceipt;
list_addString(header.openReceipts : receipt);
Logger_debug(logger : 'added open receipt: ' + receipt);
end-proc;
dcl-proc stomp_getNumberOfOpenReceipts export;
dcl-pi *N int(10);
conn pointer const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
return list_size(header.openReceipts);
end-proc;
///
// Wait for receipts
//
// The procedure receives frames over the socket of the client and waits
// until all open receipts has been received. <br/>
//
// Because receipts may not be sent directly after the frame with the receipt header
// all non RECEIPT frames will be buffered and returned by calling the
// <em>stomp_receiveFrame</em> procedure.
//
// \param Pointer to client
///
dcl-proc waitForReceipts;
dcl-pi *N;
conn pointer const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-s rc int(10);
dcl-s ptr pointer;
dcl-s backupPtr pointer;
dcl-s length int(10);
dcl-s frame pointer;
dcl-s receiptId like(stomp_receiptid_t);
dcl-s x int(10);
Logger_debug(logger : 'waiting for receipts ...');
length = 65535;
ptr = %alloc(length);
dow (not list_isEmpty(header.openReceipts));
rc = recv(header.socket : ptr : length : 0);
if (rc = -1);
Logger_debug(logger : 'no data received from connection');
else;
backupPtr = ptr;
length = rc;
translateFromAscii(ptr : length);
ptr = backupPtr;
frame = stomp_frame_parse(ptr);
Logger_debug(logger : 'received ' + stomp_frame_getCommand(frame) + ' frame');
if (stomp_frame_getCommand(frame) = 'RECEIPT');
receiptId = stomp_frame_getHeaderValue(frame : 'receipt-id');
Logger_debug(logger : 'received receipt with id: ' + receiptId);
x = list_indexOf(header.openReceipts : %addr(receiptId : *DATA) :
%len(receiptId));
if (x >=0); // open receipt found
list_remove(header.openReceipts : x);
Logger_debug(logger : 'removed receipt ' + receiptId +
' from open receipt list (position ' +
%char(x) + ')');
else;
Logger_warn(logger : 'receipt ' + receiptId + ' is not in the ' +
'list of open receipts');
endif;
else;
// no receipt => add frame to buffered frames
list_add(header.bufferedFrames : %addr(frame) : %size(frame));
Logger_debug(logger : 'buffered frame ' + stomp_frame_getCommand(frame) +
'. waiting for receipt ...');
endif;
endif;
enddo;
dealloc ptr;
end-proc;
///
// Ack Message Frame
//
// If the frame is a MESSAGE frame and the ack mode is "client"
// then an ACK frame must be send with the message id from the
// original frame.
//
// \param Connection
// \param Frame
///
dcl-proc ackMessage;
dcl-pi *N;
conn pointer const;
frame pointer const;
end-pi;
dcl-ds header likeds(stomp_header_t) based(conn);
dcl-s ackFrame pointer;
dcl-ds node likeds(tree_node_int_t) based(nodePtr);
if (frame <> *null and stomp_frame_getCommand(frame) = 'MESSAGE');
if (tree_rb_int_containsKey(header.options : STOMP_OPTION_ACK));
nodePtr = tree_rb_int_get(header.options : STOMP_OPTION_ACK);
if (%str(node.value) = STOMP_ACK_MODE_CLIENT);
ackFrame = stomp_frame_create('ACK');
stomp_frame_setHeader(ackFrame : 'message-id' :
stomp_frame_getHeaderValue(frame : 'message-id'));
stomp_sendFrame(conn : ackFrame);
stomp_frame_finalize(ackFrame);
endif;
endif;
endif;
end-proc;