Debian Codegraph @

c/tools/msgr-send.c

raw
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 targets;
35 uint64_t msg_count;
36 uint32_t msg_size; // of body
37 uint32_t send_batch;
38 int outgoing_window;
39 unsigned int report_interval; // in seconds
40 //Addresses_t subscriptions;
41 //Addresses_t reply_tos;
42 int get_replies;
43 int timeout; // in seconds
44 int incoming_window;
45 int recv_count;
46 const char *name;
47 char *certificate;
48 char *privatekey; // used to sign certificate
49 char *password; // for private key file
50 char *ca_db; // trusted CA database
51 } Options_t;
52
53
54 static void usage(int rc)
55 {
56 printf("Usage: msgr-send [OPTIONS] \n"
57 " -a <addr>[,<addr>]* \tThe target address [amqp[s]://domain[/name]]\n"
58 " -c # \tNumber of messages to send before exiting [0=forever]\n"
59 " -b # \tSize of message body in bytes [1024]\n"
60 " -p # \tSend batches of # messages (wait for replies before sending next batch if -R) [1024]\n"
61 " -w # \t# outgoing window size [0]\n"
62 " -e # \t# seconds to report statistics, 0 = end of test [0]\n"
63 " -R \tWait for a reply to each sent message\n"
64 " -t # \tInactivity timeout in seconds, -1 = no timeout [-1]\n"
65 " -W # \tIncoming window size [0]\n"
66 " -B # \tArgument to Messenger::recv(n) [-1]\n"
67 " -N <name> \tSet the container name to <name>\n"
68 " -V \tEnable debug logging\n"
69 " SSL options:\n"
70 " -T <path> \tDatabase of trusted CA certificates for validating peer\n"
71 " -C <path> \tFile containing self-identifying certificate\n"
72 " -K <path> \tFile containing private key used to sign certificate\n"
73 " -P [pass:<password>|path] \tPassword to unlock private key file.\n"
74 );
75 //printf("-p \t*TODO* Add N sample properties to each message [3]\n");
76 exit(rc);
77 }
78
79 static void parse_options( int argc, char **argv, Options_t *opts )
80 {
81 int c;
82 opterr = 0;
83
84 memset( opts, 0, sizeof(*opts) );
85 opts->msg_size = 1024;
86 opts->send_batch = 1024;
87 opts->timeout = -1;
88 opts->recv_count = -1;
89 addresses_init(&opts->targets);
90
91 while ((c = getopt(argc, argv,
92 "a:c:b:p:w:e:l:Rt:W:B:VN:T:C:K:P:")) != -1) {
93 switch(c) {
94 case 'a': addresses_merge( &opts->targets, optarg ); break;
95 case 'c':
96 if (sscanf( optarg, "%" SCNu64, &opts->msg_count ) != 1) {
97 fprintf(stderr, "Option -%c requires an integer argument.\n", optopt);
98 usage(1);
99 }
100 break;
101 case 'b':
102 if (sscanf( optarg, "%u", &opts->msg_size ) != 1) {
103 fprintf(stderr, "Option -%c requires an integer argument.\n", optopt);
104 usage(1);
105 }
106 break;
107 case 'p':
108 if (sscanf( optarg, "%u", &opts->send_batch ) != 1) {
109 fprintf(stderr, "Option -%c requires an integer argument.\n", optopt);
110 usage(1);
111 }
112 break;
113 case 'w':
114 if (sscanf( optarg, "%d", &opts->outgoing_window ) != 1) {
115 fprintf(stderr, "Option -%c requires an integer argument.\n", optopt);
116 usage(1);
117 }
118 break;
119 case 'e':
120 if (sscanf( optarg, "%u", &opts->report_interval ) != 1) {
121 fprintf(stderr, "Option -%c requires an integer argument.\n", optopt);
122 usage(1);
123 }
124 break;
125 case 'R': opts->get_replies = 1; break;
126 case 't':
127 if (sscanf( optarg, "%d", &opts->timeout ) != 1) {
128 fprintf(stderr, "Option -%c requires an integer argument.\n", optopt);
129 usage(1);
130 }
131 if (opts->timeout > 0) opts->timeout *= 1000;
132 break;
133 case 'W':
134 if (sscanf( optarg, "%d", &opts->incoming_window ) != 1) {
135 fprintf(stderr, "Option -%c requires an integer argument.\n", optopt);
136 usage(1);
137 }
138 break;
139 case 'B':
140 if (sscanf( optarg, "%d", &opts->recv_count ) != 1) {
141 fprintf(stderr, "Option -%c requires an integer argument.\n", optopt);
142 usage(1);
143 }
144 break;
145 case 'V': enable_logging(); break;
146 case 'N': opts->name = optarg; break;
147 case 'T': opts->ca_db = optarg; break;
148 case 'C': opts->certificate = optarg; break;
149 case 'K': opts->privatekey = optarg; break;
150 case 'P': parse_password( optarg, &opts->password ); break;
151
152 default:
153 usage(1);
154 }
155 }
156
157 // default target if none specified
158 if (opts->targets.count == 0) addresses_add( &opts->targets, "amqp://0.0.0.0" );
159 }
160
161
162 // return the # of reply messages received
163 static int process_replies( pn_messenger_t *messenger,
164 pn_message_t *message,
165 Statistics_t *stats,
166 int max_count)
167 {
168 int received = 0;
169 LOG("Calling pn_messenger_recv(%d)\n", max_count);
170 int rc = pn_messenger_recv( messenger, max_count );
171 check((rc == 0 || rc == PN_TIMEOUT), "pn_messenger_recv() failed");
172 LOG("Messages on incoming queue: %d\n", pn_messenger_incoming(messenger));
173 while (pn_messenger_incoming(messenger)) {
174 pn_messenger_get(messenger, message);
175 check_messenger(messenger);
176 received++;
177 // TODO: header decoding?
178 statistics_msg_received( stats, message );
179 // uint64_t id = pn_message_get_correlation_id( message ).u.as_ulong;
180 }
181 return received;
182 }
183
184
185
186 int main(int argc, char** argv)
187 {
188 Options_t opts;
189 Statistics_t stats;
190 uint64_t sent = 0;
191 uint64_t received = 0;
192 int target_index = 0;
193 int rc;
194
195 pn_message_t *message = 0;
196 pn_message_t *reply_message = 0;
197 pn_messenger_t *messenger = 0;
198
199 parse_options( argc, argv, &opts );
200
201 messenger = pn_messenger( opts.name );
202
203 if (opts.certificate) {
204 rc = pn_messenger_set_certificate(messenger, opts.certificate);
205 check( rc == 0, "Failed to set certificate" );
206 }
207
208 if (opts.privatekey) {
209 rc = pn_messenger_set_private_key(messenger, opts.privatekey);
210 check( rc == 0, "Failed to set private key" );
211 }
212
213 if (opts.password) {
214 rc = pn_messenger_set_password(messenger, opts.password);
215 free(opts.password);
216 check( rc == 0, "Failed to set password" );
217 }
218
219 if (opts.ca_db) {
220 rc = pn_messenger_set_trusted_certificates(messenger, opts.ca_db);
221 check( rc == 0, "Failed to set trusted CA database" );
222 }
223
224 if (opts.outgoing_window) {
225 pn_messenger_set_outgoing_window( messenger, opts.outgoing_window );
226 }
227 pn_messenger_set_timeout( messenger, opts.timeout );
228 pn_messenger_start(messenger);
229
230 message = pn_message();
231 check(message, "failed to allocate a message");
232 pn_message_set_reply_to(message, "~");
233 pn_data_t *body = pn_message_body(message);
234 char *data = (char *)calloc(1, opts.msg_size);
235 pn_data_put_binary(body, pn_bytes(opts.msg_size, data));
236 free(data);
237 pn_atom_t id;
238 id.type = PN_ULONG;
239
240 #if 0
241 // TODO: how do we effectively benchmark header processing overhead???
242 pn_data_t *props = pn_message_properties(message);
243 pn_data_put_map(props);
244 pn_data_enter(props);
245 //
246 //pn_data_put_string(props, pn_bytes(6, "string"));
247 //pn_data_put_string(props, pn_bytes(10, "this is awkward"));
248 //
249 //pn_data_put_string(props, pn_bytes(4, "long"));
250 pn_data_put_long(props, 12345);
251 //
252 //pn_data_put_string(props, pn_bytes(9, "timestamp"));
253 pn_data_put_timestamp(props, (pn_timestamp_t) 54321);
254 pn_data_exit(props);
255 #endif
256
257 const int get_replies = opts.get_replies;
258
259 if (get_replies) {
260 // disable the timeout so that pn_messenger_recv() won't block
261 reply_message = pn_message();
262 check(reply_message, "failed to allocate a message");
263 }
264
265 statistics_start( &stats );
266 while (!opts.msg_count || (sent < opts.msg_count)) {
267
268 // setup the message to send
269 pn_message_set_address(message, opts.targets.addresses[target_index]);
270 target_index = NEXT_ADDRESS(opts.targets, target_index);
271 id.u.as_ulong = sent;
272 pn_message_set_correlation_id( message, id );
273 pn_message_set_creation_time( message, msgr_now() );
274 pn_messenger_put(messenger, message);
275 sent++;
276 if (opts.send_batch && (pn_messenger_outgoing(messenger) >= (int)opts.send_batch)) {
277 if (get_replies) {
278 while (received < sent) {
279 // this will also transmit any pending sent messages
280 received += process_replies( messenger, reply_message,
281 &stats, opts.recv_count );
282 }
283 } else {
284 LOG("Calling pn_messenger_send()\n");
285 rc = pn_messenger_send(messenger, -1);
286 check((rc == 0 || rc == PN_TIMEOUT), "pn_messenger_send() failed");
287 }
288 }
289 check_messenger(messenger);
290 }
291
292 LOG("Messages received=%" PRIu64 " sent=%" PRIu64 "\n", received, sent);
293
294 if (get_replies) {
295 // wait for the last of the replies
296 while (received < sent) {
297 int count = process_replies( messenger, reply_message,
298 &stats, opts.recv_count );
299 check( count > 0 || (opts.timeout == 0),
300 "Error: timed out waiting for reply messages\n");
301 received += count;
302 LOG("Messages received=%" PRIu64 " sent=%" PRIu64 "\n", received, sent);
303 }
304 } else if (pn_messenger_outgoing(messenger) > 0) {
305 LOG("Calling pn_messenger_send()\n");
306 rc = pn_messenger_send(messenger, -1);
307 check(rc == 0, "pn_messenger_send() failed");
308 }
309
310 rc = pn_messenger_stop(messenger);
311 check(rc == 0, "pn_messenger_stop() failed");
312 check_messenger(messenger);
313
314 statistics_report( &stats, sent, received );
315
316 pn_messenger_free(messenger);
317 pn_message_free(message);
318 if (reply_message) pn_message_free( reply_message );
319 addresses_free( &opts.targets );
320
321 return 0;
322 }
323
324 #undef PN_USE_DEPRECATED_API