| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
/* |
| 22 |
* Implements a subset of msgr-recv.c using reactor events. |
| 23 |
*/ |
| 24 |
|
| 25 |
#define PN_USE_DEPRECATED_API 1 |
| 26 |
|
| 27 |
#include "proton/message.h" |
| 28 |
#include "proton/error.h" |
| 29 |
#include "proton/types.h" |
| 30 |
#include "proton/reactor.h" |
| 31 |
#include "proton/handlers.h" |
| 32 |
#include "proton/engine.h" |
| 33 |
#include "proton/url.h" |
| 34 |
#include "msgr-common.h" |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
#include <ctype.h> |
| 40 |
|
| 41 |
// The exact struct from msgr-recv, mostly fallow. |
| 42 |
typedef struct { |
| 43 |
Addresses_t subscriptions; |
| 44 |
uint64_t msg_count; |
| 45 |
int recv_count; |
| 46 |
int incoming_window; |
| 47 |
int timeout; // seconds |
| 48 |
unsigned int report_interval; // in seconds |
| 49 |
int outgoing_window; |
| 50 |
int reply; |
| 51 |
const char *name; |
| 52 |
const char *ready_text; |
| 53 |
char *certificate; |
| 54 |
char *privatekey; // used to sign certificate |
| 55 |
char *password; // for private key file |
| 56 |
char *ca_db; // trusted CA database |
| 57 |
} Options_t; |
| 58 |
|
| 59 |
|
| 60 |
static void usage(int rc) |
| 61 |
{ |
| 62 |
printf("Usage: reactor-recv [OPTIONS] \n" |
| 63 |
" -c # \tNumber of messages to receive before exiting [0=forever]\n" |
| 64 |
" -R \tSend reply if 'reply-to' present\n" |
| 65 |
" -t # \tInactivity timeout in seconds, -1 = no timeout [-1]\n" |
| 66 |
" -X <text> \tPrint '<text>\\n' to stdout after all subscriptions are created\n" |
| 67 |
); |
| 68 |
exit(rc); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
// Global context for this process |
| 73 |
typedef struct { |
| 74 |
Options_t *opts; |
| 75 |
Statistics_t *stats; |
| 76 |
uint64_t sent; |
| 77 |
uint64_t received; |
| 78 |
pn_message_t *message; |
| 79 |
pn_acceptor_t *acceptor; |
| 80 |
char *encoded_data; |
| 81 |
size_t encoded_data_size; |
| 82 |
int connections; |
| 83 |
pn_list_t *active_connections; |
| 84 |
bool shutting_down; |
| 85 |
pn_handler_t *listener_handler; |
| 86 |
int quiesce_count; |
| 87 |
} global_context_t; |
| 88 |
|
| 89 |
// Per connection context |
| 90 |
typedef struct { |
| 91 |
global_context_t *global; |
| 92 |
int connection_id; |
| 93 |
pn_link_t *recv_link; |
| 94 |
pn_link_t *reply_link; |
| 95 |
} connection_context_t; |
| 96 |
|
| 97 |
/** |
| 98 |
* @return buffer of sufficient size, or NULL |
| 99 |
*/ |
| 100 |
static char *ensure_buffer(char *buf, size_t needed, size_t *actual) |
| 101 |
{ |
| 102 |
// Make room for the largest message seen so far, plus extra for slight changes in metadata content |
| 103 |
if (needed + 1024 <= *actual) |
| 104 |
return buf; |
| 105 |
needed += 2048; |
| 106 |
buf = (char *) realloc(buf, needed); |
| 107 |
*actual = buf ? needed : 0; |
| 108 |
return buf; |
| 109 |
} |
| 110 |
|
| 111 |
void global_shutdown(global_context_t *gc) |
| 112 |
{ |
| 113 |
if (gc->shutting_down) return; |
| 114 |
gc->shutting_down = true; |
| 115 |
pn_acceptor_close(gc->acceptor); |
| 116 |
size_t n = pn_list_size(gc->active_connections); |
| 117 |
for (size_t i = 0; i < n; i++) { |
| 118 |
pn_connection_t *conn = (pn_connection_t *) pn_list_get(gc->active_connections, i); |
| 119 |
if (!(pn_connection_state(conn) & PN_LOCAL_CLOSED)) { |
| 120 |
pn_connection_close(conn); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
connection_context_t *connection_context(pn_handler_t *h) |
| 126 |
{ |
| 127 |
connection_context_t *p = (connection_context_t *) pn_handler_mem(h); |
| 128 |
return p; |
| 129 |
} |
| 130 |
|
| 131 |
void connection_context_init(connection_context_t *cc, global_context_t *gc) |
| 132 |
{ |
| 133 |
cc->global = gc; |
| 134 |
pn_incref(gc->listener_handler); |
| 135 |
cc->connection_id = gc->connections++; |
| 136 |
cc->recv_link = 0; |
| 137 |
cc->reply_link = 0; |
| 138 |
} |
| 139 |
|
| 140 |
void connection_cleanup(pn_handler_t *h) |
| 141 |
{ |
| 142 |
connection_context_t *cc = connection_context(h); |
| 143 |
// Undo pn_incref() from connection_context_init() |
| 144 |
pn_decref(cc->global->listener_handler); |
| 145 |
} |
| 146 |
|
| 147 |
void connection_dispatch(pn_handler_t *h, pn_event_t *event, pn_event_type_t type) |
| 148 |
{ |
| 149 |
connection_context_t *cc = connection_context(h); |
| 150 |
bool replying = cc->global->opts->reply; |
| 151 |
|
| 152 |
switch (type) { |
| 153 |
case PN_LINK_REMOTE_OPEN: |
| 154 |
{ |
| 155 |
pn_link_t *link = pn_event_link(event); |
| 156 |
if (pn_link_is_receiver(link)) { |
| 157 |
check(cc->recv_link == NULL, "Multiple incoming links on one connection"); |
| 158 |
cc->recv_link = link; |
| 159 |
pn_connection_t *conn = pn_event_connection(event); |
| 160 |
pn_list_add(cc->global->active_connections, conn); |
| 161 |
if (cc->global->shutting_down) { |
| 162 |
pn_connection_close(conn); |
| 163 |
break; |
| 164 |
} |
| 165 |
if (replying) { |
| 166 |
// Set up a reply link and defer granting credit to the incoming link |
| 167 |
pn_connection_t *conn = pn_session_connection(pn_link_session(link)); |
| 168 |
pn_session_t *ssn = pn_session(conn); |
| 169 |
pn_session_open(ssn); |
| 170 |
char name[100]; // prefer a multiplatform uuid generator |
| 171 |
sprintf(name, "reply_sender_%d", cc->connection_id); |
| 172 |
cc->reply_link = pn_sender(ssn, name); |
| 173 |
pn_link_open(cc->reply_link); |
| 174 |
} |
| 175 |
else { |
| 176 |
pn_flowcontroller_t *fc = pn_flowcontroller(1024); |
| 177 |
pn_handler_add(h, fc); |
| 178 |
pn_decref(fc); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
break; |
| 183 |
case PN_LINK_FLOW: |
| 184 |
{ |
| 185 |
if (replying) { |
| 186 |
pn_link_t *reply_link = pn_event_link(event); |
| 187 |
// pn_flowcontroller handles the non-reply case |
| 188 |
check(reply_link == cc->reply_link, "internal error"); |
| 189 |
|
| 190 |
// Grant the sender as much credit as just given to us for replies |
| 191 |
int delta = pn_link_credit(reply_link) - pn_link_credit(cc->recv_link); |
| 192 |
if (delta > 0) |
| 193 |
pn_link_flow(cc->recv_link, delta); |
| 194 |
} |
| 195 |
} |
| 196 |
break; |
| 197 |
case PN_DELIVERY: |
| 198 |
{ |
| 199 |
pn_link_t *recv_link = pn_event_link(event); |
| 200 |
pn_delivery_t *dlv = pn_event_delivery(event); |
| 201 |
if (pn_link_is_receiver(recv_link) && !pn_delivery_partial(dlv)) { |
| 202 |
if (cc->global->received == 0) statistics_start(cc->global->stats); |
| 203 |
|
| 204 |
size_t encoded_size = pn_delivery_pending(dlv); |
| 205 |
cc->global->encoded_data = ensure_buffer(cc->global->encoded_data, encoded_size, |
| 206 |
&cc->global->encoded_data_size); |
| 207 |
check(cc->global->encoded_data, "decoding buffer realloc failure"); |
| 208 |
|
| 209 |
ssize_t n = pn_link_recv(recv_link, cc->global->encoded_data, encoded_size); |
| 210 |
check(n == (ssize_t) encoded_size, "message data read fail"); |
| 211 |
pn_message_t *msg = cc->global->message; |
| 212 |
int err = pn_message_decode(msg, cc->global->encoded_data, n); |
| 213 |
check(err == 0, "message decode error"); |
| 214 |
cc->global->received++; |
| 215 |
pn_delivery_settle(dlv); |
| 216 |
statistics_msg_received(cc->global->stats, msg); |
| 217 |
|
| 218 |
if (replying) { |
| 219 |
const char *reply_addr = pn_message_get_reply_to(msg); |
| 220 |
if (reply_addr) { |
| 221 |
pn_link_t *rl = cc->reply_link; |
| 222 |
check(pn_link_credit(rl) > 0, "message received without corresponding reply credit"); |
| 223 |
LOG("Replying to: %s\n", reply_addr ); |
| 224 |
|
| 225 |
pn_message_set_address(msg, reply_addr); |
| 226 |
pn_message_set_creation_time(msg, msgr_now()); |
| 227 |
|
| 228 |
char tag[8]; |
| 229 |
void *ptr = &tag; |
| 230 |
*((uint64_t *) ptr) = cc->global->sent; |
| 231 |
pn_delivery_t *dlv = pn_delivery(rl, pn_dtag(tag, 8)); |
| 232 |
size_t size = cc->global->encoded_data_size; |
| 233 |
int err = pn_message_encode(msg, cc->global->encoded_data, &size); |
| 234 |
check(err == 0, "message encoding error"); |
| 235 |
pn_link_send(rl, cc->global->encoded_data, size); |
| 236 |
pn_delivery_settle(dlv); |
| 237 |
|
| 238 |
cc->global->sent++; |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
if (cc->global->received >= cc->global->opts->msg_count) { |
| 243 |
global_shutdown(cc->global); |
| 244 |
} |
| 245 |
} |
| 246 |
break; |
| 247 |
case PN_CONNECTION_UNBOUND: |
| 248 |
{ |
| 249 |
pn_connection_t *conn = pn_event_connection(event); |
| 250 |
pn_list_remove(cc->global->active_connections, conn); |
| 251 |
pn_connection_release(conn); |
| 252 |
} |
| 253 |
break; |
| 254 |
default: |
| 255 |
break; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
pn_handler_t *connection_handler(global_context_t *gc) |
| 260 |
{ |
| 261 |
pn_handler_t *h = pn_handler_new(connection_dispatch, sizeof(connection_context_t), connection_cleanup); |
| 262 |
connection_context_t *cc = connection_context(h); |
| 263 |
connection_context_init(cc, gc); |
| 264 |
return h; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
void start_listener(global_context_t *gc, pn_reactor_t *reactor) |
| 269 |
{ |
| 270 |
check(gc->opts->subscriptions.count > 0, "no listening address"); |
| 271 |
pn_url_t *listen_url = pn_url_parse(gc->opts->subscriptions.addresses[0]); |
| 272 |
const char *host = pn_url_get_host(listen_url); |
| 273 |
const char *port = pn_url_get_port(listen_url); |
| 274 |
if (port == 0 || strlen(port) == 0) |
| 275 |
port = "5672"; |
| 276 |
if (host == 0 || strlen(host) == 0) |
| 277 |
host = "0.0.0.0"; |
| 278 |
if (*host == '~') host++; |
| 279 |
gc->acceptor = pn_reactor_acceptor(reactor, host, port, NULL); |
| 280 |
check(gc->acceptor, "acceptor creation failed"); |
| 281 |
pn_url_free(listen_url); |
| 282 |
} |
| 283 |
|
| 284 |
void global_context_init(global_context_t *gc, Options_t *o, Statistics_t *s) |
| 285 |
{ |
| 286 |
gc->opts = o; |
| 287 |
gc->stats = s; |
| 288 |
gc->sent = 0; |
| 289 |
gc->received = 0; |
| 290 |
gc->encoded_data_size = 0; |
| 291 |
gc->encoded_data = 0; |
| 292 |
gc->message = pn_message(); |
| 293 |
check(gc->message, "failed to allocate a message"); |
| 294 |
gc->connections = 0; |
| 295 |
gc->active_connections = pn_list(PN_OBJECT, 0); |
| 296 |
gc->acceptor = 0; |
| 297 |
gc->shutting_down = false; |
| 298 |
gc->listener_handler = 0; |
| 299 |
gc->quiesce_count = 0; |
| 300 |
} |
| 301 |
|
| 302 |
global_context_t *global_context(pn_handler_t *h) |
| 303 |
{ |
| 304 |
return (global_context_t *) pn_handler_mem(h); |
| 305 |
} |
| 306 |
|
| 307 |
void listener_cleanup(pn_handler_t *h) |
| 308 |
{ |
| 309 |
global_context_t *gc = global_context(h); |
| 310 |
pn_message_free(gc->message); |
| 311 |
free(gc->encoded_data); |
| 312 |
pn_free(gc->active_connections); |
| 313 |
} |
| 314 |
|
| 315 |
void listener_dispatch(pn_handler_t *h, pn_event_t *event, pn_event_type_t type) |
| 316 |
{ |
| 317 |
global_context_t *gc = global_context(h); |
| 318 |
if (type == PN_REACTOR_QUIESCED) |
| 319 |
gc->quiesce_count++; |
| 320 |
else |
| 321 |
gc->quiesce_count = 0; |
| 322 |
|
| 323 |
switch (type) { |
| 324 |
case PN_CONNECTION_INIT: |
| 325 |
{ |
| 326 |
pn_connection_t *connection = pn_event_connection(event); |
| 327 |
|
| 328 |
// New incoming connection on listener socket. Give each a separate handler. |
| 329 |
pn_handler_t *ch = connection_handler(gc); |
| 330 |
pn_handshaker_t *handshaker = pn_handshaker(); |
| 331 |
pn_handler_add(ch, handshaker); |
| 332 |
pn_decref(handshaker); |
| 333 |
pn_record_t *record = pn_connection_attachments(connection); |
| 334 |
pn_record_set_handler(record, ch); |
| 335 |
pn_decref(ch); |
| 336 |
} |
| 337 |
break; |
| 338 |
case PN_REACTOR_QUIESCED: |
| 339 |
{ |
| 340 |
// Two quiesce in a row means we have been idle for a timeout period |
| 341 |
if (gc->opts->timeout != -1 && gc->quiesce_count > 1) |
| 342 |
global_shutdown(gc); |
| 343 |
} |
| 344 |
break; |
| 345 |
case PN_REACTOR_INIT: |
| 346 |
{ |
| 347 |
pn_reactor_t *reactor = pn_event_reactor(event); |
| 348 |
start_listener(gc, reactor); |
| 349 |
|
| 350 |
// hack to let test scripts know when the receivers are ready (so |
| 351 |
// that the senders may be started) |
| 352 |
if (gc->opts->ready_text) { |
| 353 |
fprintf(stdout, "%s\n", gc->opts->ready_text); |
| 354 |
fflush(stdout); |
| 355 |
} |
| 356 |
if (gc->opts->timeout != -1) |
| 357 |
pn_reactor_set_timeout(pn_event_reactor(event), gc->opts->timeout); |
| 358 |
} |
| 359 |
break; |
| 360 |
case PN_REACTOR_FINAL: |
| 361 |
{ |
| 362 |
if (gc->received == 0) statistics_start(gc->stats); |
| 363 |
statistics_report(gc->stats, gc->sent, gc->received); |
| 364 |
} |
| 365 |
break; |
| 366 |
default: |
| 367 |
break; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
pn_handler_t *listener_handler(Options_t *opts, Statistics_t *stats) |
| 372 |
{ |
| 373 |
pn_handler_t *h = pn_handler_new(listener_dispatch, sizeof(global_context_t), listener_cleanup); |
| 374 |
global_context_t *gc = global_context(h); |
| 375 |
global_context_init(gc, opts, stats); |
| 376 |
gc->listener_handler = h; |
| 377 |
return h; |
| 378 |
} |
| 379 |
|
| 380 |
static void parse_options( int argc, char **argv, Options_t *opts ) |
| 381 |
{ |
| 382 |
int c; |
| 383 |
opterr = 0; |
| 384 |
|
| 385 |
memset( opts, 0, sizeof(*opts) ); |
| 386 |
opts->recv_count = -1; |
| 387 |
opts->timeout = -1; |
| 388 |
addresses_init( &opts->subscriptions); |
| 389 |
|
| 390 |
while ((c = getopt(argc, argv, |
| 391 |
"a:c:b:w:t:e:RW:F:VN:X:T:C:K:P:")) != -1) { |
| 392 |
switch (c) { |
| 393 |
case 'a': |
| 394 |
{ |
| 395 |
// TODO: multiple addresses? |
| 396 |
char *comma = strchr(optarg, ','); |
| 397 |
check(comma == 0, "multiple addresses not implemented"); |
| 398 |
check(opts->subscriptions.count == 0, "multiple addresses not implemented"); |
| 399 |
addresses_merge( &opts->subscriptions, optarg ); |
| 400 |
} |
| 401 |
break; |
| 402 |
case 'c': |
| 403 |
if (sscanf( optarg, "%" SCNu64, &opts->msg_count ) != 1) { |
| 404 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 405 |
usage(1); |
| 406 |
} |
| 407 |
break; |
| 408 |
case 't': |
| 409 |
if (sscanf( optarg, "%d", &opts->timeout ) != 1) { |
| 410 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 411 |
usage(1); |
| 412 |
} |
| 413 |
if (opts->timeout > 0) opts->timeout *= 1000; |
| 414 |
break; |
| 415 |
case 'R': opts->reply = 1; break; |
| 416 |
case 'V': enable_logging(); break; |
| 417 |
case 'X': opts->ready_text = optarg; break; |
| 418 |
default: |
| 419 |
usage(1); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
if (opts->subscriptions.count == 0) addresses_add( &opts->subscriptions, |
| 424 |
"amqp://~0.0.0.0" ); |
| 425 |
} |
| 426 |
|
| 427 |
int main(int argc, char** argv) |
| 428 |
{ |
| 429 |
Options_t opts; |
| 430 |
Statistics_t stats; |
| 431 |
parse_options( argc, argv, &opts ); |
| 432 |
pn_reactor_t *reactor = pn_reactor(); |
| 433 |
|
| 434 |
// set up default handlers for our reactor |
| 435 |
pn_handler_t *root = pn_reactor_get_handler(reactor); |
| 436 |
pn_handler_t *lh = listener_handler(&opts, &stats); |
| 437 |
pn_handler_add(root, lh); |
| 438 |
pn_handshaker_t *handshaker = pn_handshaker(); |
| 439 |
pn_handler_add(root, handshaker); |
| 440 |
|
| 441 |
// Omit decrefs else segfault. Not sure why they are necessary |
| 442 |
// to keep valgrind happy for the connection_handler, but not here. |
| 443 |
// pn_decref(handshaker); |
| 444 |
// pn_decref(lh); |
| 445 |
|
| 446 |
pn_reactor_run(reactor); |
| 447 |
pn_reactor_free(reactor); |
| 448 |
|
| 449 |
addresses_free( &opts.subscriptions ); |
| 450 |
return 0; |
| 451 |
} |
| 452 |
|
| 453 |
#undef PN_USE_DEPRECATED_API |