"Fossies" - the Fresh Open Source Software Archive

Member "muscle/html/muscle-by-example/examples/message/example_2_nested_messages.cpp" (8 Jun 2019, 4468 Bytes) of package /linux/privat/muscle7.30.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "system/SetupSystem.h"  // for CompleteSetupSystem
    2 #include "message/Message.h"
    3 #include "util/MiscUtilityFunctions.h"  // for PrintHexBytes()
    4 
    5 using namespace muscle;
    6 
    7 static void PrintExampleDescription()
    8 {
    9    printf("\n");
   10    printf("This program demonstrates nesting of Messages by extending the previous Message example to include the user's address info in a sub-Message\n");
   11    printf("\n");
   12 }
   13 
   14 enum {
   15    COMMAND_CODE_ORDER_PIZZA = 1887074913, // 'pzza' (arbitrary value generated by muscle/test/calctypecode.cpp)
   16    COMMAND_CODE_DELIVERY_INFO
   17 };
   18 
   19 int main(int argc, char ** argv)
   20 {
   21    CompleteSetupSystem css;
   22 
   23    PrintExampleDescription();
   24 
   25    // Let's create a Message and add some data to it
   26    Message orderPizzaMsg(COMMAND_CODE_ORDER_PIZZA);
   27    (void) orderPizzaMsg.AddInt32( "size_inches", 16);       // Error checking ommitted for clarity
   28    (void) orderPizzaMsg.AddBool(  "vegan",       false);    // feh!
   29    (void) orderPizzaMsg.AddString("toppings",    "cheese");
   30    (void) orderPizzaMsg.AddString("toppings",    "pepperoni");
   31    (void) orderPizzaMsg.AddString("toppings",    "mushrooms");
   32    (void) orderPizzaMsg.AddFloat( "price",       16.50f);   // in this scenario, the user gets to specify the price he wants to pay!?
   33 
   34    // Now let's add the user's delivery info -- we'll use a MessageRef to avoid an unnecessary data-copy in the AddMessage() call, below
   35    MessageRef deliveryInfoMsg = GetMessageFromPool(COMMAND_CODE_DELIVERY_INFO);
   36    (void) deliveryInfoMsg()->AddString("name",    "Hungry Joe");
   37    (void) deliveryInfoMsg()->AddString("address", "20 West Montecito Ave");
   38    (void) deliveryInfoMsg()->AddString("city",    "Sierra Madre");
   39    (void) deliveryInfoMsg()->AddString("state",   "California");
   40    (void) deliveryInfoMsg()->AddInt32("zip_code", 91024);
   41    (void) orderPizzaMsg.AddMessage("delivery_info", deliveryInfoMsg);
   42 
   43    // Let's review our order
   44    printf("Our pizza-order Message is:\n");
   45    orderPizzaMsg.PrintToStream();
   46 
   47    // Now let's flatten the Message into a ByteBuffer and see what it looks like as flattened data
   48    ByteBuffer buf(orderPizzaMsg.FlattenedSize());
   49    orderPizzaMsg.Flatten(buf.GetBuffer());
   50 
   51    printf("\n");
   52    printf("In Flattened/serialized form, the data looks like this:\n");
   53    PrintHexBytes(buf);
   54 
   55    // Next we'll parse the flattened bytes back in to a separate Message object, just to show that we can
   56    Message anotherMsg;
   57    if (anotherMsg.Unflatten(buf.GetBuffer(), buf.GetNumBytes()) == B_NO_ERROR)
   58    {
   59       printf("\n");
   60       printf("Unflattened the ByteBuffer back into anotherMsg.  anotherMsg now contains this:\n");
   61       anotherMsg.PrintToStream();
   62    }
   63    else printf("Error, unable to Unflatten the byte-buffer back to anotherMsg?!\n");
   64 
   65    printf("\n");
   66    printf("ORDER SUMMARY:\n");
   67 
   68    // And finally we'll extract some values from (anotherMsg) programatically, just to demonstrate how
   69    int32 sizeInches;
   70    if (anotherMsg.FindInt32("size_inches", sizeInches) == B_NO_ERROR)
   71    {
   72       printf("The customer wants a " INT32_FORMAT_SPEC "-inch pizza.\n", sizeInches);
   73    }
   74    else printf("size_inches wasn't specified!?\n");
   75 
   76    // Here's a convenient way of retrieving a value from the Message, or a default value if no value is present
   77    const float price = anotherMsg.GetFloat("price", 19.99f);
   78    printf("The user expects to pay $%.02f for this pizza.\n", price);
   79    printf("The pizza is to be %s\n", anotherMsg.GetBool("vegan") ? "VEGAN" : "non-vegan");
   80    
   81    // And we'll list out all of the toppings (note multiple values in a single field here!)
   82    String nextTopping;
   83    for (int32 i=0; anotherMsg.FindString("toppings", i, nextTopping) == B_NO_ERROR; i++)
   84    {
   85       printf("User specified topping:  %s\n", nextTopping());
   86    }
   87 
   88    // And dig out the delivery-info sub-Message
   89    MessageRef anotherSubMsg;
   90    if (anotherMsg.FindMessage("delivery_info", anotherSubMsg) == B_NO_ERROR)
   91    {
   92       printf("\n");
   93       printf("DELIVER TO:\n");
   94       printf("      name = %s\n", anotherSubMsg()->GetString("name")());
   95       printf("   address = %s\n", anotherSubMsg()->GetString("address")());
   96       printf("      city = %s\n", anotherSubMsg()->GetString("city")());
   97       printf("     state = %s\n", anotherSubMsg()->GetString("state")());
   98       printf("  ZIP code = " INT32_FORMAT_SPEC "\n", anotherSubMsg()->GetInt32("zip_code"));
   99    }
  100    else printf("No delivery_info sub-Message was present in (anotherMsg) !?\n");
  101  
  102    printf("\n");
  103    return 0;
  104 }