A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
amqp_management.c@19:000ab4e6a2c1, 2017-02-24 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Feb 24 14:00:58 2017 -0800
- Revision:
- 19:000ab4e6a2c1
- Parent:
- 17:923575db8b2d
- Child:
- 21:f9c433d8e6ca
1.1.8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:6ae2f7bca550 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:6ae2f7bca550 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:6ae2f7bca550 | 3 | |
Azure.IoT Build | 0:6ae2f7bca550 | 4 | #include <stdlib.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 5 | #include <stdio.h> |
AzureIoTClient | 6:641a9672db08 | 6 | #include <stdbool.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 7 | #include <string.h> |
AzureIoTClient | 19:000ab4e6a2c1 | 8 | #include "azure_c_shared_utility/optimize_size.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 9 | #include "azure_uamqp_c/amqp_management.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 10 | #include "azure_uamqp_c/link.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 11 | #include "azure_uamqp_c/amqpalloc.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | #include "azure_uamqp_c/message_sender.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 13 | #include "azure_uamqp_c/message_receiver.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 14 | #include "azure_uamqp_c/messaging.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 15 | #include "azure_uamqp_c/amqpvalue_to_string.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 16 | |
Azure.IoT Build | 0:6ae2f7bca550 | 17 | typedef enum OPERATION_STATE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 18 | { |
AzureIoTClient | 6:641a9672db08 | 19 | OPERATION_STATE_NOT_SENT, |
AzureIoTClient | 6:641a9672db08 | 20 | OPERATION_STATE_AWAIT_REPLY |
Azure.IoT Build | 0:6ae2f7bca550 | 21 | } OPERATION_STATE; |
Azure.IoT Build | 0:6ae2f7bca550 | 22 | |
Azure.IoT Build | 0:6ae2f7bca550 | 23 | typedef struct OPERATION_MESSAGE_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 24 | { |
AzureIoTClient | 6:641a9672db08 | 25 | MESSAGE_HANDLE message; |
AzureIoTClient | 6:641a9672db08 | 26 | OPERATION_STATE operation_state; |
AzureIoTClient | 6:641a9672db08 | 27 | ON_OPERATION_COMPLETE on_operation_complete; |
AzureIoTClient | 6:641a9672db08 | 28 | void* callback_context; |
AzureIoTClient | 6:641a9672db08 | 29 | unsigned long message_id; |
Azure.IoT Build | 0:6ae2f7bca550 | 30 | } OPERATION_MESSAGE_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 31 | |
Azure.IoT Build | 0:6ae2f7bca550 | 32 | typedef struct AMQP_MANAGEMENT_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 33 | { |
AzureIoTClient | 6:641a9672db08 | 34 | SESSION_HANDLE session; |
AzureIoTClient | 6:641a9672db08 | 35 | LINK_HANDLE sender_link; |
AzureIoTClient | 6:641a9672db08 | 36 | LINK_HANDLE receiver_link; |
AzureIoTClient | 6:641a9672db08 | 37 | MESSAGE_SENDER_HANDLE message_sender; |
AzureIoTClient | 6:641a9672db08 | 38 | MESSAGE_RECEIVER_HANDLE message_receiver; |
AzureIoTClient | 6:641a9672db08 | 39 | OPERATION_MESSAGE_INSTANCE** operation_messages; |
AzureIoTClient | 6:641a9672db08 | 40 | size_t operation_message_count; |
AzureIoTClient | 6:641a9672db08 | 41 | unsigned long next_message_id; |
AzureIoTClient | 6:641a9672db08 | 42 | ON_AMQP_MANAGEMENT_STATE_CHANGED on_amqp_management_state_changed; |
AzureIoTClient | 6:641a9672db08 | 43 | void* callback_context; |
AzureIoTClient | 6:641a9672db08 | 44 | AMQP_MANAGEMENT_STATE amqp_management_state; |
AzureIoTClient | 6:641a9672db08 | 45 | AMQP_MANAGEMENT_STATE previous_amqp_management_state; |
AzureIoTClient | 6:641a9672db08 | 46 | int sender_connected : 1; |
AzureIoTClient | 6:641a9672db08 | 47 | int receiver_connected : 1; |
Azure.IoT Build | 0:6ae2f7bca550 | 48 | } AMQP_MANAGEMENT_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 49 | |
Azure.IoT Build | 0:6ae2f7bca550 | 50 | static void amqpmanagement_set_state(AMQP_MANAGEMENT_INSTANCE* amqp_management_instance, AMQP_MANAGEMENT_STATE amqp_management_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 51 | { |
AzureIoTClient | 6:641a9672db08 | 52 | amqp_management_instance->previous_amqp_management_state = amqp_management_instance->amqp_management_state; |
AzureIoTClient | 6:641a9672db08 | 53 | amqp_management_instance->amqp_management_state = amqp_management_state; |
Azure.IoT Build | 0:6ae2f7bca550 | 54 | |
AzureIoTClient | 6:641a9672db08 | 55 | if (amqp_management_instance->on_amqp_management_state_changed != NULL) |
AzureIoTClient | 6:641a9672db08 | 56 | { |
AzureIoTClient | 6:641a9672db08 | 57 | amqp_management_instance->on_amqp_management_state_changed(amqp_management_instance->callback_context, amqp_management_instance->amqp_management_state, amqp_management_instance->previous_amqp_management_state); |
AzureIoTClient | 6:641a9672db08 | 58 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 59 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 60 | |
Azure.IoT Build | 0:6ae2f7bca550 | 61 | static void remove_operation_message_by_index(AMQP_MANAGEMENT_INSTANCE* amqp_management_instance, size_t index) |
Azure.IoT Build | 0:6ae2f7bca550 | 62 | { |
AzureIoTClient | 6:641a9672db08 | 63 | message_destroy(amqp_management_instance->operation_messages[index]->message); |
AzureIoTClient | 6:641a9672db08 | 64 | amqpalloc_free(amqp_management_instance->operation_messages[index]); |
Azure.IoT Build | 0:6ae2f7bca550 | 65 | |
AzureIoTClient | 6:641a9672db08 | 66 | if (amqp_management_instance->operation_message_count - index > 1) |
AzureIoTClient | 6:641a9672db08 | 67 | { |
AzureIoTClient | 6:641a9672db08 | 68 | memmove(&amqp_management_instance->operation_messages[index], &amqp_management_instance->operation_messages[index + 1], sizeof(OPERATION_MESSAGE_INSTANCE*) * (amqp_management_instance->operation_message_count - index - 1)); |
AzureIoTClient | 6:641a9672db08 | 69 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 70 | |
AzureIoTClient | 6:641a9672db08 | 71 | if (amqp_management_instance->operation_message_count == 1) |
AzureIoTClient | 6:641a9672db08 | 72 | { |
AzureIoTClient | 6:641a9672db08 | 73 | amqpalloc_free(amqp_management_instance->operation_messages); |
AzureIoTClient | 6:641a9672db08 | 74 | amqp_management_instance->operation_messages = NULL; |
AzureIoTClient | 6:641a9672db08 | 75 | } |
AzureIoTClient | 6:641a9672db08 | 76 | else |
AzureIoTClient | 6:641a9672db08 | 77 | { |
AzureIoTClient | 6:641a9672db08 | 78 | OPERATION_MESSAGE_INSTANCE** new_operation_messages = (OPERATION_MESSAGE_INSTANCE**)amqpalloc_realloc(amqp_management_instance->operation_messages, sizeof(OPERATION_MESSAGE_INSTANCE*) * (amqp_management_instance->operation_message_count - 1)); |
AzureIoTClient | 6:641a9672db08 | 79 | if (new_operation_messages != NULL) |
AzureIoTClient | 6:641a9672db08 | 80 | { |
AzureIoTClient | 6:641a9672db08 | 81 | amqp_management_instance->operation_messages = new_operation_messages; |
AzureIoTClient | 6:641a9672db08 | 82 | } |
AzureIoTClient | 6:641a9672db08 | 83 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 84 | |
AzureIoTClient | 6:641a9672db08 | 85 | amqp_management_instance->operation_message_count--; |
Azure.IoT Build | 0:6ae2f7bca550 | 86 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 87 | |
Azure.IoT Build | 0:6ae2f7bca550 | 88 | static AMQP_VALUE on_message_received(const void* context, MESSAGE_HANDLE message) |
Azure.IoT Build | 0:6ae2f7bca550 | 89 | { |
AzureIoTClient | 6:641a9672db08 | 90 | AMQP_MANAGEMENT_INSTANCE* amqp_management_instance = (AMQP_MANAGEMENT_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 91 | |
AzureIoTClient | 6:641a9672db08 | 92 | AMQP_VALUE application_properties; |
AzureIoTClient | 6:641a9672db08 | 93 | if (message_get_application_properties(message, &application_properties) != 0) |
AzureIoTClient | 6:641a9672db08 | 94 | { |
AzureIoTClient | 6:641a9672db08 | 95 | /* error */ |
AzureIoTClient | 6:641a9672db08 | 96 | } |
AzureIoTClient | 6:641a9672db08 | 97 | else |
AzureIoTClient | 6:641a9672db08 | 98 | { |
AzureIoTClient | 6:641a9672db08 | 99 | PROPERTIES_HANDLE response_properties; |
Azure.IoT Build | 0:6ae2f7bca550 | 100 | |
AzureIoTClient | 6:641a9672db08 | 101 | if (message_get_properties(message, &response_properties) != 0) |
AzureIoTClient | 6:641a9672db08 | 102 | { |
AzureIoTClient | 6:641a9672db08 | 103 | /* error */ |
AzureIoTClient | 6:641a9672db08 | 104 | } |
AzureIoTClient | 6:641a9672db08 | 105 | else |
AzureIoTClient | 6:641a9672db08 | 106 | { |
AzureIoTClient | 1:eab586236bfe | 107 | AMQP_VALUE key; |
AzureIoTClient | 1:eab586236bfe | 108 | AMQP_VALUE value; |
AzureIoTClient | 1:eab586236bfe | 109 | AMQP_VALUE desc_key; |
AzureIoTClient | 6:641a9672db08 | 110 | AMQP_VALUE desc_value; |
AzureIoTClient | 6:641a9672db08 | 111 | AMQP_VALUE map; |
AzureIoTClient | 6:641a9672db08 | 112 | AMQP_VALUE correlation_id_value; |
Azure.IoT Build | 0:6ae2f7bca550 | 113 | |
AzureIoTClient | 6:641a9672db08 | 114 | if (properties_get_correlation_id(response_properties, &correlation_id_value) != 0) |
AzureIoTClient | 6:641a9672db08 | 115 | { |
AzureIoTClient | 6:641a9672db08 | 116 | /* error */ |
AzureIoTClient | 6:641a9672db08 | 117 | } |
AzureIoTClient | 6:641a9672db08 | 118 | else |
AzureIoTClient | 6:641a9672db08 | 119 | { |
AzureIoTClient | 6:641a9672db08 | 120 | map = amqpvalue_get_inplace_described_value(application_properties); |
AzureIoTClient | 6:641a9672db08 | 121 | if (map == NULL) |
AzureIoTClient | 6:641a9672db08 | 122 | { |
AzureIoTClient | 6:641a9672db08 | 123 | /* error */ |
AzureIoTClient | 6:641a9672db08 | 124 | } |
AzureIoTClient | 6:641a9672db08 | 125 | else |
AzureIoTClient | 6:641a9672db08 | 126 | { |
AzureIoTClient | 6:641a9672db08 | 127 | key = amqpvalue_create_string("status-code"); |
AzureIoTClient | 6:641a9672db08 | 128 | if (key == NULL) |
AzureIoTClient | 6:641a9672db08 | 129 | { |
AzureIoTClient | 6:641a9672db08 | 130 | /* error */ |
AzureIoTClient | 6:641a9672db08 | 131 | } |
AzureIoTClient | 6:641a9672db08 | 132 | else |
AzureIoTClient | 6:641a9672db08 | 133 | { |
AzureIoTClient | 6:641a9672db08 | 134 | value = amqpvalue_get_map_value(map, key); |
AzureIoTClient | 6:641a9672db08 | 135 | if (value == NULL) |
AzureIoTClient | 6:641a9672db08 | 136 | { |
AzureIoTClient | 6:641a9672db08 | 137 | /* error */ |
AzureIoTClient | 6:641a9672db08 | 138 | } |
AzureIoTClient | 6:641a9672db08 | 139 | else |
AzureIoTClient | 6:641a9672db08 | 140 | { |
AzureIoTClient | 6:641a9672db08 | 141 | int32_t status_code; |
AzureIoTClient | 6:641a9672db08 | 142 | if (amqpvalue_get_int(value, &status_code) != 0) |
AzureIoTClient | 6:641a9672db08 | 143 | { |
AzureIoTClient | 6:641a9672db08 | 144 | /* error */ |
AzureIoTClient | 6:641a9672db08 | 145 | } |
AzureIoTClient | 1:eab586236bfe | 146 | else |
AzureIoTClient | 1:eab586236bfe | 147 | { |
AzureIoTClient | 1:eab586236bfe | 148 | desc_key = amqpvalue_create_string("status-description"); |
AzureIoTClient | 1:eab586236bfe | 149 | if (desc_key == NULL) |
AzureIoTClient | 1:eab586236bfe | 150 | { |
AzureIoTClient | 1:eab586236bfe | 151 | /* error */ |
AzureIoTClient | 1:eab586236bfe | 152 | } |
AzureIoTClient | 1:eab586236bfe | 153 | else |
AzureIoTClient | 1:eab586236bfe | 154 | { |
AzureIoTClient | 1:eab586236bfe | 155 | const char* status_description = NULL; |
AzureIoTClient | 1:eab586236bfe | 156 | |
AzureIoTClient | 1:eab586236bfe | 157 | desc_value = amqpvalue_get_map_value(map, desc_key); |
AzureIoTClient | 1:eab586236bfe | 158 | if (desc_value != NULL) |
AzureIoTClient | 1:eab586236bfe | 159 | { |
AzureIoTClient | 1:eab586236bfe | 160 | amqpvalue_get_string(desc_value, &status_description); |
AzureIoTClient | 1:eab586236bfe | 161 | } |
AzureIoTClient | 1:eab586236bfe | 162 | |
AzureIoTClient | 1:eab586236bfe | 163 | size_t i = 0; |
AzureIoTClient | 1:eab586236bfe | 164 | while (i < amqp_management_instance->operation_message_count) |
AzureIoTClient | 1:eab586236bfe | 165 | { |
AzureIoTClient | 1:eab586236bfe | 166 | if (amqp_management_instance->operation_messages[i]->operation_state == OPERATION_STATE_AWAIT_REPLY) |
AzureIoTClient | 1:eab586236bfe | 167 | { |
AzureIoTClient | 1:eab586236bfe | 168 | AMQP_VALUE expected_message_id = amqpvalue_create_ulong(amqp_management_instance->operation_messages[i]->message_id); |
AzureIoTClient | 1:eab586236bfe | 169 | OPERATION_RESULT operation_result; |
Azure.IoT Build | 0:6ae2f7bca550 | 170 | |
AzureIoTClient | 1:eab586236bfe | 171 | if (expected_message_id == NULL) |
AzureIoTClient | 1:eab586236bfe | 172 | { |
AzureIoTClient | 1:eab586236bfe | 173 | break; |
AzureIoTClient | 1:eab586236bfe | 174 | } |
AzureIoTClient | 1:eab586236bfe | 175 | else |
AzureIoTClient | 1:eab586236bfe | 176 | { |
AzureIoTClient | 1:eab586236bfe | 177 | if (amqpvalue_are_equal(correlation_id_value, expected_message_id)) |
AzureIoTClient | 1:eab586236bfe | 178 | { |
AzureIoTClient | 1:eab586236bfe | 179 | /* 202 is not mentioned in the draft in any way, this is a workaround for an EH bug for now */ |
AzureIoTClient | 1:eab586236bfe | 180 | if ((status_code != 200) && (status_code != 202)) |
AzureIoTClient | 1:eab586236bfe | 181 | { |
AzureIoTClient | 1:eab586236bfe | 182 | operation_result = OPERATION_RESULT_OPERATION_FAILED; |
AzureIoTClient | 1:eab586236bfe | 183 | } |
AzureIoTClient | 1:eab586236bfe | 184 | else |
AzureIoTClient | 1:eab586236bfe | 185 | { |
AzureIoTClient | 1:eab586236bfe | 186 | operation_result = OPERATION_RESULT_OK; |
AzureIoTClient | 1:eab586236bfe | 187 | } |
AzureIoTClient | 1:eab586236bfe | 188 | |
AzureIoTClient | 1:eab586236bfe | 189 | amqp_management_instance->operation_messages[i]->on_operation_complete(amqp_management_instance->operation_messages[i]->callback_context, operation_result, status_code, status_description); |
AzureIoTClient | 1:eab586236bfe | 190 | |
AzureIoTClient | 1:eab586236bfe | 191 | remove_operation_message_by_index(amqp_management_instance, i); |
Azure.IoT Build | 0:6ae2f7bca550 | 192 | |
AzureIoTClient | 1:eab586236bfe | 193 | amqpvalue_destroy(expected_message_id); |
Azure.IoT Build | 0:6ae2f7bca550 | 194 | |
AzureIoTClient | 1:eab586236bfe | 195 | break; |
AzureIoTClient | 1:eab586236bfe | 196 | } |
AzureIoTClient | 1:eab586236bfe | 197 | else |
AzureIoTClient | 1:eab586236bfe | 198 | { |
AzureIoTClient | 1:eab586236bfe | 199 | i++; |
AzureIoTClient | 1:eab586236bfe | 200 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 201 | |
AzureIoTClient | 1:eab586236bfe | 202 | amqpvalue_destroy(expected_message_id); |
AzureIoTClient | 1:eab586236bfe | 203 | } |
AzureIoTClient | 1:eab586236bfe | 204 | } |
AzureIoTClient | 1:eab586236bfe | 205 | else |
AzureIoTClient | 1:eab586236bfe | 206 | { |
AzureIoTClient | 1:eab586236bfe | 207 | i++; |
AzureIoTClient | 1:eab586236bfe | 208 | } |
AzureIoTClient | 1:eab586236bfe | 209 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 210 | |
AzureIoTClient | 1:eab586236bfe | 211 | if (desc_value != NULL) |
AzureIoTClient | 1:eab586236bfe | 212 | { |
AzureIoTClient | 1:eab586236bfe | 213 | amqpvalue_destroy(desc_value); |
AzureIoTClient | 1:eab586236bfe | 214 | } |
AzureIoTClient | 1:eab586236bfe | 215 | amqpvalue_destroy(desc_key); |
AzureIoTClient | 6:641a9672db08 | 216 | } |
AzureIoTClient | 6:641a9672db08 | 217 | } |
AzureIoTClient | 6:641a9672db08 | 218 | amqpvalue_destroy(value); |
AzureIoTClient | 6:641a9672db08 | 219 | } |
AzureIoTClient | 6:641a9672db08 | 220 | amqpvalue_destroy(key); |
AzureIoTClient | 6:641a9672db08 | 221 | } |
AzureIoTClient | 6:641a9672db08 | 222 | } |
AzureIoTClient | 6:641a9672db08 | 223 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 224 | |
AzureIoTClient | 6:641a9672db08 | 225 | properties_destroy(response_properties); |
AzureIoTClient | 6:641a9672db08 | 226 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 227 | |
AzureIoTClient | 6:641a9672db08 | 228 | application_properties_destroy(application_properties); |
AzureIoTClient | 6:641a9672db08 | 229 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 230 | |
AzureIoTClient | 6:641a9672db08 | 231 | return messaging_delivery_accepted(); |
Azure.IoT Build | 0:6ae2f7bca550 | 232 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 233 | |
Azure.IoT Build | 0:6ae2f7bca550 | 234 | static int send_operation_messages(AMQP_MANAGEMENT_INSTANCE* amqp_management_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 235 | { |
AzureIoTClient | 6:641a9672db08 | 236 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 237 | |
AzureIoTClient | 6:641a9672db08 | 238 | if ((amqp_management_instance->sender_connected != 0) && |
AzureIoTClient | 6:641a9672db08 | 239 | (amqp_management_instance->receiver_connected != 0)) |
AzureIoTClient | 6:641a9672db08 | 240 | { |
AzureIoTClient | 6:641a9672db08 | 241 | size_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 242 | |
AzureIoTClient | 6:641a9672db08 | 243 | for (i = 0; i < amqp_management_instance->operation_message_count; i++) |
AzureIoTClient | 6:641a9672db08 | 244 | { |
AzureIoTClient | 6:641a9672db08 | 245 | if (amqp_management_instance->operation_messages[i]->operation_state == OPERATION_STATE_NOT_SENT) |
AzureIoTClient | 6:641a9672db08 | 246 | { |
AzureIoTClient | 6:641a9672db08 | 247 | if (messagesender_send(amqp_management_instance->message_sender, amqp_management_instance->operation_messages[i]->message, NULL, NULL) != 0) |
AzureIoTClient | 6:641a9672db08 | 248 | { |
AzureIoTClient | 6:641a9672db08 | 249 | /* error */ |
AzureIoTClient | 6:641a9672db08 | 250 | break; |
AzureIoTClient | 6:641a9672db08 | 251 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 252 | |
AzureIoTClient | 6:641a9672db08 | 253 | amqp_management_instance->operation_messages[i]->operation_state = OPERATION_STATE_AWAIT_REPLY; |
AzureIoTClient | 6:641a9672db08 | 254 | } |
AzureIoTClient | 6:641a9672db08 | 255 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 256 | |
AzureIoTClient | 6:641a9672db08 | 257 | if (i < amqp_management_instance->operation_message_count) |
AzureIoTClient | 6:641a9672db08 | 258 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 259 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 260 | } |
AzureIoTClient | 6:641a9672db08 | 261 | else |
AzureIoTClient | 6:641a9672db08 | 262 | { |
AzureIoTClient | 6:641a9672db08 | 263 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 264 | } |
AzureIoTClient | 6:641a9672db08 | 265 | } |
AzureIoTClient | 6:641a9672db08 | 266 | else |
AzureIoTClient | 6:641a9672db08 | 267 | { |
AzureIoTClient | 6:641a9672db08 | 268 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 269 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 270 | |
AzureIoTClient | 6:641a9672db08 | 271 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 272 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 273 | |
Azure.IoT Build | 0:6ae2f7bca550 | 274 | static void on_message_sender_state_changed(void* context, MESSAGE_SENDER_STATE new_state, MESSAGE_SENDER_STATE previous_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 275 | { |
AzureIoTClient | 6:641a9672db08 | 276 | AMQP_MANAGEMENT_INSTANCE* amqp_management_instance = (AMQP_MANAGEMENT_INSTANCE*)context; |
AzureIoTClient | 6:641a9672db08 | 277 | (void)previous_state; |
AzureIoTClient | 6:641a9672db08 | 278 | switch (new_state) |
AzureIoTClient | 6:641a9672db08 | 279 | { |
AzureIoTClient | 6:641a9672db08 | 280 | default: |
AzureIoTClient | 6:641a9672db08 | 281 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 282 | |
AzureIoTClient | 6:641a9672db08 | 283 | case MESSAGE_SENDER_STATE_OPEN: |
AzureIoTClient | 6:641a9672db08 | 284 | amqp_management_instance->sender_connected = 1; |
AzureIoTClient | 6:641a9672db08 | 285 | (void)send_operation_messages(amqp_management_instance); |
AzureIoTClient | 6:641a9672db08 | 286 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 287 | |
AzureIoTClient | 6:641a9672db08 | 288 | case MESSAGE_SENDER_STATE_CLOSING: |
AzureIoTClient | 6:641a9672db08 | 289 | case MESSAGE_SENDER_STATE_ERROR: |
AzureIoTClient | 6:641a9672db08 | 290 | amqp_management_instance->sender_connected = 0; |
AzureIoTClient | 6:641a9672db08 | 291 | amqpmanagement_set_state(amqp_management_instance, AMQP_MANAGEMENT_STATE_ERROR); |
AzureIoTClient | 6:641a9672db08 | 292 | break; |
AzureIoTClient | 6:641a9672db08 | 293 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 294 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 295 | |
Azure.IoT Build | 0:6ae2f7bca550 | 296 | static void on_message_receiver_state_changed(const void* context, MESSAGE_RECEIVER_STATE new_state, MESSAGE_RECEIVER_STATE previous_state) |
Azure.IoT Build | 0:6ae2f7bca550 | 297 | { |
AzureIoTClient | 6:641a9672db08 | 298 | AMQP_MANAGEMENT_INSTANCE* amqp_management_instance = (AMQP_MANAGEMENT_INSTANCE*)context; |
AzureIoTClient | 6:641a9672db08 | 299 | (void)previous_state; |
AzureIoTClient | 6:641a9672db08 | 300 | switch (new_state) |
AzureIoTClient | 6:641a9672db08 | 301 | { |
AzureIoTClient | 6:641a9672db08 | 302 | default: |
AzureIoTClient | 6:641a9672db08 | 303 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 304 | |
AzureIoTClient | 6:641a9672db08 | 305 | case MESSAGE_RECEIVER_STATE_OPEN: |
AzureIoTClient | 6:641a9672db08 | 306 | amqp_management_instance->receiver_connected = 1; |
AzureIoTClient | 6:641a9672db08 | 307 | (void)send_operation_messages(amqp_management_instance); |
AzureIoTClient | 6:641a9672db08 | 308 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 309 | |
AzureIoTClient | 6:641a9672db08 | 310 | case MESSAGE_RECEIVER_STATE_CLOSING: |
AzureIoTClient | 6:641a9672db08 | 311 | case MESSAGE_RECEIVER_STATE_ERROR: |
AzureIoTClient | 6:641a9672db08 | 312 | amqp_management_instance->receiver_connected = 0; |
AzureIoTClient | 6:641a9672db08 | 313 | amqpmanagement_set_state(amqp_management_instance, AMQP_MANAGEMENT_STATE_ERROR); |
AzureIoTClient | 6:641a9672db08 | 314 | break; |
AzureIoTClient | 6:641a9672db08 | 315 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 316 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 317 | |
Azure.IoT Build | 0:6ae2f7bca550 | 318 | static int set_message_id(MESSAGE_HANDLE message, unsigned long next_message_id) |
Azure.IoT Build | 0:6ae2f7bca550 | 319 | { |
AzureIoTClient | 6:641a9672db08 | 320 | int result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 321 | |
AzureIoTClient | 6:641a9672db08 | 322 | PROPERTIES_HANDLE properties; |
AzureIoTClient | 6:641a9672db08 | 323 | if (message_get_properties(message, &properties) != 0) |
AzureIoTClient | 6:641a9672db08 | 324 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 325 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 326 | } |
AzureIoTClient | 6:641a9672db08 | 327 | else |
AzureIoTClient | 6:641a9672db08 | 328 | { |
AzureIoTClient | 6:641a9672db08 | 329 | AMQP_VALUE message_id = amqpvalue_create_message_id_ulong(next_message_id); |
AzureIoTClient | 6:641a9672db08 | 330 | if (message_id == NULL) |
AzureIoTClient | 6:641a9672db08 | 331 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 332 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 333 | } |
AzureIoTClient | 6:641a9672db08 | 334 | else |
AzureIoTClient | 6:641a9672db08 | 335 | { |
AzureIoTClient | 6:641a9672db08 | 336 | if (properties_set_message_id(properties, message_id) != 0) |
AzureIoTClient | 6:641a9672db08 | 337 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 338 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 339 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 340 | |
AzureIoTClient | 6:641a9672db08 | 341 | amqpvalue_destroy(message_id); |
AzureIoTClient | 6:641a9672db08 | 342 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 343 | |
AzureIoTClient | 6:641a9672db08 | 344 | if (message_set_properties(message, properties) != 0) |
AzureIoTClient | 6:641a9672db08 | 345 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 346 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 347 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 348 | |
AzureIoTClient | 6:641a9672db08 | 349 | properties_destroy(properties); |
AzureIoTClient | 6:641a9672db08 | 350 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 351 | |
AzureIoTClient | 6:641a9672db08 | 352 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 353 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 354 | |
Azure.IoT Build | 0:6ae2f7bca550 | 355 | static int add_string_key_value_pair_to_map(AMQP_VALUE map, const char* key, const char* value) |
Azure.IoT Build | 0:6ae2f7bca550 | 356 | { |
AzureIoTClient | 6:641a9672db08 | 357 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 358 | |
AzureIoTClient | 6:641a9672db08 | 359 | AMQP_VALUE key_value = amqpvalue_create_string(key); |
AzureIoTClient | 6:641a9672db08 | 360 | if (key == NULL) |
AzureIoTClient | 6:641a9672db08 | 361 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 362 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 363 | } |
AzureIoTClient | 6:641a9672db08 | 364 | else |
AzureIoTClient | 6:641a9672db08 | 365 | { |
AzureIoTClient | 6:641a9672db08 | 366 | AMQP_VALUE value_value = amqpvalue_create_string(value); |
AzureIoTClient | 6:641a9672db08 | 367 | if (value_value == NULL) |
AzureIoTClient | 6:641a9672db08 | 368 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 369 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 370 | } |
AzureIoTClient | 6:641a9672db08 | 371 | else |
AzureIoTClient | 6:641a9672db08 | 372 | { |
AzureIoTClient | 6:641a9672db08 | 373 | if (amqpvalue_set_map_value(map, key_value, value_value) != 0) |
AzureIoTClient | 6:641a9672db08 | 374 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 375 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 376 | } |
AzureIoTClient | 6:641a9672db08 | 377 | else |
AzureIoTClient | 6:641a9672db08 | 378 | { |
AzureIoTClient | 6:641a9672db08 | 379 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 380 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 381 | |
AzureIoTClient | 6:641a9672db08 | 382 | amqpvalue_destroy(key_value); |
AzureIoTClient | 6:641a9672db08 | 383 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 384 | |
AzureIoTClient | 6:641a9672db08 | 385 | amqpvalue_destroy(value_value); |
AzureIoTClient | 6:641a9672db08 | 386 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 387 | |
AzureIoTClient | 6:641a9672db08 | 388 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 389 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 390 | |
Azure.IoT Build | 0:6ae2f7bca550 | 391 | AMQP_MANAGEMENT_HANDLE amqpmanagement_create(SESSION_HANDLE session, const char* management_node, ON_AMQP_MANAGEMENT_STATE_CHANGED on_amqp_management_state_changed, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 392 | { |
AzureIoTClient | 6:641a9672db08 | 393 | AMQP_MANAGEMENT_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 394 | |
AzureIoTClient | 6:641a9672db08 | 395 | if (session == NULL) |
AzureIoTClient | 6:641a9672db08 | 396 | { |
AzureIoTClient | 6:641a9672db08 | 397 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 398 | } |
AzureIoTClient | 6:641a9672db08 | 399 | else |
AzureIoTClient | 6:641a9672db08 | 400 | { |
AzureIoTClient | 6:641a9672db08 | 401 | result = (AMQP_MANAGEMENT_INSTANCE*)amqpalloc_malloc(sizeof(AMQP_MANAGEMENT_INSTANCE)); |
AzureIoTClient | 6:641a9672db08 | 402 | if (result != NULL) |
AzureIoTClient | 6:641a9672db08 | 403 | { |
AzureIoTClient | 6:641a9672db08 | 404 | result->session = session; |
AzureIoTClient | 6:641a9672db08 | 405 | result->sender_connected = 0; |
AzureIoTClient | 6:641a9672db08 | 406 | result->receiver_connected = 0; |
AzureIoTClient | 6:641a9672db08 | 407 | result->operation_message_count = 0; |
AzureIoTClient | 6:641a9672db08 | 408 | result->operation_messages = NULL; |
AzureIoTClient | 6:641a9672db08 | 409 | result->on_amqp_management_state_changed = on_amqp_management_state_changed; |
AzureIoTClient | 6:641a9672db08 | 410 | result->callback_context = callback_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 411 | |
AzureIoTClient | 6:641a9672db08 | 412 | AMQP_VALUE source = messaging_create_source(management_node); |
AzureIoTClient | 6:641a9672db08 | 413 | if (source == NULL) |
AzureIoTClient | 6:641a9672db08 | 414 | { |
AzureIoTClient | 6:641a9672db08 | 415 | amqpalloc_free(result); |
AzureIoTClient | 6:641a9672db08 | 416 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 417 | } |
AzureIoTClient | 6:641a9672db08 | 418 | else |
AzureIoTClient | 6:641a9672db08 | 419 | { |
AzureIoTClient | 6:641a9672db08 | 420 | AMQP_VALUE target = messaging_create_target(management_node); |
AzureIoTClient | 6:641a9672db08 | 421 | if (target == NULL) |
AzureIoTClient | 6:641a9672db08 | 422 | { |
AzureIoTClient | 6:641a9672db08 | 423 | amqpalloc_free(result); |
AzureIoTClient | 6:641a9672db08 | 424 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 425 | } |
AzureIoTClient | 6:641a9672db08 | 426 | else |
AzureIoTClient | 6:641a9672db08 | 427 | { |
AzureIoTClient | 6:641a9672db08 | 428 | static const char* sender_suffix = "-sender"; |
Azure.IoT Build | 0:6ae2f7bca550 | 429 | |
AzureIoTClient | 6:641a9672db08 | 430 | char* sender_link_name = (char*)amqpalloc_malloc(strlen(management_node) + strlen(sender_suffix) + 1); |
AzureIoTClient | 6:641a9672db08 | 431 | if (sender_link_name == NULL) |
AzureIoTClient | 6:641a9672db08 | 432 | { |
AzureIoTClient | 6:641a9672db08 | 433 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 434 | } |
AzureIoTClient | 6:641a9672db08 | 435 | else |
AzureIoTClient | 6:641a9672db08 | 436 | { |
AzureIoTClient | 6:641a9672db08 | 437 | static const char* receiver_suffix = "-receiver"; |
Azure.IoT Build | 0:6ae2f7bca550 | 438 | |
AzureIoTClient | 6:641a9672db08 | 439 | (void)strcpy(sender_link_name, management_node); |
AzureIoTClient | 6:641a9672db08 | 440 | (void)strcat(sender_link_name, sender_suffix); |
Azure.IoT Build | 0:6ae2f7bca550 | 441 | |
AzureIoTClient | 6:641a9672db08 | 442 | char* receiver_link_name = (char*)amqpalloc_malloc(strlen(management_node) + strlen(receiver_suffix) + 1); |
AzureIoTClient | 6:641a9672db08 | 443 | if (receiver_link_name == NULL) |
AzureIoTClient | 6:641a9672db08 | 444 | { |
AzureIoTClient | 6:641a9672db08 | 445 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 446 | } |
AzureIoTClient | 6:641a9672db08 | 447 | else |
AzureIoTClient | 6:641a9672db08 | 448 | { |
AzureIoTClient | 6:641a9672db08 | 449 | (void)strcpy(receiver_link_name, management_node); |
AzureIoTClient | 6:641a9672db08 | 450 | (void)strcat(receiver_link_name, receiver_suffix); |
Azure.IoT Build | 0:6ae2f7bca550 | 451 | |
AzureIoTClient | 6:641a9672db08 | 452 | result->sender_link = link_create(session, "cbs-sender", role_sender, source, target); |
AzureIoTClient | 6:641a9672db08 | 453 | if (result->sender_link == NULL) |
AzureIoTClient | 6:641a9672db08 | 454 | { |
AzureIoTClient | 6:641a9672db08 | 455 | amqpalloc_free(result); |
AzureIoTClient | 6:641a9672db08 | 456 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 457 | } |
AzureIoTClient | 6:641a9672db08 | 458 | else |
AzureIoTClient | 6:641a9672db08 | 459 | { |
AzureIoTClient | 6:641a9672db08 | 460 | result->receiver_link = link_create(session, "cbs-receiver", role_receiver, source, target); |
AzureIoTClient | 6:641a9672db08 | 461 | if (result->receiver_link == NULL) |
AzureIoTClient | 6:641a9672db08 | 462 | { |
AzureIoTClient | 6:641a9672db08 | 463 | link_destroy(result->sender_link); |
AzureIoTClient | 6:641a9672db08 | 464 | amqpalloc_free(result); |
AzureIoTClient | 6:641a9672db08 | 465 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 466 | } |
AzureIoTClient | 6:641a9672db08 | 467 | else |
AzureIoTClient | 6:641a9672db08 | 468 | { |
AzureIoTClient | 6:641a9672db08 | 469 | if ((link_set_max_message_size(result->sender_link, 65535) != 0) || |
AzureIoTClient | 6:641a9672db08 | 470 | (link_set_max_message_size(result->receiver_link, 65535) != 0)) |
AzureIoTClient | 6:641a9672db08 | 471 | { |
AzureIoTClient | 6:641a9672db08 | 472 | link_destroy(result->sender_link); |
AzureIoTClient | 6:641a9672db08 | 473 | link_destroy(result->receiver_link); |
AzureIoTClient | 6:641a9672db08 | 474 | amqpalloc_free(result); |
AzureIoTClient | 6:641a9672db08 | 475 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 476 | } |
AzureIoTClient | 6:641a9672db08 | 477 | else |
AzureIoTClient | 6:641a9672db08 | 478 | { |
AzureIoTClient | 6:641a9672db08 | 479 | result->message_sender = messagesender_create(result->sender_link, on_message_sender_state_changed, result); |
AzureIoTClient | 6:641a9672db08 | 480 | if (result->message_sender == NULL) |
AzureIoTClient | 6:641a9672db08 | 481 | { |
AzureIoTClient | 6:641a9672db08 | 482 | link_destroy(result->sender_link); |
AzureIoTClient | 6:641a9672db08 | 483 | link_destroy(result->receiver_link); |
AzureIoTClient | 6:641a9672db08 | 484 | amqpalloc_free(result); |
AzureIoTClient | 6:641a9672db08 | 485 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 486 | } |
AzureIoTClient | 6:641a9672db08 | 487 | else |
AzureIoTClient | 6:641a9672db08 | 488 | { |
AzureIoTClient | 6:641a9672db08 | 489 | result->message_receiver = messagereceiver_create(result->receiver_link, on_message_receiver_state_changed, result); |
AzureIoTClient | 6:641a9672db08 | 490 | if (result->message_receiver == NULL) |
AzureIoTClient | 6:641a9672db08 | 491 | { |
AzureIoTClient | 6:641a9672db08 | 492 | messagesender_destroy(result->message_sender); |
AzureIoTClient | 6:641a9672db08 | 493 | link_destroy(result->sender_link); |
AzureIoTClient | 6:641a9672db08 | 494 | link_destroy(result->receiver_link); |
AzureIoTClient | 6:641a9672db08 | 495 | amqpalloc_free(result); |
AzureIoTClient | 6:641a9672db08 | 496 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 497 | } |
AzureIoTClient | 6:641a9672db08 | 498 | else |
AzureIoTClient | 6:641a9672db08 | 499 | { |
AzureIoTClient | 6:641a9672db08 | 500 | result->next_message_id = 0; |
AzureIoTClient | 6:641a9672db08 | 501 | } |
AzureIoTClient | 6:641a9672db08 | 502 | } |
AzureIoTClient | 6:641a9672db08 | 503 | } |
AzureIoTClient | 6:641a9672db08 | 504 | } |
AzureIoTClient | 6:641a9672db08 | 505 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 506 | |
AzureIoTClient | 6:641a9672db08 | 507 | amqpalloc_free(receiver_link_name); |
AzureIoTClient | 6:641a9672db08 | 508 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 509 | |
AzureIoTClient | 6:641a9672db08 | 510 | amqpalloc_free(sender_link_name); |
AzureIoTClient | 6:641a9672db08 | 511 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 512 | |
AzureIoTClient | 6:641a9672db08 | 513 | amqpvalue_destroy(target); |
AzureIoTClient | 6:641a9672db08 | 514 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 515 | |
AzureIoTClient | 6:641a9672db08 | 516 | amqpvalue_destroy(source); |
AzureIoTClient | 6:641a9672db08 | 517 | } |
AzureIoTClient | 6:641a9672db08 | 518 | } |
AzureIoTClient | 6:641a9672db08 | 519 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 520 | |
AzureIoTClient | 6:641a9672db08 | 521 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 522 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 523 | |
Azure.IoT Build | 0:6ae2f7bca550 | 524 | void amqpmanagement_destroy(AMQP_MANAGEMENT_HANDLE amqp_management) |
Azure.IoT Build | 0:6ae2f7bca550 | 525 | { |
AzureIoTClient | 6:641a9672db08 | 526 | if (amqp_management != NULL) |
AzureIoTClient | 6:641a9672db08 | 527 | { |
AzureIoTClient | 6:641a9672db08 | 528 | (void)amqpmanagement_close(amqp_management); |
Azure.IoT Build | 0:6ae2f7bca550 | 529 | |
AzureIoTClient | 6:641a9672db08 | 530 | if (amqp_management->operation_message_count > 0) |
AzureIoTClient | 6:641a9672db08 | 531 | { |
AzureIoTClient | 6:641a9672db08 | 532 | size_t i; |
AzureIoTClient | 6:641a9672db08 | 533 | for (i = 0; i < amqp_management->operation_message_count; i++) |
AzureIoTClient | 6:641a9672db08 | 534 | { |
AzureIoTClient | 6:641a9672db08 | 535 | message_destroy(amqp_management->operation_messages[i]->message); |
AzureIoTClient | 6:641a9672db08 | 536 | amqpalloc_free(amqp_management->operation_messages[i]); |
AzureIoTClient | 6:641a9672db08 | 537 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 538 | |
AzureIoTClient | 6:641a9672db08 | 539 | amqpalloc_free(amqp_management->operation_messages); |
AzureIoTClient | 6:641a9672db08 | 540 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 541 | |
AzureIoTClient | 6:641a9672db08 | 542 | link_destroy(amqp_management->sender_link); |
AzureIoTClient | 6:641a9672db08 | 543 | link_destroy(amqp_management->receiver_link); |
AzureIoTClient | 6:641a9672db08 | 544 | messagesender_destroy(amqp_management->message_sender); |
AzureIoTClient | 6:641a9672db08 | 545 | messagereceiver_destroy(amqp_management->message_receiver); |
AzureIoTClient | 6:641a9672db08 | 546 | amqpalloc_free(amqp_management); |
AzureIoTClient | 6:641a9672db08 | 547 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 548 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 549 | |
Azure.IoT Build | 0:6ae2f7bca550 | 550 | int amqpmanagement_open(AMQP_MANAGEMENT_HANDLE amqp_management) |
Azure.IoT Build | 0:6ae2f7bca550 | 551 | { |
AzureIoTClient | 6:641a9672db08 | 552 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 553 | |
AzureIoTClient | 6:641a9672db08 | 554 | if (amqp_management == NULL) |
AzureIoTClient | 6:641a9672db08 | 555 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 556 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 557 | } |
AzureIoTClient | 6:641a9672db08 | 558 | else |
AzureIoTClient | 6:641a9672db08 | 559 | { |
AzureIoTClient | 6:641a9672db08 | 560 | if (messagereceiver_open(amqp_management->message_receiver, on_message_received, amqp_management) != 0) |
AzureIoTClient | 6:641a9672db08 | 561 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 562 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 563 | } |
AzureIoTClient | 6:641a9672db08 | 564 | else |
AzureIoTClient | 6:641a9672db08 | 565 | { |
AzureIoTClient | 6:641a9672db08 | 566 | if (messagesender_open(amqp_management->message_sender) != 0) |
AzureIoTClient | 6:641a9672db08 | 567 | { |
AzureIoTClient | 6:641a9672db08 | 568 | messagereceiver_close(amqp_management->message_receiver); |
AzureIoTClient | 19:000ab4e6a2c1 | 569 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 570 | } |
AzureIoTClient | 6:641a9672db08 | 571 | else |
AzureIoTClient | 6:641a9672db08 | 572 | { |
AzureIoTClient | 6:641a9672db08 | 573 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 574 | } |
AzureIoTClient | 6:641a9672db08 | 575 | } |
AzureIoTClient | 6:641a9672db08 | 576 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 577 | |
AzureIoTClient | 6:641a9672db08 | 578 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 579 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 580 | |
Azure.IoT Build | 0:6ae2f7bca550 | 581 | int amqpmanagement_close(AMQP_MANAGEMENT_HANDLE amqp_management) |
Azure.IoT Build | 0:6ae2f7bca550 | 582 | { |
AzureIoTClient | 6:641a9672db08 | 583 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 584 | |
AzureIoTClient | 6:641a9672db08 | 585 | if (amqp_management == NULL) |
AzureIoTClient | 6:641a9672db08 | 586 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 587 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 588 | } |
AzureIoTClient | 6:641a9672db08 | 589 | else |
AzureIoTClient | 6:641a9672db08 | 590 | { |
AzureIoTClient | 6:641a9672db08 | 591 | if ((messagesender_close(amqp_management->message_sender) != 0) || |
AzureIoTClient | 6:641a9672db08 | 592 | (messagereceiver_close(amqp_management->message_receiver) != 0)) |
AzureIoTClient | 6:641a9672db08 | 593 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 594 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 595 | } |
AzureIoTClient | 6:641a9672db08 | 596 | else |
AzureIoTClient | 6:641a9672db08 | 597 | { |
AzureIoTClient | 6:641a9672db08 | 598 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 599 | } |
AzureIoTClient | 6:641a9672db08 | 600 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 601 | |
AzureIoTClient | 6:641a9672db08 | 602 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 603 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 604 | |
Azure.IoT Build | 0:6ae2f7bca550 | 605 | int amqpmanagement_start_operation(AMQP_MANAGEMENT_HANDLE amqp_management, const char* operation, const char* type, const char* locales, MESSAGE_HANDLE message, ON_OPERATION_COMPLETE on_operation_complete, void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 606 | { |
AzureIoTClient | 6:641a9672db08 | 607 | int result; |
AzureIoTClient | 6:641a9672db08 | 608 | |
AzureIoTClient | 6:641a9672db08 | 609 | if ((amqp_management == NULL) || |
AzureIoTClient | 6:641a9672db08 | 610 | (operation == NULL)) |
AzureIoTClient | 6:641a9672db08 | 611 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 612 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 613 | } |
AzureIoTClient | 6:641a9672db08 | 614 | else |
AzureIoTClient | 6:641a9672db08 | 615 | { |
AzureIoTClient | 6:641a9672db08 | 616 | AMQP_VALUE application_properties; |
AzureIoTClient | 6:641a9672db08 | 617 | if (message_get_application_properties(message, &application_properties) != 0) |
AzureIoTClient | 6:641a9672db08 | 618 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 619 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 620 | } |
AzureIoTClient | 6:641a9672db08 | 621 | else |
AzureIoTClient | 6:641a9672db08 | 622 | { |
AzureIoTClient | 6:641a9672db08 | 623 | if ((add_string_key_value_pair_to_map(application_properties, "operation", operation) != 0) || |
AzureIoTClient | 6:641a9672db08 | 624 | (add_string_key_value_pair_to_map(application_properties, "type", type) != 0) || |
AzureIoTClient | 6:641a9672db08 | 625 | ((locales != NULL) && (add_string_key_value_pair_to_map(application_properties, "locales", locales) != 0))) |
AzureIoTClient | 6:641a9672db08 | 626 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 627 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 628 | } |
AzureIoTClient | 6:641a9672db08 | 629 | else |
AzureIoTClient | 6:641a9672db08 | 630 | { |
AzureIoTClient | 6:641a9672db08 | 631 | if ((message_set_application_properties(message, application_properties) != 0) || |
AzureIoTClient | 6:641a9672db08 | 632 | (set_message_id(message, amqp_management->next_message_id) != 0)) |
AzureIoTClient | 6:641a9672db08 | 633 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 634 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 635 | } |
AzureIoTClient | 6:641a9672db08 | 636 | else |
AzureIoTClient | 6:641a9672db08 | 637 | { |
AzureIoTClient | 6:641a9672db08 | 638 | OPERATION_MESSAGE_INSTANCE* pending_operation_message = amqpalloc_malloc(sizeof(OPERATION_MESSAGE_INSTANCE)); |
AzureIoTClient | 6:641a9672db08 | 639 | if (pending_operation_message == NULL) |
AzureIoTClient | 6:641a9672db08 | 640 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 641 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 642 | } |
AzureIoTClient | 6:641a9672db08 | 643 | else |
AzureIoTClient | 6:641a9672db08 | 644 | { |
AzureIoTClient | 6:641a9672db08 | 645 | pending_operation_message->message = message_clone(message); |
AzureIoTClient | 6:641a9672db08 | 646 | pending_operation_message->callback_context = context; |
AzureIoTClient | 6:641a9672db08 | 647 | pending_operation_message->on_operation_complete = on_operation_complete; |
AzureIoTClient | 6:641a9672db08 | 648 | pending_operation_message->operation_state = OPERATION_STATE_NOT_SENT; |
AzureIoTClient | 6:641a9672db08 | 649 | pending_operation_message->message_id = amqp_management->next_message_id; |
Azure.IoT Build | 0:6ae2f7bca550 | 650 | |
AzureIoTClient | 6:641a9672db08 | 651 | amqp_management->next_message_id++; |
AzureIoTClient | 6:641a9672db08 | 652 | |
AzureIoTClient | 6:641a9672db08 | 653 | OPERATION_MESSAGE_INSTANCE** new_operation_messages = amqpalloc_realloc(amqp_management->operation_messages, (amqp_management->operation_message_count + 1) * sizeof(OPERATION_MESSAGE_INSTANCE*)); |
AzureIoTClient | 6:641a9672db08 | 654 | if (new_operation_messages == NULL) |
AzureIoTClient | 6:641a9672db08 | 655 | { |
AzureIoTClient | 6:641a9672db08 | 656 | message_destroy(message); |
AzureIoTClient | 6:641a9672db08 | 657 | amqpalloc_free(pending_operation_message); |
AzureIoTClient | 19:000ab4e6a2c1 | 658 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 659 | } |
AzureIoTClient | 6:641a9672db08 | 660 | else |
AzureIoTClient | 6:641a9672db08 | 661 | { |
AzureIoTClient | 6:641a9672db08 | 662 | amqp_management->operation_messages = new_operation_messages; |
AzureIoTClient | 6:641a9672db08 | 663 | amqp_management->operation_messages[amqp_management->operation_message_count] = pending_operation_message; |
AzureIoTClient | 6:641a9672db08 | 664 | amqp_management->operation_message_count++; |
AzureIoTClient | 6:641a9672db08 | 665 | |
AzureIoTClient | 6:641a9672db08 | 666 | if (send_operation_messages(amqp_management) != 0) |
AzureIoTClient | 6:641a9672db08 | 667 | { |
AzureIoTClient | 6:641a9672db08 | 668 | if (on_operation_complete != NULL) |
AzureIoTClient | 6:641a9672db08 | 669 | { |
AzureIoTClient | 6:641a9672db08 | 670 | on_operation_complete(context, OPERATION_RESULT_CBS_ERROR, 0, NULL); |
AzureIoTClient | 6:641a9672db08 | 671 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 672 | |
AzureIoTClient | 19:000ab4e6a2c1 | 673 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 674 | } |
AzureIoTClient | 6:641a9672db08 | 675 | else |
AzureIoTClient | 6:641a9672db08 | 676 | { |
AzureIoTClient | 6:641a9672db08 | 677 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 678 | } |
AzureIoTClient | 6:641a9672db08 | 679 | } |
AzureIoTClient | 6:641a9672db08 | 680 | } |
AzureIoTClient | 6:641a9672db08 | 681 | } |
AzureIoTClient | 6:641a9672db08 | 682 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 683 | |
AzureIoTClient | 6:641a9672db08 | 684 | amqpvalue_destroy(application_properties); |
AzureIoTClient | 6:641a9672db08 | 685 | } |
AzureIoTClient | 6:641a9672db08 | 686 | } |
AzureIoTClient | 6:641a9672db08 | 687 | return result; |
AzureIoTClient | 6:641a9672db08 | 688 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 689 | |
AzureIoTClient | 6:641a9672db08 | 690 | void amqpmanagement_set_trace(AMQP_MANAGEMENT_HANDLE amqp_management, bool traceOn) |
AzureIoTClient | 6:641a9672db08 | 691 | { |
AzureIoTClient | 6:641a9672db08 | 692 | if (amqp_management != NULL) |
AzureIoTClient | 6:641a9672db08 | 693 | { |
AzureIoTClient | 6:641a9672db08 | 694 | messagesender_set_trace(amqp_management->message_sender, traceOn); |
AzureIoTClient | 6:641a9672db08 | 695 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 696 | } |