This repository was archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdte_consumer.c
388 lines (346 loc) · 13.3 KB
/
dte_consumer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* dte_consumer
*
* This Sample demonstrates creating a durable subscription
* with a topic using an AMQP address prefix and consuming
* messages from the subscription.
*/
#include <proton/connection.h>
#include <proton/condition.h>
#include <proton/delivery.h>
#include <proton/link.h>
#include <proton/message.h>
#include <proton/proactor.h>
#include <proton/session.h>
#include <proton/transport.h>
#include <proton/sasl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "util.h"
typedef struct app_data_t {
const char *host, *port;
const char *username, *password;
const char *subscription_name;
const char *amqp_address;
char *amqp_address_prefix;
const char *container_id;
int message_count;
pn_proactor_t *proactor;
int received;
bool finished;
pn_rwbytes_t msgin; /* Partially received message */
} app_data_t;
static const int BATCH = 1000; /* Batch size for unlimited receive */
static int exit_code = 0;
extern int optind;
extern char* optarg;
extern int optopt;
extern int opterr;
#define str_free(strptr) free((void *)strptr)
static void check_condition(pn_event_t *e, pn_condition_t *cond) {
if (pn_condition_is_set(cond)) {
fprintf(stderr, "%s: %s: %s\n", pn_event_type_name(pn_event_type(e)),
pn_condition_get_name(cond), pn_condition_get_description(cond));
pn_connection_close(pn_event_connection(e));
exit_code = 1;
}
}
#define TOPIC_PREFIX_KEY "topic-prefix"
/*
* Certain brokers have AMQP connections may advertise their topic prefix in the
* connection open response. It is possible to read the topic prefix value from
* the connection open response. Solace Pubsub+ advertises the topic prefix
* with the 'topic-prefix' property key.
*
* set_topic_prefix_from_connection checks the remote AMQP connection properties
* for the 'topic-prefix' property key and reads and assign the amqp topic prefix
* to app_data_t if the property is present.
*
* */
static int set_topic_prefix_from_connection(app_data_t *app, pn_connection_t *pnc) {
pn_data_t* properties = pn_connection_remote_properties(pnc);
static const size_t amqp_topic_prefix_len = 255;
char amqp_topic_prefix[amqp_topic_prefix_len];
/* get remote connection property value */
int rc = get_data_map_string_property(properties,
TOPIC_PREFIX_KEY,
amqp_topic_prefix,
amqp_topic_prefix_len);
/*
* < 0 indicates not found or invalid arguments
*
* = 0 indicates property is present but is not representable as a string
* or does not fit in buffer
*
* > 0 indicates property is found and was written into amqp_topic_prefix
* */
if (rc > 0) {
/* found property, store to format amqp address for topics */
str_free(app->amqp_address_prefix);
app->amqp_address_prefix = strdup(amqp_topic_prefix);
}
return rc;
}
static void decode_message(pn_rwbytes_t data) {
pn_message_t *m = pn_message();
int err = pn_message_decode(m, data.start, data.size);
if (!err) {
/* Print the decoded message */
pn_string_t *s = pn_string(NULL);
pn_inspect(pn_message_body(m), s);
printf("%s\n", pn_string_get(s));
pn_free(s);
pn_message_free(m);
free(data.start);
} else {
fprintf(stderr, "decode_message: %s\n", pn_code(err));
exit_code = 1;
}
}
/* Return true to continue, false to exit */
static bool handle(app_data_t* app, pn_event_t* event) {
switch (pn_event_type(event)) {
case PN_CONNECTION_INIT: {
pn_connection_t* c = pn_event_connection(event);
/* Set authenticate creadentials if present */
if (app->username) {
pn_connection_set_user(c, app->username);
pn_connection_set_password(c, app->password);
}
pn_connection_set_container(c, app->container_id);
pn_connection_open(c);
} break;
case PN_CONNECTION_REMOTE_OPEN: {
pn_connection_t* c = pn_event_connection(event);
/* read amqp topic prefix from connection remote properties */
set_topic_prefix_from_connection(app, c);
pn_session_t* s = pn_session(c);
pn_session_open(s);
{
char amqp_address[PN_MAX_ADDR];
/*
* To Create a durable subscription create an AMQP Receiver link
* with the following:
* 1) set a uniquely identifiable link name
* 2) set an AMQP topic terminus source address using the topic
* address prefix
* 3) the terminus expiry policy set to PN_EXPIRE_NEVER
* 4) the terminus durability set PN_CONFIGURATION
*
* Where the link name is the subscription name.
* And the terminus source address sets the subscription's topic.
* And the terminus expiry policy and durability sets the
* subscription's durability.
* */
/* the subscription name is the name of the link */
pn_link_t* l = pn_receiver(s, app->subscription_name);
/* format terminus address with topic prefix */
if(amqp_destination_address(amqp_address, PN_MAX_ADDR,
app->amqp_address, strlen(app->amqp_address),
app->amqp_address_prefix, strlen(app->amqp_address_prefix)) < 0) {
fprintf(stderr, "failed to format amqp terminus address\n");
exit_code=1;
return false;
}
printf("Setting amqp link terminus address to: '%s'\n", amqp_address);
pn_terminus_t *source = pn_link_source(l);
/* set the topic on the subscription */
pn_terminus_set_address(source, amqp_address);
/* set terminus fields to indicate a durable subscription */
pn_terminus_set_expiry_policy(source, PN_EXPIRE_NEVER);
pn_terminus_set_durability(source, PN_CONFIGURATION);
/* open link */
pn_link_open(l);
/* cannot receive without granting credit: */
pn_link_flow(l, app->message_count ? app->message_count : BATCH);
}
} break;
case PN_DELIVERY: {
/* A message has been received */
pn_delivery_t *d = pn_event_delivery(event);
if (pn_delivery_readable(d)) {
pn_link_t *l = pn_delivery_link(d);
size_t size = pn_delivery_pending(d);
pn_rwbytes_t* m = &app->msgin; /* Append data to incoming message buffer */
int recv;
size_t oldsize = m->size;
m->size += size;
m->start = (char*)realloc(m->start, m->size);
recv = pn_link_recv(l, m->start + oldsize, m->size);
if (recv == PN_ABORTED) {
fprintf(stderr, "Message aborted\n");
m->size = 0; /* Forget the data we accumulated */
pn_delivery_settle(d); /* Free the delivery so we can receive the next message */
pn_link_flow(l, 1); /* Replace credit for aborted message */
} else if (recv < 0 && recv != PN_EOS) { /* Unexpected error */
pn_condition_format(pn_link_condition(l), "broker", "PN_DELIVERY error: %s", pn_code(recv));
pn_link_close(l); /* Unexpected error, close the link */
} else if (!pn_delivery_partial(d)) { /* Message is complete */
decode_message(*m);
*m = pn_rwbytes_null; /* Reset the buffer for the next message*/
/* Accept the delivery */
pn_delivery_update(d, PN_ACCEPTED);
pn_delivery_settle(d); /* settle and free d */
if (app->message_count == 0) {
/* receive forever - see if more credit is needed */
if (pn_link_credit(l) < BATCH/2) {
/* Grant enough credit to bring it up to BATCH: */
pn_link_flow(l, BATCH - pn_link_credit(l));
}
} else if (++app->received >= app->message_count) {
pn_session_t *ssn = pn_link_session(l);
printf("%d messages received\n", app->received);
pn_link_close(l);
pn_session_close(ssn);
pn_connection_close(pn_session_connection(ssn));
}
}
}
break;
}
case PN_TRANSPORT_CLOSED:
check_condition(event, pn_transport_condition(pn_event_transport(event)));
break;
case PN_CONNECTION_REMOTE_CLOSE:
check_condition(event, pn_connection_remote_condition(pn_event_connection(event)));
pn_connection_close(pn_event_connection(event));
break;
case PN_SESSION_REMOTE_CLOSE:
check_condition(event, pn_session_remote_condition(pn_event_session(event)));
pn_connection_close(pn_event_connection(event));
break;
case PN_LINK_REMOTE_CLOSE:
case PN_LINK_REMOTE_DETACH:
check_condition(event, pn_link_remote_condition(pn_event_link(event)));
pn_connection_close(pn_event_connection(event));
break;
case PN_PROACTOR_INACTIVE:
return false;
break;
default:
break;
}
return true;
}
void run(app_data_t *app) {
/* Loop and handle events */
do {
pn_event_batch_t *events = pn_proactor_wait(app->proactor);
pn_event_t *e;
for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) {
if (!handle(app, e) || exit_code != 0) {
return;
}
}
pn_proactor_done(app->proactor, events);
} while(true);
}
void usage() {
printf("Usage: dte_consumer [options] \n");
printf("[Options]:\n");
printf("\t-a The host address [localhost]\n");
printf("\t-p The host port [5672]\n");
printf("\t-c # of messages to consume [10]\n");
printf("\t-t Target topic address [my_topic]\n");
printf("\t-n Subscription name [my_sub]\n");
printf("\t-i Container id [dte_consumer:<pid>]\n");
printf("\t-u Client authentication username []\n");
printf("\t-P Client authentication password []\n");
printf("\t-h Displays this message\n");
exit(0);
}
#define DEFAULT_AMQP_TOPIC_PREFIX "topic://"
#define AMQP_TOPIC_PREFIX DEFAULT_AMQP_TOPIC_PREFIX
#define AMQP_TOPIC_PREFIX_SIZE sizeof(AMQP_TOPIC_PREFIX)
#define AMQP_TOPIC_PREFIX_LEN AMQP_TOPIC_PREFIX_SIZE -1
void parse_args(int argc, char **argv, app_data_t *app) {
char c;
char con_id[PN_MAX_ADDR];
if (container_id(con_id, PN_MAX_ADDR, argv[0], sizeof(argv[0])) < 0){
fprintf(stderr, "Unable to format container id from source: %s", argv[0]);
exit(1);
}
/* initialize default values*/
app->container_id = strdup(con_id); /* default to using argv[0] */
app->host = "localhost";
app->port = "amqp";
app->subscription_name = "my_sub";
app->amqp_address = "my_topic";
app->message_count = 10;
app->username = NULL;
app->password = NULL;
/*
* Set a default amqp topic prefix since broker do not always
* advertise a topic prefix.
* The 'topic://' is the address prefix for topics for the
* Solace PubSub+ Message Broker.
*/
app->amqp_address_prefix = (char*)malloc(sizeof(char) * AMQP_TOPIC_PREFIX_SIZE);
strncpy(app->amqp_address_prefix, AMQP_TOPIC_PREFIX, AMQP_TOPIC_PREFIX_SIZE);
/* command line options */
opterr = 0;
while((c = getopt(argc, argv, "i:a:c:t:p:u:P:n:h")) != -1) {
switch(c) {
case 'h': usage(); break;
case 'c':
app->message_count = atoi(optarg);
if (app->message_count < 0) usage();
break;
case 'a': app->host = optarg; break;
case 'i':
if (container_id(con_id, PN_MAX_ADDR, optarg, sizeof(optarg)) < 0) {
fprintf(stderr, "Unable to format container id from source: %s", optarg);
exit(1);
}
str_free(app->container_id);
app->container_id = strdup(con_id);
break;
case 't': app->amqp_address = optarg; break;
case 'n': app->subscription_name = optarg; break;
case 'p': app->port = optarg; break;
case 'u': app->username = optarg; break;
case 'P': app->password = optarg; break;
default: usage(); break;
}
}
}
int main(int argc, char **argv) {
struct app_data_t app = {0};
char addr[PN_MAX_ADDR];
parse_args(argc, argv, &app);
/* Create the proactor and connect */
app.proactor = pn_proactor();
pn_proactor_addr(addr, sizeof(addr), app.host, app.port);
fprintf(stdout, "Connecting to host: %s\n", addr);
/* Initialize Sasl transport */
pn_transport_t *pnt = pn_transport();
pn_sasl_set_allow_insecure_mechs(pn_sasl(pnt), true);
pn_proactor_connect2(app.proactor, NULL, pnt, addr);
fprintf(stdout, "waiting to receive %d messages from amqp address: %s\n", app.message_count, app.amqp_address);
run(&app);
pn_proactor_free(app.proactor);
/* app cleanup */
str_free(app.container_id);
str_free(app.amqp_address_prefix);
return exit_code;
}