Debian Codegraph @

src/vendor/azure-uamqp-c/samples/local_client_sample/main.c

raw
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/socketio.h"
10 #include "azure_c_shared_utility/tickcounter.h"
11 #include "azure_uamqp_c/uamqp.h"
12
13 #if _WIN32
14 #include "windows.h"
15 #endif
16
17 static const size_t msg_count = 1000;
18 static unsigned int sent_messages = 0;
19
20 static void on_message_send_complete(void* context, MESSAGE_SEND_RESULT send_result, AMQP_VALUE delivery_state)
21 {
22 (void)send_result;
23 (void)context;
24 (void)delivery_state;
25
26 sent_messages++;
27 }
28
29 int main(int argc, char** argv)
30 {
31 int result;
32
33 (void)argc;
34 (void)argv;
35
36 if (platform_init() != 0)
37 {
38 result = -1;
39 }
40 else
41 {
42 CONNECTION_HANDLE connection;
43 SESSION_HANDLE session;
44 LINK_HANDLE link;
45 MESSAGE_SENDER_HANDLE message_sender;
46 MESSAGE_HANDLE message;
47 AMQP_VALUE source;
48 AMQP_VALUE target;
49 unsigned char hello[] = { 'H', 'e', 'l', 'l', 'o' };
50 BINARY_DATA binary_data;
51
52 size_t last_memory_used = 0;
53
54 XIO_HANDLE socket_io;
55 SOCKETIO_CONFIG socketio_config = { "localhost", 5672, NULL };
56
57 gballoc_init();
58
59 /* create socket IO */
60 socket_io = xio_create(socketio_get_interface_description(), &socketio_config);
61
62 /* create the connection, session and link */
63 connection = connection_create(socket_io, "localhost", "some", NULL, NULL);
64 session = session_create(connection, NULL, NULL);
65 session_set_incoming_window(session, 2147483647);
66 session_set_outgoing_window(session, 65536);
67
68 source = messaging_create_source("ingress");
69 target = messaging_create_target("localhost/ingress");
70 link = link_create(session, "sender-link", role_sender, source, target);
71 link_set_snd_settle_mode(link, sender_settle_mode_settled);
72 (void)link_set_max_message_size(link, 65536);
73
74 amqpvalue_destroy(source);
75 amqpvalue_destroy(target);
76
77 message = message_create();
78
79 binary_data.bytes = hello;
80 binary_data.length = sizeof(hello);
81 message_add_body_amqp_data(message, binary_data);
82
83 /* create a message sender */
84 message_sender = messagesender_create(link, NULL, NULL);
85 if (messagesender_open(message_sender) == 0)
86 {
87 uint32_t i;
88 bool keep_running = true;
89 tickcounter_ms_t start_time;
90 TICK_COUNTER_HANDLE tick_counter = tickcounter_create();
91
92 if (tickcounter_get_current_ms(tick_counter, &start_time) != 0)
93 {
94 (void)printf("Error getting start time\r\n");
95 }
96 else
97 {
98 for (i = 0; i < msg_count; i++)
99 {
100 (void)messagesender_send_async(message_sender, message, on_message_send_complete, message, 0);
101 }
102
103 message_destroy(message);
104
105 while (keep_running)
106 {
107 size_t current_memory_used;
108 size_t maximum_memory_used;
109 connection_dowork(connection);
110
111 current_memory_used = gballoc_getCurrentMemoryUsed();
112 maximum_memory_used = gballoc_getMaximumMemoryUsed();
113
114 if (current_memory_used != last_memory_used)
115 {
116 (void)printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used);
117 last_memory_used = current_memory_used;
118 }
119
120 if (sent_messages == msg_count)
121 {
122 break;
123 }
124 }
125
126 {
127 tickcounter_ms_t end_time;
128 if (tickcounter_get_current_ms(tick_counter, &end_time) != 0)
129 {
130 (void)printf("Error getting end time\r\n");
131 }
132 else
133 {
134 (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));
135 }
136 }
137 }
138 }
139
140 messagesender_destroy(message_sender);
141 link_destroy(link);
142 session_destroy(session);
143 connection_destroy(connection);
144 xio_destroy(socket_io);
145 platform_deinit();
146
147 (void)printf("Max memory usage:%lu\r\n", (unsigned long)gballoc_getCurrentMemoryUsed());
148 (void)printf("Current memory usage:%lu\r\n", (unsigned long)gballoc_getMaximumMemoryUsed());
149
150 result = 0;
151
152 gballoc_deinit();
153 }
154
155 return result;
156 }