| 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 |
#include "msgr-common.h" |
| 24 |
#include "proton/message.h" |
| 25 |
#include "proton/messenger.h" |
| 26 |
#include "proton/error.h" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <ctype.h> |
| 32 |
|
| 33 |
typedef struct { |
| 34 |
Addresses_t subscriptions; |
| 35 |
uint64_t msg_count; |
| 36 |
int recv_count; |
| 37 |
int incoming_window; |
| 38 |
int timeout; // seconds |
| 39 |
unsigned int report_interval; // in seconds |
| 40 |
int outgoing_window; |
| 41 |
Addresses_t forwarding_targets; |
| 42 |
int reply; |
| 43 |
const char *name; |
| 44 |
const char *ready_text; |
| 45 |
char *certificate; |
| 46 |
char *privatekey; // used to sign certificate |
| 47 |
char *password; // for private key file |
| 48 |
char *ca_db; // trusted CA database |
| 49 |
} Options_t; |
| 50 |
|
| 51 |
|
| 52 |
static void usage(int rc) |
| 53 |
{ |
| 54 |
printf("Usage: msgr-recv [OPTIONS] \n" |
| 55 |
" -a <addr>[,<addr>]* \tAddresses to listen on [amqp://~0.0.0.0]\n" |
| 56 |
" -c # \tNumber of messages to receive before exiting [0=forever]\n" |
| 57 |
" -b # \tArgument to Messenger::recv(n) [2048]\n" |
| 58 |
" -w # \tSize for incoming window [0]\n" |
| 59 |
" -t # \tInactivity timeout in seconds, -1 = no timeout [-1]\n" |
| 60 |
" -e # \t# seconds to report statistics, 0 = end of test [0] *TBD*\n" |
| 61 |
" -R \tSend reply if 'reply-to' present\n" |
| 62 |
" -W # \t# outgoing window size [0]\n" |
| 63 |
" -F <addr>[,<addr>]* \tAddresses used for forwarding received messages\n" |
| 64 |
" -N <name> \tSet the container name to <name>\n" |
| 65 |
" -X <text> \tPrint '<text>\\n' to stdout after all subscriptions are created\n" |
| 66 |
" -V \tEnable debug logging\n" |
| 67 |
" SSL options:\n" |
| 68 |
" -T <path> \tDatabase of trusted CA certificates for validating peer\n" |
| 69 |
" -C <path> \tFile containing self-identifying certificate\n" |
| 70 |
" -K <path> \tFile containing private key used to sign certificate\n" |
| 71 |
" -P [pass:<password>|path] \tPassword to unlock private key file.\n" |
| 72 |
); |
| 73 |
exit(rc); |
| 74 |
} |
| 75 |
|
| 76 |
static void parse_options( int argc, char **argv, Options_t *opts ) |
| 77 |
{ |
| 78 |
int c; |
| 79 |
opterr = 0; |
| 80 |
|
| 81 |
memset( opts, 0, sizeof(*opts) ); |
| 82 |
opts->recv_count = -1; |
| 83 |
opts->timeout = -1; |
| 84 |
addresses_init(&opts->subscriptions); |
| 85 |
addresses_init(&opts->forwarding_targets); |
| 86 |
|
| 87 |
while ((c = getopt(argc, argv, |
| 88 |
"a:c:b:w:t:e:RW:F:VN:X:T:C:K:P:")) != -1) { |
| 89 |
switch (c) { |
| 90 |
case 'a': addresses_merge( &opts->subscriptions, optarg ); break; |
| 91 |
case 'c': |
| 92 |
if (sscanf( optarg, "%" SCNu64, &opts->msg_count ) != 1) { |
| 93 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 94 |
usage(1); |
| 95 |
} |
| 96 |
break; |
| 97 |
case 'b': |
| 98 |
if (sscanf( optarg, "%d", &opts->recv_count ) != 1) { |
| 99 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 100 |
usage(1); |
| 101 |
} |
| 102 |
break; |
| 103 |
case 'w': |
| 104 |
if (sscanf( optarg, "%d", &opts->incoming_window ) != 1) { |
| 105 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 106 |
usage(1); |
| 107 |
} |
| 108 |
break; |
| 109 |
case 't': |
| 110 |
if (sscanf( optarg, "%d", &opts->timeout ) != 1) { |
| 111 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 112 |
usage(1); |
| 113 |
} |
| 114 |
if (opts->timeout > 0) opts->timeout *= 1000; |
| 115 |
break; |
| 116 |
case 'e': |
| 117 |
if (sscanf( optarg, "%u", &opts->report_interval ) != 1) { |
| 118 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 119 |
usage(1); |
| 120 |
} |
| 121 |
break; |
| 122 |
case 'R': opts->reply = 1; break; |
| 123 |
case 'W': |
| 124 |
if (sscanf( optarg, "%d", &opts->outgoing_window ) != 1) { |
| 125 |
fprintf(stderr, "Option -%c requires an integer argument.\n", optopt); |
| 126 |
usage(1); |
| 127 |
} |
| 128 |
break; |
| 129 |
case 'F': addresses_merge( &opts->forwarding_targets, optarg ); break; |
| 130 |
case 'V': enable_logging(); break; |
| 131 |
case 'N': opts->name = optarg; break; |
| 132 |
case 'X': opts->ready_text = optarg; break; |
| 133 |
case 'T': opts->ca_db = optarg; break; |
| 134 |
case 'C': opts->certificate = optarg; break; |
| 135 |
case 'K': opts->privatekey = optarg; break; |
| 136 |
case 'P': parse_password( optarg, &opts->password ); break; |
| 137 |
|
| 138 |
default: |
| 139 |
usage(1); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// default subscription if none specified |
| 144 |
if (opts->subscriptions.count == 0) addresses_add( &opts->subscriptions, |
| 145 |
"amqp://~0.0.0.0" ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
int main(int argc, char** argv) |
| 151 |
{ |
| 152 |
Options_t opts; |
| 153 |
Statistics_t stats; |
| 154 |
uint64_t sent = 0; |
| 155 |
uint64_t received = 0; |
| 156 |
int forwarding_index = 0; |
| 157 |
int rc; |
| 158 |
|
| 159 |
pn_message_t *message; |
| 160 |
pn_messenger_t *messenger; |
| 161 |
|
| 162 |
parse_options( argc, argv, &opts ); |
| 163 |
|
| 164 |
const int forward = opts.forwarding_targets.count != 0; |
| 165 |
|
| 166 |
message = pn_message(); |
| 167 |
messenger = pn_messenger( opts.name ); |
| 168 |
|
| 169 |
/* load the various command line options if they're set */ |
| 170 |
if (opts.certificate) { |
| 171 |
rc = pn_messenger_set_certificate(messenger, opts.certificate); |
| 172 |
check_messenger(messenger); |
| 173 |
check( rc == 0, "Failed to set certificate" ); |
| 174 |
} |
| 175 |
|
| 176 |
if (opts.privatekey) { |
| 177 |
rc = pn_messenger_set_private_key(messenger, opts.privatekey); |
| 178 |
check_messenger(messenger); |
| 179 |
check( rc == 0, "Failed to set private key" ); |
| 180 |
} |
| 181 |
|
| 182 |
if (opts.password) { |
| 183 |
rc = pn_messenger_set_password(messenger, opts.password); |
| 184 |
free(opts.password); |
| 185 |
check_messenger(messenger); |
| 186 |
check( rc == 0, "Failed to set password" ); |
| 187 |
} |
| 188 |
|
| 189 |
if (opts.ca_db) { |
| 190 |
rc = pn_messenger_set_trusted_certificates(messenger, opts.ca_db); |
| 191 |
check_messenger(messenger); |
| 192 |
check( rc == 0, "Failed to set trusted CA database" ); |
| 193 |
} |
| 194 |
|
| 195 |
if (opts.incoming_window) { |
| 196 |
// RAFI: seems to cause receiver to hang: |
| 197 |
pn_messenger_set_incoming_window( messenger, opts.incoming_window ); |
| 198 |
} |
| 199 |
|
| 200 |
pn_messenger_set_timeout( messenger, opts.timeout ); |
| 201 |
|
| 202 |
pn_messenger_start(messenger); |
| 203 |
check_messenger(messenger); |
| 204 |
|
| 205 |
int i; |
| 206 |
for (i = 0; i < opts.subscriptions.count; i++) { |
| 207 |
pn_messenger_subscribe(messenger, opts.subscriptions.addresses[i]); |
| 208 |
check_messenger(messenger); |
| 209 |
LOG("Subscribing to '%s'\n", opts.subscriptions.addresses[i]); |
| 210 |
} |
| 211 |
|
| 212 |
// hack to let test scripts know when the receivers are ready (so |
| 213 |
// that the senders may be started) |
| 214 |
if (opts.ready_text) { |
| 215 |
fprintf(stdout, "%s\n", opts.ready_text); |
| 216 |
fflush(stdout); |
| 217 |
} |
| 218 |
|
| 219 |
while (!opts.msg_count || received < opts.msg_count) { |
| 220 |
|
| 221 |
LOG("Calling pn_messenger_recv(%d)\n", opts.recv_count); |
| 222 |
rc = pn_messenger_recv(messenger, opts.recv_count); |
| 223 |
check_messenger(messenger); |
| 224 |
check(rc == 0 || (opts.timeout == 0 && rc == PN_TIMEOUT), "pn_messenger_recv() failed"); |
| 225 |
|
| 226 |
// start the timer only after receiving the first msg |
| 227 |
if (received == 0) statistics_start( &stats ); |
| 228 |
|
| 229 |
LOG("Messages on incoming queue: %d\n", pn_messenger_incoming(messenger)); |
| 230 |
while (pn_messenger_incoming(messenger)) { |
| 231 |
pn_messenger_get(messenger, message); |
| 232 |
check_messenger(messenger); |
| 233 |
received++; |
| 234 |
// TODO: header decoding? |
| 235 |
// uint64_t id = pn_message_get_correlation_id( message ).u.as_ulong; |
| 236 |
statistics_msg_received( &stats, message ); |
| 237 |
|
| 238 |
if (opts.reply) { |
| 239 |
const char *reply_addr = pn_message_get_reply_to( message ); |
| 240 |
if (reply_addr) { |
| 241 |
LOG("Replying to: %s\n", reply_addr ); |
| 242 |
pn_message_set_address( message, reply_addr ); |
| 243 |
pn_message_set_creation_time( message, msgr_now() ); |
| 244 |
pn_messenger_put(messenger, message); |
| 245 |
sent++; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (forward) { |
| 250 |
const char *forward_addr = opts.forwarding_targets.addresses[forwarding_index]; |
| 251 |
forwarding_index = NEXT_ADDRESS(opts.forwarding_targets, forwarding_index); |
| 252 |
LOG("Forwarding to: %s\n", forward_addr ); |
| 253 |
pn_message_set_address( message, forward_addr ); |
| 254 |
pn_message_set_reply_to( message, NULL ); // else points to origin sender |
| 255 |
pn_message_set_creation_time( message, msgr_now() ); |
| 256 |
pn_messenger_put(messenger, message); |
| 257 |
sent++; |
| 258 |
} |
| 259 |
|
| 260 |
} |
| 261 |
LOG("Messages received=%" PRIu64 " sent=%" PRIu64 "\n", received, sent); |
| 262 |
} |
| 263 |
|
| 264 |
// this will flush any pending sends |
| 265 |
if (pn_messenger_outgoing(messenger) > 0) { |
| 266 |
LOG("Calling pn_messenger_send()\n"); |
| 267 |
rc = pn_messenger_send(messenger, -1); |
| 268 |
check_messenger(messenger); |
| 269 |
check(rc == 0, "pn_messenger_send() failed"); |
| 270 |
} |
| 271 |
|
| 272 |
rc = pn_messenger_stop(messenger); |
| 273 |
check(rc == 0, "pn_messenger_stop() failed"); |
| 274 |
check_messenger(messenger); |
| 275 |
|
| 276 |
statistics_report( &stats, sent, received ); |
| 277 |
|
| 278 |
pn_messenger_free(messenger); |
| 279 |
pn_message_free(message); |
| 280 |
addresses_free( &opts.subscriptions ); |
| 281 |
addresses_free( &opts.forwarding_targets ); |
| 282 |
|
| 283 |
return 0; |
| 284 |
} |
| 285 |
|
| 286 |
#undef PN_USE_DEPRECATED_API |