muscle.anubis 49 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 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691

 Standard MUSCLE field type-constants

read tools/basis.anubis
read network/tools.anubis
read system/string.anubis
read system/types.anubis
read system/convert.anubis
read system/data_io.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 define Int32 _MUSCLE_MESSAGE_ENCODING_DEFAULT = 1164862256. //header for network transfert

public type Message:...

public define Maybe(Message) unflatten_message ( Data_IO io ).
public define Maybe(ByteArray) flatten_message ( Message msg ).

  
public type Message_Send_Result:
  flatten_error,
  network_error,
  ok.

  
type Muscle_object:
  b_bool(Bool),
  b_double(Float),
  b_float(Float32),
  b_int64(Int64),
  b_int32(Int32),
  b_int16(Int16),
  b_int8(Word8),
  b_message(Message),
  b_pointer(Int32),
  b_point(B_Point),
  b_rect(B_Rect),
  b_string(String),
  b_custom(ByteArray),
  b_raw(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_t,
  b_point_t,
  b_rect_t,
  b_string_t,
  b_custom_t(Int32), /*,
  b_object,*/
  b_raw_t.
  
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
  ).
       
       
  /** Create an empty message with only his what code */
public define Message 
  message
  (
    Int32 what
  )=
  message(var(what), var(0), var([ ])).

define Int32 
  get_Int32_type_code
  (
    Muscle_type type_code
  )=
  if type_code is
  {
     b_any_t      then  _B_ANY_TYPE,
     b_bool_t     then  _B_BOOL_TYPE,
     b_double_t   then  _B_DOUBLE_TYPE,
     b_float_t    then  _B_FLOAT_TYPE,
     b_int64_t    then  _B_INT64_TYPE,
     b_int32_t    then  _B_INT32_TYPE,
     b_int16_t    then  _B_INT16_TYPE,
     b_int8_t     then  _B_INT8_TYPE,
     b_message_t  then  _B_MESSAGE_TYPE,
     b_pointer_t  then  _B_POINTER_TYPE,
     b_point_t    then  _B_POINT_TYPE,
     b_rect_t     then  _B_RECT_TYPE,
     b_string_t   then  _B_STRING_TYPE,
     b_custom_t(v)then  v,  
     b_raw_t      then  _B_RAW_TYPE,
  }
  .
  
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_POINTER_TYPE then
    success(b_message_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 if type_code = _B_STRING_TYPE then
    success(b_string_t)
  else if type_code = _B_RAW_TYPE then
    success(b_raw_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" +
        "entry count=" + *msg.numField +"\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
  (
    Muscle_type type_code
  ) =
  if type_code = b_bool_t    |
     type_code = b_double_t  |
     type_code = b_float_t   |
     type_code = b_int64_t   |
     type_code = b_int32_t   |
     type_code = b_int16_t   |
     type_code = b_int8_t    |
     type_code = b_pointer_t |
     type_code = b_point_t   |
     type_code = b_rect_t then 
    true
  else
    false.

define Maybe(ByteArray)
  read_data
  (
    Data_IO io,
    Int32   nb_bytes
  ) =
  if read_bytes(io, nb_bytes) is
  {
    failure        then failure,
    time_out       then failure, 
    success(bytes) then success(bytes),
    truncated(_)   then failure
  }.
  
define Maybe(Int32)
  read_Int32
  (
    Data_IO  io,
  ) =
  if read_bytes(io, 4) 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
  (
    Data_IO  io,
  ) =
  if read_bytes(io, 8) 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
  (
    Data_IO  io,
  ) =
  if read_bytes(io, 4) 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
  (
    Data_IO  io,
  ) =
  if read_bytes(io, 8) 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
  (
    Data_IO  io,
  ) =
  if read_bytes(io, 2) 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
  (
    Data_IO     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_POINTER field. Remark, we read it for compatibility, but itsn't useful in
    //Anubis Language, because pointer doesn't exist.
    b_pointer_t     then
    if read_Int32(source) is
    {
      failure           then failure,
      success(value)  then 
        //print("B_POINTER = " + value + "\n");
        success(b_pointer(value)) //convert into Anubis Int32
    }

  //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))
        }
     }
    //read one RAW field
    b_raw_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_raw)  then 
            success(b_raw(ba_raw))
        }
     }
  }
  .

