Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
uamqp/src/cbs.c@0:f7f1f0d76dd6, 2018-08-23 (annotated)
- Committer:
- XinZhangMS
- Date:
- Thu Aug 23 06:52:14 2018 +0000
- Revision:
- 0:f7f1f0d76dd6
azure-c-sdk for mbed os supporting NUCLEO_F767ZI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
XinZhangMS | 0:f7f1f0d76dd6 | 1 | // Copyright (c) Microsoft. All rights reserved. |
XinZhangMS | 0:f7f1f0d76dd6 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
XinZhangMS | 0:f7f1f0d76dd6 | 3 | |
XinZhangMS | 0:f7f1f0d76dd6 | 4 | #include <stdlib.h> |
XinZhangMS | 0:f7f1f0d76dd6 | 5 | #include <stdbool.h> |
XinZhangMS | 0:f7f1f0d76dd6 | 6 | #include <stdio.h> |
XinZhangMS | 0:f7f1f0d76dd6 | 7 | #include <string.h> |
XinZhangMS | 0:f7f1f0d76dd6 | 8 | #include "azure_c_shared_utility/optimize_size.h" |
XinZhangMS | 0:f7f1f0d76dd6 | 9 | #include "azure_c_shared_utility/gballoc.h" |
XinZhangMS | 0:f7f1f0d76dd6 | 10 | #include "azure_c_shared_utility/singlylinkedlist.h" |
XinZhangMS | 0:f7f1f0d76dd6 | 11 | #include "azure_c_shared_utility/xlogging.h" |
XinZhangMS | 0:f7f1f0d76dd6 | 12 | #include "azure_uamqp_c/cbs.h" |
XinZhangMS | 0:f7f1f0d76dd6 | 13 | #include "azure_uamqp_c/amqp_management.h" |
XinZhangMS | 0:f7f1f0d76dd6 | 14 | #include "azure_uamqp_c/session.h" |
XinZhangMS | 0:f7f1f0d76dd6 | 15 | |
XinZhangMS | 0:f7f1f0d76dd6 | 16 | typedef enum CBS_STATE_TAG |
XinZhangMS | 0:f7f1f0d76dd6 | 17 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 18 | CBS_STATE_CLOSED, |
XinZhangMS | 0:f7f1f0d76dd6 | 19 | CBS_STATE_OPENING, |
XinZhangMS | 0:f7f1f0d76dd6 | 20 | CBS_STATE_OPEN, |
XinZhangMS | 0:f7f1f0d76dd6 | 21 | CBS_STATE_ERROR |
XinZhangMS | 0:f7f1f0d76dd6 | 22 | } CBS_STATE; |
XinZhangMS | 0:f7f1f0d76dd6 | 23 | |
XinZhangMS | 0:f7f1f0d76dd6 | 24 | typedef struct CBS_OPERATION_TAG |
XinZhangMS | 0:f7f1f0d76dd6 | 25 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 26 | ON_CBS_OPERATION_COMPLETE on_cbs_operation_complete; |
XinZhangMS | 0:f7f1f0d76dd6 | 27 | void* on_cbs_operation_complete_context; |
XinZhangMS | 0:f7f1f0d76dd6 | 28 | SINGLYLINKEDLIST_HANDLE pending_operations; |
XinZhangMS | 0:f7f1f0d76dd6 | 29 | } CBS_OPERATION; |
XinZhangMS | 0:f7f1f0d76dd6 | 30 | |
XinZhangMS | 0:f7f1f0d76dd6 | 31 | typedef struct CBS_INSTANCE_TAG |
XinZhangMS | 0:f7f1f0d76dd6 | 32 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 33 | AMQP_MANAGEMENT_HANDLE amqp_management; |
XinZhangMS | 0:f7f1f0d76dd6 | 34 | CBS_STATE cbs_state; |
XinZhangMS | 0:f7f1f0d76dd6 | 35 | ON_CBS_OPEN_COMPLETE on_cbs_open_complete; |
XinZhangMS | 0:f7f1f0d76dd6 | 36 | void* on_cbs_open_complete_context; |
XinZhangMS | 0:f7f1f0d76dd6 | 37 | ON_CBS_ERROR on_cbs_error; |
XinZhangMS | 0:f7f1f0d76dd6 | 38 | void* on_cbs_error_context; |
XinZhangMS | 0:f7f1f0d76dd6 | 39 | SINGLYLINKEDLIST_HANDLE pending_operations; |
XinZhangMS | 0:f7f1f0d76dd6 | 40 | } CBS_INSTANCE; |
XinZhangMS | 0:f7f1f0d76dd6 | 41 | |
XinZhangMS | 0:f7f1f0d76dd6 | 42 | static int add_string_key_value_pair_to_map(AMQP_VALUE map, const char* key, const char* value) |
XinZhangMS | 0:f7f1f0d76dd6 | 43 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 44 | int result; |
XinZhangMS | 0:f7f1f0d76dd6 | 45 | |
XinZhangMS | 0:f7f1f0d76dd6 | 46 | AMQP_VALUE key_value = amqpvalue_create_string(key); |
XinZhangMS | 0:f7f1f0d76dd6 | 47 | if (key_value == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 48 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 49 | /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 50 | LogError("Failed creating value for property key %s", key); |
XinZhangMS | 0:f7f1f0d76dd6 | 51 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 52 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 53 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 54 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 55 | AMQP_VALUE value_value = amqpvalue_create_string(value); |
XinZhangMS | 0:f7f1f0d76dd6 | 56 | if (value_value == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 57 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 58 | /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 59 | LogError("Failed creating value for property value %s", value); |
XinZhangMS | 0:f7f1f0d76dd6 | 60 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 61 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 62 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 63 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 64 | if (amqpvalue_set_map_value(map, key_value, value_value) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 65 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 66 | /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 67 | LogError("Failed inserting key/value pair in the map"); |
XinZhangMS | 0:f7f1f0d76dd6 | 68 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 69 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 70 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 71 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 72 | result = 0; |
XinZhangMS | 0:f7f1f0d76dd6 | 73 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 74 | |
XinZhangMS | 0:f7f1f0d76dd6 | 75 | amqpvalue_destroy(value_value); |
XinZhangMS | 0:f7f1f0d76dd6 | 76 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 77 | |
XinZhangMS | 0:f7f1f0d76dd6 | 78 | amqpvalue_destroy(key_value); |
XinZhangMS | 0:f7f1f0d76dd6 | 79 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 80 | |
XinZhangMS | 0:f7f1f0d76dd6 | 81 | return result; |
XinZhangMS | 0:f7f1f0d76dd6 | 82 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 83 | |
XinZhangMS | 0:f7f1f0d76dd6 | 84 | static void on_underlying_amqp_management_open_complete(void* context, AMQP_MANAGEMENT_OPEN_RESULT open_result) |
XinZhangMS | 0:f7f1f0d76dd6 | 85 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 86 | if (context == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 87 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 88 | /* Codes_SRS_CBS_01_105: [ When `on_amqp_management_open_complete` is called with NULL `context`, it shall do nothing. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 89 | LogError("on_underlying_amqp_management_open_complete called with NULL context"); |
XinZhangMS | 0:f7f1f0d76dd6 | 90 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 91 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 92 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 93 | CBS_HANDLE cbs = (CBS_HANDLE)context; |
XinZhangMS | 0:f7f1f0d76dd6 | 94 | |
XinZhangMS | 0:f7f1f0d76dd6 | 95 | switch (cbs->cbs_state) |
XinZhangMS | 0:f7f1f0d76dd6 | 96 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 97 | default: |
XinZhangMS | 0:f7f1f0d76dd6 | 98 | LogError("AMQP management open complete in unknown state"); |
XinZhangMS | 0:f7f1f0d76dd6 | 99 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 100 | |
XinZhangMS | 0:f7f1f0d76dd6 | 101 | case CBS_STATE_CLOSED: |
XinZhangMS | 0:f7f1f0d76dd6 | 102 | case CBS_STATE_ERROR: |
XinZhangMS | 0:f7f1f0d76dd6 | 103 | LogError("Unexpected AMQP management open complete"); |
XinZhangMS | 0:f7f1f0d76dd6 | 104 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 105 | |
XinZhangMS | 0:f7f1f0d76dd6 | 106 | case CBS_STATE_OPEN: |
XinZhangMS | 0:f7f1f0d76dd6 | 107 | LogError("Unexpected AMQP management open complete in OPEN"); |
XinZhangMS | 0:f7f1f0d76dd6 | 108 | /* Codes_SRS_CBS_01_109: [ When `on_amqp_management_open_complete` is called when the CBS is OPEN, the callback `on_cbs_error` shall be called and the `on_cbs_error_context` shall be passed as argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 109 | cbs->cbs_state = CBS_STATE_ERROR; |
XinZhangMS | 0:f7f1f0d76dd6 | 110 | cbs->on_cbs_error(cbs->on_cbs_error_context); |
XinZhangMS | 0:f7f1f0d76dd6 | 111 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 112 | |
XinZhangMS | 0:f7f1f0d76dd6 | 113 | case CBS_STATE_OPENING: |
XinZhangMS | 0:f7f1f0d76dd6 | 114 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 115 | switch (open_result) |
XinZhangMS | 0:f7f1f0d76dd6 | 116 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 117 | default: |
XinZhangMS | 0:f7f1f0d76dd6 | 118 | LogError("Unknown AMQP management state"); |
XinZhangMS | 0:f7f1f0d76dd6 | 119 | |
XinZhangMS | 0:f7f1f0d76dd6 | 120 | case AMQP_MANAGEMENT_OPEN_ERROR: |
XinZhangMS | 0:f7f1f0d76dd6 | 121 | cbs->cbs_state = CBS_STATE_CLOSED; |
XinZhangMS | 0:f7f1f0d76dd6 | 122 | /* Codes_SRS_CBS_01_113: [ When `on_amqp_management_open_complete` reports a failure, the underlying AMQP management shall be closed by calling `amqp_management_close`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 123 | (void)amqp_management_close(cbs->amqp_management); |
XinZhangMS | 0:f7f1f0d76dd6 | 124 | /* Codes_SRS_CBS_01_107: [ If CBS is OPENING and `open_result` is `AMQP_MANAGEMENT_OPEN_ERROR` the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_ERROR` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 125 | cbs->on_cbs_open_complete(cbs->on_cbs_open_complete_context, CBS_OPEN_ERROR); |
XinZhangMS | 0:f7f1f0d76dd6 | 126 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 127 | |
XinZhangMS | 0:f7f1f0d76dd6 | 128 | case AMQP_MANAGEMENT_OPEN_CANCELLED: |
XinZhangMS | 0:f7f1f0d76dd6 | 129 | cbs->cbs_state = CBS_STATE_CLOSED; |
XinZhangMS | 0:f7f1f0d76dd6 | 130 | /* Codes_SRS_CBS_01_113: [ When `on_amqp_management_open_complete` reports a failure, the underlying AMQP management shall be closed by calling `amqp_management_close`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 131 | (void)amqp_management_close(cbs->amqp_management); |
XinZhangMS | 0:f7f1f0d76dd6 | 132 | /* Codes_SRS_CBS_01_108: [ If CBS is OPENING and `open_result` is `AMQP_MANAGEMENT_OPEN_CANCELLED` the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_CANCELLED` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 133 | cbs->on_cbs_open_complete(cbs->on_cbs_open_complete_context, CBS_OPEN_CANCELLED); |
XinZhangMS | 0:f7f1f0d76dd6 | 134 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 135 | |
XinZhangMS | 0:f7f1f0d76dd6 | 136 | case AMQP_MANAGEMENT_OPEN_OK: |
XinZhangMS | 0:f7f1f0d76dd6 | 137 | /* Codes_SRS_CBS_01_106: [ If CBS is OPENING and `open_result` is `AMQP_MANAGEMENT_OPEN_OK` the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_OK` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 138 | cbs->cbs_state = CBS_STATE_OPEN; |
XinZhangMS | 0:f7f1f0d76dd6 | 139 | cbs->on_cbs_open_complete(cbs->on_cbs_open_complete_context, CBS_OPEN_OK); |
XinZhangMS | 0:f7f1f0d76dd6 | 140 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 141 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 142 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 143 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 144 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 145 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 146 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 147 | |
XinZhangMS | 0:f7f1f0d76dd6 | 148 | static void on_underlying_amqp_management_error(void* context) |
XinZhangMS | 0:f7f1f0d76dd6 | 149 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 150 | if (context == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 151 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 152 | /* Codes_SRS_CBS_01_110: [ When `on_amqp_management_error` is called with NULL `context`, it shall do nothing. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 153 | LogError("on_underlying_amqp_management_error called with NULL context"); |
XinZhangMS | 0:f7f1f0d76dd6 | 154 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 155 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 156 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 157 | CBS_HANDLE cbs = (CBS_HANDLE)context; |
XinZhangMS | 0:f7f1f0d76dd6 | 158 | |
XinZhangMS | 0:f7f1f0d76dd6 | 159 | switch (cbs->cbs_state) |
XinZhangMS | 0:f7f1f0d76dd6 | 160 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 161 | default: |
XinZhangMS | 0:f7f1f0d76dd6 | 162 | LogError("AMQP management error in unknown state"); |
XinZhangMS | 0:f7f1f0d76dd6 | 163 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 164 | |
XinZhangMS | 0:f7f1f0d76dd6 | 165 | case CBS_STATE_CLOSED: |
XinZhangMS | 0:f7f1f0d76dd6 | 166 | LogError("Unexpected AMQP error in CLOSED state"); |
XinZhangMS | 0:f7f1f0d76dd6 | 167 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 168 | |
XinZhangMS | 0:f7f1f0d76dd6 | 169 | case CBS_STATE_OPENING: |
XinZhangMS | 0:f7f1f0d76dd6 | 170 | cbs->cbs_state = CBS_STATE_CLOSED; |
XinZhangMS | 0:f7f1f0d76dd6 | 171 | /* Codes_SRS_CBS_01_114: [ Additionally the underlying AMQP management shall be closed by calling `amqp_management_close`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 172 | (void)amqp_management_close(cbs->amqp_management); |
XinZhangMS | 0:f7f1f0d76dd6 | 173 | /* Codes_SRS_CBS_01_111: [ If CBS is OPENING the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_ERROR` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 174 | cbs->on_cbs_open_complete(cbs->on_cbs_open_complete_context, CBS_OPEN_ERROR); |
XinZhangMS | 0:f7f1f0d76dd6 | 175 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 176 | |
XinZhangMS | 0:f7f1f0d76dd6 | 177 | case CBS_STATE_OPEN: |
XinZhangMS | 0:f7f1f0d76dd6 | 178 | /* Codes_SRS_CBS_01_112: [ If CBS is OPEN the callback `on_cbs_error` shall be called and the `on_cbs_error_context` shall be passed as argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 179 | cbs->cbs_state = CBS_STATE_ERROR; |
XinZhangMS | 0:f7f1f0d76dd6 | 180 | cbs->on_cbs_error(cbs->on_cbs_error_context); |
XinZhangMS | 0:f7f1f0d76dd6 | 181 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 182 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 183 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 184 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 185 | |
XinZhangMS | 0:f7f1f0d76dd6 | 186 | static void on_amqp_management_execute_operation_complete(void* context, AMQP_MANAGEMENT_EXECUTE_OPERATION_RESULT execute_operation_result, unsigned int status_code, const char* status_description, MESSAGE_HANDLE message) |
XinZhangMS | 0:f7f1f0d76dd6 | 187 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 188 | if (context == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 189 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 190 | /* Codes_SRS_CBS_01_091: [ When `on_amqp_management_execute_operation_complete` is called with a NULL context it shall do nothing. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 191 | LogError("on_amqp_management_execute_operation_complete called with NULL context"); |
XinZhangMS | 0:f7f1f0d76dd6 | 192 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 193 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 194 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 195 | /* Codes_SRS_CBS_01_103: [ The `context` shall be used to obtain the pending operation information stored in the pending operations linked list by calling `singlylinkedlist_item_get_value`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 196 | CBS_OPERATION* cbs_operation = (CBS_OPERATION*)singlylinkedlist_item_get_value((LIST_ITEM_HANDLE)context); |
XinZhangMS | 0:f7f1f0d76dd6 | 197 | CBS_OPERATION_RESULT cbs_operation_result; |
XinZhangMS | 0:f7f1f0d76dd6 | 198 | |
XinZhangMS | 0:f7f1f0d76dd6 | 199 | (void)message; |
XinZhangMS | 0:f7f1f0d76dd6 | 200 | |
XinZhangMS | 0:f7f1f0d76dd6 | 201 | if (cbs_operation == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 202 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 203 | LogError("NULL cbs_operation"); |
XinZhangMS | 0:f7f1f0d76dd6 | 204 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 205 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 206 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 207 | switch (execute_operation_result) |
XinZhangMS | 0:f7f1f0d76dd6 | 208 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 209 | default: |
XinZhangMS | 0:f7f1f0d76dd6 | 210 | cbs_operation_result = CBS_OPERATION_RESULT_CBS_ERROR; |
XinZhangMS | 0:f7f1f0d76dd6 | 211 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 212 | |
XinZhangMS | 0:f7f1f0d76dd6 | 213 | case AMQP_MANAGEMENT_EXECUTE_OPERATION_FAILED_BAD_STATUS: |
XinZhangMS | 0:f7f1f0d76dd6 | 214 | /* Tests_SRS_CBS_01_094: [ When `on_amqp_management_execute_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_FAILED_BAD_STATUS`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_OPERATION_FAILED` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 215 | cbs_operation_result = CBS_OPERATION_RESULT_OPERATION_FAILED; |
XinZhangMS | 0:f7f1f0d76dd6 | 216 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 217 | |
XinZhangMS | 0:f7f1f0d76dd6 | 218 | case AMQP_MANAGEMENT_EXECUTE_OPERATION_INSTANCE_CLOSED: |
XinZhangMS | 0:f7f1f0d76dd6 | 219 | /* Tests_SRS_CBS_01_115: [ When `on_amqp_management_execute_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_INSTANCE_CLOSED`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_INSTANCE_CLOSED` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 220 | cbs_operation_result = CBS_OPERATION_RESULT_INSTANCE_CLOSED; |
XinZhangMS | 0:f7f1f0d76dd6 | 221 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 222 | |
XinZhangMS | 0:f7f1f0d76dd6 | 223 | case AMQP_MANAGEMENT_EXECUTE_OPERATION_ERROR: |
XinZhangMS | 0:f7f1f0d76dd6 | 224 | /* Tests_SRS_CBS_01_093: [ When `on_amqp_management_execute_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_ERROR`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_CBS_ERROR` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 225 | cbs_operation_result = CBS_OPERATION_RESULT_CBS_ERROR; |
XinZhangMS | 0:f7f1f0d76dd6 | 226 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 227 | |
XinZhangMS | 0:f7f1f0d76dd6 | 228 | case AMQP_MANAGEMENT_EXECUTE_OPERATION_OK: |
XinZhangMS | 0:f7f1f0d76dd6 | 229 | /* Codes_SRS_CBS_01_092: [ When `on_amqp_management_execute_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_OK`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_OK` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 230 | cbs_operation_result = CBS_OPERATION_RESULT_OK; |
XinZhangMS | 0:f7f1f0d76dd6 | 231 | break; |
XinZhangMS | 0:f7f1f0d76dd6 | 232 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 233 | |
XinZhangMS | 0:f7f1f0d76dd6 | 234 | /* Codes_SRS_CBS_01_095: [ `status_code` and `status_description` shall be passed as they are to the cbs operation complete callback. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 235 | /* Codes_SRS_CBS_01_014: [ The response message has the following application-properties: ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 236 | /* Codes_SRS_CBS_01_013: [ status-code No int HTTP response code [RFC2616]. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 237 | /* Codes_SRS_CBS_01_015: [ status-description Yes string Description of the status. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 238 | /* Codes_SRS_CBS_01_016: [ The body of the message MUST be empty. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 239 | /* Codes_SRS_CBS_01_026: [ The response message has the following application-properties: ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 240 | /* Codes_SRS_CBS_01_027: [ status-code Yes int HTTP response code [RFC2616]. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 241 | /* Codes_SRS_CBS_01_028: [ status-description No string Description of the status. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 242 | /* Codes_SRS_CBS_01_029: [ The body of the message MUST be empty. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 243 | cbs_operation->on_cbs_operation_complete(cbs_operation->on_cbs_operation_complete_context, cbs_operation_result, status_code, status_description); |
XinZhangMS | 0:f7f1f0d76dd6 | 244 | |
XinZhangMS | 0:f7f1f0d76dd6 | 245 | /* Codes_SRS_CBS_01_102: [ The pending operation shall be removed from the pending operations list by calling `singlylinkedlist_remove`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 246 | if (singlylinkedlist_remove(cbs_operation->pending_operations, (LIST_ITEM_HANDLE)context) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 247 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 248 | LogError("Failed removing operation from the pending list"); |
XinZhangMS | 0:f7f1f0d76dd6 | 249 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 250 | |
XinZhangMS | 0:f7f1f0d76dd6 | 251 | /* Codes_SRS_CBS_01_096: [ The `context` for the operation shall also be freed. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 252 | free(cbs_operation); |
XinZhangMS | 0:f7f1f0d76dd6 | 253 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 254 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 255 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 256 | |
XinZhangMS | 0:f7f1f0d76dd6 | 257 | CBS_HANDLE cbs_create(SESSION_HANDLE session) |
XinZhangMS | 0:f7f1f0d76dd6 | 258 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 259 | CBS_INSTANCE* cbs; |
XinZhangMS | 0:f7f1f0d76dd6 | 260 | |
XinZhangMS | 0:f7f1f0d76dd6 | 261 | if (session == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 262 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 263 | /* Codes_SRS_CBS_01_033: [** If `session` is NULL then `cbs_create` shall fail and return NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 264 | LogError("NULL session handle"); |
XinZhangMS | 0:f7f1f0d76dd6 | 265 | cbs = NULL; |
XinZhangMS | 0:f7f1f0d76dd6 | 266 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 267 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 268 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 269 | cbs = (CBS_INSTANCE*)malloc(sizeof(CBS_INSTANCE)); |
XinZhangMS | 0:f7f1f0d76dd6 | 270 | if (cbs == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 271 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 272 | /* Codes_SRS_CBS_01_076: [ If allocating memory for the new handle fails, `cbs_create` shall fail and return NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 273 | LogError("Cannot allocate memory for cbs instance."); |
XinZhangMS | 0:f7f1f0d76dd6 | 274 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 275 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 276 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 277 | /* Codes_SRS_CBS_01_097: [ `cbs_create` shall create a singly linked list for pending operations by calling `singlylinkedlist_create`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 278 | cbs->pending_operations = singlylinkedlist_create(); |
XinZhangMS | 0:f7f1f0d76dd6 | 279 | if (cbs->pending_operations == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 280 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 281 | /* Codes_SRS_CBS_01_101: [ If `singlylinkedlist_create` fails, `cbs_create` shall fail and return NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 282 | LogError("Cannot allocate pending operations list."); |
XinZhangMS | 0:f7f1f0d76dd6 | 283 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 284 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 285 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 286 | /* Codes_SRS_CBS_01_034: [ `cbs_create` shall create an AMQP management handle by calling `amqp_management_create`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 287 | /* Codes_SRS_CBS_01_002: [ Tokens are communicated between AMQP peers by sending specially-formatted AMQP messages to the Claims-based Security Node. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 288 | /* Codes_SRS_CBS_01_003: [ The mechanism follows the scheme defined in the AMQP Management specification [AMQPMAN]. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 289 | cbs->amqp_management = amqp_management_create(session, "$cbs"); |
XinZhangMS | 0:f7f1f0d76dd6 | 290 | if (cbs->amqp_management == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 291 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 292 | LogError("Cannot create AMQP management instance for the $cbs node."); |
XinZhangMS | 0:f7f1f0d76dd6 | 293 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 294 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 295 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 296 | /* Codes_SRS_CBS_01_116: [ If setting the override key names fails, then `cbs_create` shall fail and return NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 297 | if (amqp_management_set_override_status_code_key_name(cbs->amqp_management, "status-code") != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 298 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 299 | /* Codes_SRS_CBS_01_116: [ If setting the override key names fails, then `cbs_create` shall fail and return NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 300 | LogError("Cannot set the override status code key name"); |
XinZhangMS | 0:f7f1f0d76dd6 | 301 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 302 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 303 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 304 | /* Codes_SRS_CBS_01_118: [ `cbs_create` shall set the override status description key name on the AMQP management handle to `status-description` by calling `amqp_management_set_override_status_description_key_name`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 305 | if (amqp_management_set_override_status_description_key_name(cbs->amqp_management, "status-description") != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 306 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 307 | /* Codes_SRS_CBS_01_116: [ If setting the override key names fails, then `cbs_create` shall fail and return NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 308 | LogError("Cannot set the override status description key name"); |
XinZhangMS | 0:f7f1f0d76dd6 | 309 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 310 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 311 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 312 | /* Codes_SRS_CBS_01_001: [ `cbs_create` shall create a new CBS instance and on success return a non-NULL handle to it. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 313 | cbs->cbs_state = CBS_STATE_CLOSED; |
XinZhangMS | 0:f7f1f0d76dd6 | 314 | |
XinZhangMS | 0:f7f1f0d76dd6 | 315 | goto all_ok; |
XinZhangMS | 0:f7f1f0d76dd6 | 316 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 317 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 318 | |
XinZhangMS | 0:f7f1f0d76dd6 | 319 | amqp_management_destroy(cbs->amqp_management); |
XinZhangMS | 0:f7f1f0d76dd6 | 320 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 321 | |
XinZhangMS | 0:f7f1f0d76dd6 | 322 | singlylinkedlist_destroy(cbs->pending_operations); |
XinZhangMS | 0:f7f1f0d76dd6 | 323 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 324 | |
XinZhangMS | 0:f7f1f0d76dd6 | 325 | free(cbs); |
XinZhangMS | 0:f7f1f0d76dd6 | 326 | cbs = NULL; |
XinZhangMS | 0:f7f1f0d76dd6 | 327 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 328 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 329 | |
XinZhangMS | 0:f7f1f0d76dd6 | 330 | all_ok: |
XinZhangMS | 0:f7f1f0d76dd6 | 331 | return cbs; |
XinZhangMS | 0:f7f1f0d76dd6 | 332 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 333 | |
XinZhangMS | 0:f7f1f0d76dd6 | 334 | void cbs_destroy(CBS_HANDLE cbs) |
XinZhangMS | 0:f7f1f0d76dd6 | 335 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 336 | if (cbs == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 337 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 338 | /* Codes_SRS_CBS_01_037: [ If `cbs` is NULL, `cbs_destroy` shall do nothing. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 339 | LogError("NULL cbs handle"); |
XinZhangMS | 0:f7f1f0d76dd6 | 340 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 341 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 342 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 343 | LIST_ITEM_HANDLE first_pending_operation; |
XinZhangMS | 0:f7f1f0d76dd6 | 344 | |
XinZhangMS | 0:f7f1f0d76dd6 | 345 | /* Codes_SRS_CBS_01_100: [ If the CBS instance is not closed, all actions performed by `cbs_close` shall be performed. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 346 | if (cbs->cbs_state != CBS_STATE_CLOSED) |
XinZhangMS | 0:f7f1f0d76dd6 | 347 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 348 | (void)amqp_management_close(cbs->amqp_management); |
XinZhangMS | 0:f7f1f0d76dd6 | 349 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 350 | |
XinZhangMS | 0:f7f1f0d76dd6 | 351 | /* Codes_SRS_CBS_01_036: [ `cbs_destroy` shall free all resources associated with the handle `cbs`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 352 | /* Codes_SRS_CBS_01_038: [ `cbs_destroy` shall free the AMQP management handle created in `cbs_create` by calling `amqp_management_destroy`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 353 | amqp_management_destroy(cbs->amqp_management); |
XinZhangMS | 0:f7f1f0d76dd6 | 354 | |
XinZhangMS | 0:f7f1f0d76dd6 | 355 | /* Codes_SRS_CBS_01_099: [ All pending operations shall be freed. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 356 | while ((first_pending_operation = singlylinkedlist_get_head_item(cbs->pending_operations)) != NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 357 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 358 | CBS_OPERATION* pending_operation = (CBS_OPERATION*)singlylinkedlist_item_get_value(first_pending_operation); |
XinZhangMS | 0:f7f1f0d76dd6 | 359 | if (pending_operation != NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 360 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 361 | pending_operation->on_cbs_operation_complete(pending_operation->on_cbs_operation_complete_context, CBS_OPERATION_RESULT_INSTANCE_CLOSED, 0, NULL); |
XinZhangMS | 0:f7f1f0d76dd6 | 362 | free(pending_operation); |
XinZhangMS | 0:f7f1f0d76dd6 | 363 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 364 | |
XinZhangMS | 0:f7f1f0d76dd6 | 365 | singlylinkedlist_remove(cbs->pending_operations, first_pending_operation); |
XinZhangMS | 0:f7f1f0d76dd6 | 366 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 367 | |
XinZhangMS | 0:f7f1f0d76dd6 | 368 | /* Codes_SRS_CBS_01_098: [ `cbs_destroy` shall free the pending operations list by calling `singlylinkedlist_destroy`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 369 | singlylinkedlist_destroy(cbs->pending_operations); |
XinZhangMS | 0:f7f1f0d76dd6 | 370 | free(cbs); |
XinZhangMS | 0:f7f1f0d76dd6 | 371 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 372 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 373 | |
XinZhangMS | 0:f7f1f0d76dd6 | 374 | int cbs_open_async(CBS_HANDLE cbs, ON_CBS_OPEN_COMPLETE on_cbs_open_complete, void* on_cbs_open_complete_context, ON_CBS_ERROR on_cbs_error, void* on_cbs_error_context) |
XinZhangMS | 0:f7f1f0d76dd6 | 375 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 376 | int result; |
XinZhangMS | 0:f7f1f0d76dd6 | 377 | |
XinZhangMS | 0:f7f1f0d76dd6 | 378 | /* Codes_SRS_CBS_01_042: [ `on_cbs_open_complete_context` and `on_cbs_error_context` shall be allowed to be NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 379 | if ((cbs == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 380 | (on_cbs_open_complete == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 381 | (on_cbs_error == NULL)) |
XinZhangMS | 0:f7f1f0d76dd6 | 382 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 383 | /* Codes_SRS_CBS_01_040: [ If any of the arguments `cbs`, `on_cbs_open_complete` or `on_cbs_error` is NULL, then `cbs_open_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 384 | LogError("Bad arguments: cbs = %p, on_cbs_open_complete = %p, on_cbs_error = %p", |
XinZhangMS | 0:f7f1f0d76dd6 | 385 | cbs, on_cbs_open_complete, on_cbs_error); |
XinZhangMS | 0:f7f1f0d76dd6 | 386 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 387 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 388 | else if (cbs->cbs_state != CBS_STATE_CLOSED) |
XinZhangMS | 0:f7f1f0d76dd6 | 389 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 390 | /* Codes_SRS_CBS_01_043: [ `cbs_open_async` while already open or opening shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 391 | LogError("cbs instance already open"); |
XinZhangMS | 0:f7f1f0d76dd6 | 392 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 393 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 394 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 395 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 396 | /* Codes_SRS_CBS_01_078: [ The cbs instance shall be considered OPENING until the `on_amqp_management_open_complete` callback is called by the AMQP management instance indicating that the open has completed (succesfull or not). ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 397 | cbs->cbs_state = CBS_STATE_OPENING; |
XinZhangMS | 0:f7f1f0d76dd6 | 398 | cbs->on_cbs_open_complete = on_cbs_open_complete; |
XinZhangMS | 0:f7f1f0d76dd6 | 399 | cbs->on_cbs_open_complete_context = on_cbs_open_complete_context; |
XinZhangMS | 0:f7f1f0d76dd6 | 400 | cbs->on_cbs_error = on_cbs_error; |
XinZhangMS | 0:f7f1f0d76dd6 | 401 | cbs->on_cbs_error_context = on_cbs_error_context; |
XinZhangMS | 0:f7f1f0d76dd6 | 402 | |
XinZhangMS | 0:f7f1f0d76dd6 | 403 | /* Codes_SRS_CBS_01_039: [ `cbs_open_async` shall open the cbs communication by calling `amqp_management_open_async` on the AMQP management handle created in `cbs_create`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 404 | if (amqp_management_open_async(cbs->amqp_management, on_underlying_amqp_management_open_complete, cbs, on_underlying_amqp_management_error, cbs) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 405 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 406 | /* Codes_SRS_CBS_01_041: [ If `amqp_management_open_async` fails, shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 407 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 408 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 409 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 410 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 411 | /* Codes_SRS_CBS_01_077: [ On success, `cbs_open_async` shall return 0. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 412 | result = 0; |
XinZhangMS | 0:f7f1f0d76dd6 | 413 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 414 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 415 | |
XinZhangMS | 0:f7f1f0d76dd6 | 416 | return result; |
XinZhangMS | 0:f7f1f0d76dd6 | 417 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 418 | |
XinZhangMS | 0:f7f1f0d76dd6 | 419 | int cbs_close(CBS_HANDLE cbs) |
XinZhangMS | 0:f7f1f0d76dd6 | 420 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 421 | int result; |
XinZhangMS | 0:f7f1f0d76dd6 | 422 | |
XinZhangMS | 0:f7f1f0d76dd6 | 423 | if (cbs == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 424 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 425 | /* Codes_SRS_CBS_01_045: [ If the argument `cbs` is NULL, `cbs_close` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 426 | LogError("NULL cbs handle"); |
XinZhangMS | 0:f7f1f0d76dd6 | 427 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 428 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 429 | else if (cbs->cbs_state == CBS_STATE_CLOSED) |
XinZhangMS | 0:f7f1f0d76dd6 | 430 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 431 | /* Codes_SRS_CBS_01_047: [ `cbs_close` when closed shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 432 | /* Codes_SRS_CBS_01_048: [ `cbs_close` when not opened shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 433 | LogError("Already closed"); |
XinZhangMS | 0:f7f1f0d76dd6 | 434 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 435 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 436 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 437 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 438 | /* Codes_SRS_CBS_01_044: [ `cbs_close` shall close the CBS instance by calling `amqp_management_close` on the underlying AMQP management handle. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 439 | if (amqp_management_close(cbs->amqp_management) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 440 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 441 | /* Codes_SRS_CBS_01_046: [ If `amqp_management_close` fails, `cbs_close` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 442 | LogError("Failed closing AMQP management instance"); |
XinZhangMS | 0:f7f1f0d76dd6 | 443 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 444 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 445 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 446 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 447 | if (cbs->cbs_state == CBS_STATE_OPENING) |
XinZhangMS | 0:f7f1f0d76dd6 | 448 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 449 | /* Codes_SRS_CBS_01_079: [ If `cbs_close` is called while OPENING, the `on_cbs_open_complete` callback shall be called with `CBS_OPEN_CANCELLED`, while passing the `on_cbs_open_complete_context` as argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 450 | cbs->on_cbs_open_complete(cbs->on_cbs_open_complete_context, CBS_OPEN_CANCELLED); |
XinZhangMS | 0:f7f1f0d76dd6 | 451 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 452 | |
XinZhangMS | 0:f7f1f0d76dd6 | 453 | cbs->cbs_state = CBS_STATE_CLOSED; |
XinZhangMS | 0:f7f1f0d76dd6 | 454 | |
XinZhangMS | 0:f7f1f0d76dd6 | 455 | /* Codes_SRS_CBS_01_080: [ On success, `cbs_close` shall return 0. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 456 | result = 0; |
XinZhangMS | 0:f7f1f0d76dd6 | 457 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 458 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 459 | |
XinZhangMS | 0:f7f1f0d76dd6 | 460 | return result; |
XinZhangMS | 0:f7f1f0d76dd6 | 461 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 462 | |
XinZhangMS | 0:f7f1f0d76dd6 | 463 | int cbs_put_token_async(CBS_HANDLE cbs, const char* type, const char* audience, const char* token, ON_CBS_OPERATION_COMPLETE on_cbs_put_token_complete, void* on_cbs_put_token_complete_context) |
XinZhangMS | 0:f7f1f0d76dd6 | 464 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 465 | int result; |
XinZhangMS | 0:f7f1f0d76dd6 | 466 | |
XinZhangMS | 0:f7f1f0d76dd6 | 467 | /* Codes_SRS_CBS_01_083: [ `on_cbs_put_token_complete_context` shall be allowed to be NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 468 | if ((cbs == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 469 | (type == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 470 | (audience == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 471 | (token == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 472 | (on_cbs_put_token_complete == NULL)) |
XinZhangMS | 0:f7f1f0d76dd6 | 473 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 474 | /* Codes_SRS_CBS_01_050: [ If any of the arguments `cbs`, `type`, `audience`, `token` or `on_cbs_put_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 475 | LogError("Bad arguments: cbs = %p, type = %p, audience = %p, token = %p, on_cbs_put_token_complete = %p", |
XinZhangMS | 0:f7f1f0d76dd6 | 476 | cbs, type, audience, token, on_cbs_put_token_complete); |
XinZhangMS | 0:f7f1f0d76dd6 | 477 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 478 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 479 | else if ((cbs->cbs_state == CBS_STATE_CLOSED) || |
XinZhangMS | 0:f7f1f0d76dd6 | 480 | (cbs->cbs_state == CBS_STATE_ERROR)) |
XinZhangMS | 0:f7f1f0d76dd6 | 481 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 482 | /* Codes_SRS_CBS_01_058: [ If `cbs_put_token_async` is called when the CBS instance is not yet open or in error, it shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 483 | LogError("put token called while closed or in error"); |
XinZhangMS | 0:f7f1f0d76dd6 | 484 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 485 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 486 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 487 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 488 | /* Codes_SRS_CBS_01_049: [ `cbs_put_token_async` shall construct a request message for the `put-token` operation. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 489 | MESSAGE_HANDLE message = message_create(); |
XinZhangMS | 0:f7f1f0d76dd6 | 490 | if (message == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 491 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 492 | /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 493 | LogError("message_create failed"); |
XinZhangMS | 0:f7f1f0d76dd6 | 494 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 495 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 496 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 497 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 498 | AMQP_VALUE token_value = amqpvalue_create_string(token); |
XinZhangMS | 0:f7f1f0d76dd6 | 499 | if (token_value == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 500 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 501 | /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 502 | LogError("Failed creating token AMQP value"); |
XinZhangMS | 0:f7f1f0d76dd6 | 503 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 504 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 505 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 506 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 507 | /* Codes_SRS_CBS_01_009: [ The body of the message MUST contain the token. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 508 | if (message_set_body_amqp_value(message, token_value) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 509 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 510 | /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 511 | LogError("Failed setting the token in the message body"); |
XinZhangMS | 0:f7f1f0d76dd6 | 512 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 513 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 514 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 515 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 516 | AMQP_VALUE application_properties = amqpvalue_create_map(); |
XinZhangMS | 0:f7f1f0d76dd6 | 517 | if (application_properties == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 518 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 519 | /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 520 | LogError("Failed creating application properties map"); |
XinZhangMS | 0:f7f1f0d76dd6 | 521 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 522 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 523 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 524 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 525 | if (add_string_key_value_pair_to_map(application_properties, "name", audience) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 526 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 527 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 528 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 529 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 530 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 531 | if (message_set_application_properties(message, application_properties) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 532 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 533 | /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 534 | LogError("Failed setting message application properties"); |
XinZhangMS | 0:f7f1f0d76dd6 | 535 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 536 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 537 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 538 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 539 | CBS_OPERATION* cbs_operation = (CBS_OPERATION*)malloc(sizeof(CBS_OPERATION)); |
XinZhangMS | 0:f7f1f0d76dd6 | 540 | if (cbs_operation == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 541 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 542 | LogError("Failed allocating CBS operation instance"); |
XinZhangMS | 0:f7f1f0d76dd6 | 543 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 544 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 545 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 546 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 547 | LIST_ITEM_HANDLE list_item; |
XinZhangMS | 0:f7f1f0d76dd6 | 548 | |
XinZhangMS | 0:f7f1f0d76dd6 | 549 | cbs_operation->on_cbs_operation_complete = on_cbs_put_token_complete; |
XinZhangMS | 0:f7f1f0d76dd6 | 550 | cbs_operation->on_cbs_operation_complete_context = on_cbs_put_token_complete_context; |
XinZhangMS | 0:f7f1f0d76dd6 | 551 | cbs_operation->pending_operations = cbs->pending_operations; |
XinZhangMS | 0:f7f1f0d76dd6 | 552 | |
XinZhangMS | 0:f7f1f0d76dd6 | 553 | list_item = singlylinkedlist_add(cbs->pending_operations, cbs_operation); |
XinZhangMS | 0:f7f1f0d76dd6 | 554 | if (list_item == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 555 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 556 | free(cbs_operation); |
XinZhangMS | 0:f7f1f0d76dd6 | 557 | LogError("Failed adding pending operation to list"); |
XinZhangMS | 0:f7f1f0d76dd6 | 558 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 559 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 560 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 561 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 562 | /* Codes_SRS_CBS_01_051: [ `cbs_put_token_async` shall start the AMQP management operation by calling `amqp_management_execute_operation_async`, while passing to it: ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 563 | /* Codes_SRS_CBS_01_052: [ The `amqp_management` argument shall be the one for the AMQP management instance created in `cbs_create`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 564 | /* Codes_SRS_CBS_01_053: [ The `operation` argument shall be `put-token`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 565 | /* Codes_SRS_CBS_01_054: [ The `type` argument shall be set to the `type` argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 566 | /* Codes_SRS_CBS_01_055: [ The `locales` argument shall be set to NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 567 | /* Codes_SRS_CBS_01_056: [ The `message` argument shall be the message constructed earlier according to the CBS spec. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 568 | /* Codes_SRS_CBS_01_057: [ The arguments `on_execute_operation_complete` and `context` shall be set to a callback that is to be called by the AMQP management module when the operation is complete. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 569 | /* Codes_SRS_CBS_01_005: [ operation No string "put-token" ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 570 | /* Codes_SRS_CBS_01_006: [ Type No string The type of the token being put, e.g., "amqp:jwt". ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 571 | /* Codes_SRS_CBS_01_007: [ name No string The "audience" to which the token applies. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 572 | if (amqp_management_execute_operation_async(cbs->amqp_management, "put-token", type, NULL, message, on_amqp_management_execute_operation_complete, list_item) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 573 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 574 | singlylinkedlist_remove(cbs->pending_operations, list_item); |
XinZhangMS | 0:f7f1f0d76dd6 | 575 | free(cbs_operation); |
XinZhangMS | 0:f7f1f0d76dd6 | 576 | /* Codes_SRS_CBS_01_084: [ If `amqp_management_execute_operation_async` fails `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 577 | LogError("Failed starting AMQP management operation"); |
XinZhangMS | 0:f7f1f0d76dd6 | 578 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 579 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 580 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 581 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 582 | /* Codes_SRS_CBS_01_081: [ On success `cbs_put_token_async` shall return 0. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 583 | result = 0; |
XinZhangMS | 0:f7f1f0d76dd6 | 584 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 585 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 586 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 587 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 588 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 589 | |
XinZhangMS | 0:f7f1f0d76dd6 | 590 | amqpvalue_destroy(application_properties); |
XinZhangMS | 0:f7f1f0d76dd6 | 591 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 592 | |
XinZhangMS | 0:f7f1f0d76dd6 | 593 | amqpvalue_destroy(token_value); |
XinZhangMS | 0:f7f1f0d76dd6 | 594 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 595 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 596 | |
XinZhangMS | 0:f7f1f0d76dd6 | 597 | message_destroy(message); |
XinZhangMS | 0:f7f1f0d76dd6 | 598 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 599 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 600 | |
XinZhangMS | 0:f7f1f0d76dd6 | 601 | return result; |
XinZhangMS | 0:f7f1f0d76dd6 | 602 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 603 | |
XinZhangMS | 0:f7f1f0d76dd6 | 604 | int cbs_delete_token_async(CBS_HANDLE cbs, const char* type, const char* audience, ON_CBS_OPERATION_COMPLETE on_cbs_delete_token_complete, void* on_cbs_delete_token_complete_context) |
XinZhangMS | 0:f7f1f0d76dd6 | 605 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 606 | int result; |
XinZhangMS | 0:f7f1f0d76dd6 | 607 | |
XinZhangMS | 0:f7f1f0d76dd6 | 608 | /* Codes_SRS_CBS_01_086: [ `on_cbs_delete_token_complete_context` shall be allowed to be NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 609 | if ((cbs == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 610 | (type == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 611 | (audience == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 612 | (on_cbs_delete_token_complete == NULL)) |
XinZhangMS | 0:f7f1f0d76dd6 | 613 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 614 | /* Codes_SRS_CBS_01_060: [ If any of the arguments `cbs`, `type`, `audience` or `on_cbs_delete_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 615 | LogError("Bad arguments: cbs = %p, type = %p, audience = %p, on_cbs_delete_token_complete = %p", |
XinZhangMS | 0:f7f1f0d76dd6 | 616 | cbs, type, audience, on_cbs_delete_token_complete); |
XinZhangMS | 0:f7f1f0d76dd6 | 617 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 618 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 619 | else if ((cbs->cbs_state == CBS_STATE_CLOSED) || |
XinZhangMS | 0:f7f1f0d76dd6 | 620 | (cbs->cbs_state == CBS_STATE_ERROR)) |
XinZhangMS | 0:f7f1f0d76dd6 | 621 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 622 | /* Codes_SRS_CBS_01_067: [ If `cbs_delete_token_async` is called when the CBS instance is not yet open or in error, it shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 623 | LogError("put token called while closed or in error"); |
XinZhangMS | 0:f7f1f0d76dd6 | 624 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 625 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 626 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 627 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 628 | /* Codes_SRS_CBS_01_025: [ The body of the message MUST be empty. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 629 | /* Codes_SRS_CBS_01_059: [ `cbs_delete_token_async` shall construct a request message for the `delete-token` operation. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 630 | MESSAGE_HANDLE message = message_create(); |
XinZhangMS | 0:f7f1f0d76dd6 | 631 | if (message == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 632 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 633 | /* Codes_SRS_CBS_01_071: [ If constructing the message fails, `cbs_delete_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 634 | LogError("message_create failed"); |
XinZhangMS | 0:f7f1f0d76dd6 | 635 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 636 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 637 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 638 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 639 | AMQP_VALUE application_properties = amqpvalue_create_map(); |
XinZhangMS | 0:f7f1f0d76dd6 | 640 | if (application_properties == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 641 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 642 | /* Codes_SRS_CBS_01_071: [ If constructing the message fails, `cbs_delete_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 643 | LogError("Failed creating application properties map"); |
XinZhangMS | 0:f7f1f0d76dd6 | 644 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 645 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 646 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 647 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 648 | if (add_string_key_value_pair_to_map(application_properties, "name", audience) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 649 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 650 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 651 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 652 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 653 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 654 | /* Codes_SRS_CBS_01_021: [ The request message has the following application-properties: ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 655 | if (message_set_application_properties(message, application_properties) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 656 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 657 | /* Codes_SRS_CBS_01_071: [ If constructing the message fails, `cbs_delete_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 658 | LogError("Failed setting message application properties"); |
XinZhangMS | 0:f7f1f0d76dd6 | 659 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 660 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 661 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 662 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 663 | CBS_OPERATION* cbs_operation = (CBS_OPERATION*)malloc(sizeof(CBS_OPERATION)); |
XinZhangMS | 0:f7f1f0d76dd6 | 664 | if (cbs_operation == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 665 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 666 | LogError("Failed allocating CBS operation instance"); |
XinZhangMS | 0:f7f1f0d76dd6 | 667 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 668 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 669 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 670 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 671 | LIST_ITEM_HANDLE list_item; |
XinZhangMS | 0:f7f1f0d76dd6 | 672 | |
XinZhangMS | 0:f7f1f0d76dd6 | 673 | cbs_operation->on_cbs_operation_complete = on_cbs_delete_token_complete; |
XinZhangMS | 0:f7f1f0d76dd6 | 674 | cbs_operation->on_cbs_operation_complete_context = on_cbs_delete_token_complete_context; |
XinZhangMS | 0:f7f1f0d76dd6 | 675 | cbs_operation->pending_operations = cbs->pending_operations; |
XinZhangMS | 0:f7f1f0d76dd6 | 676 | |
XinZhangMS | 0:f7f1f0d76dd6 | 677 | list_item = singlylinkedlist_add(cbs->pending_operations, cbs_operation); |
XinZhangMS | 0:f7f1f0d76dd6 | 678 | if (list_item == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 679 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 680 | free(cbs_operation); |
XinZhangMS | 0:f7f1f0d76dd6 | 681 | LogError("Failed adding pending operation to list"); |
XinZhangMS | 0:f7f1f0d76dd6 | 682 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 683 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 684 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 685 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 686 | /* Codes_SRS_CBS_01_061: [ `cbs_delete_token_async` shall start the AMQP management operation by calling `amqp_management_execute_operation_async`, while passing to it: ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 687 | /* Codes_SRS_CBS_01_085: [ The `amqp_management` argument shall be the one for the AMQP management instance created in `cbs_create`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 688 | /* Codes_SRS_CBS_01_062: [ The `operation` argument shall be `delete-token`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 689 | /* Codes_SRS_CBS_01_063: [ The `type` argument shall be set to the `type` argument. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 690 | /* Codes_SRS_CBS_01_064: [ The `locales` argument shall be set to NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 691 | /* Codes_SRS_CBS_01_065: [ The `message` argument shall be the message constructed earlier according to the CBS spec. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 692 | /* Codes_SRS_CBS_01_066: [ The arguments `on_operation_complete` and `context` shall be set to a callback that is to be called by the AMQP management module when the operation is complete. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 693 | /* Codes_SRS_CBS_01_020: [ To instruct a peer to delete a token associated with a specific audience, a "delete-token" message can be sent to the CBS Node ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 694 | /* Codes_SRS_CBS_01_022: [ operation Yes string "delete-token" ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 695 | /* Codes_SRS_CBS_01_023: [ Type Yes string The type of the token being deleted, e.g., "amqp:jwt". ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 696 | /* Codes_SRS_CBS_01_024: [ name Yes string The "audience" of the token being deleted. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 697 | if (amqp_management_execute_operation_async(cbs->amqp_management, "delete-token", type, NULL, message, on_amqp_management_execute_operation_complete, list_item) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 698 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 699 | /* Codes_SRS_CBS_01_087: [ If `amqp_management_execute_operation_async` fails `cbs_put_token_async` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 700 | singlylinkedlist_remove(cbs->pending_operations, list_item); |
XinZhangMS | 0:f7f1f0d76dd6 | 701 | free(cbs_operation); |
XinZhangMS | 0:f7f1f0d76dd6 | 702 | LogError("Failed starting AMQP management operation"); |
XinZhangMS | 0:f7f1f0d76dd6 | 703 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 704 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 705 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 706 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 707 | /* Codes_SRS_CBS_01_082: [ On success `cbs_delete_token_async` shall return 0. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 708 | result = 0; |
XinZhangMS | 0:f7f1f0d76dd6 | 709 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 710 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 711 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 712 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 713 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 714 | |
XinZhangMS | 0:f7f1f0d76dd6 | 715 | amqpvalue_destroy(application_properties); |
XinZhangMS | 0:f7f1f0d76dd6 | 716 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 717 | |
XinZhangMS | 0:f7f1f0d76dd6 | 718 | message_destroy(message); |
XinZhangMS | 0:f7f1f0d76dd6 | 719 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 720 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 721 | return result; |
XinZhangMS | 0:f7f1f0d76dd6 | 722 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 723 | |
XinZhangMS | 0:f7f1f0d76dd6 | 724 | int cbs_set_trace(CBS_HANDLE cbs, bool trace_on) |
XinZhangMS | 0:f7f1f0d76dd6 | 725 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 726 | int result; |
XinZhangMS | 0:f7f1f0d76dd6 | 727 | |
XinZhangMS | 0:f7f1f0d76dd6 | 728 | if (cbs == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 729 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 730 | /* Codes_SRS_CBS_01_090: [ If the argument `cbs` is NULL, `cbs_set_trace` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 731 | LogError("NULL cbs handle"); |
XinZhangMS | 0:f7f1f0d76dd6 | 732 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 733 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 734 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 735 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 736 | /* Codes_SRS_CBS_01_088: [ `cbs_set_trace` shall enable or disable tracing by calling `amqp_management_set_trace` to pass down the `trace_on` value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 737 | amqp_management_set_trace(cbs->amqp_management, trace_on); |
XinZhangMS | 0:f7f1f0d76dd6 | 738 | |
XinZhangMS | 0:f7f1f0d76dd6 | 739 | /* Codes_SRS_CBS_01_089: [ On success, `cbs_set_trace` shall return 0. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 740 | result = 0; |
XinZhangMS | 0:f7f1f0d76dd6 | 741 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 742 | |
XinZhangMS | 0:f7f1f0d76dd6 | 743 | return result; |
XinZhangMS | 0:f7f1f0d76dd6 | 744 | } |