A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Revision:
34:6be9c2058664
Parent:
28:add19eb7defa
--- a/messaging.c	Mon Sep 25 13:38:40 2017 -0700
+++ b/messaging.c	Sat Oct 21 20:12:19 2017 +0000
@@ -2,7 +2,9 @@
 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
 
 #include <stdlib.h>
+#include <stdint.h>
 #include <stdbool.h>
+#include "azure_c_shared_utility/xlogging.h"
 #include "azure_uamqp_c/amqpvalue.h"
 #include "azure_uamqp_c/amqp_definitions.h"
 
@@ -10,8 +12,10 @@
 {
     AMQP_VALUE result;
     SOURCE_HANDLE source = source_create();
+
     if (source == NULL)
     {
+        LogError("NULL source");
         result = NULL;
     }
     else
@@ -19,17 +23,27 @@
         AMQP_VALUE address_value = amqpvalue_create_string(address);
         if (address_value == NULL)
         {
+            LogError("Cannot create address AMQP string");
             result = NULL;
         }
         else
         {
             if (source_set_address(source, address_value) != 0)
             {
+                LogError("Cannot set address on source");
                 result = NULL;
             }
             else
             {
                 result = amqpvalue_create_source(source);
+                if (result == NULL)
+                {
+                    LogError("Cannot create source");
+                }
+                else
+                {
+                    /* all ok */
+                }
             }
 
             amqpvalue_destroy(address_value);
@@ -45,8 +59,10 @@
 {
     AMQP_VALUE result;
     TARGET_HANDLE target = target_create();
+
     if (target == NULL)
     {
+        LogError("NULL target");
         result = NULL;
     }
     else
@@ -54,17 +70,27 @@
         AMQP_VALUE address_value = amqpvalue_create_string(address);
         if (address_value == NULL)
         {
+            LogError("Cannot create address AMQP string");
             result = NULL;
         }
         else
         {
             if (target_set_address(target, address_value) != 0)
             {
+                LogError("Cannot set address on target");
                 result = NULL;
             }
             else
             {
                 result = amqpvalue_create_target(target);
+                if (result == NULL)
+                {
+                    LogError("Cannot create target");
+                }
+                else
+                {
+                    /* all ok */
+                }
             }
 
             amqpvalue_destroy(address_value);
@@ -82,11 +108,21 @@
     RECEIVED_HANDLE received = received_create(section_number, section_offset);
     if (received == NULL)
     {
+        LogError("Cannot create RECEIVED delivery state handle");
         result = NULL;
     }
     else
     {
         result = amqpvalue_create_received(received);
+        if (result == NULL)
+        {
+            LogError("Cannot create RECEIVED delivery state AMQP value");
+        }
+        else
+        {
+            /* all ok */
+        }
+
         received_destroy(received);
     }
 
@@ -99,11 +135,21 @@
     ACCEPTED_HANDLE accepted = accepted_create();
     if (accepted == NULL)
     {
+        LogError("Cannot create ACCEPTED delivery state handle");
         result = NULL;
     }
     else
     {
         result = amqpvalue_create_accepted(accepted);
+        if (result == NULL)
+        {
+            LogError("Cannot create ACCEPTED delivery state AMQP value");
+        }
+        else
+        {
+            /* all ok */
+        }
+
         accepted_destroy(accepted);
     }
 
@@ -116,6 +162,7 @@
     REJECTED_HANDLE rejected = rejected_create();
     if (rejected == NULL)
     {
+        LogError("Cannot create REJECTED delivery state handle");
         result = NULL;
     }
     else
@@ -128,6 +175,7 @@
             error_handle = error_create(error_condition);
             if (error_handle == NULL)
             {
+                LogError("Cannot create error AMQP value for REJECTED state");
                 error_constructing = true;
             }
             else
@@ -135,12 +183,14 @@
                 if ((error_description != NULL) &&
                     (error_set_description(error_handle, error_description) != 0))
                 {
+                    LogError("Cannot set error description on error AMQP value for REJECTED state");
                     error_constructing = true;
                 }
                 else
                 {
                     if (rejected_set_error(rejected, error_handle) != 0)
                     {
+                        LogError("Cannot set error on REJECTED state handle");
                         error_constructing = true;
                     }
                 }
@@ -156,6 +206,14 @@
         else
         {
             result = amqpvalue_create_rejected(rejected);
+            if (result == NULL)
+            {
+                LogError("Cannot create REJECTED delivery state AMQP value");
+            }
+            else
+            {
+                /* all ok */
+            }
         }
 
         rejected_destroy(rejected);
@@ -170,11 +228,21 @@
     RELEASED_HANDLE released = released_create();
     if (released == NULL)
     {
+        LogError("Cannot create RELEASED delivery state handle");
         result = NULL;
     }
     else
     {
         result = amqpvalue_create_released(released);
+        if (result == NULL)
+        {
+            LogError("Cannot create RELEASED delivery state AMQP value");
+        }
+        else
+        {
+            /* all ok */
+        }
+
         released_destroy(released);
     }
 
@@ -187,19 +255,37 @@
     MODIFIED_HANDLE modified = modified_create();
     if (modified == NULL)
     {
+        LogError("Cannot create MODIFIED delivery state handle");
         result = NULL;
     }
     else
     {
-        if ((modified_set_delivery_failed(modified, delivery_failed) != 0) ||
-            (modified_set_undeliverable_here(modified, undeliverable_here) != 0) ||
-            ((message_annotations != NULL) && (modified_set_message_annotations(modified, message_annotations) != 0)))
+        if (modified_set_delivery_failed(modified, delivery_failed) != 0)
+        {
+            LogError("Cannot set delivery failed on MODIFIED delivery state");
+            result = NULL;
+        }
+        else if (modified_set_undeliverable_here(modified, undeliverable_here) != 0)
         {
+            LogError("Cannot set undeliverable here on MODIFIED delivery state");
+            result = NULL;
+        }
+        else if ((message_annotations != NULL) && (modified_set_message_annotations(modified, message_annotations) != 0))
+        {
+            LogError("Cannot set message annotations on MODIFIED delivery state");
             result = NULL;
         }
         else
         {
             result = amqpvalue_create_modified(modified);
+            if (result == NULL)
+            {
+                LogError("Cannot create MODIFIED delivery state AMQP value");
+            }
+            else
+            {
+                /* all ok */
+            }
         }
 
         modified_destroy(modified);