Debian Codegraph @

c/examples/direct.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include <proton/condition.h>
23 #include <proton/connection.h>
24 #include <proton/delivery.h>
25 #include <proton/link.h>
26 #include <proton/listener.h>
27 #include <proton/netaddr.h>
28 #include <proton/message.h>
29 #include <proton/proactor.h>
30 #include <proton/sasl.h>
31 #include <proton/session.h>
32 #include <proton/transport.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 typedef struct app_data_t {
38 const char *host, *port;
39 const char *amqp_address;
40 const char *container_id;
41 int message_count;
42
43 pn_proactor_t *proactor;
44 pn_listener_t *listener;
45 pn_rwbytes_t msgin, msgout; /* Buffers for incoming/outgoing messages */
46
47 /* Sender values */
48 int sent;
49 int acknowledged;
50 pn_link_t *sender;
51
52 /* Receiver values */
53 int received;
54 } app_data_t;
55
56 static const int BATCH = 1000; /* Batch size for unlimited receive */
57
58 static int exit_code = 0;
59
60 /* Close the connection and the listener so so we will get a
61 * PN_PROACTOR_INACTIVE event and exit, once all outstanding events
62 * are processed.
63 */
64 static void close_all(pn_connection_t *c, app_data_t *app) {
65 if (c) pn_connection_close(c);
66 if (app->listener) pn_listener_close(app->listener);
67 }
68
69 static void check_condition(pn_event_t *e, pn_condition_t *cond, app_data_t *app) {
70 if (pn_condition_is_set(cond)) {
71 fprintf(stderr, "%s: %s: %s\n", pn_event_type_name(pn_event_type(e)),
72 pn_condition_get_name(cond), pn_condition_get_description(cond));
73 close_all(pn_event_connection(e), app);
74 exit_code = 1;
75 }
76 }
77
78 /* Create a message with a map { "sequence" : number } encode it and return the encoded buffer. */
79 static void send_message(app_data_t *app, pn_link_t *sender) {
80 /* Construct a message with the map { "sequence": app.sent } */
81 pn_message_t* message = pn_message();
82 pn_data_t* body = pn_message_body(message);
83 pn_message_set_id(message, (pn_atom_t){.type=PN_ULONG, .u.as_ulong=app->sent});
84 pn_data_put_map(body);
85 pn_data_enter(body);
86 pn_data_put_string(body, pn_bytes(sizeof("sequence")-1, "sequence"));
87 pn_data_put_int(body, app->sent); /* The sequence number */
88 pn_data_exit(body);
89 if (pn_message_send(message, sender, &app->msgout) < 0) {
90 fprintf(stderr, "send error: %s\n", pn_error_text(pn_message_error(message)));
91 exit_code = 1;
92 }
93 pn_message_free(message);
94 }
95
96 static void decode_message(pn_rwbytes_t data) {
97 pn_message_t *m = pn_message();
98 int err = pn_message_decode(m, data.start, data.size);
99 if (!err) {
100 /* Print the decoded message */
101 pn_string_t *s = pn_string(NULL);
102 pn_inspect(pn_message_body(m), s);
103 printf("%s\n", pn_string_get(s));
104 fflush(stdout);
105 pn_free(s);
106 pn_message_free(m);
107 free(data.start);
108 } else {
109 fprintf(stderr, "decode error: %s\n", pn_error_text(pn_message_error(m)));
110 exit_code = 1;
111 }
112 }
113
114 /* This function handles events when we are acting as the receiver */
115 static void handle_receive(app_data_t *app, pn_event_t* event) {
116 switch (pn_event_type(event)) {
117
118 case PN_LINK_REMOTE_OPEN: {
119 pn_link_t *l = pn_event_link(event);
120 pn_link_open(l);
121 pn_link_flow(l, app->message_count ? app->message_count : BATCH);
122 } break;
123
124 case PN_DELIVERY: { /* Incoming message data */
125 pn_delivery_t *d = pn_event_delivery(event);
126 if (pn_delivery_readable(d)) {
127 pn_link_t *l = pn_delivery_link(d);
128 size_t size = pn_delivery_pending(d);
129 pn_rwbytes_t* m = &app->msgin; /* Append data to incoming message buffer */
130 ssize_t recv;
131 m->size += size;
132 m->start = (char*)realloc(m->start, m->size);
133 recv = pn_link_recv(l, m->start, m->size);
134 if (recv == PN_ABORTED) {
135 printf("Message aborted\n");
136 fflush(stdout);
137 m->size = 0; /* Forget the data we accumulated */
138 pn_delivery_settle(d); /* Free the delivery so we can receive the next message */
139 pn_link_flow(l, 1); /* Replace credit for aborted message */
140 } else if (recv < 0 && recv != PN_EOS) { /* Unexpected error */
141 pn_condition_format(pn_link_condition(l), "broker", "PN_DELIVERY error: %s", pn_code(recv));
142 pn_link_close(l); /* Unexpected error, close the link */
143 } else if (!pn_delivery_partial(d)) { /* Message is complete */
144 decode_message(*m);
145 *m = pn_rwbytes_null;
146 pn_delivery_update(d, PN_ACCEPTED);
147 pn_delivery_settle(d); /* settle and free d */
148 if (app->message_count == 0) {
149 /* receive forever - see if more credit is needed */
150 if (pn_link_credit(l) < BATCH/2) {
151 pn_link_flow(l, BATCH - pn_link_credit(l));
152 }
153 } else if (++app->received >= app->message_count) {
154 printf("%d messages received\n", app->received);
155 close_all(pn_event_connection(event), app);
156 }
157 }
158 }
159 break;
160 }
161 default:
162 break;
163 }
164 }
165
166 /* This function handles events when we are acting as the sender */
167 static void handle_send(app_data_t* app, pn_event_t* event) {
168 switch (pn_event_type(event)) {
169
170 case PN_LINK_REMOTE_OPEN: {
171 pn_link_t* l = pn_event_link(event);
172 pn_terminus_set_address(pn_link_target(l), app->amqp_address);
173 pn_link_open(l);
174 } break;
175
176 case PN_LINK_FLOW: {
177 /* The peer has given us some credit, now we can send messages */
178 pn_link_t *sender = pn_event_link(event);
179 while (pn_link_credit(sender) > 0 && app->sent < app->message_count) {
180 ++app->sent;
181 /* Use sent counter as unique delivery tag. */
182 pn_delivery(sender, pn_dtag((const char *)&app->sent, sizeof(app->sent)));
183 send_message(app, sender);
184 }
185 break;
186 }
187
188 case PN_DELIVERY: {
189 /* We received acknowledgement from the peer that a message was delivered. */
190 pn_delivery_t* d = pn_event_delivery(event);
191 if (pn_delivery_remote_state(d) == PN_ACCEPTED) {
192 if (++app->acknowledged == app->message_count) {
193 printf("%d messages sent and acknowledged\n", app->acknowledged);
194 close_all(pn_event_connection(event), app);
195 }
196 }
197 } break;
198
199 default:
200 break;
201 }
202 }
203
204 /* Handle all events, delegate to handle_send or handle_receive depending on link mode.
205 Return true to continue, false to exit
206 */
207 static bool handle(app_data_t* app, pn_event_t* event) {
208 switch (pn_event_type(event)) {
209
210 case PN_LISTENER_OPEN: {
211 char port[256]; /* Get the listening port */
212 pn_netaddr_host_port(pn_listener_addr(pn_event_listener(event)), NULL, 0, port, sizeof(port));
213 printf("listening on %s\n", port);
214 fflush(stdout);
215 break;
216 }
217 case PN_LISTENER_ACCEPT:
218 pn_listener_accept2(pn_event_listener(event), NULL, NULL);
219 break;
220
221 case PN_CONNECTION_INIT:
222 pn_connection_set_container(pn_event_connection(event), app->container_id);
223 break;
224
225 case PN_CONNECTION_BOUND: {
226 /* Turn off security */
227 pn_transport_t *t = pn_event_transport(event);
228 pn_transport_require_auth(t, false);
229 pn_sasl_allowed_mechs(pn_sasl(t), "ANONYMOUS");
230 break;
231 }
232 case PN_CONNECTION_REMOTE_OPEN: {
233 pn_connection_open(pn_event_connection(event)); /* Complete the open */
234 break;
235 }
236
237 case PN_SESSION_REMOTE_OPEN: {
238 pn_session_open(pn_event_session(event));
239 break;
240 }
241
242 case PN_TRANSPORT_CLOSED:
243 check_condition(event, pn_transport_condition(pn_event_transport(event)), app);
244 break;
245
246 case PN_CONNECTION_REMOTE_CLOSE:
247 check_condition(event, pn_connection_remote_condition(pn_event_connection(event)), app);
248 pn_connection_close(pn_event_connection(event)); /* Return the close */
249 break;
250
251 case PN_SESSION_REMOTE_CLOSE:
252 check_condition(event, pn_session_remote_condition(pn_event_session(event)), app);
253 pn_session_close(pn_event_session(event)); /* Return the close */
254 pn_session_free(pn_event_session(event));
255 break;
256
257 case PN_LINK_REMOTE_CLOSE:
258 case PN_LINK_REMOTE_DETACH:
259 check_condition(event, pn_link_remote_condition(pn_event_link(event)), app);
260 pn_link_close(pn_event_link(event)); /* Return the close */
261 pn_link_free(pn_event_link(event));
262 break;
263
264 case PN_PROACTOR_TIMEOUT:
265 /* Wake the sender's connection */
266 pn_connection_wake(pn_session_connection(pn_link_session(app->sender)));
267 break;
268
269 case PN_LISTENER_CLOSE:
270 app->listener = NULL; /* Listener is closed */
271 check_condition(event, pn_listener_condition(pn_event_listener(event)), app);
272 break;
273
274 case PN_PROACTOR_INACTIVE:
275 return false;
276 break;
277
278 default: {
279 pn_link_t *l = pn_event_link(event);
280 if (l) { /* Only delegate link-related events */
281 if (pn_link_is_sender(l)) {
282 handle_send(app, event);
283 } else {
284 handle_receive(app, event);
285 }
286 }
287 }
288 }
289 return exit_code == 0;
290 }
291
292 void run(app_data_t *app) {
293 /* Loop and handle events */
294 do {
295 pn_event_batch_t *events = pn_proactor_wait(app->proactor);
296 pn_event_t *e;
297 for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) {
298 if (!handle(app, e)) {
299 return;
300 }
301 }
302 pn_proactor_done(app->proactor, events);
303 } while(true);
304 }
305
306 int main(int argc, char **argv) {
307 struct app_data_t app = {0};
308 char addr[PN_MAX_ADDR];
309 app.container_id = argv[0]; /* Should be unique */
310 app.host = (argc > 1) ? argv[1] : "";
311 app.port = (argc > 2) ? argv[2] : "amqp";
312 app.amqp_address = (argc > 3) ? argv[3] : "examples";
313 app.message_count = (argc > 4) ? atoi(argv[4]) : 10;
314
315 /* Create the proactor and connect */
316 app.proactor = pn_proactor();
317 app.listener = pn_listener();
318 pn_proactor_addr(addr, sizeof(addr), app.host, app.port);
319 pn_proactor_listen(app.proactor, app.listener, addr, 16);
320 run(&app);
321 pn_proactor_free(app.proactor);
322 free(app.msgout.start);
323 free(app.msgin.start);
324 return exit_code;
325 }