| 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 "azure_c_shared_utility/gballoc.h" |
| 8 |
#include "azure_c_shared_utility/platform.h" |
| 9 |
#include "azure_c_shared_utility/tlsio.h" |
| 10 |
#include "azure_uamqp_c/uamqp.h" |
| 11 |
|
| 12 |
/* This sample connects to an Event Hub, authenticates using SASL PLAIN (key name/key) and then it received all messages for partition 0 */ |
| 13 |
/* Replace the below settings with your own.*/ |
| 14 |
|
| 15 |
#define EH_HOST "<<<Replace with your own EH host (like myeventhub.servicebus.windows.net)>>>" |
| 16 |
#define EH_KEY_NAME "<<<Replace with your own key name>>>" |
| 17 |
#define EH_KEY "<<<Replace with your own key>>>" |
| 18 |
#define EH_NAME "<<<Insert your event hub name here>>>" |
| 19 |
|
| 20 |
static AMQP_VALUE on_message_received(const void* context, MESSAGE_HANDLE message) |
| 21 |
{ |
| 22 |
(void)context; |
| 23 |
(void)message; |
| 24 |
|
| 25 |
(void)printf("Message received.\r\n"); |
| 26 |
|
| 27 |
return messaging_delivery_accepted(); |
| 28 |
} |
| 29 |
|
| 30 |
int main(int argc, char** argv) |
| 31 |
{ |
| 32 |
int result; |
| 33 |
|
| 34 |
XIO_HANDLE sasl_io = NULL; |
| 35 |
CONNECTION_HANDLE connection = NULL; |
| 36 |
SESSION_HANDLE session = NULL; |
| 37 |
LINK_HANDLE link = NULL; |
| 38 |
MESSAGE_RECEIVER_HANDLE message_receiver = NULL; |
| 39 |
|
| 40 |
(void)argc; |
| 41 |
(void)argv; |
| 42 |
|
| 43 |
if (platform_init() != 0) |
| 44 |
{ |
| 45 |
result = -1; |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
size_t last_memory_used = 0; |
| 50 |
AMQP_VALUE source; |
| 51 |
AMQP_VALUE target; |
| 52 |
SASL_PLAIN_CONFIG sasl_plain_config; |
| 53 |
SASL_MECHANISM_HANDLE sasl_mechanism_handle; |
| 54 |
TLSIO_CONFIG tls_io_config; |
| 55 |
const IO_INTERFACE_DESCRIPTION* tlsio_interface; |
| 56 |
XIO_HANDLE tls_io; |
| 57 |
SASLCLIENTIO_CONFIG sasl_io_config; |
| 58 |
|
| 59 |
gballoc_init(); |
| 60 |
|
| 61 |
/* create SASL plain handler */ |
| 62 |
sasl_plain_config.authcid = EH_KEY_NAME; |
| 63 |
sasl_plain_config.authzid = NULL; |
| 64 |
sasl_plain_config.passwd = EH_KEY; |
| 65 |
|
| 66 |
sasl_mechanism_handle = saslmechanism_create(saslplain_get_interface(), &sasl_plain_config); |
| 67 |
|
| 68 |
/* create the TLS IO */ |
| 69 |
tls_io_config.hostname = EH_HOST; |
| 70 |
tls_io_config.port = 5671; |
| 71 |
tls_io_config.underlying_io_interface = NULL; |
| 72 |
tls_io_config.underlying_io_parameters = NULL; |
| 73 |
|
| 74 |
tlsio_interface = platform_get_default_tlsio(); |
| 75 |
tls_io = xio_create(tlsio_interface, &tls_io_config); |
| 76 |
|
| 77 |
/* create the SASL client IO using the TLS IO */ |
| 78 |
sasl_io_config.underlying_io = tls_io; |
| 79 |
sasl_io_config.sasl_mechanism = sasl_mechanism_handle; |
| 80 |
sasl_io = xio_create(saslclientio_get_interface_description(), &sasl_io_config); |
| 81 |
|
| 82 |
/* create the connection, session and link */ |
| 83 |
connection = connection_create(sasl_io, EH_HOST, "whatever", NULL, NULL); |
| 84 |
session = session_create(connection, NULL, NULL); |
| 85 |
|
| 86 |
/* set incoming window to 100 for the session */ |
| 87 |
session_set_incoming_window(session, 100); |
| 88 |
|
| 89 |
/* listen only on partition 0 */ |
| 90 |
source = messaging_create_source("amqps://" EH_HOST "/" EH_NAME "/ConsumerGroups/$Default/Partitions/0"); |
| 91 |
target = messaging_create_target("ingress-rx"); |
| 92 |
link = link_create(session, "receiver-link", role_receiver, source, target); |
| 93 |
link_set_rcv_settle_mode(link, receiver_settle_mode_first); |
| 94 |
amqpvalue_destroy(source); |
| 95 |
amqpvalue_destroy(target); |
| 96 |
|
| 97 |
/* create a message receiver */ |
| 98 |
message_receiver = messagereceiver_create(link, NULL, NULL); |
| 99 |
if ((message_receiver == NULL) || |
| 100 |
(messagereceiver_open(message_receiver, on_message_received, message_receiver) != 0)) |
| 101 |
{ |
| 102 |
(void)printf("Cannot open the message receiver."); |
| 103 |
result = -1; |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
bool keep_running = true; |
| 108 |
while (keep_running) |
| 109 |
{ |
| 110 |
size_t current_memory_used; |
| 111 |
size_t maximum_memory_used; |
| 112 |
connection_dowork(connection); |
| 113 |
|
| 114 |
current_memory_used = gballoc_getCurrentMemoryUsed(); |
| 115 |
maximum_memory_used = gballoc_getMaximumMemoryUsed(); |
| 116 |
|
| 117 |
if (current_memory_used != last_memory_used) |
| 118 |
{ |
| 119 |
(void)printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used); |
| 120 |
last_memory_used = current_memory_used; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
result = 0; |
| 125 |
} |
| 126 |
|
| 127 |
messagereceiver_destroy(message_receiver); |
| 128 |
link_destroy(link); |
| 129 |
session_destroy(session); |
| 130 |
connection_destroy(connection); |
| 131 |
platform_deinit(); |
| 132 |
|
| 133 |
(void)printf("Max memory usage:%lu\r\n", (unsigned long)gballoc_getCurrentMemoryUsed()); |
| 134 |
(void)printf("Current memory usage:%lu\r\n", (unsigned long)gballoc_getMaximumMemoryUsed()); |
| 135 |
|
| 136 |
gballoc_deinit(); |
| 137 |
} |
| 138 |
|
| 139 |
return result; |
| 140 |
} |