| 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 |
#define PN_USE_DEPRECATED_API 1 |
| 22 |
|
| 23 |
/* |
| 24 |
* Implements a subset of msgr-send.c using reactor events. |
| 25 |
*/ |
| 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 |
|
| 42 |
typedef struct { |
| 43 |
Addresses_t targets; |
| 44 |
uint64_t msg_count; |
| 45 |
uint32_t msg_size; // of body |
| 46 |
uint32_t send_batch; |
| 47 |
int outgoing_window; |
| 48 |
unsigned int report_interval; // in seconds |
| 49 |
//Addresses_t subscriptions; |
| 50 |
//Addresses_t reply_tos; |
| 51 |
int get_replies; |
| 52 |
int unique_message; // 1 -> create and free a pn_message_t for each send/recv |
| 53 |
int timeout; // in seconds |
| 54 |
int incoming_window; |
| 55 |
int recv_count; |
| 56 |
const char *name; |
| 57 |
char *certificate; |
| 58 |
char *privatekey; // used to sign certificate |
| 59 |
char *password; // for private key file |
| 60 |
char *ca_db; // trusted CA database |
| 61 |
} Options_t; |
| 62 |
|
| 63 |
|
| 64 |
static void usage(int rc) |
| 65 |
{ |
| 66 |
printf("Usage: reactor-send [OPTIONS] \n" |
| 67 |
" -a <addr> \tThe target address [amqp[s]://domain[/name]]\n" |
| 68 |
" -c # \tNumber of messages to send before exiting [0=forever]\n" |
| 69 |
" -b # \tSize of message body in bytes [1024]\n" |
| 70 |
" -R \tWait for a reply to each sent message\n" |
| 71 |
" -V \tEnable debug logging\n" |
| 72 |
); |
| 73 |
exit(rc); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
typedef struct { |
| 78 |
Options_t *opts; |
| 79 |
Statistics_t *stats; |
| 80 |
uint64_t sent; |
| 81 |
uint64_t received; |
| 82 |
pn_message_t *message; |
| 83 |
pn_message_t *reply_message; |
| 84 |
pn_atom_t id; |
| 85 |
char *encoded_data; |
| 86 |
size_t encoded_data_size; |
| 87 |
pn_url_t *send_url; |
| 88 |
pn_string_t *hostname; |
| 89 |
pn_string_t *container_id; |
| 90 |
pn_string_t *reply_to; |
| 91 |
} sender_context_t; |
| 92 |
|
| 93 |
void sender_context_init(sender_context_t *sc, Options_t *opts, Statistics_t *stats) |
| 94 |
{ |
| 95 |
sc->opts = opts; |
| 96 |
sc->stats = stats; |
| 97 |
sc->sent = 0; |
| 98 |
sc->received = 0; |
| 99 |
sc->id.type = PN_ULONG; |
| 100 |
// 4096 extra bytes should easily cover the message metadata |
| 101 |
sc->encoded_data_size = sc->opts->msg_size + 4096; |
| 102 |
sc->encoded_data = (char *)calloc(1, sc->encoded_data_size); |
| 103 |
check(sc->encoded_data, "failed to allocate encoding buffer"); |
| 104 |
sc->container_id = pn_string("reactor-send"); // prefer uuid-like name |
| 105 |
|
| 106 |
sc->reply_message = (sc->opts->get_replies) ? pn_message() : 0; |
| 107 |
sc->message = pn_message(); |
| 108 |
check(sc->message, "failed to allocate a message"); |
| 109 |
sc->reply_to = pn_string("amqp://"); |
| 110 |
pn_string_addf(sc->reply_to, "%s", pn_string_get(sc->container_id)); |
| 111 |
pn_message_set_reply_to(sc->message, pn_string_get(sc->reply_to)); |
| 112 |
pn_data_t *body = pn_message_body(sc->message); |
| 113 |
// borrow the encoding buffer this one time |
| 114 |
char *data = sc->encoded_data; |
| 115 |
pn_data_put_binary(body, pn_bytes(sc->opts->msg_size, data)); |
| 116 |
|
| 117 |
check(sc->opts->targets.count > 0, "no specified address"); |
| 118 |
sc->send_url = pn_url_parse(sc->opts->targets.addresses[0]); |
| 119 |
const char *host = pn_url_get_host(sc->send_url); |
| 120 |
const char *port = pn_url_get_port(sc->send_url); |
| 121 |
sc->hostname = pn_string(host); |
| 122 |
if (port && strlen(port)) |
| 123 |
pn_string_addf(sc->hostname, ":%s", port); |
| 124 |
} |
| 125 |
|
| 126 |
sender_context_t *sender_context(pn_handler_t *h) |
| 127 |
{ |
| 128 |
return (sender_context_t *) pn_handler_mem(h); |
| 129 |
} |
| 130 |
|
| 131 |
void sender_cleanup(pn_handler_t *h) |
| 132 |
{ |
| 133 |
sender_context_t *sc = sender_context(h); |
| 134 |
pn_message_free(sc->message); |
| 135 |
pn_message_free(sc->reply_message); |
| 136 |
pn_url_free(sc->send_url); |
| 137 |
pn_free(sc->hostname); |
| 138 |
pn_free(sc->container_id); |
| 139 |
pn_free(sc->reply_to); |
| 140 |
free(sc->encoded_data); |
| 141 |
} |
| 142 |
|
| 143 |
pn_handler_t *replyto_handler(sender_context_t *sc); |
| 144 |
|
| 145 |
pn_message_t* get_message(sender_context_t *sc, bool sending) { |
| 146 |
if (sc->opts->unique_message) { |
| 147 |
pn_message_t *m = pn_message(); |
| 148 |
check(m, "failed to allocate a message"); |
| 149 |
if (sending) { |
| 150 |
pn_message_set_reply_to(m, pn_string_get(sc->reply_to)); |
| 151 |
// copy the data |
| 152 |
pn_data_t *body = pn_message_body(m); |
| 153 |
pn_data_t *template_body = pn_message_body(sc->message); |
| 154 |
pn_data_put_binary(body, pn_data_get_binary(template_body)); |
| 155 |
} |
| 156 |
return m; |
| 157 |
} |
| 158 |
else |
| 159 |
return sending ? sc->message : sc->reply_message; // our simplified "message pool" |
| 160 |
} |
| 161 |
|
| 162 |
void return_message(sender_context_t *sc, pn_message_t *m) { |
| 163 |
if (sc->opts->unique_message) |
| 164 |
pn_message_free(m); |
| 165 |
} |
| 166 |
|
| 167 |
void sender_dispatch(pn_handler_t *h, pn_event_t *event, pn_event_type_t type) |
| 168 |
{ |
| 169 |
sender_context_t *sc = sender_context(h); |
| 170 |
|
| 171 |
switch (type) { |
| 172 |
case PN_CONNECTION_INIT: |
| 173 |
{ |
| 174 |
pn_connection_t *conn = pn_event_connection(event); |
| 175 |
pn_connection_set_container(conn, pn_string_get(sc->container_id)); |
| 176 |
pn_connection_set_hostname(conn, pn_string_get(sc->hostname)); |
| 177 |
pn_connection_open(conn); |
| 178 |
pn_session_t *ssn = pn_session(conn); |
| 179 |
pn_session_open(ssn); |
| 180 |
pn_link_t *snd = pn_sender(ssn, "sender"); |
| 181 |
const char *path = pn_url_get_path(sc->send_url); |
| 182 |
if (path && strlen(path)) { |
| 183 |
pn_terminus_set_address(pn_link_target(snd), path); |
| 184 |
pn_terminus_set_address(pn_link_source(snd), path); |
| 185 |
} |
| 186 |
pn_link_open(snd); |
| 187 |
} |
| 188 |
break; |
| 189 |
case PN_LINK_FLOW: |
| 190 |
{ |
| 191 |
pn_link_t *snd = pn_event_link(event); |
| 192 |
while (pn_link_credit(snd) > 0 && sc->sent < sc->opts->msg_count) { |
| 193 |
if (sc->sent == 0) |
| 194 |
statistics_start(sc->stats); |
| 195 |
|
| 196 |
char tag[8]; |
| 197 |
void *ptr = &tag; |
| 198 |
*((uint64_t *) ptr) = sc->sent; |
| 199 |
pn_delivery_t *dlv = pn_delivery(snd, pn_dtag(tag, 8)); |
| 200 |
|
| 201 |
// setup the message to send |
| 202 |
pn_message_t *msg = get_message(sc, true);; |
| 203 |
pn_message_set_address(msg, sc->opts->targets.addresses[0]); |
| 204 |
sc->id.u.as_ulong = sc->sent; |
| 205 |
pn_message_set_correlation_id(msg, sc->id); |
| 206 |
pn_message_set_creation_time(msg, msgr_now()); |
| 207 |
|
| 208 |
size_t size = sc->encoded_data_size; |
| 209 |
int err = pn_message_encode(msg, sc->encoded_data, &size); |
| 210 |
check(err == 0, "message encoding error"); |
| 211 |
pn_link_send(snd, sc->encoded_data, size); |
| 212 |
pn_delivery_settle(dlv); |
| 213 |
sc->sent++; |
| 214 |
return_message(sc, msg); |
| 215 |
} |
| 216 |
if (sc->sent == sc->opts->msg_count && !sc->opts->get_replies) { |
| 217 |
pn_link_close(snd); |
| 218 |
pn_connection_t *conn = pn_event_connection(event); |
| 219 |
pn_connection_close(conn); |
| 220 |
} |
| 221 |
} |
| 222 |
break; |
| 223 |
case PN_LINK_INIT: |
| 224 |
{ |
| 225 |
pn_link_t *link = pn_event_link(event); |
| 226 |
if (pn_link_is_receiver(link)) { |
| 227 |
// Response messages link. Could manage credit and deliveries in this handler but |
| 228 |
// a dedicated handler also works. |
| 229 |
pn_handler_t *replyto = replyto_handler(sc); |
| 230 |
pn_flowcontroller_t *fc = pn_flowcontroller(1024); |
| 231 |
pn_handler_add(replyto, fc); |
| 232 |
pn_decref(fc); |
| 233 |
pn_handshaker_t *handshaker = pn_handshaker(); |
| 234 |
pn_handler_add(replyto, handshaker); |
| 235 |
pn_decref(handshaker); |
| 236 |
pn_record_t *record = pn_link_attachments(link); |
| 237 |
pn_record_set_handler(record, replyto); |
| 238 |
pn_decref(replyto); |
| 239 |
} |
| 240 |
} |
| 241 |
break; |
| 242 |
case PN_CONNECTION_LOCAL_CLOSE: |
| 243 |
{ |
| 244 |
statistics_report(sc->stats, sc->sent, sc->received); |
| 245 |
} |
| 246 |
break; |
| 247 |
default: |
| 248 |
break; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
pn_handler_t *sender_handler(Options_t *opts, Statistics_t *stats) |
| 253 |
{ |
| 254 |
pn_handler_t *h = pn_handler_new(sender_dispatch, sizeof(sender_context_t), sender_cleanup); |
| 255 |
sender_context_t *sc = sender_context(h); |
| 256 |
sender_context_init(sc, opts, stats); |
| 257 |
return h; |
| 258 |
} |
| 259 |
|
| 260 |
sender_context_t *replyto_sender_context(pn_handler_t *h) |
| 261 |
{ |
| 262 |
sender_context_t **p = (sender_context_t **) pn_handler_mem(h); |
| 263 |
return *p; |
| 264 |
} |
| 265 |
|
| 266 |
void replyto_cleanup(pn_handler_t *h) |
| 267 |
{} |
| 268 |
|
| 269 |
void replyto_dispatch(pn_handler_t *h, pn_event_t *event, pn_event_type_t type) { |
| 270 |
sender_context_t *sc = replyto_sender_context(h); |
| 271 |
|
| 272 |
switch (type) { |
| 273 |
case PN_DELIVERY: |
| 274 |
{ |
| 275 |
check(sc->opts->get_replies, "Unexpected reply message"); |
| 276 |
pn_link_t *recv_link = pn_event_link(event); |
| 277 |
pn_delivery_t *dlv = pn_event_delivery(event); |
| 278 |
if (pn_link_is_receiver(recv_link) && !pn_delivery_partial(dlv)) { |
| 279 |
size_t encoded_size = pn_delivery_pending(dlv); |
| 280 |
check(encoded_size <= sc->encoded_data_size, "decoding buffer too small"); |
| 281 |
ssize_t n = pn_link_recv(recv_link, sc->encoded_data, encoded_size); |
| 282 |
check(n == (ssize_t)encoded_size, "read fail on reply link"); |
| 283 |
pn_message_t *msg = get_message(sc, false); |
| 284 |
int err = pn_message_decode(msg, sc->encoded_data, n); |
| 285 |
check(err == 0, "message decode error"); |
| 286 |
statistics_msg_received(sc->stats, msg); |
| 287 |
sc->received++; |
| 288 |
pn_delivery_settle(dlv); |
| 289 |
return_message(sc, msg); |
| 290 |
} |
| 291 |
if (sc->received == sc->opts->msg_count) { |
| 292 |
pn_link_close(recv_link); |
| 293 |
pn_connection_t *conn = pn_event_connection(event); |
| 294 |
pn_connection_close(conn); |
| 295 |
} |
| 296 |
} |
| 297 |
break; |
| 298 |
default: |
| 299 |
break; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
pn_handler_t *replyto_handler(sender_context_t *sc) |
| 304 |
{ |
| 305 |
pn_handler_t *h = pn_handler_new(replyto_dispatch, sizeof(sender_context_t *), replyto_cleanup); |
| 306 |
sender_context_t **p = (sender_context_t **) pn_handler_mem(h); |
| 307 |
*p = sc; |
| 308 |
return h; |
| 309 |
} |
| 310 |
|
| 311 |
static void parse_options( int argc, char **argv, Options_t *opts ) |
| 312 |
{ |
| 313 |
int c; |
| 314 |
opterr = 0; |
| 315 |
|
| 316 |
memset( opts, 0, sizeof(*opts) ); |
| 317 |
opts->msg_size = 1024; |
| 318 |
opts->send_batch = 1024; |
| 319 |
opts->timeout = -1; |
| 320 |
opts->recv_count = -1; |
| 321 |
opts->unique_message = 0; |
| 322 |
addresses_init(&opts->targets); |
| 323 |
|
| 324 |
while ((c = getopt(argc, argv, |
| 325 |
"ua:c:b:p:w:e:l:Rt:W:B:VN:T:C:K:P:")) != -1) { |
| 326 |
switch(c) { |
| 327 |
case 'a': |
| 328 |
{ |
| 329 |
// TODO: multiple addresses? To keep tests happy, accept multiple for now, |
| 330 |
// but ignore all but the first. |
| 331 |
addresses_merge( &opts->targets, optarg ); |
| 332 |
} |
| 333 |
break; |
| 334 |
case 'c': |
| 335 |
if (sscanf( optarg, "%" SCNu64, &opts->msg_count ) != 1) { |
| 336 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 337 |
usage(1); |
| 338 |
} |
| 339 |
break; |
| 340 |
case 'b': |
| 341 |
if (sscanf( optarg, "%u", &opts->msg_size ) != 1) { |
| 342 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 343 |
usage(1); |
| 344 |
} |
| 345 |
break; |
| 346 |
case 'p': |
| 347 |
if (sscanf( optarg, "%u", &opts->send_batch ) != 1) { |
| 348 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 349 |
usage(1); |
| 350 |
} |
| 351 |
break; |
| 352 |
case 'w': |
| 353 |
if (sscanf( optarg, "%d", &opts->outgoing_window ) != 1) { |
| 354 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 355 |
usage(1); |
| 356 |
} |
| 357 |
break; |
| 358 |
case 'e': |
| 359 |
if (sscanf( optarg, "%u", &opts->report_interval ) != 1) { |
| 360 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 361 |
usage(1); |
| 362 |
} |
| 363 |
break; |
| 364 |
case 'R': opts->get_replies = 1; break; |
| 365 |
case 'u': opts->unique_message = 1; break; |
| 366 |
case 't': |
| 367 |
if (sscanf( optarg, "%d", &opts->timeout ) != 1) { |
| 368 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 369 |
usage(1); |
| 370 |
} |
| 371 |
if (opts->timeout > 0) opts->timeout *= 1000; |
| 372 |
break; |
| 373 |
case 'W': |
| 374 |
if (sscanf( optarg, "%d", &opts->incoming_window ) != 1) { |
| 375 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 376 |
usage(1); |
| 377 |
} |
| 378 |
break; |
| 379 |
case 'B': |
| 380 |
if (sscanf( optarg, "%d", &opts->recv_count ) != 1) { |
| 381 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 382 |
usage(1); |
| 383 |
} |
| 384 |
break; |
| 385 |
case 'V': enable_logging(); break; |
| 386 |
case 'N': opts->name = optarg; break; |
| 387 |
case 'T': opts->ca_db = optarg; break; |
| 388 |
case 'C': opts->certificate = optarg; break; |
| 389 |
case 'K': opts->privatekey = optarg; break; |
| 390 |
case 'P': parse_password( optarg, &opts->password ); break; |
| 391 |
|
| 392 |
default: |
| 393 |
usage(1); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
// default target if none specified |
| 398 |
if (opts->targets.count == 0) addresses_add( &opts->targets, "amqp://0.0.0.0" ); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
int main(int argc, char** argv) |
| 403 |
{ |
| 404 |
Options_t opts; |
| 405 |
Statistics_t stats; |
| 406 |
parse_options( argc, argv, &opts ); |
| 407 |
|
| 408 |
pn_reactor_t *reactor = pn_reactor(); |
| 409 |
pn_handler_t *sh = sender_handler(&opts, &stats); |
| 410 |
pn_handler_add(sh, pn_handshaker()); |
| 411 |
pn_reactor_connection(reactor, sh); |
| 412 |
pn_reactor_run(reactor); |
| 413 |
pn_reactor_free(reactor); |
| 414 |
|
| 415 |
pn_handler_free(sh); |
| 416 |
addresses_free(&opts.targets); |
| 417 |
free(opts.password); |
| 418 |
return 0; |
| 419 |
} |
| 420 |
|
| 421 |
#undef PN_USE_DEPRECATED_API |