Debian Codegraph @

c/examples/receive.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/connection.h>
23 #include <proton/condition.h>
24 #include <proton/delivery.h>
25 #include <proton/link.h>
26 #include <proton/message.h>
27 #include <proton/proactor.h>
28 #include <proton/session.h>
29 #include <proton/transport.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 typedef struct app_data_t {
35 const char *host, *port;
36 const char *amqp_address;
37 const char *container_id;
38 int message_count;
39
40 pn_proactor_t *proactor;
41 int received;
42 bool finished;
43 pn_rwbytes_t msgin; /* Partially received message */
44 } app_data_t;
45
46 static const int BATCH = 1000; /* Batch size for unlimited receive */
47
48 static int exit_code = 0;
49
50 static void check_condition(pn_event_t *e, pn_condition_t *cond) {
51 if (pn_condition_is_set(cond)) {
52 fprintf(stderr, "%s: %s: %s\n", pn_event_type_name(pn_event_type(e)),
53 pn_condition_get_name(cond), pn_condition_get_description(cond));
54 pn_connection_close(pn_event_connection(e));
55 exit_code = 1;
56 }
57 }
58
59 static void decode_message(pn_rwbytes_t data) {
60 pn_message_t *m = pn_message();
61 int err = pn_message_decode(m, data.start, data.size);
62 if (!err) {
63 /* Print the decoded message */
64 pn_string_t *s = pn_string(NULL);
65 pn_inspect(pn_message_body(m), s);
66 printf("%s\n", pn_string_get(s));
67 pn_free(s);
68 pn_message_free(m);
69 free(data.start);
70 } else {
71 fprintf(stderr, "decode_message: %s\n", pn_code(err));
72 exit_code = 1;
73 }
74 }
75
76 /* Return true to continue, false to exit */
77 static bool handle(app_data_t* app, pn_event_t* event) {
78 switch (pn_event_type(event)) {
79
80 case PN_CONNECTION_INIT: {
81 pn_connection_t* c = pn_event_connection(event);
82 pn_session_t* s = pn_session(c);
83 pn_connection_set_container(c, app->container_id);
84 pn_connection_open(c);
85 pn_session_open(s);
86 {
87 pn_link_t* l = pn_receiver(s, "my_receiver");
88 pn_terminus_set_address(pn_link_source(l), app->amqp_address);
89 pn_link_open(l);
90 /* cannot receive without granting credit: */
91 pn_link_flow(l, app->message_count ? app->message_count : BATCH);
92 }
93 } break;
94
95 case PN_DELIVERY: {
96 /* A message (or part of a message) has been received */
97 pn_delivery_t *d = pn_event_delivery(event);
98 if (pn_delivery_readable(d)) {
99 pn_link_t *l = pn_delivery_link(d);
100 size_t size = pn_delivery_pending(d);
101 pn_rwbytes_t* m = &app->msgin; /* Append data to incoming message buffer */
102 int recv;
103 size_t oldsize = m->size;
104 m->size += size;
105 m->start = (char*)realloc(m->start, m->size);
106 recv = pn_link_recv(l, m->start + oldsize, m->size);
107 if (recv == PN_ABORTED) {
108 printf("Message aborted\n");
109 m->size = 0; /* Forget the data we accumulated */
110 pn_delivery_settle(d); /* Free the delivery so we can receive the next message */
111 pn_link_flow(l, 1); /* Replace credit for aborted message */
112 } else if (recv < 0 && recv != PN_EOS) { /* Unexpected error */
113 pn_condition_format(pn_link_condition(l), "broker", "PN_DELIVERY error: %s", pn_code(recv));
114 pn_link_close(l); /* Unexpected error, close the link */
115 } else if (!pn_delivery_partial(d)) { /* Message is complete */
116 decode_message(*m);
117 *m = pn_rwbytes_null; /* Reset the buffer for the next message*/
118 /* Accept the delivery */
119 pn_delivery_update(d, PN_ACCEPTED);
120 pn_delivery_settle(d); /* settle and free d */
121 if (app->message_count == 0) {
122 /* receive forever - see if more credit is needed */
123 if (pn_link_credit(l) < BATCH/2) {
124 /* Grant enough credit to bring it up to BATCH: */
125 pn_link_flow(l, BATCH - pn_link_credit(l));
126 }
127 } else if (++app->received >= app->message_count) {
128 pn_session_t *ssn = pn_link_session(l);
129 printf("%d messages received\n", app->received);
130 pn_link_close(l);
131 pn_session_close(ssn);
132 pn_connection_close(pn_session_connection(ssn));
133 }
134 }
135 }
136 break;
137 }
138
139 case PN_TRANSPORT_CLOSED:
140 check_condition(event, pn_transport_condition(pn_event_transport(event)));
141 break;
142
143 case PN_CONNECTION_REMOTE_CLOSE:
144 check_condition(event, pn_connection_remote_condition(pn_event_connection(event)));
145 pn_connection_close(pn_event_connection(event));
146 break;
147
148 case PN_SESSION_REMOTE_CLOSE:
149 check_condition(event, pn_session_remote_condition(pn_event_session(event)));
150 pn_connection_close(pn_event_connection(event));
151 break;
152
153 case PN_LINK_REMOTE_CLOSE:
154 case PN_LINK_REMOTE_DETACH:
155 check_condition(event, pn_link_remote_condition(pn_event_link(event)));
156 pn_connection_close(pn_event_connection(event));
157 break;
158
159 case PN_PROACTOR_INACTIVE:
160 return false;
161 break;
162
163 default:
164 break;
165 }
166 return true;
167 }
168
169 void run(app_data_t *app) {
170 /* Loop and handle events */
171 do {
172 pn_event_batch_t *events = pn_proactor_wait(app->proactor);
173 pn_event_t *e;
174 for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) {
175 if (!handle(app, e) || exit_code != 0) {
176 return;
177 }
178 }
179 pn_proactor_done(app->proactor, events);
180 } while(true);
181 }
182
183 int main(int argc, char **argv) {
184 struct app_data_t app = {0};
185 char addr[PN_MAX_ADDR];
186
187 app.container_id = argv[0]; /* Should be unique */
188 app.host = (argc > 1) ? argv[1] : "";
189 app.port = (argc > 2) ? argv[2] : "amqp";
190 app.amqp_address = (argc > 3) ? argv[3] : "examples";
191 app.message_count = (argc > 4) ? atoi(argv[4]) : 10;
192
193 /* Create the proactor and connect */
194 app.proactor = pn_proactor();
195 pn_proactor_addr(addr, sizeof(addr), app.host, app.port);
196 pn_proactor_connect2(app.proactor, NULL, NULL, addr);
197 run(&app);
198 pn_proactor_free(app.proactor);
199 return exit_code;
200 }