-
Notifications
You must be signed in to change notification settings - Fork 100
/
zproto_codec_c.gsl
2693 lines (2457 loc) · 83.8 KB
/
zproto_codec_c.gsl
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.template 0
#
# Generates a codec for a protocol specification.
#
# This is a code generator built using the iMatix GSL code generation
# language. See https://github.com/imatix/gsl for details.
#
# Copyright (c) the Contributors as noted in the AUTHORS file.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This has the following changes over the v1 codec:
#
# - uses the low-level zmq API for sending/receiving messages, instead
# of CZMQ, to reduce heap usage.
# - short strings are allocated together with the message object, as
# char [256] arrays.
# - the class API is simpler, without methods to work with hash and list
# contents.
# - sending is non-destructive. The goal is to reuse the object for many
# recvs and sends, and thus reduce heap allocations.
# - class.virtual makes send/recv work with zmsg_t instead of sockets.
#
# Note that the API is NOT compatible with v1.
include "zproto_lib.gsl"
include "zproto_bnf.gsl"
resolve_includes ()
expand_headers ()
generate_bnf ()
set_defaults ()
.endtemplate
.if switches.zproject ?= 1 & switches.private?0 <> 1
. file.delete ("$(class.package_dir)/$(class.name).$(class.header_ext)")
. directory.create ("../api")
. output "../api/$(class.name).api"
<!--
$(class.name) - $(class.title:)
Codec class for $(class.name).
** WARNING *************************************************************
THIS SOURCE FILE IS 100% GENERATED. If you edit this file, you will lose
your changes at the next build cycle. This is great for temporary printf
statements. DO NOT MAKE ANY CHANGES YOU WISH TO KEEP. The correct places
for commits are:
* The XML model used for this code generation: $(filename), or
* The code generation script that built this file: $(script)
************************************************************************
. for class.license
$(string.trim (license.):block )
. endfor
-->
<class name = "$(class.name)"\
. if defined (class.state)
state = "$(class.state)"\
. endif
>
. for define
<constant name = "$(define.name)" value = "$(define.value:)" >
$(string.trim (define.?''))
</constant>
. endfor
. for message
<constant name = "$(message.name)" value = "$(id:)" >
$(string.trim (define.?''))
</constant>
. endfor
. for class.field where type = "octets"
<constant name = "$(field.name) size" value = "$(size)" >
$(string.trim (define.?''))
</constant>
. endfor
<constructor>
Create a new empty $(class.name)
</constructor>
<constructor name = "new_zpl">
Create a new $(class.name) from zpl/zconfig_t *
<argument name = "config" type = "zconfig" />
</constructor>
<destructor>
Destroy a $(class.name) instance
</destructor>
<method name = "dup">
Create a deep copy of a $(class.name) instance
<return type="$(class.name)" fresh="1" />
</method>
. if class.virtual ?= 1
<method name = "recv">
Deserialize a $(class.name) from the specified message, popping
as many frames as needed. Returns 0 if OK, -1 if there was an error.
<argument name = "input" type = "zmsg" />
<return type = "integer" />
</method>
<method name = "send">
Serialize and append the $(class.name) to the specified message
<argument name = "output" type = "zmsg" />
<return type = "integer" />
</method>
. else
<method name = "recv">
Receive a $(class.name) from the socket. Returns 0 if OK, -1 if
there was an error. Blocks if there is no message waiting.
<argument name = "input" type = "zsock" />
<return type = "integer" />
</method>
<method name = "send">
Send the $(class.name) to the output socket, does not destroy it
<argument name = "output" type = "zsock" />
<return type = "integer" />
</method>
<method name = "encode">
Encode the first frame of $(class.name). Does not destroy it. Returns the frame if OK, else NULL.
<return type = "zframe" />
</method>
. endif
<method name = "print">
Print contents of message to stdout
</method>
<method name = "zpl">
Export class as zconfig_t*. Caller is responsibe for destroying the instance
<argument name = "parent" type = "zconfig" />
<return type = "zconfig" fresh = "1" />
</method>
<method name = "routing id">
Get the message routing id, as a frame
<return type = "zframe" />
</method>
<method name = "set routing id">
Set the message routing id from a frame
<argument name = "routing id" type = "zframe" />
</method>
. if class.pubsub = 1
<method name = "topic">
Get the topic of the message for publishing over pub/sub
<return type = "string" />
</method>
<method name = "set topic">
Set the topic of the message for publishing over pub/sub
<argument name = "topic" type = "string" />
</method>
<method name = "subscribe" singleton = "1">
Subscribe a socket to a specific message id and a topic.
<argument name = "sub" type = "zsock" />
<argument name = "id" type = "char" />
<argument name = "topic" type = "string" />
</method>
<method name = "unsubscribe" singleton = "1">
Unsubscribe a socket form a specific message id and a topic.
<argument name = "sub" type = "zsock" />
<argument name = "id" type = "char" />
<argument name = "topic" type = "string" />
</method>
<method name = "is subscribe">
Get whether the last subscription received on XPUB socket was of type subscribe.
<return type = "boolean" />
</method>
.endif
<method name = "id">
Get the $(class.name) message id
.if class.pubsub = 1
<return type = "char" />
.else
<return type = "integer" />
.endif
</method>
<method name = "set id">
Set the $(class.name) message id
.if class.pubsub = 1
<argument name = "id" type = "char" />
.else
<argument name = "id" type = "integer" />
.endif
</method>
<method name = "command">
Get the $(class.name) message id as printable text
<return type = "string" />
</method>
. for class.field where !defined (value)
. if type = "number"
<method name = "$(name)">
Get the $(name) field
<return type = "number" size = "$(size)" />
</method>
<method name = "set $(name)">
Set the $(name) field
<argument name = "$(name)" type = "number" size = "$(size)" />
</method>
. elsif type = "octets"
<method name = "$(name)">
Get the $(name) field
<return type = "buffer" mutable = "1" size = "$(size)" />
</method>
<method name = "set $(name)">
Set the $(name) field
<argument name = "$(name)" type = "buffer" />
</method>
. elsif type = "string" | type = "longstr"
<method name = "$(name)">
Get the $(name) field
<return type = "string" />
</method>
<method name = "set $(name)">
Set the $(name) field
<argument name = "$(name)" type = "string" />
</method>
. elsif type = "strings"
<method name = "$(name)">
Get the $(name) field
<return type = "zlist" />
</method>
<method name = "get $(name)">
Get the $(name) field and transfer ownership to caller
<return type = "zlist" />
</method>
<method name = "set $(name)">
Set the $(name) field
<argument name = "$(name)_p" type = "zlist" by_reference = "1" />
</method>
. elsif type = "uuid"
<method name = "$(name)">
Get the $(name) field
<return type = "z$(type)" />
</method>
<method name = "get $(name)">
Get the $(name) field and transfer ownership to caller
<return type = "z$(type)" />
</method>
<method name = "set $(name)">
Set the $(name) field
<argument name = "$(name)_p" type = "z$(type)" />
</method>
. elsif type = "frame" & class.use_zmq_msg = 1
<method name = "$(name)">
Get a copy of the $(name) field
<return type = "zmq_msg" />
</method>
<method name = "get $(name)">
Get the $(name) field and transfer ownership to caller
<argument name = "$(name)" type = "zmq_msg" />
</method>
<method name = "set $(name)">
Set the $(name) field, transferring ownership from caller
<argument name = "$(name)" type = "zmq_msg" />
</method>
. elsif type = "hash" | type = "chunk" | type = "frame" | type = "msg"
<method name = "$(name)">
Get a copy of the $(name) field
<return type = "z$(type)" />
</method>
<method name = "get $(name)">
Get the $(name) field and transfer ownership to caller
<return type = "z$(type)" />
</method>
Set the $(name) field, transferring ownership from caller
<method name = "set $(name)">
<argument name = "$(name)_p" type = "z$(type)" by_reference = "1" />
</method>
. else
. echo "E: unknown type '$(type)' for field '$(name)'"
. endif
. endfor
</class>
.else
. output "$(class.package_dir)/$(class.name).$(class.header_ext)"
/* =========================================================================
$(class.name) - $(class.title:)
Codec header for $(class.name).
** WARNING *************************************************************
THIS SOURCE FILE IS 100% GENERATED. If you edit this file, you will lose
your changes at the next build cycle. This is great for temporary printf
statements. DO NOT MAKE ANY CHANGES YOU WISH TO KEEP. The correct places
for commits are:
* The XML model used for this code generation: $(filename), or
* The code generation script that built this file: $(script)
************************************************************************
. for class.license
$(string.trim (license.):block )
. endfor
=========================================================================
*/
#ifndef $(CLASS.NAME)_H_INCLUDED
#define $(CLASS.NAME)_H_INCLUDED
/* These are the $(class.name) messages:
.for message
$(NAME) - $(string.trim (.?''):left)
. for field
. if type = "number"
$(name) $(type) $(size)\
$(string.trim (field.)?'':left)
. elsif type = "octets"
$(name) $(type) [$(size)]\
$(string.trim (field.)?'':left)
. else
$(name) $(type) $(string.trim (field.)?'':left)
. endif
. endfor
.endfor
*/
.for define
#define $(CLASS.NAME)_$(DEFINE.NAME:C) $(value:)
.endfor
.for message
#define $(CLASS.NAME)_$(MESSAGE.NAME) $(id)
.endfor
.for class.field where type = "octets"
#define $(CLASS.NAME)_$(FIELD.NAME)_SIZE $(size)
.endfor
.if file.exists ("../include/czmq.h")
#include "czmq.h"
.else
#include <czmq.h>
.endif
#ifdef __cplusplus
extern "C" {
#endif
// Opaque class structure
#ifndef $(CLASS.NAME)_T_DEFINED
typedef struct _$(class.name)_t $(class.name)_t;
#define $(CLASS.NAME)_T_DEFINED
#endif
// @interface
// Create a new empty $(class.name)
$(CLASS.EXPORT_MACRO)$(class.name)_t *
$(class.name)_new (void);
// Create a new $(class.name) from zpl/zconfig_t *
$(CLASS.EXPORT_MACRO)$(class.name)_t *
$(class.name)_new_zpl (zconfig_t *config);
// Destroy a $(class.name) instance
$(CLASS.EXPORT_MACRO)void
$(class.name)_destroy ($(class.name)_t **self_p);
// Create a deep copy of a $(class.name) instance
$(CLASS.EXPORT_MACRO)$(class.name)_t *
$(class.name)_dup ($(class.name)_t *other);
.if class.virtual ?= 1
// Deserialize a $(class.name) from the specified message, popping
// as many frames as needed. Returns 0 if OK, -1 if the read was
// interrupted, or -2 if the message is malformed.
$(CLASS.EXPORT_MACRO)int
$(class.name)_recv ($(class.name)_t *self, zmsg_t *input);
// Serialize and append the $(class.name) to the specified message
$(CLASS.EXPORT_MACRO)int
$(class.name)_send ($(class.name)_t *self, zmsg_t *output);
.else
// Receive a $(class.name) from the socket. Returns 0 if OK, -1 if
// the read was interrupted, or -2 if the message is malformed.
// Blocks if there is no message waiting.
$(CLASS.EXPORT_MACRO)int
$(class.name)_recv ($(class.name)_t *self, zsock_t *input);
// Send the $(class.name) to the output socket, does not destroy it
$(CLASS.EXPORT_MACRO)int
$(class.name)_send ($(class.name)_t *self, zsock_t *output);
// Encode the first frame of $(class.name). Does not destroy it. Returns the frame if
// OK, else NULL.
$(CLASS.EXPORT_MACRO)zframe_t *
$(class.name)_encode ($(class.name)_t *self);
.endif
.if class.pubsub = 1
// Get the topic of the message for publishing over pub/sub
$(CLASS.EXPORT_MACRO)const char *
$(class.name)_topic ($(class.name)_t *self);
// Set the topic of the message for publishing over pub/sub
$(CLASS.EXPORT_MACRO) void
$(class.name)_set_topic ($(class.name)_t *self, const char* topic);
// Subscribe a socket to a specific message id and a topic.
$(CLASS.EXPORT_MACRO) void
$(class.name)_subscribe (zsock_t *sub, char id, const char* topic);
// Unsubscribe a socket from a specific message id and a topic.
$(CLASS.EXPORT_MACRO) void
$(class.name)_unsubscribe (zsock_t *sub, char id, const char* topic);
// Get the type of subscription received from a XPUB socket
$(CLASS.EXPORT_MACRO) bool
$(class.name)_is_subscribe ($(class.name)_t *self);
.endif
// Print contents of message to stdout
$(CLASS.EXPORT_MACRO)void
$(class.name)_print ($(class.name)_t *self);
// Export class as zconfig_t*. Caller is responsibe for destroying the instance
$(CLASS.EXPORT_MACRO)zconfig_t *
$(class.name)_zpl ($(class.name)_t *self, zconfig_t* parent);
// Get/set the message routing id
$(CLASS.EXPORT_MACRO)zframe_t *
$(class.name)_routing_id ($(class.name)_t *self);
$(CLASS.EXPORT_MACRO)void
$(class.name)_set_routing_id ($(class.name)_t *self, zframe_t *routing_id);
// Get the $(class.name) id and printable command
.if class.pubsub = 1
$(CLASS.EXPORT_MACRO)char
.else
$(CLASS.EXPORT_MACRO)int
.endif
$(class.name)_id ($(class.name)_t *self);
$(CLASS.EXPORT_MACRO)void
.if class.pubsub = 1
$(class.name)_set_id ($(class.name)_t *self, char id);
.else
$(class.name)_set_id ($(class.name)_t *self, int id);
.endif
$(CLASS.EXPORT_MACRO)const char *
$(class.name)_command ($(class.name)_t *self);
.for class.field where !defined (value)
. if type = "number"
// Get/set the $(name) field
$(CLASS.EXPORT_MACRO)$(ctype)
$(class.name)_$(name) ($(class.name)_t *self);
$(CLASS.EXPORT_MACRO)void
$(class.name)_set_$(name) ($(class.name)_t *self, $(ctype) $(name));
.#
. elsif type = "octets"
// Get/set the $(name) field
$(CLASS.EXPORT_MACRO)byte *
$(class.name)_$(name) ($(class.name)_t *self);
$(CLASS.EXPORT_MACRO)void
$(class.name)_set_$(name) ($(class.name)_t *self, byte *$(name));
.#
. elsif type = "string" | type = "longstr"
// Get/set the $(name) field
$(CLASS.EXPORT_MACRO)const char *
$(class.name)_$(name) ($(class.name)_t *self);
$(CLASS.EXPORT_MACRO)void
$(class.name)_set_$(name) ($(class.name)_t *self, const char *value);
.#
. elsif type = "strings"
// Get/set the $(name) field
$(CLASS.EXPORT_MACRO)zlist_t *
$(class.name)_$(name) ($(class.name)_t *self);
// Get the $(name) field and transfer ownership to caller
$(CLASS.EXPORT_MACRO)zlist_t *
$(class.name)_get_$(name) ($(class.name)_t *self);
// Set the $(name) field, transferring ownership from caller
$(CLASS.EXPORT_MACRO)void
$(class.name)_set_$(name) ($(class.name)_t *self, zlist_t **$(name)_p);
.#
. elsif type = "uuid"
// Get/set the $(name) field
$(CLASS.EXPORT_MACRO)z$(type)_t *
$(class.name)_$(name) ($(class.name)_t *self);
$(CLASS.EXPORT_MACRO)void
$(class.name)_set_$(name) ($(class.name)_t *self, z$(type)_t *$(type));
// Get the $(name) field and transfer ownership to caller
$(CLASS.EXPORT_MACRO)z$(type)_t *
$(class.name)_get_$(name) ($(class.name)_t *self);
.#
. elsif type = "frame" & class.use_zmq_msg = 1
// Get a copy of the $(name) field
$(CLASS.EXPORT_MACRO)zmq_msg_t *
$(class.name)_$(name) ($(class.name)_t *self);
// Get the $(name) field and transfer ownership to caller
$(CLASS.EXPORT_MACRO)void
$(class.name)_get_$(name) ($(class.name)_t *self, zmq_msg_t* frame);
// Set the $(name) field, transferring ownership from caller
$(CLASS.EXPORT_MACRO)void
$(class.name)_set_$(name) ($(class.name)_t *self, zmq_msg_t* frame);
.#
. elsif type = "hash" | type = "chunk" | type = "frame" | type = "msg"
// Get a copy of the $(name) field
$(CLASS.EXPORT_MACRO)z$(type)_t *
$(class.name)_$(name) ($(class.name)_t *self);
// Get the $(name) field and transfer ownership to caller
$(CLASS.EXPORT_MACRO)z$(type)_t *
$(class.name)_get_$(name) ($(class.name)_t *self);
// Set the $(name) field, transferring ownership from caller
$(CLASS.EXPORT_MACRO)void
$(class.name)_set_$(name) ($(class.name)_t *self, z$(type)_t **$(type)_p);
.#
. else
. echo "E: unknown type '$(type)' for field '$(name)'"
. endif
.endfor
// Self test of this class
$(CLASS.EXPORT_MACRO)void
$(class.name)_test (bool verbose);
// @end
// For backwards compatibility with old codecs
#define $(class.name)_dump $(class.name)_print
#ifdef __cplusplus
}
#endif
#endif
.endif
.output "$(class.source_dir)/$(class.name).$(class.source_ext)"
/* =========================================================================
$(class.name) - $(class.title:)
Codec class for $(class.name).
** WARNING *************************************************************
THIS SOURCE FILE IS 100% GENERATED. If you edit this file, you will lose
your changes at the next build cycle. This is great for temporary printf
statements. DO NOT MAKE ANY CHANGES YOU WISH TO KEEP. The correct places
for commits are:
* The XML model used for this code generation: $(filename), or
* The code generation script that built this file: $(script)
************************************************************************
. for class.license
$(string.trim (license.):block )
. endfor
=========================================================================
*/
/*
@header
$(class.name) - $(class.title:)
@discuss
@end
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
.if defined (class.project_header)
#include "$(class.project_header)"
.endif
#include "$(class.package_dir)/$(class.name).$(class.header_ext)"
// Structure of our class
struct _$(class.name)_t {
zframe_t *routing_id; // Routing_id from ROUTER, if any
. if class.pubsub = 1
char id; // $(class.name) message ID
char *topic; // Topic to send and receive over pub/sub
bool is_subscribe; // Indicate if it a subscribe or unsubscribe command
.else
int id; // $(class.name) message ID
.endif
byte *needle; // Read/write pointer for serialization
byte *ceiling; // Valid upper limit for read pointer
.for class.field
. if type = "number" & !defined (field.value)
$(ctype) $(name); // $(string.trim (field.)?name:left,block)
. elsif type = "octets"
byte $(name) [$(size)]; // $(string.trim (field.)?name:left,block)
. elsif type = "string" & !defined (field.value)
char $(name) [256]; // $(string.trim (field.)?name:left,block)
. elsif type = "longstr"
char *$(name); // $(string.trim (field.)?name:left,block)
. elsif type = "strings"
zlist_t *$(name); // $(string.trim (field.)?name:left,block)
. elsif type = "hash"
zhash_t *$(name); // $(string.trim (field.)?name:left,block)
size_t $(name)_bytes; // Size of hash content
. elsif type = "frame" & class.use_zmq_msg = 1
zmq_msg_t $(name); // $(string.trim (field.)?name:left,block)
. elsif type = "uuid" | type = "chunk" | type = "frame" | type = "msg"
z$(type)_t *$(name); // $(string.trim (field.)?name:left,block)
. endif
.endfor
};
// --------------------------------------------------------------------------
// Network data encoding macros
// Put a block of octets to the frame
#define PUT_OCTETS(host,size) { \\
memcpy (self->needle, (host), size); \\
self->needle += size; \\
}
// Get a block of octets from the frame
#define GET_OCTETS(host,size) { \\
if (self->needle + size > self->ceiling) { \\
zsys_warning ("$(class.name): GET_OCTETS failed"); \\
goto malformed; \\
} \\
memcpy ((host), self->needle, size); \\
self->needle += size; \\
}
// Put a 1-byte number to the frame
#define PUT_NUMBER1(host) { \\
*(byte *) self->needle = (byte) (host); \\
self->needle++; \\
}
// Put a 2-byte number to the frame
#define PUT_NUMBER2(host) { \\
self->needle [0] = (byte) (((host) >> 8) & 255); \\
self->needle [1] = (byte) (((host)) & 255); \\
self->needle += 2; \\
}
// Put a 4-byte number to the frame
#define PUT_NUMBER4(host) { \\
self->needle [0] = (byte) (((host) >> 24) & 255); \\
self->needle [1] = (byte) (((host) >> 16) & 255); \\
self->needle [2] = (byte) (((host) >> 8) & 255); \\
self->needle [3] = (byte) (((host)) & 255); \\
self->needle += 4; \\
}
// Put a 8-byte number to the frame
#define PUT_NUMBER8(host) { \\
self->needle [0] = (byte) (((host) >> 56) & 255); \\
self->needle [1] = (byte) (((host) >> 48) & 255); \\
self->needle [2] = (byte) (((host) >> 40) & 255); \\
self->needle [3] = (byte) (((host) >> 32) & 255); \\
self->needle [4] = (byte) (((host) >> 24) & 255); \\
self->needle [5] = (byte) (((host) >> 16) & 255); \\
self->needle [6] = (byte) (((host) >> 8) & 255); \\
self->needle [7] = (byte) (((host)) & 255); \\
self->needle += 8; \\
}
// Get a 1-byte number from the frame
#define GET_NUMBER1(host) { \\
if (self->needle + 1 > self->ceiling) { \\
zsys_warning ("$(class.name): GET_NUMBER1 failed"); \\
goto malformed; \\
} \\
(host) = *(byte *) self->needle; \\
self->needle++; \\
}
// Get a 2-byte number from the frame
#define GET_NUMBER2(host) { \\
if (self->needle + 2 > self->ceiling) { \\
zsys_warning ("$(class.name): GET_NUMBER2 failed"); \\
goto malformed; \\
} \\
(host) = ((uint16_t) (self->needle [0]) << 8) \\
+ (uint16_t) (self->needle [1]); \\
self->needle += 2; \\
}
// Get a 4-byte number from the frame
#define GET_NUMBER4(host) { \\
if (self->needle + 4 > self->ceiling) { \\
zsys_warning ("$(class.name): GET_NUMBER4 failed"); \\
goto malformed; \\
} \\
(host) = ((uint32_t) (self->needle [0]) << 24) \\
+ ((uint32_t) (self->needle [1]) << 16) \\
+ ((uint32_t) (self->needle [2]) << 8) \\
+ (uint32_t) (self->needle [3]); \\
self->needle += 4; \\
}
// Get a 8-byte number from the frame
#define GET_NUMBER8(host) { \\
if (self->needle + 8 > self->ceiling) { \\
zsys_warning ("$(class.name): GET_NUMBER8 failed"); \\
goto malformed; \\
} \\
(host) = ((uint64_t) (self->needle [0]) << 56) \\
+ ((uint64_t) (self->needle [1]) << 48) \\
+ ((uint64_t) (self->needle [2]) << 40) \\
+ ((uint64_t) (self->needle [3]) << 32) \\
+ ((uint64_t) (self->needle [4]) << 24) \\
+ ((uint64_t) (self->needle [5]) << 16) \\
+ ((uint64_t) (self->needle [6]) << 8) \\
+ (uint64_t) (self->needle [7]); \\
self->needle += 8; \\
}
// Put a string to the frame
#define PUT_STRING(host) { \\
size_t string_size = strlen (host); \\
PUT_NUMBER1 (string_size); \\
memcpy (self->needle, (host), string_size); \\
self->needle += string_size; \\
}
// Get a string from the frame
#define GET_STRING(host) { \\
size_t string_size; \\
GET_NUMBER1 (string_size); \\
if (self->needle + string_size > (self->ceiling)) { \\
zsys_warning ("$(class.name): GET_STRING failed"); \\
goto malformed; \\
} \\
memcpy ((host), self->needle, string_size); \\
(host) [string_size] = 0; \\
self->needle += string_size; \\
}
// Put a long string to the frame
#define PUT_LONGSTR(host) { \\
size_t string_size = strlen (host); \\
PUT_NUMBER4 (string_size); \\
memcpy (self->needle, (host), string_size); \\
self->needle += string_size; \\
}
// Get a long string from the frame
#define GET_LONGSTR(host) { \\
size_t string_size; \\
GET_NUMBER4 (string_size); \\
if (self->needle + string_size > (self->ceiling)) { \\
zsys_warning ("$(class.name): GET_LONGSTR failed"); \\
goto malformed; \\
} \\
free ((host)); \\
(host) = (char *) malloc (string_size + 1); \\
memcpy ((host), self->needle, string_size); \\
(host) [string_size] = 0; \\
self->needle += string_size; \\
}
// --------------------------------------------------------------------------
// bytes cstring conversion macros
// create new array of unsigned char from properly encoded string
// len of resulting array is strlen (str) / 2
// caller is responsibe for freeing up the memory
#define BYTES_FROM_STR(dst, _str) { \\
char *str = (char*) (_str); \\
if (!str || (strlen (str) % 2) != 0) \\
return NULL; \\
\\
size_t strlen_2 = strlen (str) / 2; \\
byte *mem = (byte*) zmalloc (strlen_2); \\
size_t i; \\
\\
for (i = 0; i != strlen_2; i++) \\
{ \\
char buff[3] = {0x0, 0x0, 0x0}; \\
strncpy (buff, str, 2); \\
unsigned int uint; \\
sscanf (buff, "%x", &uint); \\
assert (uint <= 0xff); \\
mem [i] = (byte) (0xff & uint); \\
str += 2; \\
} \\
dst = mem; \\
}
// convert len bytes to hex string
// caller is responsibe for freeing up the memory
#define STR_FROM_BYTES(dst, _mem, _len) { \\
byte *mem = (byte*) (_mem); \\
size_t len = (size_t) (_len); \\
char* ret = (char*) zmalloc (2*len + 1); \\
char* aux = ret; \\
size_t i; \\
for (i = 0; i != len; i++) \\
{ \\
sprintf (aux, "%02x", mem [i]); \\
aux+=2; \\
} \\
dst = ret; \\
}
// --------------------------------------------------------------------------
// Create a new $(class.name)
$(class.name)_t *
$(class.name)_new (void)
{
$(class.name)_t *self = ($(class.name)_t *) zmalloc (sizeof ($(class.name)_t));
. for class.field
. if type = "frame" & class.use_zmq_msg = 1
zmq_msg_init (&self->$(name));
. endif
. endfor
return self;
}
// --------------------------------------------------------------------------
// Create a new $(class.name) from zpl/zconfig_t *
$(class.name)_t *
$(class.name)_new_zpl (zconfig_t *config)
{
assert (config);
$(class.name)_t *self = NULL;
char *message = zconfig_get (config, "message", NULL);
if (!message) {
zsys_error ("Can't find 'message' section");
return NULL;
}
.for class.message
if (streq ("$(CLASS.NAME)_$(MESSAGE.NAME)", message)) {
self = $(class.name)_new ();
$(class.name)_set_id (self, $(CLASS.NAME)_$(MESSAGE.NAME));
}
else
.endfor
{
zsys_error ("message=%s is not known", message);
return NULL;
}
char *s = zconfig_get (config, "routing_id", NULL);
if (s) {
byte *bvalue;
BYTES_FROM_STR (bvalue, s);
if (!bvalue) {
$(class.name)_destroy (&self);
return NULL;
}
zframe_t *frame = zframe_new (bvalue, strlen (s) / 2);
free (bvalue);
self->routing_id = frame;
}
.if class.pubsub = 1
char *topic = zconfig_get (config, "topic", NULL);
if (topic) {
zstr_free (&self->topic);
self->topic = strdup (topic);
}
.endif
zconfig_t *content = NULL;
switch (self->id) {
.for class.message
case $(CLASS.NAME)_$(MESSAGE.NAME):
. if count (field) > 0
content = zconfig_locate (config, "content");
if (!content) {
zsys_error ("Can't find 'content' section");
$(class.name)_destroy (&self);
return NULL;
}
. endif
. for field where !defined (value)
. if type = "number"
{
char *es = NULL;
char *s = zconfig_get (content, "$(name)", NULL);
if (!s) {
zsys_error ("content/$(name) not found");
$(class.name)_destroy (&self);
return NULL;
}
uint64_t uvalue = (uint64_t) strtoll (s, &es, 10);
if (es != s+strlen (s)) {
zsys_error ("content/$(name): %s is not a number", s);
$(class.name)_destroy (&self);
return NULL;
}
self->$(name) = uvalue;
}
. elsif type = "octets"
{
char *s = zconfig_get (content, "$(name)", NULL);
if (!s) {
$(class.name)_destroy (&self);
return NULL;
}
byte *bvalue;
BYTES_FROM_STR (bvalue, s);
if (!bvalue) {
$(class.name)_destroy (&self);
return NULL;
}
self->$(name) = bvalue;
}
. elsif type = "string" | type = "longstr"
{
char *s = zconfig_get (content, "$(name)", NULL);
if (!s) {
$(class.name)_destroy (&self);
return NULL;
}
. if type = "string"
strncpy (self->$(name), s, 255);
. else
self->$(name) = strdup (s);
. endif
}
. elsif type = "strings"
{
zconfig_t *zstrings = zconfig_locate (content, "$(name)");
if (zstrings) {
zlist_t *strings = zlist_new ();
zlist_autofree (strings);
zconfig_t *child;
for (child = zconfig_child (zstrings);
child != NULL;
child = zconfig_next (child))
{
zlist_append (strings, zconfig_value (child));
}
self->$(name) = strings;
}
}
. elsif type = "hash"
{
zconfig_t *zhash = zconfig_locate (content, "$(name)");
if (zhash) {
zhash_t *hash = zhash_new ();
zhash_autofree (hash);
zconfig_t *child;
for (child = zconfig_child (zhash);
child != NULL;
child = zconfig_next (child))
{
zhash_update (hash, zconfig_name (child), zconfig_value (child));
}
self->$(name) = hash;
}
}
. elsif type = "chunk"
{
char *s = zconfig_get (content, "$(name)", NULL);
if (!s) {
$(class.name)_destroy (&self);
return NULL;
}
byte *bvalue;
BYTES_FROM_STR (bvalue, s);
if (!bvalue) {
$(class.name)_destroy (&self);
return NULL;
}
zchunk_t *chunk = zchunk_new (bvalue, strlen (s) / 2);
free (bvalue);
self->$(name) = chunk;
}
. elsif type = "uuid"
{
char *s = zconfig_get (content, "$(name)", NULL);
if (s) {
zuuid_t *uuid = zuuid_new ();
zuuid_set_str (uuid, s);
self->$(name) = uuid;
}
}
. elsif type = "frame"
{
char *s = zconfig_get (content, "$(name)", NULL);
if (!s) {
$(class.name)_destroy (&self);
return NULL;
}
byte *bvalue;
BYTES_FROM_STR (bvalue, s);
if (!bvalue) {
$(class.name)_destroy (&self);
return NULL;
}
. if class.use_zmq_msg = 1
zmq_msg_close (&self->$(name));
zmq_msg_init_size (&self->$(name), strlen (s) / 2);
memcpy (zmq_msg_data (&self->$(name)), bvalue, zmq_msg_size (&self->$(name)));