int iothubtransportamqp_methods_respond()

in iothub_client/src/iothubtransportamqp_methods.c [794:934]


int iothubtransportamqp_methods_respond(IOTHUBTRANSPORT_AMQP_METHOD_HANDLE method_handle, const unsigned char* response, size_t response_size, int status_code)
{
    int result;

    if (method_handle == NULL)
    {
        LogError("NULL method handle");
        result = MU_FAILURE;
    }
    else if ((response == NULL) && (response_size > 0))
    {
        LogError("NULL response buffer with > 0 response payload size");
        result = MU_FAILURE;
    }
    else
    {
        MESSAGE_HANDLE message = message_create();
        if (message == NULL)
        {
            LogError("Cannot create message");
            result = MU_FAILURE;
        }
        else
        {
            PROPERTIES_HANDLE properties = properties_create();
            if (properties == NULL)
            {
                LogError("Cannot create properties");
                result = MU_FAILURE;
            }
            else
            {
                AMQP_VALUE correlation_id = amqpvalue_create_uuid(method_handle->correlation_id);
                if (correlation_id == NULL)
                {
                    LogError("Cannot create correlation_id value");
                    result = MU_FAILURE;
                }
                else
                {
                    if (properties_set_correlation_id(properties, correlation_id) != 0)
                    {
                        LogError("Cannot set correlation_id on the properties");
                        result = MU_FAILURE;
                    }
                    else if (message_set_properties(message, properties) != 0)
                    {
                        LogError("Cannot set properties on the response message");
                        result = MU_FAILURE;
                    }
                    else
                    {
                        AMQP_VALUE application_properties_map = amqpvalue_create_map();
                        if (application_properties_map == NULL)
                        {
                            LogError("Cannot create map for application properties");
                            result = MU_FAILURE;
                        }
                        else
                        {
                            AMQP_VALUE property_key_status = amqpvalue_create_string("IoThub-status");
                            if (property_key_status == NULL)
                            {
                                LogError("Cannot create the property key for the status property");
                                result = MU_FAILURE;
                            }
                            else
                            {
                                AMQP_VALUE property_value_status = amqpvalue_create_int(status_code);
                                if (property_value_status == NULL)
                                {
                                    LogError("Cannot create the status code property value for the application properties map");
                                    result = MU_FAILURE;
                                }
                                else
                                {
                                    if (amqpvalue_set_map_value(application_properties_map, property_key_status, property_value_status) != 0)
                                    {
                                        LogError("Cannot add the status property to the application properties");
                                        result = MU_FAILURE;
                                    }
                                    else
                                    {
                                        if (message_set_application_properties(message, application_properties_map) != 0)
                                        {
                                            LogError("Cannot set the application properties on the message");
                                            result = MU_FAILURE;
                                        }
                                        else
                                        {
                                            BINARY_DATA binary_data;

                                            binary_data.bytes = response;
                                            binary_data.length = response_size;

                                            if (message_add_body_amqp_data(message, binary_data) != 0)
                                            {
                                                LogError("Cannot set the response payload on the reponse message");
                                                result = MU_FAILURE;
                                            }
                                            else
                                            {
                                                if (messagesender_send_async(method_handle->iothubtransport_amqp_methods_handle->message_sender, message, on_message_send_complete, method_handle->iothubtransport_amqp_methods_handle, 0) == NULL)
                                                {
                                                    LogError("Cannot send response message");
                                                    result = MU_FAILURE;
                                                }
                                                else
                                                {
                                                    remove_tracked_handle(method_handle->iothubtransport_amqp_methods_handle, method_handle);

                                                    free(method_handle);

                                                    result = 0;
                                                }
                                            }
                                        }
                                    }

                                    amqpvalue_destroy(property_value_status);
                                }

                                amqpvalue_destroy(property_key_status);
                            }

                            amqpvalue_destroy(application_properties_map);
                        }
                    }

                    amqpvalue_destroy(correlation_id);
                }

                properties_destroy(properties);
            }

            message_destroy(message);
        }
    }

    return result;
}