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