define Maybe(List(Muscle_object))
  read_field_items
  (
    Data_IO     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
  (
    Data_IO 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
  (
    Data_IO    source
  )=
  //read length of the field name (4 bytes)
  if read_Int32(source) is
  {
    failure               then failure,
    success(name_length)  then
    //read the field name itself  
    if read_data(source, name_length) is
    {
      failure           then failure
      success(ba_name)  then  
      with field_name = to_string(ba_name),
      //read the type code of the field (4 bytes)
      if read_Int32(source) is
      {
        failure then failure,
        success(int32_type_code) then
        //read the length of data depending on the type, the meaning of that length is different
        // * for fixed size type_code, we can determine the number of data by dividing that number
        //   by datum size.
        // * for non fixed size type_code it's only the total size of data independtly of number of data
        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 POINTER
              b_pointer_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 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 
                  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 
                  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 RAW               
              b_raw_t then
                if read_Int32(source) is    //get numbers items in that field
                {
                  failure           then failure,
                  success(nb_items) then 
                  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 each field
   *  @return 'failure' if it cannot read the required number of elements
   */
define  Maybe(List(Message_field))     
  read_fields
  (
    Data_IO  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])
        }
    }.

define Maybe(ByteArray)
  flatten_items
  (
    Muscle_type         type_code,
    List(Muscle_object) m_o,
    ByteArray           so_far
  )=
  if m_o is
  {
    []      then success(so_far),
    [h . t] then
    if h is
    {
      b_bool(data)        then
        flatten_items(type_code, t, so_far + to_ByteArray(data)),
      
      b_double(data)      then  
        flatten_items(type_code, t, so_far + host_to_lendian_Float64_ByteArray(data)),
      
      b_float(data)       then  
        flatten_items(type_code, t, so_far + host_to_lendian_Float32_ByteArray(data)),
        
     b_int64(data)        then
        flatten_items(type_code, t, so_far + host_to_lendian_Int64_ByteArray(data)),
        
     b_int32(data)        then  
        flatten_items(type_code, t, so_far + host_to_lendian_Int32_ByteArray(data)),
        
     b_int16(data)        then  
        flatten_items(type_code, t, so_far + host_to_lendian_Int16_ByteArray(data)),
        
     b_int8(data)         then  
        flatten_items(type_code, t, so_far + to_ByteArray(data)),

     b_message(data)      then  
        with result = flatten_message(data),
        if result is
        {
          failure           then  failure,
          success(message)  then
            flatten_items(type_code, t, so_far + host_to_lendian_Int32_ByteArray(length(message)) + message),             
        }
     b_pointer(data)      then  
        flatten_items(type_code, t, so_far + host_to_lendian_Int32_ByteArray(data)),
        
     b_point(data)        then  
        flatten_items(type_code, t, so_far + host_to_lendian_B_Point_ByteArray(data)),
        
     b_rect(data)         then
        flatten_items(type_code, t, so_far + host_to_lendian_B_Rect_ByteArray(data)),
     b_string(data)       then
        flatten_items(type_code, t, so_far + host_to_lendian_Int32_ByteArray(length(data)+1)
                                           + to_byte_array(data) 
                                           + constant_byte_array(1,0)),
        
     b_custom(data)       then  
        flatten_items(type_code, t, so_far + host_to_lendian_Int32_ByteArray(length(data)) + data),
     b_raw(data)          then
        flatten_items(type_code, t, so_far + host_to_lendian_Int32_ByteArray(length(data)) + data)
    }
  }.
  
define Maybe(ByteArray)
  flatten_one_field
  (
    Muscle_type         type_code,
    List(Muscle_object) m_o
  )=
  with result = flatten_items(type_code, m_o, constant_byte_array(0, 0)),
  if result is
  {
    failure             then failure
    success(flat_items) then
      if type_code is
      {
        b_any_t then  failure,
        
        b_bool_t then 
          success(host_to_lendian_Int32_ByteArray(length(m_o)) + flat_items )
          
        b_double_t then  
          success(host_to_lendian_Int32_ByteArray(length(m_o)<<3) + flat_items )
          
        b_float_t then  
          success(host_to_lendian_Int32_ByteArray(length(m_o)<<2) + flat_items )
        b_int64_t then  
          success(host_to_lendian_Int32_ByteArray(length(m_o)<<3) + flat_items )
        b_int32_t then  
          success(host_to_lendian_Int32_ByteArray(length(m_o)<<2) + flat_items )
        b_int16_t then
          success(host_to_lendian_Int32_ByteArray(length(m_o)<<1) + flat_items )    
        b_int8_t then  
          success(host_to_lendian_Int32_ByteArray(length(m_o)) + flat_items )
        b_message_t then
          success(host_to_lendian_Int32_ByteArray(length(flat_items)) + flat_items )
        b_pointer_t then  
          success(host_to_lendian_Int32_ByteArray(length(m_o)) + flat_items )
        b_point_t then
          success(host_to_lendian_Int32_ByteArray(length(m_o)<<3) + flat_items )   
        b_rect_t then  
          success(host_to_lendian_Int32_ByteArray(length(m_o)<<4) + flat_items )        
        b_string_t then  
          success(host_to_lendian_Int32_ByteArray(length(flat_items) + 4) + //size of all items
                  host_to_lendian_Int32_ByteArray(length(m_o)) +        //number of items
                  flat_items )
        b_custom_t(Int32 _0) then
          success(host_to_lendian_Int32_ByteArray(length(flat_items) + 4) + //size of all items
                  host_to_lendian_Int32_ByteArray(length(m_o)) +        //number of items
                  flat_items )
        b_raw_t then  
          success(host_to_lendian_Int32_ByteArray(length(flat_items) + 4) + //size of all items
                  host_to_lendian_Int32_ByteArray(length(m_o)) +        //number of items
                  flat_items )
      }
   }
  .
  
define Maybe(ByteArray)
  flatten_fields
  (
    List(Message_field) fields,
    ByteArray           so_far
  )=
  if fields is
  {
    [ ]       then success(so_far), //in reality, this is an empty bytearray, because there aren't any field
    [ h . t ] then
      //write size of name + 1 for NULL erminated string
      with flatten_header = host_to_lendian_Int32_ByteArray(length(h.name)+1) +
      //write the name + NULL
                            to_byte_array(h.name) + constant_byte_array(1,0) +
      //write the type_code of the field
                            host_to_lendian_Int32_ByteArray(get_Int32_type_code(h.type_code))
                            ,
                            
      if flatten_one_field(h.type_code , *h.items) is
      {
        failure               then  failure,
        success(flatten_body) then
        if t is 
        {
          []      then  success(so_far + flatten_header + flatten_body),
          [_ ._]  then  flatten_fields(t, so_far + flatten_header + flatten_body )
        }
      }
  }.  
    
  /**
   *  Converts the given Message into a flattened buffer of bytes (ByteArray) that can be saved to disk
   *  or sent over a network, and later converted back into an identical Message type.
   *  @param msg  The message to be flatten
   *  @return     failure if any error occur otherwise the ByteArray with the flattened message
   */
define Maybe(ByteArray)
  flatten_message
  (
    Message msg
  )=
  with message_header = 
    host_to_lendian_Int32_ByteArray(_CURRENT_PROTOCOL_VERSION) +
    host_to_lendian_Int32_ByteArray(*msg.what) +
    host_to_lendian_Int32_ByteArray(length(*msg.fields)),
    
    if flatten_fields(*msg.fields, to_byte_array("")) is
    {
      failure                   then  failure
      success(flattened_fields) then  success(message_header + flattened_fields)
    }.
   

   /* 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
  (
    Data_IO io
  )=
  if read_Int32(io) is
  {
    failure                   then failure,
    success(protocolVersion)  then
      if protocolVersion = _CURRENT_PROTOCOL_VERSION then
        //get the 'what' code 
        if read_Int32(io) is
        {
          failure             then failure,
          success(what_code)  then
           //print("what code = [" + to_ascii(what_code) + "] 0x"+ to_hexa(what_code) + " " + what_code + "\n");
           //get the number of entries at this level 
           if read_Int32(io)  is
           {
              failure             then failure,
              success(nb_entries) then
              //print("Nb Entries = " + nb_entries + "\n");
              //get all fields of this level
              if read_fields(io, 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
  }
  .


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
  (
    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
  (
    Muscle_type  type_code,
    Message     msg,
    String      name,
    Int32       index
  )=
  find_data(*msg.fields, type_code, name, index).
  
/********************** Add collection ********************/

  /** @fn add_data
   *  Generic method, capable of adding any type of data to the Message.
   *  @param message     The message where the data to be add
   *  @param field_name  Name of the field to add (or add to)
   *  @param type_code   The _B_*_TYPE code indicating the type of data that will be added.
   *  @param m_object    Muscle_object that contain the bytes of data to add.  The data is copied out
   *                     and exactly how it is interpreted depends on (type).
   *  @return success(One) on success, failure
   */

define Maybe(One)
  add_data
  (
    Message       msg,
    String        field_name,
    Muscle_type   type_code,
    Muscle_object m_object
  )=
  if find_field(*msg.fields, type_code, field_name) is
  {
    failure then  //we can't find a list of fields, then we create it
      with new_field = message_field(field_name, type_code, var([m_object])),
        msg.fields <- [ new_field . *msg.fields]; 
        success(unique),
      success(items_list) then
        items_list.items <- [ m_object . *items_list.items];
        success(unique)
  }.
  
  /** Adds a new string to the Message.
   *  @param name Name of the field to add (or add to)
   *  @param value The string to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_string
  (
    Message msg,
    String  name,
    String  value
  )=
  add_data(msg, name, b_string_t, b_string(value)).

  /** Adds a new Word8 to the Message.
   *  @param name Name of the field to add (or add to)
   *  @param value The Word8 to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_int8
  (
    Message msg,
    String  name,
    Word8  value
  )=
  add_data(msg, name, b_int8_t, b_int8(value)).

  /** Adds a new Int16 to the Message.
   *  @param name Name of the field to add (or add to)
   *  @param value The Int16 to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_int16
  (
    Message msg,
    String  name,
    Int16  value
  )=
  add_data(msg, name, b_int16_t, b_int16(value)).

  /** Adds a new Int32 to the Message.
   *  @param name Name of the field to add (or add to)
   *  @param value The Int32 to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_int32
  (
    Message msg,
    String  name,
    Int32   value
  )=
  add_data(msg, name, b_int32_t, b_int32(value)).

  /** Adds a new string to the Message.
   *  @param name Name of the field to add (or add to)
   *  @param value The string to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_int64
  (
    Message msg,
    String  name,
    Int64   value
  )=
  add_data(msg, name, b_int64_t, b_int64(value)).

  /** Adds a new Bool to the Message.
   *  @param name Name of the field to add (or add to)
   *  @param value The Bool to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_bool
  (
    Message msg,
    String  name,
    Bool  value
  )=
  add_data(msg, name, b_bool_t, b_bool(value)).
  
  /** Adds a new Float (Float32) to the Message.
   *  @param name Name of the field to add (or add to)
   *  @param value The Float (Float32) to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_float
  (
    Message msg,
    String  name,
    Float32 value
  )=
  add_data(msg, name, b_float_t, b_float(value)).

  /** Adds a new Double (Float64) to the Message.
   *  @param name Name of the field to add (or add to)
   *  @param value The Double (Float 64) to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_double
  (
    Message msg,
    String  name,
    Float   value
  )=
  add_data(msg, name, b_double_t, b_double(value)).

  /** Adds a new Message to the Message.
   *  @param name Name of the field to add (or add to)
   *  @param value The Message to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_message
  (
    Message msg,
    String  name,
    Message value
  )=
  add_data(msg, name, b_message_t, b_message(value)).

  /** Adds a new B_Point to the Message.
   *  @param name Name of the field to add (or add to)
   *  @param value The B_Point to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_point
  (
    Message msg,
    String  name,
    B_Point value
  )=
  add_data(msg, name, b_point_t, b_point(value)).

  /** Adds a new B_Rect to the Message.
   *  @param name Name of the field to add (or add to)
   *  @param value The B_Rect to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_rect
  (
    Message msg,
    String  name,
    B_Rect  value
  )=
  add_data(msg, name, b_rect_t, b_rect(value)).

  /** Adds a new ByteArray to the Message.
   *  @param name     Name of the field to add (or add to)
   *  @param content  The bytes array to add
   *  @return success(One) on success, failure
   */
public define Maybe(One)
  add_raw
  (
    Message   msg,
    String    name,
    ByteArray content
  )=
  add_data(msg, name, b_raw_t, b_raw(content)).


/********************** Find collection ********************/

  /** 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(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 Word8 value from the Message.
   *  @param msg Message where we search the Word8
   *  @param name The field name to look for the int8 value under.
   *  @param index The index of the Word8 item in its field entry.
   *  @return 'success(Word8)' if the Word8 value was found, or 'failure' if it wasn't.
   */
public define Maybe(Word8)
  find_int8
  (
    Message msg,
    String  name,
    Int32   index
  )=
  if find_data(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(Word8)
  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 Int16
   *  @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(b_int16_t, msg, name, index) is success(mo) then
    if mo is b_int16(value) then
      success(value)
    else
      failure
  else
    failure.
    
   /** As above, only (index) isn't specified.  It is assumed to be zero. */
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 Int32
   *  @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(b_int32_t, msg, name, index) is success(mo) then
    if mo is b_int32(value) then
      success(value)
    else
      failure
  else
    failure.
    
   /** As above, only (index) isn't specified.  It is assumed to be zero. */
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 Int64
   *  @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 Int64 value was found, or 'failure' if it wasn't.
   */
public define Maybe(Int64)
  find_int64
  (
    Message msg,
    String  name,
    Int32   index
  )=
  if find_data(b_int64_t, msg, name, index) is success(mo) then
    if mo is b_int64(value) then
      success(value)
    else
      failure
  else
    failure.
    
   /** As above, only (index) isn't specified.  It is assumed to be zero. */
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 Bool
   *  @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(b_bool_t, msg, name, index) is success(mo) then
    if mo is b_bool(value) then
      success(value)
    else
      failure
  else
    failure.

   /** As above, only (index) isn't specified.  It is assumed to be zero. */
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(b_float_t, msg, name, index) is success(mo) then
    if mo is b_float(value) then
      success(value)
    else
      failure
  else
    failure.

   /** As above, only (index) isn't specified.  It is assumed to be zero. */
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(b_double_t, msg, name, index) is success(mo) then
    if mo is b_double(value) then
      success(value)
    else
      failure
  else
    failure.

   /** As above, only (index) isn't specified.  It is assumed to be zero. */
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(b_message_t, msg, name, index) is success(mo) then
    if mo is b_message(value) then
      success(value)
    else
      failure
  else
    failure.

   /** As above, only (index) isn't specified.  It is assumed to be zero. */
public define Maybe(Message)
  find_message
  (
    Message msg,
    String  name
  )= find_message(msg, name, 0).

  /** Retrieve a B_Point from the Message.
   *  @param msg Message where we search the Message
   *  @param name The field name to look for the B_Point under.
   *  @param index The index of the B_Point item in its field entry.
   *  @return 'success(Message)' if the B_Point was found, or 'failure' if it wasn't.
   */
public define Maybe(B_Point)
  find_point
  (
    Message msg,
    String  name,
    Int32   index
  )=
  if find_data(b_point_t, msg, name, index) is success(mo) then
    if mo is b_point(value) then
      success(value)
    else
      failure
  else
    failure.

   /** As above, only (index) isn't specified.  It is assumed to be zero. */
public define Maybe(B_Point)
  find_point
  (
    Message msg,
    String  name
  )= find_point(msg, name, 0).
  
  /** Retrieve a B_Rect from the Message.
   *  @param msg Message where we search the Message
   *  @param name The field name to look for the B_Rect under.
   *  @param index The index of the B_Rect item in its field entry.
   *  @return 'success(Message)' if the B_Rect was found, or 'failure' if it wasn't.
   */
   
public define Maybe(B_Rect)
  find_rect
  (
    Message msg,
    String  name,
    Int32   index
  )=
  if find_data(b_rect_t, msg, name, index) is success(mo) then
    if mo is b_rect(value) then
      success(value)
    else
      failure
  else
    failure.

   /** As above, only (index) isn't specified.  It is assumed to be zero. */
public define Maybe(B_Rect)
  find_rect
  (
    Message msg,
    String  name
  )= find_rect(msg, name, 0).

  /** Retrieve a Raw (ByteArray) from the Message.
   *  @param msg Message where we search the Message
   *  @param name The field name to look for the Raw under.
   *  @param index The index of the Raw item in its field entry.
   *  @return 'success(Message)' if the B_Rect was found, or 'failure' if it wasn't.
   */
   
public define Maybe(ByteArray)
  find_raw
  (
    Message msg,
    String  name,
    Int32   index
  )=
  if find_data(b_raw_t, msg, name, index) is success(mo) then
    if mo is b_raw(value) then
      success(value)
    else
      failure
  else
    failure.

   /** As above, only (index) isn't specified.  It is assumed to be zero. */
public define Maybe(ByteArray)
  find_raw
  (
    Message msg,
    String  name
  )= find_raw(msg, name, 0).

/********************** I/O socket *****************************/

  /**
   * create the right udp socket. determine if we must create broadcast
   * or not udp socket
   */
   
define Create_UDP_Client_Socket_Result
  create_udp_socket
  (
    Int32  ip,
  )=
  if ip = ip_address((255,255,255,255)) then
    create_udp_client_broadcast_socket
  else
    create_udp_client_socket
  .
  

public define Message_Send_Result
  send_message_by_udp
  (
    Message      msg,
    Int32        ip_address,  // where you want to send the data
    Int32        ip_port, 
  )=
  if flatten_message(msg) is
  {
      failure  then print("Error flattening identity Message"); flatten_error,
      success(buffer) then
        if create_udp_socket(ip_address) is
        {
          cannot_create_the_socket then network_error,
          ok(socket)               then
            //the necessary UDP socket was created, hence we send the Message packet
            with header = host_to_lendian_Int32_ByteArray(length(buffer)) + 
                          host_to_lendian_Int32_ByteArray(_MUSCLE_MESSAGE_ENCODING_DEFAULT) +
                          buffer,
            //send the header + buffer. that header is construct as follow (size of the following message + header mark)
            if udp_send(socket, ip_address, ip_port, header) is
            {
              network_unreachable then network_error,
              packet_sent         then ok
            }
        }
    }.

public define Maybe(Message)
  receive_message_from_io
  (
    Data_IO io
  ) =
    if read_Int32(io) is
    {
      failure                   then print("Can't read data \n"); failure,
      success(data_len)  then
        if read_Int32(io) is
        {
          failure                   then print("Can't read data \n"); failure,
          success(encoding)  then
            if encoding = _MUSCLE_MESSAGE_ENCODING_DEFAULT then
              unflatten_message(io)
            else print("Bad header or unknown encoding:" + encoding); failure
        }
    }.
  
  
public define Maybe(Int32)
  send_buffer
  (
    RWStream  conn,
    ByteArray buffer,
    Int32     left_to_send,
    Int32     so_far
  )=
  if left_to_send = 0 then
    success(so_far)
  else
    if write(weaken(conn), buffer) is
    {
      failure     then failure,
      success(len)then 
      if left_to_send = len then
        success(so_far+len)
      else
        send_buffer(conn, extract(buffer, len, length(buffer)), left_to_send - len, so_far+len)
    }
  .
  
public define Message_Send_Result
  send_message_by_Stream
  (
    RWStream  conn,
    Message   msg
  )=
  if flatten_message(msg) is
  {
    failure          then println("Error flattening identity Message"); flatten_error,
    success(buffer) then 
    with header = host_to_lendian_Int32_ByteArray(length(buffer)) + 
                  host_to_lendian_Int32_ByteArray(_MUSCLE_MESSAGE_ENCODING_DEFAULT),
    //send the header. that header is construct as follow (size of the following message + header mark)
    if send_buffer(conn,header, length(header), 0) is
    {
      failure    then network_error,
      success(_) then 
      //now we send the real message
      if send_buffer(conn, buffer, length(buffer), 0) is
      {
        failure     then network_error,
        success(len)then ok
      }
    }
  }.
  



 global define One
  muscle_file
  (
    List(String)  args
  ) =
  if (Maybe(RStream))file("c:/muscle/python/test_multi_msg.msg", read) is
  {
     failure          then print("Open file Error\n"),
     success(source)  then 
      if unflatten_message(make_data_io(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
              forget(add_string(sub_message, "toto", "tata"));
              if find_string(sub_message, "toto") is
              {
                failure             then print("can't find string \"string\" \n"),
                success(the_string) then print("the string found is [" + the_string + "] \n")
                              
              }
          };*/
          if flatten_message(message) is
          {
            failure         then print("Can't flatten message\n"),
            success(ba_msg) then 
            //now write the result into file
            if file("c:/muscle/python/Anubis.test_multi_msg.msg", new) is
            {
              failure     then print("Can't open file \n"),
              success(fd) then forget(write(weaken(fd), ba_msg))
            }
            
          }
      }
  }
  .