muscle.anubis
29.8 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
Standard MUSCLE field type-constants
read tools/basis.anubis
read network/tools.anubis
read system/string.anubis
read system/types.anubis
public define Int32 _B_ANY_TYPE = 1095653716. // 'ANYT', // wild card
public define Int32 _B_BOOL_TYPE = 1112493900. // 'BOOL',
public define Int32 _B_DOUBLE_TYPE = 1145195589. // 'DBLE',
public define Int32 _B_FLOAT_TYPE = 1179406164. // 'FLOT',
public define Int32 _B_INT64_TYPE = 1280069191. // 'LLNG',
public define Int32 _B_INT32_TYPE = 1280265799. // 'LONG',
public define Int32 _B_INT16_TYPE = 1397248596. // 'SHRT',
public define Int32 _B_INT8_TYPE = 1113150533. // 'BYTE',
public define Int32 _B_MESSAGE_TYPE = 1297303367. // 'MSGG',
public define Int32 _B_POINTER_TYPE = 1347310674. // 'PNTR',
public define Int32 _B_POINT_TYPE = 1112559188. // 'BPNT',
public define Int32 _B_RECT_TYPE = 1380270932. // 'RECT',
public define Int32 _B_STRING_TYPE = 1129534546. // 'CSTR',
public define Int32 _B_OBJECT_TYPE = 1330664530. // 'OPTR', // used for flattened objects
public define Int32 _B_RAW_TYPE = 1380013908. // 'RAWT', // used for raw byte arrays
public define Int32 _CURRENT_PROTOCOL_VERSION = 1347235888. // 'PM00' -- our magic number 0x504D3030
public type Message:...
define Maybe(Message) unflatten_message ( RAddr(Int8) stream ).
public type B_Point:
point(Float32 x, Float32 y).
public type B_Rect:
rect(B_Point up_left, B_Point down_right),
rect(Float32 x, Float32 y, Float32 x1, Float32 y1).
type Muscle_object:
b_bool(Bool),
b_double(Float),
b_float(Float32),
b_int64(Int64),
b_int32(Int32),
b_int16(Int16),
b_int8(Int8),
b_message(Message),
b_point(B_Point),
b_rect(B_Rect),
b_string(String),
b_custom(ByteArray).
type Muscle_type:
b_any_t,
b_bool_t,
b_double_t,
b_float_t,
b_int64_t,
b_int32_t,
b_int16_t,
b_int8_t,
b_message_t,
/* b_pointer,*/
b_point_t,
b_rect_t,
b_string_t,
b_custom_t(Int32). /*,
b_object,
b_raw*/
type Message_field:
message_field
(
String name,
Muscle_type type_code,
Var(List(Muscle_object)) items
).
public type Message:
message
(
Var(Int32) what,
Var(Int32) numField,
Var(List(Message_field)) fields
).
define Maybe(Muscle_type)
get_muscle_type
(
Int32 type_code
)=
if type_code = _B_BOOL_TYPE then
success(b_bool_t)
else if type_code = _B_DOUBLE_TYPE then
success(b_double_t)
else if type_code = _B_FLOAT_TYPE then
success(b_float_t)
else if type_code = _B_INT64_TYPE then
success(b_int64_t)
else if type_code = _B_INT32_TYPE then
success(b_int32_t)
else if type_code = _B_INT16_TYPE then
success(b_int16_t)
else if type_code = _B_INT8_TYPE then
success(b_int8_t)
else if type_code = _B_MESSAGE_TYPE then
success(b_message_t)
else if type_code = _B_STRING_TYPE then
success(b_string_t)
else if type_code = _B_POINT_TYPE then
success(b_point_t)
else if type_code = _B_RECT_TYPE then
success(b_rect_t)
else
print("muscle custom type " + to_hexa(type_code) + "\n");
success(b_custom_t(type_code)).
public define One
print_to_stream
(
Message msg
)=
print("Message: what = " + *msg.what + ", ASCII = \"" + to_ascii(*msg.what) + "\", Hexa = 0x" + to_hexa(*msg.what) + "\n").
public define One
set_what
(
Message msg,
Int32 new_what
) =
msg.what <- new_what.
/** Don't allow the user to put data blobs under any of the non-data-blob type codes,
* because that would mess up our internal logic which assumes that fields of those
* types have the appropriate data for their type
* @param typeCode
*/
public define Bool
is_type_code_variable_size
(
Int32 type_code
) =
if type_code = _B_BOOL_TYPE |
type_code = _B_DOUBLE_TYPE |
type_code = _B_FLOAT_TYPE |
type_code = _B_INT64_TYPE |
type_code = _B_INT32_TYPE |
type_code = _B_INT16_TYPE |
type_code = _B_INT8_TYPE |
type_code = _B_POINTER_TYPE |
type_code = _B_POINT_TYPE |
type_code = _B_RECT_TYPE then
true
else
false.
define Maybe(ByteArray)
read_data
(
RAddr(Int8) stream,
Int32 nb_bytes
) =
if read_network_bytes(stream, nb_bytes, 10) is
{
failure then failure,
time_out then failure,
success(bytes) then success(bytes),
truncated(_) then failure
}.
define Maybe(Int32)
read_Int32
(
RAddr(Int8) stream,
) =
if read_network_bytes(stream, 4, 10) is
{
failure then failure,
time_out then failure,
success(bytes) then
//TODO convert to right the platform endian
with value = lendian_to_host_Int32(bytes),
// print("Int32["+value+"] "+to_ascii(value)+ "\n");
success(value),
truncated(_) then failure
}.
define Maybe(Int64)
read_Int64
(
RAddr(Int8) stream,
) =
if read_network_bytes(stream, 8, 10) is
{
failure then failure,
time_out then failure,
success(bytes) then
//TODO convert to right the platform endian
with value = lendian_to_host_Int64(bytes),
// print("Int32["+value+"] "+to_ascii(value)+ "\n");
success(value),
truncated(_) then failure
}.
define Maybe(Float32)
read_float32
(
RAddr(Int8) stream,
) =
if read_network_bytes(stream, 4, 10) is
{
failure then failure,
time_out then failure,
success(bytes) then
//TODO convert to right the platform endian
with value = lendian_to_host_Int32(bytes),
// print("Int32["+value+"] "+to_ascii(value)+ "\n");
success(float32(value)),
truncated(_) then failure
}.
define Maybe(Float)
read_float64
(
RAddr(Int8) stream,
) =
if read_network_bytes(stream, 8, 10) is
{
failure then failure,
time_out then failure,
success(bytes) then
//TODO convert to right the platform endian
lendian_to_host_Float64(bytes),
truncated(_) then failure
}.
define Maybe(Int16)
read_Int16
(
RAddr(Int8) stream,
) =
if read_network_bytes(stream, 2, 10) is
{
failure then failure,
time_out then failure,
success(bytes) then
with value = lendian_to_host_Int16(bytes),
success(value),
truncated(_) then failure
}.
define Maybe(Muscle_object)
read_one_field_item
(
RAddr(Int8) source,
Muscle_type b_type
)=
if b_type is
{
b_any_t then
failure,
//read one B_BOOL field
b_bool_t then
if read_data(source, 1) is
{
failure then failure,
success(ba_bool) then
with bool8 = force_nth(0, ba_bool),
print("B_BOOL = " + bool8 + "\n");
with real_bool = if bool8 = 0 then false else true,
success(b_bool(real_bool)) //convert into Anubis Bool
}
//read one B_DOUBLE field
b_double_t then
if read_float64(source) is
{
failure then failure,
success(value) then
print("B_DOUBLE = " + float_to_string(value,5) + "\n");
success(b_double(value)) //convert into Anubis Int32
}
//read one B_FLOAT field
b_float_t then
if read_float32(source) is
{
failure then failure,
success(value) then
print("B_FLOAT = Float32 not yet available\n");
success(b_float(value)) //convert into Anubis Int32
}
//read one B_INT64 field
b_int64_t then
if read_Int64(source) is
{
failure then failure,
success(value) then
print("B_INT64 = not yet printable\n");
success(b_int64(value)) //convert into Anubis Int64
}
//read one B_INT32 field
b_int32_t then
if read_Int32(source) is
{
failure then failure,
success(value) then
print("B_INT32 = " + value + "\n");
success(b_int32(value)) //convert into Anubis Int32
}
//read one B_INT16 field
b_int16_t then
if read_Int16(source) is
{
failure then failure,
success(value) then
print("B_INT16 = " + value + "\n");
success(b_int16(value)) //convert into Anubis Int32
}
//read one B_INT8 field
b_int8_t then
if read_data(source, 1) is
{
failure then failure,
success(ba_value) then
with value = force_nth(0,ba_value),
print("B_INT8 = " + value + "\n");
success(b_int8(value)) //convert into Anubis Int32
}
//read one B_MESSAGE field
b_message_t then
if unflatten_message(source) is
{
failure then failure,
success(message) then
success(b_message(message))
}
//read one B_POINT field
b_point_t then
if read_float32(source) is
{
failure then failure,
success(x) then
if read_float32(source) is
{
failure then failure,
success(y) then
print("B_POINT = Float32 not yet available\n");
success(b_point(point(x, y))) //return the point value
}
}
//read one B_RECT field
b_rect_t then
if read_float32(source) is
{
failure then failure,
success(x) then
if read_float32(source) is
{
failure then failure,
success(y) then
if read_float32(source) is
{
failure then failure,
success(w) then
if read_float32(source) is
{
failure then failure,
success(h) then
print("B_RECT = Float32 not yet available\n");
success(b_rect(rect(point(x, y), point(w,h)))) //return the point value
}
}
}
}
//read one B_STRING field
b_string_t then
if read_Int32(source) is
{
failure then failure,
success(size) then //get his size
if read_data(source, size) is //and read it
{
failure then failure,
success(ba_string) then
print("B_STRING = " + to_string(ba_string) + "\n");
success(b_string(to_string(ba_string))) //convert into Anubis String
}
}
//read one CUSTOM field
b_custom_t(_) then
if read_Int32(source) is
{
failure then failure,
success(size) then //get his size
if read_data(source, size) is //and read it
{
failure then failure,
success(ba_custom) then
success(b_custom(ba_custom)) //convert into Anubis String
}
}
}
.
define Maybe(List(Muscle_object))
read_field_items
(
RAddr(Int8) source,
Muscle_type b_type,
Int32 count
)=
if count =< 0 then
success([ ])
else
if read_one_field_item(source, b_type) is
{
failure then failure,
success(first_one) then
if read_field_items(source, b_type, count-1) is
{
failure then failure,
success(others) then success([first_one . others])
}
}.
define Maybe(List(Muscle_object))
read_message_field_items
(
RAddr(Int8) source,
Int32 amount
)=
if amount =< 0 then
success([ ])
else
if read_Int32(source) is
{
failure then failure,
success(current_message_size) then
//now we have the size of current message
if read_one_field_item(source, b_message_t) is
{
failure then failure,
success(first_one) then
if read_message_field_items(source, amount - (current_message_size + 4)) is
{
failure then failure,
success(others) then success([first_one . others])
}
}
}.
define Maybe(Message_field)
read_one_field
(
RAddr(Int8) source
)=
//read length of the field name
if read_Int32(source) is
{
failure then failure,
success(name_length) then
if read_data(source, name_length) is
{
failure then failure
success(ba_name) then
with field_name = to_string(ba_name),
print("\nfield_name =\"" + field_name + "\"\n");
if read_Int32(source) is
{
failure then failure,
success(int32_type_code) then
if read_Int32(source) is
{
failure then failure
success(data_length) then
if get_muscle_type(int32_type_code) is
{
failure then failure,
success(type_code) then
if type_code is
{
b_any_t then failure,
// read BOOL
b_bool_t then
if read_field_items(source, type_code, data_length) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
// read DOUBLE
b_double_t then
if read_field_items(source, type_code, data_length >> 3) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
// read FLOAT
b_float_t then
if read_field_items(source, type_code, data_length >> 2) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
// read INT64
b_int64_t then
if read_field_items(source, type_code, data_length >> 3) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
// read INT32
b_int32_t then
if read_field_items(source, type_code, data_length >> 2) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
// read INT16
b_int16_t then
if read_field_items(source, type_code, data_length >> 1) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
// read INT8
b_int8_t then
if read_field_items(source, type_code, data_length) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
// read MESSAGE
b_message_t then
if read_message_field_items(source, data_length) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
// read POINT
b_point_t then
if read_field_items(source, type_code, data_length >> 3) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
// read RECT
b_rect_t then
if read_field_items(source, type_code, data_length >> 4) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
// read STRING
b_string_t then
if read_Int32(source) is //get numbers items in that field
{
failure then failure,
success(nb_items) then
print("nb_items" + nb_items + "\n");
if read_field_items(source, type_code, nb_items) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
}
// read CUSTOM
b_custom_t(code) then
if read_Int32(source) is //get numbers items in that field
{
failure then failure,
success(nb_items) then
print("nb_items custom " + nb_items + "\n");
if read_field_items(source, type_code, nb_items) is
{
failure then failure,
success(field_items) then success(message_field(field_name, type_code, var(field_items)))
}
}
}
}
}
}
}
}.
define Maybe(List(Message_field)) /* returns 'failure' if it cannot the required number of elements */
read_fields
(
RAddr(Int8) source,
Int32 n // number of elements to read
) =
if n =< 0 then
success([ ])
else
if read_one_field(source) is
{
failure then failure,
success(first_one) then
if read_fields(source, n-1) is
{
failure then failure,
success(others) then success([first_one . others])
}
}.
/* Format: 0. Protocol revision number (4 bytes, always set to CURRENT_PROTOCOL_VERSION) */
/* 1. 'what' code (4 bytes) */
/* 2. Number of entries (4 bytes) */
/* 3. Entry name length (4 bytes) */
/* 4. Entry name string (flattened String) */
/* 5. Entry type code (4 bytes) */
/* 6. Entry data length (4 bytes) */
/* 7. Entry data (n bytes) */
/* 8. loop to 3 as necessary */
define Maybe(Message)
unflatten_message
(
RAddr(Int8) stream
)=
if read_Int32(stream) is
{
failure then failure,
success(protocolVersion) then
if protocolVersion = _CURRENT_PROTOCOL_VERSION then
print("CURRENT_PROTOCOL_VERSION OK\n");
//get the 'what' code
if read_Int32(stream) is
{
failure then failure,
success(what_code) then
print("what code = [" + to_ascii(what_code) + "] "+ what_code + "\n");
//get the number of entries at this level
if read_Int32(stream) is
{
failure then failure,
success(nb_entries) then
print("Nb Entries = " + nb_entries + "\n");
//get all fields of this level
if read_fields(stream, nb_entries) is
{
failure then failure,
success(fields) then success(message(var(what_code), var(nb_entries), var(fields)))
}
}
}
else //This is not the same level version of message protocol
failure
}
.
/********************** Find collection ********************/
define Maybe(Message_field)
find_field
(
List(Message_field) msg_fields,
Muscle_type search_type_code,
String search_name
)=
if msg_fields is
{
[] then failure,
[ h . t ] then
//test if the head of list contain the field with same name and type
if search_type_code = b_any_t then //With ANY_TYPE we don't care about type_code
if h.name = search_name then
success(h)
else
find_field(t, search_type_code, search_name)
else //we search for exact type and name
if (h.name = search_name) & (h.type_code = search_type_code) then
success(h)
else
find_field(t, search_type_code, search_name)
}.
define Maybe(Muscle_object)
find_data_type
(
List(Message_field) fields,
Muscle_type type_code,
String name,
Int32 index
)=
if find_field(fields, type_code, name) is
{
failure then
failure, //we don't find any fields matching with query
success(field) then
nth(index, *field.items)
}.
define Maybe(Muscle_object)
find_data_type
(
Muscle_type type_code,
Message msg,
String name,
Int32 index
)=
find_data_type(*msg.fields, type_code, name, index).
/** Retrieve a string value from the Message.
* @param msg Message where we search the string
* @param name The field name to look for the string value under.
* @param index The index of the string item in its field entry.
* @return 'success(String)' if the string value was found, or 'failure' if it wasn't.
*/
public define Maybe(String)
find_string
(
Message msg,
String name,
Int32 index
)=
if find_data_type(b_string_t, msg, name, index) is success(mo) then
if mo is b_string(value) then
success(value)
else
failure
else
failure.
/** As above, only (index) isn't specified. It is assumed to be zero. */
public define Maybe(String)
find_string
(
Message msg,
String name
)= find_string(msg, name, 0).
/** Retrieve an int8 value from the Message.
* @param msg Message where we search the string
* @param name The field name to look for the int8 value under.
* @param index The index of the int8 item in its field entry.
* @return 'success(Int8)' if the int8 value was found, or 'failure' if it wasn't.
*/
public define Maybe(Int8)
find_int8
(
Message msg,
String name,
Int32 index
)=
if find_data_type(b_int8_t, msg, name, index) is success(mo) then
if mo is b_int8(value) then
success(value)
else
failure
else
failure.
/** As above, only (index) isn't specified. It is assumed to be zero. */
public define Maybe(Int8)
find_int8
(
Message msg,
String name
)= find_int8(msg, name, 0).
/** Retrieve an int16 value from the Message.
* @param msg Message where we search the string
* @param name The field name to look for the int16 value under.
* @param index The index of the int16 item in its field entry.
* @return 'success(Int16)' if the int16 value was found, or 'failure' if it wasn't.
*/
public define Maybe(Int16)
find_int16
(
Message msg,
String name,
Int32 index
)=
if find_data_type(b_int16_t, msg, name, index) is success(mo) then
if mo is b_int16(value) then
success(value)
else
failure
else
failure.
public define Maybe(Int16)
find_int16
(
Message msg,
String name
)= find_int16(msg, name, 0).
/** Retrieve an int32 value from the Message.
* @param msg Message where we search the string
* @param name The field name to look for the int32 value under.
* @param index The index of the int32 item in its field entry.
* @return 'success(Int32)' if the int32 value was found, or 'failure' if it wasn't.
*/
public define Maybe(Int32)
find_int32
(
Message msg,
String name,
Int32 index
)=
if find_data_type(b_int32_t, msg, name, index) is success(mo) then
if mo is b_int32(value) then
success(value)
else
failure
else
failure.
public define Maybe(Int32)
find_int32
(
Message msg,
String name
)= find_int32(msg, name, 0).
/** Retrieve an int64 value from the Message.
* @param msg Message where we search the string
* @param name The field name to look for the int64 value under.
* @param index The index of the int64 item in its field entry.
* @return 'success(Int64)' if the int16 value was found, or 'failure' if it wasn't.
*/
public define Maybe(Int64)
find_int64
(
Message msg,
String name,
Int32 index
)=
if find_data_type(b_int64_t, msg, name, index) is success(mo) then
if mo is b_int64(value) then
success(value)
else
failure
else
failure.
public define Maybe(Int64)
find_int64
(
Message msg,
String name
)= find_int64(msg, name, 0).
/** Retrieve an Bool value from the Message.
* @param msg Message where we search the string
* @param name The field name to look for the Bool value under.
* @param index The index of the Bool item in its field entry.
* @return 'success(Bool)' if the Bool value was found, or 'failure' if it wasn't.
*/
public define Maybe(Bool)
find_bool
(
Message msg,
String name,
Int32 index
)=
if find_data_type(b_bool_t, msg, name, index) is success(mo) then
if mo is b_bool(value) then
success(value)
else
failure
else
failure.
public define Maybe(Bool)
find_bool
(
Message msg,
String name
)= find_bool(msg, name, 0).
/** Retrieve an Float32 value from the Message.
* @param msg Message where we search the string
* @param name The field name to look for the Float32 value under.
* @param index The index of the Float32 item in its field entry.
* @return 'success(Float32)' if the Float32 value was found, or 'failure' if it wasn't.
*/
public define Maybe(Float32)
find_float32
(
Message msg,
String name,
Int32 index
)=
if find_data_type(b_float_t, msg, name, index) is success(mo) then
if mo is b_float(value) then
success(value)
else
failure
else
failure.
public define Maybe(Float32)
find_float32
(
Message msg,
String name
)= find_float32(msg, name, 0).
/** Retrieve an Float64 value from the Message.
* @param msg Message where we search the string
* @param name The field name to look for the Float64 value under.
* @param index The index of the Float64 item in its field entry.
* @return 'success(Float64)' if the Float64 value was found, or 'failure' if it wasn't.
*/
public define Maybe(Float)
find_float64
(
Message msg,
String name,
Int32 index
)=
if find_data_type(b_double_t, msg, name, index) is success(mo) then
if mo is b_double(value) then
success(value)
else
failure
else
failure.
public define Maybe(Float)
find_float64
(
Message msg,
String name
)= find_float64(msg, name, 0).
/** Retrieve a Message from the Message.
* @param msg Message where we search the Message
* @param name The field name to look for the Message under.
* @param index The index of the Message item in its field entry.
* @return 'success(Message)' if the Message was found, or 'failure' if it wasn't.
*/
public define Maybe(Message)
find_message
(
Message msg,
String name,
Int32 index
)=
if find_data_type(b_message_t, msg, name, index) is success(mo) then
if mo is b_message(value) then
success(value)
else
failure
else
failure.
public define Maybe(Message)
find_message
(
Message msg,
String name
)= find_message(msg, name, 0).
/********************** TESTING SERVER *************************/
define One
message_received
(
Message msg
)=
print("MESSAGE [" + to_ascii(*msg.what) + "] received\n");
if * msg.what = 323232 then
unique
else
unique
.
define One
message_receiver
(
RWAddr(Int8) conn
) =
if unflatten_message(weaken(conn)) is
{
failure then
print("can't unflatten any message\n");
message_receiver(conn),
success(msg) then
message_received(msg);
message_receiver(conn)
}.
define Server -> (RWAddr(Int8)) -> One
muscle_handler
(
One dummy
) =
(Server server) |-> (RWAddr(Int8) conn) |->
if remote_IP_address_and_port(conn) is (num_peer,_) then
//convert IP address of the client to string
with peer = ip_addr_to_string(num_peer),
print("Accepting connection with "+peer+"\n");
//now managing the AUTHORIZATION state
message_receiver(conn).
public define One
start_muscle_server
(
One dummy
) =
if start_server(0,
44701,
muscle_handler(unique),
(One u) |-> unique) is
{
cannot_create_the_socket then print("Cannot create the listening socket.\n"),
cannot_bind_to_port then print("Cannot bind to port 44701\n"),
cannot_listen_on_port then print("Cannot listen on port 44701\n"),
ok(server) then print("Muscle Server started on port 44701 \n")
}.
global define One
muscle
(
List(String) args
) =
start_muscle_server(unique);
print("Muscle Message Test\n")
.
global define One
muscle_file
(
List(String) args
) =
if (Maybe(RAddr(Int8)))file("c:/muscle/python/test.msg", read) is
{
failure then print("Open file Error\n"),
success(source) then
if unflatten_message(source) is
{
failure then print("File Message ERROR \n"),
success(message) then
print("File Message decoded\n");
print_to_stream(message);
if find_message(message, "message") is
{
failure then print("Can't find message \"message\" \n"),
success(sub_message) then
if find_string(sub_message, "string") is
{
failure then print("can't find string \"string\" \n"),
success(the_string) then print("the string found is [" + the_string + "] \n")
}
}
}
}
.