Commit 2911154322ec856f006c7218c3d054c8b736195d
1 parent
ca8d4c3f
muscle
Showing
1 changed file
with
1067 additions
and
0 deletions
Show diff stats
| 1 | + | |
| 2 | + Standard MUSCLE field type-constants | |
| 3 | + | |
| 4 | +read tools/basis.anubis | |
| 5 | +read network/tools.anubis | |
| 6 | +read system/string.anubis | |
| 7 | +read system/types.anubis | |
| 8 | + | |
| 9 | +public define Int32 _B_ANY_TYPE = 1095653716. // 'ANYT', // wild card | |
| 10 | +public define Int32 _B_BOOL_TYPE = 1112493900. // 'BOOL', | |
| 11 | +public define Int32 _B_DOUBLE_TYPE = 1145195589. // 'DBLE', | |
| 12 | +public define Int32 _B_FLOAT_TYPE = 1179406164. // 'FLOT', | |
| 13 | +public define Int32 _B_INT64_TYPE = 1280069191. // 'LLNG', | |
| 14 | +public define Int32 _B_INT32_TYPE = 1280265799. // 'LONG', | |
| 15 | +public define Int32 _B_INT16_TYPE = 1397248596. // 'SHRT', | |
| 16 | +public define Int32 _B_INT8_TYPE = 1113150533. // 'BYTE', | |
| 17 | +public define Int32 _B_MESSAGE_TYPE = 1297303367. // 'MSGG', | |
| 18 | +public define Int32 _B_POINTER_TYPE = 1347310674. // 'PNTR', | |
| 19 | +public define Int32 _B_POINT_TYPE = 1112559188. // 'BPNT', | |
| 20 | +public define Int32 _B_RECT_TYPE = 1380270932. // 'RECT', | |
| 21 | +public define Int32 _B_STRING_TYPE = 1129534546. // 'CSTR', | |
| 22 | +public define Int32 _B_OBJECT_TYPE = 1330664530. // 'OPTR', // used for flattened objects | |
| 23 | +public define Int32 _B_RAW_TYPE = 1380013908. // 'RAWT', // used for raw byte arrays | |
| 24 | + | |
| 25 | +public define Int32 _CURRENT_PROTOCOL_VERSION = 1347235888. // 'PM00' -- our magic number 0x504D3030 | |
| 26 | + | |
| 27 | +public type Message:... | |
| 28 | + | |
| 29 | +define Maybe(Message) unflatten_message ( RAddr(Int8) stream ). | |
| 30 | + | |
| 31 | + | |
| 32 | +public type B_Point: | |
| 33 | + point(Float32 x, Float32 y). | |
| 34 | + | |
| 35 | +public type B_Rect: | |
| 36 | + rect(B_Point up_left, B_Point down_right), | |
| 37 | + rect(Float32 x, Float32 y, Float32 x1, Float32 y1). | |
| 38 | + | |
| 39 | +type Muscle_object: | |
| 40 | + b_bool(Bool), | |
| 41 | + b_double(Float), | |
| 42 | + b_float(Float32), | |
| 43 | + b_int64(Int64), | |
| 44 | + b_int32(Int32), | |
| 45 | + b_int16(Int16), | |
| 46 | + b_int8(Int8), | |
| 47 | + b_message(Message), | |
| 48 | + b_point(B_Point), | |
| 49 | + b_rect(B_Rect), | |
| 50 | + b_string(String), | |
| 51 | + b_custom(ByteArray). | |
| 52 | + | |
| 53 | +type Muscle_type: | |
| 54 | + b_any_t, | |
| 55 | + b_bool_t, | |
| 56 | + b_double_t, | |
| 57 | + b_float_t, | |
| 58 | + b_int64_t, | |
| 59 | + b_int32_t, | |
| 60 | + b_int16_t, | |
| 61 | + b_int8_t, | |
| 62 | + b_message_t, | |
| 63 | +/* b_pointer,*/ | |
| 64 | + b_point_t, | |
| 65 | + b_rect_t, | |
| 66 | + b_string_t, | |
| 67 | + b_custom_t(Int32). /*, | |
| 68 | + b_object, | |
| 69 | + b_raw*/ | |
| 70 | + | |
| 71 | +type Message_field: | |
| 72 | + message_field | |
| 73 | + ( | |
| 74 | + String name, | |
| 75 | + Muscle_type type_code, | |
| 76 | + Var(List(Muscle_object)) items | |
| 77 | + ). | |
| 78 | + | |
| 79 | +public type Message: | |
| 80 | + message | |
| 81 | + ( | |
| 82 | + Var(Int32) what, | |
| 83 | + Var(Int32) numField, | |
| 84 | + Var(List(Message_field)) fields | |
| 85 | + ). | |
| 86 | + | |
| 87 | +define Maybe(Muscle_type) | |
| 88 | + get_muscle_type | |
| 89 | + ( | |
| 90 | + Int32 type_code | |
| 91 | + )= | |
| 92 | + if type_code = _B_BOOL_TYPE then | |
| 93 | + success(b_bool_t) | |
| 94 | + else if type_code = _B_DOUBLE_TYPE then | |
| 95 | + success(b_double_t) | |
| 96 | + else if type_code = _B_FLOAT_TYPE then | |
| 97 | + success(b_float_t) | |
| 98 | + else if type_code = _B_INT64_TYPE then | |
| 99 | + success(b_int64_t) | |
| 100 | + else if type_code = _B_INT32_TYPE then | |
| 101 | + success(b_int32_t) | |
| 102 | + else if type_code = _B_INT16_TYPE then | |
| 103 | + success(b_int16_t) | |
| 104 | + else if type_code = _B_INT8_TYPE then | |
| 105 | + success(b_int8_t) | |
| 106 | + else if type_code = _B_MESSAGE_TYPE then | |
| 107 | + success(b_message_t) | |
| 108 | + else if type_code = _B_STRING_TYPE then | |
| 109 | + success(b_string_t) | |
| 110 | + else if type_code = _B_POINT_TYPE then | |
| 111 | + success(b_point_t) | |
| 112 | + else if type_code = _B_RECT_TYPE then | |
| 113 | + success(b_rect_t) | |
| 114 | + else | |
| 115 | + print("muscle custom type " + to_hexa(type_code) + "\n"); | |
| 116 | + success(b_custom_t(type_code)). | |
| 117 | + | |
| 118 | + | |
| 119 | +public define One | |
| 120 | + print_to_stream | |
| 121 | + ( | |
| 122 | + Message msg | |
| 123 | + )= | |
| 124 | + print("Message: what = " + *msg.what + ", ASCII = \"" + to_ascii(*msg.what) + "\", Hexa = 0x" + to_hexa(*msg.what) + "\n"). | |
| 125 | + | |
| 126 | + | |
| 127 | +public define One | |
| 128 | + set_what | |
| 129 | + ( | |
| 130 | + Message msg, | |
| 131 | + Int32 new_what | |
| 132 | + ) = | |
| 133 | + msg.what <- new_what. | |
| 134 | + | |
| 135 | + /** Don't allow the user to put data blobs under any of the non-data-blob type codes, | |
| 136 | + * because that would mess up our internal logic which assumes that fields of those | |
| 137 | + * types have the appropriate data for their type | |
| 138 | + * @param typeCode | |
| 139 | + */ | |
| 140 | + | |
| 141 | +public define Bool | |
| 142 | + is_type_code_variable_size | |
| 143 | + ( | |
| 144 | + Int32 type_code | |
| 145 | + ) = | |
| 146 | + if type_code = _B_BOOL_TYPE | | |
| 147 | + type_code = _B_DOUBLE_TYPE | | |
| 148 | + type_code = _B_FLOAT_TYPE | | |
| 149 | + type_code = _B_INT64_TYPE | | |
| 150 | + type_code = _B_INT32_TYPE | | |
| 151 | + type_code = _B_INT16_TYPE | | |
| 152 | + type_code = _B_INT8_TYPE | | |
| 153 | + type_code = _B_POINTER_TYPE | | |
| 154 | + type_code = _B_POINT_TYPE | | |
| 155 | + type_code = _B_RECT_TYPE then | |
| 156 | + true | |
| 157 | + else | |
| 158 | + false. | |
| 159 | + | |
| 160 | +define Maybe(ByteArray) | |
| 161 | + read_data | |
| 162 | + ( | |
| 163 | + RAddr(Int8) stream, | |
| 164 | + Int32 nb_bytes | |
| 165 | + ) = | |
| 166 | + if read_network_bytes(stream, nb_bytes, 10) is | |
| 167 | + { | |
| 168 | + failure then failure, | |
| 169 | + time_out then failure, | |
| 170 | + success(bytes) then success(bytes), | |
| 171 | + truncated(_) then failure | |
| 172 | + }. | |
| 173 | + | |
| 174 | +define Maybe(Int32) | |
| 175 | + read_Int32 | |
| 176 | + ( | |
| 177 | + RAddr(Int8) stream, | |
| 178 | + ) = | |
| 179 | + if read_network_bytes(stream, 4, 10) is | |
| 180 | + { | |
| 181 | + failure then failure, | |
| 182 | + time_out then failure, | |
| 183 | + success(bytes) then | |
| 184 | + //TODO convert to right the platform endian | |
| 185 | + with value = lendian_to_host_Int32(bytes), | |
| 186 | +// print("Int32["+value+"] "+to_ascii(value)+ "\n"); | |
| 187 | + success(value), | |
| 188 | + truncated(_) then failure | |
| 189 | + }. | |
| 190 | + | |
| 191 | +define Maybe(Int64) | |
| 192 | + read_Int64 | |
| 193 | + ( | |
| 194 | + RAddr(Int8) stream, | |
| 195 | + ) = | |
| 196 | + if read_network_bytes(stream, 8, 10) is | |
| 197 | + { | |
| 198 | + failure then failure, | |
| 199 | + time_out then failure, | |
| 200 | + success(bytes) then | |
| 201 | + //TODO convert to right the platform endian | |
| 202 | + with value = lendian_to_host_Int64(bytes), | |
| 203 | +// print("Int32["+value+"] "+to_ascii(value)+ "\n"); | |
| 204 | + success(value), | |
| 205 | + truncated(_) then failure | |
| 206 | + }. | |
| 207 | + | |
| 208 | +define Maybe(Float32) | |
| 209 | + read_float32 | |
| 210 | + ( | |
| 211 | + RAddr(Int8) stream, | |
| 212 | + ) = | |
| 213 | + if read_network_bytes(stream, 4, 10) is | |
| 214 | + { | |
| 215 | + failure then failure, | |
| 216 | + time_out then failure, | |
| 217 | + success(bytes) then | |
| 218 | + //TODO convert to right the platform endian | |
| 219 | + with value = lendian_to_host_Int32(bytes), | |
| 220 | +// print("Int32["+value+"] "+to_ascii(value)+ "\n"); | |
| 221 | + success(float32(value)), | |
| 222 | + truncated(_) then failure | |
| 223 | + }. | |
| 224 | + | |
| 225 | +define Maybe(Float) | |
| 226 | + read_float64 | |
| 227 | + ( | |
| 228 | + RAddr(Int8) stream, | |
| 229 | + ) = | |
| 230 | + if read_network_bytes(stream, 8, 10) is | |
| 231 | + { | |
| 232 | + failure then failure, | |
| 233 | + time_out then failure, | |
| 234 | + success(bytes) then | |
| 235 | + //TODO convert to right the platform endian | |
| 236 | + lendian_to_host_Float64(bytes), | |
| 237 | + truncated(_) then failure | |
| 238 | + }. | |
| 239 | + | |
| 240 | +define Maybe(Int16) | |
| 241 | + read_Int16 | |
| 242 | + ( | |
| 243 | + RAddr(Int8) stream, | |
| 244 | + ) = | |
| 245 | + if read_network_bytes(stream, 2, 10) is | |
| 246 | + { | |
| 247 | + failure then failure, | |
| 248 | + time_out then failure, | |
| 249 | + success(bytes) then | |
| 250 | + with value = lendian_to_host_Int16(bytes), | |
| 251 | + success(value), | |
| 252 | + truncated(_) then failure | |
| 253 | + }. | |
| 254 | + | |
| 255 | +define Maybe(Muscle_object) | |
| 256 | + read_one_field_item | |
| 257 | + ( | |
| 258 | + RAddr(Int8) source, | |
| 259 | + Muscle_type b_type | |
| 260 | + )= | |
| 261 | + if b_type is | |
| 262 | + { | |
| 263 | + b_any_t then | |
| 264 | + failure, | |
| 265 | + //read one B_BOOL field | |
| 266 | + b_bool_t then | |
| 267 | + if read_data(source, 1) is | |
| 268 | + { | |
| 269 | + failure then failure, | |
| 270 | + success(ba_bool) then | |
| 271 | + with bool8 = force_nth(0, ba_bool), | |
| 272 | + print("B_BOOL = " + bool8 + "\n"); | |
| 273 | + with real_bool = if bool8 = 0 then false else true, | |
| 274 | + success(b_bool(real_bool)) //convert into Anubis Bool | |
| 275 | + } | |
| 276 | + //read one B_DOUBLE field | |
| 277 | + b_double_t then | |
| 278 | + if read_float64(source) is | |
| 279 | + { | |
| 280 | + failure then failure, | |
| 281 | + success(value) then | |
| 282 | + print("B_DOUBLE = " + float_to_string(value,5) + "\n"); | |
| 283 | + success(b_double(value)) //convert into Anubis Int32 | |
| 284 | + } | |
| 285 | + //read one B_FLOAT field | |
| 286 | + b_float_t then | |
| 287 | + if read_float32(source) is | |
| 288 | + { | |
| 289 | + failure then failure, | |
| 290 | + success(value) then | |
| 291 | + print("B_FLOAT = Float32 not yet available\n"); | |
| 292 | + success(b_float(value)) //convert into Anubis Int32 | |
| 293 | + } | |
| 294 | + //read one B_INT64 field | |
| 295 | + b_int64_t then | |
| 296 | + if read_Int64(source) is | |
| 297 | + { | |
| 298 | + failure then failure, | |
| 299 | + success(value) then | |
| 300 | + print("B_INT64 = not yet printable\n"); | |
| 301 | + success(b_int64(value)) //convert into Anubis Int64 | |
| 302 | + } | |
| 303 | + //read one B_INT32 field | |
| 304 | + b_int32_t then | |
| 305 | + if read_Int32(source) is | |
| 306 | + { | |
| 307 | + failure then failure, | |
| 308 | + success(value) then | |
| 309 | + print("B_INT32 = " + value + "\n"); | |
| 310 | + success(b_int32(value)) //convert into Anubis Int32 | |
| 311 | + } | |
| 312 | + //read one B_INT16 field | |
| 313 | + b_int16_t then | |
| 314 | + if read_Int16(source) is | |
| 315 | + { | |
| 316 | + failure then failure, | |
| 317 | + success(value) then | |
| 318 | + print("B_INT16 = " + value + "\n"); | |
| 319 | + success(b_int16(value)) //convert into Anubis Int32 | |
| 320 | + } | |
| 321 | + | |
| 322 | + //read one B_INT8 field | |
| 323 | + b_int8_t then | |
| 324 | + if read_data(source, 1) is | |
| 325 | + { | |
| 326 | + failure then failure, | |
| 327 | + success(ba_value) then | |
| 328 | + with value = force_nth(0,ba_value), | |
| 329 | + print("B_INT8 = " + value + "\n"); | |
| 330 | + success(b_int8(value)) //convert into Anubis Int32 | |
| 331 | + } | |
| 332 | + | |
| 333 | + //read one B_MESSAGE field | |
| 334 | + b_message_t then | |
| 335 | + if unflatten_message(source) is | |
| 336 | + { | |
| 337 | + failure then failure, | |
| 338 | + success(message) then | |
| 339 | + success(b_message(message)) | |
| 340 | + } | |
| 341 | + | |
| 342 | + //read one B_POINT field | |
| 343 | + b_point_t then | |
| 344 | + if read_float32(source) is | |
| 345 | + { | |
| 346 | + failure then failure, | |
| 347 | + success(x) then | |
| 348 | + if read_float32(source) is | |
| 349 | + { | |
| 350 | + failure then failure, | |
| 351 | + success(y) then | |
| 352 | + print("B_POINT = Float32 not yet available\n"); | |
| 353 | + success(b_point(point(x, y))) //return the point value | |
| 354 | + } | |
| 355 | + } | |
| 356 | + | |
| 357 | + //read one B_RECT field | |
| 358 | + b_rect_t then | |
| 359 | + if read_float32(source) is | |
| 360 | + { | |
| 361 | + failure then failure, | |
| 362 | + success(x) then | |
| 363 | + if read_float32(source) is | |
| 364 | + { | |
| 365 | + failure then failure, | |
| 366 | + success(y) then | |
| 367 | + if read_float32(source) is | |
| 368 | + { | |
| 369 | + failure then failure, | |
| 370 | + success(w) then | |
| 371 | + if read_float32(source) is | |
| 372 | + { | |
| 373 | + failure then failure, | |
| 374 | + success(h) then | |
| 375 | + print("B_RECT = Float32 not yet available\n"); | |
| 376 | + success(b_rect(rect(point(x, y), point(w,h)))) //return the point value | |
| 377 | + } | |
| 378 | + } | |
| 379 | + } | |
| 380 | + } | |
| 381 | + | |
| 382 | + //read one B_STRING field | |
| 383 | + b_string_t then | |
| 384 | + if read_Int32(source) is | |
| 385 | + { | |
| 386 | + failure then failure, | |
| 387 | + success(size) then //get his size | |
| 388 | + if read_data(source, size) is //and read it | |
| 389 | + { | |
| 390 | + failure then failure, | |
| 391 | + success(ba_string) then | |
| 392 | + print("B_STRING = " + to_string(ba_string) + "\n"); | |
| 393 | + success(b_string(to_string(ba_string))) //convert into Anubis String | |
| 394 | + } | |
| 395 | + } | |
| 396 | + //read one CUSTOM field | |
| 397 | + b_custom_t(_) then | |
| 398 | + if read_Int32(source) is | |
| 399 | + { | |
| 400 | + failure then failure, | |
| 401 | + success(size) then //get his size | |
| 402 | + if read_data(source, size) is //and read it | |
| 403 | + { | |
| 404 | + failure then failure, | |
| 405 | + success(ba_custom) then | |
| 406 | + success(b_custom(ba_custom)) //convert into Anubis String | |
| 407 | + } | |
| 408 | + } | |
| 409 | + } | |
| 410 | + . | |
| 411 | + | |
| 412 | +define Maybe(List(Muscle_object)) | |
| 413 | + read_field_items | |
| 414 | + ( | |
| 415 | + RAddr(Int8) source, | |
| 416 | + Muscle_type b_type, | |
| 417 | + Int32 count | |
| 418 | + )= | |
| 419 | + if count =< 0 then | |
| 420 | + success([ ]) | |
| 421 | + else | |
| 422 | + if read_one_field_item(source, b_type) is | |
| 423 | + { | |
| 424 | + failure then failure, | |
| 425 | + success(first_one) then | |
| 426 | + if read_field_items(source, b_type, count-1) is | |
| 427 | + { | |
| 428 | + failure then failure, | |
| 429 | + success(others) then success([first_one . others]) | |
| 430 | + } | |
| 431 | + }. | |
| 432 | + | |
| 433 | +define Maybe(List(Muscle_object)) | |
| 434 | + read_message_field_items | |
| 435 | + ( | |
| 436 | + RAddr(Int8) source, | |
| 437 | + Int32 amount | |
| 438 | + )= | |
| 439 | + if amount =< 0 then | |
| 440 | + success([ ]) | |
| 441 | + else | |
| 442 | + if read_Int32(source) is | |
| 443 | + { | |
| 444 | + failure then failure, | |
| 445 | + success(current_message_size) then | |
| 446 | + //now we have the size of current message | |
| 447 | + if read_one_field_item(source, b_message_t) is | |
| 448 | + { | |
| 449 | + failure then failure, | |
| 450 | + success(first_one) then | |
| 451 | + if read_message_field_items(source, amount - (current_message_size + 4)) is | |
| 452 | + { | |
| 453 | + failure then failure, | |
| 454 | + success(others) then success([first_one . others]) | |
| 455 | + } | |
| 456 | + } | |
| 457 | + }. | |
| 458 | + | |
| 459 | +define Maybe(Message_field) | |
| 460 | + read_one_field | |
| 461 | + ( | |
| 462 | + RAddr(Int8) source | |
| 463 | + )= | |
| 464 | + //read length of the field name | |
| 465 | + if read_Int32(source) is | |
| 466 | + { | |
| 467 | + failure then failure, | |
| 468 | + success(name_length) then | |
| 469 | + if read_data(source, name_length) is | |
| 470 | + { | |
| 471 | + failure then failure | |
| 472 | + success(ba_name) then | |
| 473 | + with field_name = to_string(ba_name), | |
| 474 | + print("\nfield_name =\"" + field_name + "\"\n"); | |
| 475 | + if read_Int32(source) is | |
| 476 | + { | |
| 477 | + failure then failure, | |
| 478 | + success(int32_type_code) then | |
| 479 | + if read_Int32(source) is | |
| 480 | + { | |
| 481 | + failure then failure | |
| 482 | + success(data_length) then | |
| 483 | + if get_muscle_type(int32_type_code) is | |
| 484 | + { | |
| 485 | + failure then failure, | |
| 486 | + success(type_code) then | |
| 487 | + if type_code is | |
| 488 | + { | |
| 489 | + b_any_t then failure, | |
| 490 | + // read BOOL | |
| 491 | + b_bool_t then | |
| 492 | + if read_field_items(source, type_code, data_length) is | |
| 493 | + { | |
| 494 | + failure then failure, | |
| 495 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 496 | + } | |
| 497 | + // read DOUBLE | |
| 498 | + b_double_t then | |
| 499 | + if read_field_items(source, type_code, data_length >> 3) is | |
| 500 | + { | |
| 501 | + failure then failure, | |
| 502 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 503 | + } | |
| 504 | + // read FLOAT | |
| 505 | + b_float_t then | |
| 506 | + if read_field_items(source, type_code, data_length >> 2) is | |
| 507 | + { | |
| 508 | + failure then failure, | |
| 509 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 510 | + } | |
| 511 | + // read INT64 | |
| 512 | + b_int64_t then | |
| 513 | + if read_field_items(source, type_code, data_length >> 3) is | |
| 514 | + { | |
| 515 | + failure then failure, | |
| 516 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 517 | + } | |
| 518 | + // read INT32 | |
| 519 | + b_int32_t then | |
| 520 | + if read_field_items(source, type_code, data_length >> 2) is | |
| 521 | + { | |
| 522 | + failure then failure, | |
| 523 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 524 | + } | |
| 525 | + // read INT16 | |
| 526 | + b_int16_t then | |
| 527 | + if read_field_items(source, type_code, data_length >> 1) is | |
| 528 | + { | |
| 529 | + failure then failure, | |
| 530 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 531 | + } | |
| 532 | + // read INT8 | |
| 533 | + b_int8_t then | |
| 534 | + if read_field_items(source, type_code, data_length) is | |
| 535 | + { | |
| 536 | + failure then failure, | |
| 537 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 538 | + } | |
| 539 | + | |
| 540 | + // read MESSAGE | |
| 541 | + b_message_t then | |
| 542 | + if read_message_field_items(source, data_length) is | |
| 543 | + { | |
| 544 | + failure then failure, | |
| 545 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 546 | + } | |
| 547 | + | |
| 548 | + // read POINT | |
| 549 | + b_point_t then | |
| 550 | + if read_field_items(source, type_code, data_length >> 3) is | |
| 551 | + { | |
| 552 | + failure then failure, | |
| 553 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 554 | + } | |
| 555 | + // read RECT | |
| 556 | + b_rect_t then | |
| 557 | + if read_field_items(source, type_code, data_length >> 4) is | |
| 558 | + { | |
| 559 | + failure then failure, | |
| 560 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 561 | + } | |
| 562 | + | |
| 563 | + // read STRING | |
| 564 | + b_string_t then | |
| 565 | + if read_Int32(source) is //get numbers items in that field | |
| 566 | + { | |
| 567 | + failure then failure, | |
| 568 | + success(nb_items) then | |
| 569 | + print("nb_items" + nb_items + "\n"); | |
| 570 | + if read_field_items(source, type_code, nb_items) is | |
| 571 | + { | |
| 572 | + failure then failure, | |
| 573 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 574 | + } | |
| 575 | + } | |
| 576 | + // read CUSTOM | |
| 577 | + b_custom_t(code) then | |
| 578 | + if read_Int32(source) is //get numbers items in that field | |
| 579 | + { | |
| 580 | + failure then failure, | |
| 581 | + success(nb_items) then | |
| 582 | + print("nb_items custom " + nb_items + "\n"); | |
| 583 | + if read_field_items(source, type_code, nb_items) is | |
| 584 | + { | |
| 585 | + failure then failure, | |
| 586 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 587 | + } | |
| 588 | + } | |
| 589 | + } | |
| 590 | + } | |
| 591 | + } | |
| 592 | + } | |
| 593 | + } | |
| 594 | + }. | |
| 595 | + | |
| 596 | +define Maybe(List(Message_field)) /* returns 'failure' if it cannot the required number of elements */ | |
| 597 | + read_fields | |
| 598 | + ( | |
| 599 | + RAddr(Int8) source, | |
| 600 | + Int32 n // number of elements to read | |
| 601 | + ) = | |
| 602 | + if n =< 0 then | |
| 603 | + success([ ]) | |
| 604 | + else | |
| 605 | + if read_one_field(source) is | |
| 606 | + { | |
| 607 | + failure then failure, | |
| 608 | + success(first_one) then | |
| 609 | + if read_fields(source, n-1) is | |
| 610 | + { | |
| 611 | + failure then failure, | |
| 612 | + success(others) then success([first_one . others]) | |
| 613 | + } | |
| 614 | + }. | |
| 615 | + | |
| 616 | + | |
| 617 | + /* Format: 0. Protocol revision number (4 bytes, always set to CURRENT_PROTOCOL_VERSION) */ | |
| 618 | + /* 1. 'what' code (4 bytes) */ | |
| 619 | + /* 2. Number of entries (4 bytes) */ | |
| 620 | + | |
| 621 | + /* 3. Entry name length (4 bytes) */ | |
| 622 | + /* 4. Entry name string (flattened String) */ | |
| 623 | + /* 5. Entry type code (4 bytes) */ | |
| 624 | + /* 6. Entry data length (4 bytes) */ | |
| 625 | + /* 7. Entry data (n bytes) */ | |
| 626 | + /* 8. loop to 3 as necessary */ | |
| 627 | + | |
| 628 | +define Maybe(Message) | |
| 629 | + unflatten_message | |
| 630 | + ( | |
| 631 | + RAddr(Int8) stream | |
| 632 | + )= | |
| 633 | + if read_Int32(stream) is | |
| 634 | + { | |
| 635 | + failure then failure, | |
| 636 | + success(protocolVersion) then | |
| 637 | + if protocolVersion = _CURRENT_PROTOCOL_VERSION then | |
| 638 | + print("CURRENT_PROTOCOL_VERSION OK\n"); | |
| 639 | + //get the 'what' code | |
| 640 | + if read_Int32(stream) is | |
| 641 | + { | |
| 642 | + failure then failure, | |
| 643 | + success(what_code) then | |
| 644 | + print("what code = [" + to_ascii(what_code) + "] "+ what_code + "\n"); | |
| 645 | + //get the number of entries at this level | |
| 646 | + if read_Int32(stream) is | |
| 647 | + { | |
| 648 | + failure then failure, | |
| 649 | + success(nb_entries) then | |
| 650 | + print("Nb Entries = " + nb_entries + "\n"); | |
| 651 | + //get all fields of this level | |
| 652 | + if read_fields(stream, nb_entries) is | |
| 653 | + { | |
| 654 | + failure then failure, | |
| 655 | + success(fields) then success(message(var(what_code), var(nb_entries), var(fields))) | |
| 656 | + } | |
| 657 | + } | |
| 658 | + } | |
| 659 | + else //This is not the same level version of message protocol | |
| 660 | + failure | |
| 661 | + } | |
| 662 | + . | |
| 663 | +/********************** Find collection ********************/ | |
| 664 | + | |
| 665 | +define Maybe(Message_field) | |
| 666 | + find_field | |
| 667 | + ( | |
| 668 | + List(Message_field) msg_fields, | |
| 669 | + Muscle_type search_type_code, | |
| 670 | + String search_name | |
| 671 | + )= | |
| 672 | + if msg_fields is | |
| 673 | + { | |
| 674 | + [] then failure, | |
| 675 | + [ h . t ] then | |
| 676 | + //test if the head of list contain the field with same name and type | |
| 677 | + if search_type_code = b_any_t then //With ANY_TYPE we don't care about type_code | |
| 678 | + if h.name = search_name then | |
| 679 | + success(h) | |
| 680 | + else | |
| 681 | + find_field(t, search_type_code, search_name) | |
| 682 | + else //we search for exact type and name | |
| 683 | + if (h.name = search_name) & (h.type_code = search_type_code) then | |
| 684 | + success(h) | |
| 685 | + else | |
| 686 | + find_field(t, search_type_code, search_name) | |
| 687 | + }. | |
| 688 | + | |
| 689 | +define Maybe(Muscle_object) | |
| 690 | + find_data_type | |
| 691 | + ( | |
| 692 | + List(Message_field) fields, | |
| 693 | + Muscle_type type_code, | |
| 694 | + String name, | |
| 695 | + Int32 index | |
| 696 | + )= | |
| 697 | + if find_field(fields, type_code, name) is | |
| 698 | + { | |
| 699 | + failure then | |
| 700 | + failure, //we don't find any fields matching with query | |
| 701 | + success(field) then | |
| 702 | + nth(index, *field.items) | |
| 703 | + }. | |
| 704 | + | |
| 705 | + | |
| 706 | +define Maybe(Muscle_object) | |
| 707 | + find_data_type | |
| 708 | + ( | |
| 709 | + Muscle_type type_code, | |
| 710 | + Message msg, | |
| 711 | + String name, | |
| 712 | + Int32 index | |
| 713 | + )= | |
| 714 | + find_data_type(*msg.fields, type_code, name, index). | |
| 715 | + | |
| 716 | + /** Retrieve a string value from the Message. | |
| 717 | + * @param msg Message where we search the string | |
| 718 | + * @param name The field name to look for the string value under. | |
| 719 | + * @param index The index of the string item in its field entry. | |
| 720 | + * @return 'success(String)' if the string value was found, or 'failure' if it wasn't. | |
| 721 | + */ | |
| 722 | +public define Maybe(String) | |
| 723 | + find_string | |
| 724 | + ( | |
| 725 | + Message msg, | |
| 726 | + String name, | |
| 727 | + Int32 index | |
| 728 | + )= | |
| 729 | + if find_data_type(b_string_t, msg, name, index) is success(mo) then | |
| 730 | + if mo is b_string(value) then | |
| 731 | + success(value) | |
| 732 | + else | |
| 733 | + failure | |
| 734 | + else | |
| 735 | + failure. | |
| 736 | + | |
| 737 | + /** As above, only (index) isn't specified. It is assumed to be zero. */ | |
| 738 | +public define Maybe(String) | |
| 739 | + find_string | |
| 740 | + ( | |
| 741 | + Message msg, | |
| 742 | + String name | |
| 743 | + )= find_string(msg, name, 0). | |
| 744 | + | |
| 745 | + /** Retrieve an int8 value from the Message. | |
| 746 | + * @param msg Message where we search the string | |
| 747 | + * @param name The field name to look for the int8 value under. | |
| 748 | + * @param index The index of the int8 item in its field entry. | |
| 749 | + * @return 'success(Int8)' if the int8 value was found, or 'failure' if it wasn't. | |
| 750 | + */ | |
| 751 | +public define Maybe(Int8) | |
| 752 | + find_int8 | |
| 753 | + ( | |
| 754 | + Message msg, | |
| 755 | + String name, | |
| 756 | + Int32 index | |
| 757 | + )= | |
| 758 | + if find_data_type(b_int8_t, msg, name, index) is success(mo) then | |
| 759 | + if mo is b_int8(value) then | |
| 760 | + success(value) | |
| 761 | + else | |
| 762 | + failure | |
| 763 | + else | |
| 764 | + failure. | |
| 765 | + | |
| 766 | + /** As above, only (index) isn't specified. It is assumed to be zero. */ | |
| 767 | +public define Maybe(Int8) | |
| 768 | + find_int8 | |
| 769 | + ( | |
| 770 | + Message msg, | |
| 771 | + String name | |
| 772 | + )= find_int8(msg, name, 0). | |
| 773 | + | |
| 774 | + /** Retrieve an int16 value from the Message. | |
| 775 | + * @param msg Message where we search the string | |
| 776 | + * @param name The field name to look for the int16 value under. | |
| 777 | + * @param index The index of the int16 item in its field entry. | |
| 778 | + * @return 'success(Int16)' if the int16 value was found, or 'failure' if it wasn't. | |
| 779 | + */ | |
| 780 | +public define Maybe(Int16) | |
| 781 | + find_int16 | |
| 782 | + ( | |
| 783 | + Message msg, | |
| 784 | + String name, | |
| 785 | + Int32 index | |
| 786 | + )= | |
| 787 | + if find_data_type(b_int16_t, msg, name, index) is success(mo) then | |
| 788 | + if mo is b_int16(value) then | |
| 789 | + success(value) | |
| 790 | + else | |
| 791 | + failure | |
| 792 | + else | |
| 793 | + failure. | |
| 794 | + | |
| 795 | +public define Maybe(Int16) | |
| 796 | + find_int16 | |
| 797 | + ( | |
| 798 | + Message msg, | |
| 799 | + String name | |
| 800 | + )= find_int16(msg, name, 0). | |
| 801 | + | |
| 802 | + /** Retrieve an int32 value from the Message. | |
| 803 | + * @param msg Message where we search the string | |
| 804 | + * @param name The field name to look for the int32 value under. | |
| 805 | + * @param index The index of the int32 item in its field entry. | |
| 806 | + * @return 'success(Int32)' if the int32 value was found, or 'failure' if it wasn't. | |
| 807 | + */ | |
| 808 | +public define Maybe(Int32) | |
| 809 | + find_int32 | |
| 810 | + ( | |
| 811 | + Message msg, | |
| 812 | + String name, | |
| 813 | + Int32 index | |
| 814 | + )= | |
| 815 | + if find_data_type(b_int32_t, msg, name, index) is success(mo) then | |
| 816 | + if mo is b_int32(value) then | |
| 817 | + success(value) | |
| 818 | + else | |
| 819 | + failure | |
| 820 | + else | |
| 821 | + failure. | |
| 822 | + | |
| 823 | +public define Maybe(Int32) | |
| 824 | + find_int32 | |
| 825 | + ( | |
| 826 | + Message msg, | |
| 827 | + String name | |
| 828 | + )= find_int32(msg, name, 0). | |
| 829 | + | |
| 830 | + /** Retrieve an int64 value from the Message. | |
| 831 | + * @param msg Message where we search the string | |
| 832 | + * @param name The field name to look for the int64 value under. | |
| 833 | + * @param index The index of the int64 item in its field entry. | |
| 834 | + * @return 'success(Int64)' if the int16 value was found, or 'failure' if it wasn't. | |
| 835 | + */ | |
| 836 | +public define Maybe(Int64) | |
| 837 | + find_int64 | |
| 838 | + ( | |
| 839 | + Message msg, | |
| 840 | + String name, | |
| 841 | + Int32 index | |
| 842 | + )= | |
| 843 | + if find_data_type(b_int64_t, msg, name, index) is success(mo) then | |
| 844 | + if mo is b_int64(value) then | |
| 845 | + success(value) | |
| 846 | + else | |
| 847 | + failure | |
| 848 | + else | |
| 849 | + failure. | |
| 850 | + | |
| 851 | +public define Maybe(Int64) | |
| 852 | + find_int64 | |
| 853 | + ( | |
| 854 | + Message msg, | |
| 855 | + String name | |
| 856 | + )= find_int64(msg, name, 0). | |
| 857 | + | |
| 858 | + /** Retrieve an Bool value from the Message. | |
| 859 | + * @param msg Message where we search the string | |
| 860 | + * @param name The field name to look for the Bool value under. | |
| 861 | + * @param index The index of the Bool item in its field entry. | |
| 862 | + * @return 'success(Bool)' if the Bool value was found, or 'failure' if it wasn't. | |
| 863 | + */ | |
| 864 | +public define Maybe(Bool) | |
| 865 | + find_bool | |
| 866 | + ( | |
| 867 | + Message msg, | |
| 868 | + String name, | |
| 869 | + Int32 index | |
| 870 | + )= | |
| 871 | + if find_data_type(b_bool_t, msg, name, index) is success(mo) then | |
| 872 | + if mo is b_bool(value) then | |
| 873 | + success(value) | |
| 874 | + else | |
| 875 | + failure | |
| 876 | + else | |
| 877 | + failure. | |
| 878 | + | |
| 879 | +public define Maybe(Bool) | |
| 880 | + find_bool | |
| 881 | + ( | |
| 882 | + Message msg, | |
| 883 | + String name | |
| 884 | + )= find_bool(msg, name, 0). | |
| 885 | + | |
| 886 | + /** Retrieve an Float32 value from the Message. | |
| 887 | + * @param msg Message where we search the string | |
| 888 | + * @param name The field name to look for the Float32 value under. | |
| 889 | + * @param index The index of the Float32 item in its field entry. | |
| 890 | + * @return 'success(Float32)' if the Float32 value was found, or 'failure' if it wasn't. | |
| 891 | + */ | |
| 892 | +public define Maybe(Float32) | |
| 893 | + find_float32 | |
| 894 | + ( | |
| 895 | + Message msg, | |
| 896 | + String name, | |
| 897 | + Int32 index | |
| 898 | + )= | |
| 899 | + if find_data_type(b_float_t, msg, name, index) is success(mo) then | |
| 900 | + if mo is b_float(value) then | |
| 901 | + success(value) | |
| 902 | + else | |
| 903 | + failure | |
| 904 | + else | |
| 905 | + failure. | |
| 906 | + | |
| 907 | +public define Maybe(Float32) | |
| 908 | + find_float32 | |
| 909 | + ( | |
| 910 | + Message msg, | |
| 911 | + String name | |
| 912 | + )= find_float32(msg, name, 0). | |
| 913 | + | |
| 914 | + /** Retrieve an Float64 value from the Message. | |
| 915 | + * @param msg Message where we search the string | |
| 916 | + * @param name The field name to look for the Float64 value under. | |
| 917 | + * @param index The index of the Float64 item in its field entry. | |
| 918 | + * @return 'success(Float64)' if the Float64 value was found, or 'failure' if it wasn't. | |
| 919 | + */ | |
| 920 | +public define Maybe(Float) | |
| 921 | + find_float64 | |
| 922 | + ( | |
| 923 | + Message msg, | |
| 924 | + String name, | |
| 925 | + Int32 index | |
| 926 | + )= | |
| 927 | + if find_data_type(b_double_t, msg, name, index) is success(mo) then | |
| 928 | + if mo is b_double(value) then | |
| 929 | + success(value) | |
| 930 | + else | |
| 931 | + failure | |
| 932 | + else | |
| 933 | + failure. | |
| 934 | + | |
| 935 | +public define Maybe(Float) | |
| 936 | + find_float64 | |
| 937 | + ( | |
| 938 | + Message msg, | |
| 939 | + String name | |
| 940 | + )= find_float64(msg, name, 0). | |
| 941 | + | |
| 942 | + /** Retrieve a Message from the Message. | |
| 943 | + * @param msg Message where we search the Message | |
| 944 | + * @param name The field name to look for the Message under. | |
| 945 | + * @param index The index of the Message item in its field entry. | |
| 946 | + * @return 'success(Message)' if the Message was found, or 'failure' if it wasn't. | |
| 947 | + */ | |
| 948 | +public define Maybe(Message) | |
| 949 | + find_message | |
| 950 | + ( | |
| 951 | + Message msg, | |
| 952 | + String name, | |
| 953 | + Int32 index | |
| 954 | + )= | |
| 955 | + if find_data_type(b_message_t, msg, name, index) is success(mo) then | |
| 956 | + if mo is b_message(value) then | |
| 957 | + success(value) | |
| 958 | + else | |
| 959 | + failure | |
| 960 | + else | |
| 961 | + failure. | |
| 962 | + | |
| 963 | +public define Maybe(Message) | |
| 964 | + find_message | |
| 965 | + ( | |
| 966 | + Message msg, | |
| 967 | + String name | |
| 968 | + )= find_message(msg, name, 0). | |
| 969 | + | |
| 970 | +/********************** TESTING SERVER *************************/ | |
| 971 | + | |
| 972 | +define One | |
| 973 | + message_received | |
| 974 | + ( | |
| 975 | + Message msg | |
| 976 | + )= | |
| 977 | + print("MESSAGE [" + to_ascii(*msg.what) + "] received\n"); | |
| 978 | + if * msg.what = 323232 then | |
| 979 | + unique | |
| 980 | + else | |
| 981 | + unique | |
| 982 | + . | |
| 983 | + | |
| 984 | +define One | |
| 985 | + message_receiver | |
| 986 | + ( | |
| 987 | + RWAddr(Int8) conn | |
| 988 | + ) = | |
| 989 | + | |
| 990 | + if unflatten_message(weaken(conn)) is | |
| 991 | + { | |
| 992 | + failure then | |
| 993 | + print("can't unflatten any message\n"); | |
| 994 | + message_receiver(conn), | |
| 995 | + success(msg) then | |
| 996 | + message_received(msg); | |
| 997 | + message_receiver(conn) | |
| 998 | + }. | |
| 999 | + | |
| 1000 | +define Server -> (RWAddr(Int8)) -> One | |
| 1001 | + muscle_handler | |
| 1002 | + ( | |
| 1003 | + One dummy | |
| 1004 | + ) = | |
| 1005 | + (Server server) |-> (RWAddr(Int8) conn) |-> | |
| 1006 | + if remote_IP_address_and_port(conn) is (num_peer,_) then | |
| 1007 | + //convert IP address of the client to string | |
| 1008 | + with peer = ip_addr_to_string(num_peer), | |
| 1009 | + print("Accepting connection with "+peer+"\n"); | |
| 1010 | + | |
| 1011 | + //now managing the AUTHORIZATION state | |
| 1012 | + message_receiver(conn). | |
| 1013 | + | |
| 1014 | +public define One | |
| 1015 | + start_muscle_server | |
| 1016 | + ( | |
| 1017 | + One dummy | |
| 1018 | + ) = | |
| 1019 | + if start_server(0, | |
| 1020 | + 44701, | |
| 1021 | + muscle_handler(unique), | |
| 1022 | + (One u) |-> unique) is | |
| 1023 | + { | |
| 1024 | + cannot_create_the_socket then print("Cannot create the listening socket.\n"), | |
| 1025 | + cannot_bind_to_port then print("Cannot bind to port 44701\n"), | |
| 1026 | + cannot_listen_on_port then print("Cannot listen on port 44701\n"), | |
| 1027 | + ok(server) then print("Muscle Server started on port 44701 \n") | |
| 1028 | + }. | |
| 1029 | + | |
| 1030 | +global define One | |
| 1031 | + muscle | |
| 1032 | + ( | |
| 1033 | + List(String) args | |
| 1034 | + ) = | |
| 1035 | + start_muscle_server(unique); | |
| 1036 | + print("Muscle Message Test\n") | |
| 1037 | + . | |
| 1038 | + | |
| 1039 | +global define One | |
| 1040 | + muscle_file | |
| 1041 | + ( | |
| 1042 | + List(String) args | |
| 1043 | + ) = | |
| 1044 | + if (Maybe(RAddr(Int8)))file("c:/muscle/python/test.msg", read) is | |
| 1045 | + { | |
| 1046 | + failure then print("Open file Error\n"), | |
| 1047 | + success(source) then | |
| 1048 | + if unflatten_message(source) is | |
| 1049 | + { | |
| 1050 | + failure then print("File Message ERROR \n"), | |
| 1051 | + success(message) then | |
| 1052 | + print("File Message decoded\n"); | |
| 1053 | + print_to_stream(message); | |
| 1054 | + if find_message(message, "message") is | |
| 1055 | + { | |
| 1056 | + failure then print("Can't find message \"message\" \n"), | |
| 1057 | + success(sub_message) then | |
| 1058 | + if find_string(sub_message, "string") is | |
| 1059 | + { | |
| 1060 | + failure then print("can't find string \"string\" \n"), | |
| 1061 | + success(the_string) then print("the string found is [" + the_string + "] \n") | |
| 1062 | + } | |
| 1063 | + } | |
| 1064 | + } | |
| 1065 | + } | |
| 1066 | + . | |
| 1067 | + | ... | ... |