| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <stdbool.h> |
| 7 |
#include <time.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include "azure_c_shared_utility/gballoc.h" |
| 11 |
#include "azure_c_shared_utility/platform.h" |
| 12 |
#include "azure_c_shared_utility/strings.h" |
| 13 |
#include "azure_c_shared_utility/buffer_.h" |
| 14 |
#include "azure_c_shared_utility/azure_base64.h" |
| 15 |
#include "azure_c_shared_utility/urlencode.h" |
| 16 |
#include "azure_c_shared_utility/sastoken.h" |
| 17 |
#include "azure_c_shared_utility/tlsio.h" |
| 18 |
#include "azure_c_shared_utility/tickcounter.h" |
| 19 |
#include "azure_uamqp_c/uamqp.h" |
| 20 |
|
| 21 |
#if _WIN32 |
| 22 |
#include "windows.h" |
| 23 |
#endif |
| 24 |
|
| 25 |
/* This sample connects to an Event Hub, authenticates using SASL MSSBCBS (SAS token given by a put-token) and sends 1 message to the EH specifying a publisher ID */ |
| 26 |
/* The SAS token is generated based on the policy name/key */ |
| 27 |
/* Replace the below settings with your own.*/ |
| 28 |
|
| 29 |
#define EH_HOST "<<<Replace with your own EH host (like myeventhub.servicebus.windows.net)>>>" |
| 30 |
#define EH_KEY_NAME "<<<Replace with your own key name>>>" |
| 31 |
#define EH_KEY "<<<Replace with your own key>>>" |
| 32 |
#define EH_NAME "<<<Replace with your own EH name (like ingress_eh)>>>" |
| 33 |
|
| 34 |
#define EH_PUBLISHER "test_publisher" |
| 35 |
|
| 36 |
static const size_t msg_count = 1; |
| 37 |
static unsigned int sent_messages = 0; |
| 38 |
static bool auth = false; |
| 39 |
|
| 40 |
static void on_cbs_open_complete(void* context, CBS_OPEN_COMPLETE_RESULT open_complete_result) |
| 41 |
{ |
| 42 |
(void)context; |
| 43 |
(void)open_complete_result; |
| 44 |
(void)printf("CBS instance open.\r\n"); |
| 45 |
} |
| 46 |
|
| 47 |
static void on_cbs_error(void* context) |
| 48 |
{ |
| 49 |
(void)context; |
| 50 |
(void)printf("CBS error.\r\n"); |
| 51 |
} |
| 52 |
|
| 53 |
static void on_cbs_put_token_complete(void* context, CBS_OPERATION_RESULT cbs_operation_result, unsigned int status_code, const char* status_description) |
| 54 |
{ |
| 55 |
(void)context; |
| 56 |
(void)status_code; |
| 57 |
(void)status_description; |
| 58 |
|
| 59 |
if (cbs_operation_result == CBS_OPERATION_RESULT_OK) |
| 60 |
{ |
| 61 |
auth = true; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
static void on_message_send_complete(void* context, MESSAGE_SEND_RESULT send_result, AMQP_VALUE delivery_state) |
| 66 |
{ |
| 67 |
(void)send_result; |
| 68 |
(void)context; |
| 69 |
(void)delivery_state; |
| 70 |
|
| 71 |
sent_messages++; |
| 72 |
} |
| 73 |
|
| 74 |
int main(int argc, char** argv) |
| 75 |
{ |
| 76 |
int result; |
| 77 |
|
| 78 |
(void)argc; |
| 79 |
(void)argv; |
| 80 |
|
| 81 |
if (platform_init() != 0) |
| 82 |
{ |
| 83 |
result = -1; |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
XIO_HANDLE sasl_io; |
| 88 |
CONNECTION_HANDLE connection; |
| 89 |
SESSION_HANDLE session; |
| 90 |
LINK_HANDLE link; |
| 91 |
MESSAGE_SENDER_HANDLE message_sender; |
| 92 |
MESSAGE_HANDLE message; |
| 93 |
|
| 94 |
size_t last_memory_used = 0; |
| 95 |
|
| 96 |
/* create SASL PLAIN handler */ |
| 97 |
SASL_MECHANISM_HANDLE sasl_mechanism_handle = saslmechanism_create(saslmssbcbs_get_interface(), NULL); |
| 98 |
XIO_HANDLE tls_io; |
| 99 |
STRING_HANDLE sas_key_name; |
| 100 |
STRING_HANDLE sas_key_value; |
| 101 |
STRING_HANDLE resource_uri; |
| 102 |
STRING_HANDLE encoded_resource_uri; |
| 103 |
STRING_HANDLE sas_token; |
| 104 |
BUFFER_HANDLE buffer; |
| 105 |
TLSIO_CONFIG tls_io_config = { EH_HOST, 5671 }; |
| 106 |
const IO_INTERFACE_DESCRIPTION* tlsio_interface; |
| 107 |
SASLCLIENTIO_CONFIG sasl_io_config; |
| 108 |
time_t currentTime; |
| 109 |
size_t expiry_time; |
| 110 |
CBS_HANDLE cbs; |
| 111 |
AMQP_VALUE source; |
| 112 |
AMQP_VALUE target; |
| 113 |
unsigned char hello[] = { 'H', 'e', 'l', 'l', 'o' }; |
| 114 |
BINARY_DATA binary_data; |
| 115 |
|
| 116 |
gballoc_init(); |
| 117 |
|
| 118 |
/* create the TLS IO */ |
| 119 |
tlsio_interface = platform_get_default_tlsio(); |
| 120 |
tls_io = xio_create(tlsio_interface, &tls_io_config); |
| 121 |
|
| 122 |
/* create the SASL client IO using the TLS IO */ |
| 123 |
sasl_io_config.underlying_io = tls_io; |
| 124 |
sasl_io_config.sasl_mechanism = sasl_mechanism_handle; |
| 125 |
sasl_io = xio_create(saslclientio_get_interface_description(), &sasl_io_config); |
| 126 |
|
| 127 |
/* create the connection, session and link */ |
| 128 |
connection = connection_create(sasl_io, EH_HOST, "some", NULL, NULL); |
| 129 |
session = session_create(connection, NULL, NULL); |
| 130 |
session_set_incoming_window(session, 2147483647); |
| 131 |
session_set_outgoing_window(session, 65536); |
| 132 |
|
| 133 |
/* Construct a SAS token */ |
| 134 |
sas_key_name = STRING_construct(EH_KEY_NAME); |
| 135 |
|
| 136 |
/* unfortunately SASToken wants an encoded key - this should be fixed at a later time */ |
| 137 |
buffer = BUFFER_create((unsigned char*)EH_KEY, strlen(EH_KEY)); |
| 138 |
sas_key_value = Azure_Base64_Encode(buffer); |
| 139 |
BUFFER_delete(buffer); |
| 140 |
resource_uri = STRING_construct("sb://" EH_HOST "/" EH_NAME "/publishers/" EH_PUBLISHER); |
| 141 |
encoded_resource_uri = URL_EncodeString(STRING_c_str(resource_uri)); |
| 142 |
|
| 143 |
/* Make a token that expires in one hour */ |
| 144 |
currentTime = time(NULL); |
| 145 |
expiry_time = (size_t)(difftime(currentTime, 0) + 3600); |
| 146 |
|
| 147 |
sas_token = SASToken_Create(sas_key_value, encoded_resource_uri, sas_key_name, expiry_time); |
| 148 |
|
| 149 |
cbs = cbs_create(session); |
| 150 |
if (cbs_open_async(cbs, on_cbs_open_complete, cbs, on_cbs_error, cbs) == 0) |
| 151 |
{ |
| 152 |
(void)cbs_put_token_async(cbs, "servicebus.windows.net:sastoken", "sb://" EH_HOST "/" EH_NAME "/publishers/" EH_PUBLISHER, STRING_c_str(sas_token), on_cbs_put_token_complete, cbs); |
| 153 |
|
| 154 |
while (!auth) |
| 155 |
{ |
| 156 |
size_t current_memory_used; |
| 157 |
size_t maximum_memory_used; |
| 158 |
connection_dowork(connection); |
| 159 |
|
| 160 |
current_memory_used = gballoc_getCurrentMemoryUsed(); |
| 161 |
maximum_memory_used = gballoc_getMaximumMemoryUsed(); |
| 162 |
|
| 163 |
if (current_memory_used != last_memory_used) |
| 164 |
{ |
| 165 |
(void)printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used); |
| 166 |
last_memory_used = current_memory_used; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
STRING_delete(sas_token); |
| 172 |
STRING_delete(sas_key_name); |
| 173 |
STRING_delete(sas_key_value); |
| 174 |
STRING_delete(resource_uri); |
| 175 |
STRING_delete(encoded_resource_uri); |
| 176 |
|
| 177 |
source = messaging_create_source("ingress"); |
| 178 |
target = messaging_create_target("amqps://" EH_HOST "/" EH_NAME "/publishers/" EH_PUBLISHER); |
| 179 |
link = link_create(session, "sender-link", role_sender, source, target); |
| 180 |
link_set_snd_settle_mode(link, sender_settle_mode_settled); |
| 181 |
(void)link_set_max_message_size(link, 65536); |
| 182 |
|
| 183 |
amqpvalue_destroy(source); |
| 184 |
amqpvalue_destroy(target); |
| 185 |
|
| 186 |
message = message_create(); |
| 187 |
|
| 188 |
binary_data.bytes = hello; |
| 189 |
binary_data.length = sizeof(hello); |
| 190 |
message_add_body_amqp_data(message, binary_data); |
| 191 |
|
| 192 |
/* create a message sender */ |
| 193 |
message_sender = messagesender_create(link, NULL, NULL); |
| 194 |
if (messagesender_open(message_sender) == 0) |
| 195 |
{ |
| 196 |
uint32_t i; |
| 197 |
bool keep_running = true; |
| 198 |
tickcounter_ms_t start_time; |
| 199 |
TICK_COUNTER_HANDLE tick_counter = tickcounter_create(); |
| 200 |
|
| 201 |
if (tickcounter_get_current_ms(tick_counter, &start_time) != 0) |
| 202 |
{ |
| 203 |
(void)printf("Error getting start time\r\n"); |
| 204 |
} |
| 205 |
else |
| 206 |
{ |
| 207 |
for (i = 0; i < msg_count; i++) |
| 208 |
{ |
| 209 |
/* timeout if it takes longer than 10s */ |
| 210 |
(void)messagesender_send_async(message_sender, message, on_message_send_complete, message, 10000); |
| 211 |
} |
| 212 |
|
| 213 |
message_destroy(message); |
| 214 |
|
| 215 |
while (keep_running) |
| 216 |
{ |
| 217 |
size_t current_memory_used; |
| 218 |
size_t maximum_memory_used; |
| 219 |
connection_dowork(connection); |
| 220 |
|
| 221 |
current_memory_used = gballoc_getCurrentMemoryUsed(); |
| 222 |
maximum_memory_used = gballoc_getMaximumMemoryUsed(); |
| 223 |
|
| 224 |
if (current_memory_used != last_memory_used) |
| 225 |
{ |
| 226 |
(void)printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used); |
| 227 |
last_memory_used = current_memory_used; |
| 228 |
} |
| 229 |
|
| 230 |
if (sent_messages == msg_count) |
| 231 |
{ |
| 232 |
break; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
{ |
| 237 |
tickcounter_ms_t end_time; |
| 238 |
if (tickcounter_get_current_ms(tick_counter, &end_time) != 0) |
| 239 |
{ |
| 240 |
(void)printf("Error getting end time\r\n"); |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
(void)printf("Send %u messages in %lu ms: %.02f msgs/sec\r\n", (unsigned int)msg_count, (unsigned long)(end_time - start_time), (float)msg_count / ((float)(end_time - start_time) / 1000)); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
tickcounter_destroy(tick_counter); |
| 250 |
} |
| 251 |
|
| 252 |
cbs_destroy(cbs); |
| 253 |
messagesender_destroy(message_sender); |
| 254 |
link_destroy(link); |
| 255 |
session_destroy(session); |
| 256 |
connection_destroy(connection); |
| 257 |
xio_destroy(sasl_io); |
| 258 |
xio_destroy(tls_io); |
| 259 |
saslmechanism_destroy(sasl_mechanism_handle); |
| 260 |
platform_deinit(); |
| 261 |
|
| 262 |
(void)printf("Max memory usage:%lu\r\n", (unsigned long)gballoc_getCurrentMemoryUsed()); |
| 263 |
(void)printf("Current memory usage:%lu\r\n", (unsigned long)gballoc_getMaximumMemoryUsed()); |
| 264 |
|
| 265 |
gballoc_deinit(); |
| 266 |
|
| 267 |
result = 0; |
| 268 |
} |
| 269 |
|
| 270 |
return result; |
| 271 |
} |