| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <proton/connection.h> |
| 23 |
#include <proton/condition.h> |
| 24 |
#include <proton/delivery.h> |
| 25 |
#include <proton/link.h> |
| 26 |
#include <proton/message.h> |
| 27 |
#include <proton/proactor.h> |
| 28 |
#include <proton/session.h> |
| 29 |
#include <proton/transport.h> |
| 30 |
|
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
|
| 34 |
/* |
| 35 |
* Send aborted messages. |
| 36 |
* |
| 37 |
* An 80 Kbyte (MSG_SIZE) message is generated. |
| 38 |
* The size is chosen to be large enough so that some of the message |
| 39 |
* will go out on the wire before the abort is sent. |
| 40 |
* |
| 41 |
* 79 Kbytes (MSG_SIZE - HOLDBACK) are sent on one link flow event. |
| 42 |
* The message is aborted on the next link flow event. |
| 43 |
*/ |
| 44 |
|
| 45 |
#define MSG_SIZE 80000 |
| 46 |
#define HOLDBACK 1000 |
| 47 |
|
| 48 |
#if MSG_SIZE <= HOLDBACK |
| 49 |
#error "MSG_SIZE must be greater than HOLDBACK" |
| 50 |
#endif |
| 51 |
|
| 52 |
typedef struct app_data_t { |
| 53 |
const char *host, *port; |
| 54 |
const char *amqp_address; |
| 55 |
const char *container_id; |
| 56 |
int message_count; |
| 57 |
|
| 58 |
pn_proactor_t *proactor; |
| 59 |
pn_rwbytes_t message_buffer; |
| 60 |
int sent; |
| 61 |
int aborted; |
| 62 |
bool in_progress; |
| 63 |
} app_data_t; |
| 64 |
|
| 65 |
static int exit_code = 0; |
| 66 |
|
| 67 |
static void check_condition(pn_event_t *e, pn_condition_t *cond) { |
| 68 |
if (pn_condition_is_set(cond)) { |
| 69 |
fprintf(stderr, "%s: %s: %s\n", pn_event_type_name(pn_event_type(e)), |
| 70 |
pn_condition_get_name(cond), pn_condition_get_description(cond)); |
| 71 |
pn_connection_close(pn_event_connection(e)); |
| 72 |
exit_code = 1; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/* Create a message with a map { "sequence" : number } encode it and return the encoded buffer. */ |
| 77 |
static pn_bytes_t encode_message(app_data_t* app) { |
| 78 |
/* Construct a message with the map { "sequence": app.sent } */ |
| 79 |
pn_message_t* message = pn_message(); |
| 80 |
char data[MSG_SIZE + 11] = {0}; |
| 81 |
pn_data_t* body; |
| 82 |
pn_message_set_id(message, (pn_atom_t){.type=PN_ULONG, .u.as_ulong=app->sent}); |
| 83 |
body = pn_message_body(message); |
| 84 |
pn_data_enter(body); |
| 85 |
pn_data_put_string(body, pn_bytes(MSG_SIZE, data)); |
| 86 |
pn_data_exit(body); |
| 87 |
|
| 88 |
/* encode the message, expanding the encode buffer as needed */ |
| 89 |
if (app->message_buffer.start == NULL) { |
| 90 |
static const size_t initial_size = MSG_SIZE + 1000; |
| 91 |
app->message_buffer = pn_rwbytes(initial_size, (char*)malloc(initial_size)); |
| 92 |
} |
| 93 |
/* app->message_buffer is the total buffer space available. */ |
| 94 |
/* mbuf wil point at just the portion used by the encoded message */ |
| 95 |
{ |
| 96 |
pn_rwbytes_t mbuf = pn_rwbytes(app->message_buffer.size, app->message_buffer.start); |
| 97 |
int status = 0; |
| 98 |
while ((status = pn_message_encode(message, mbuf.start, &mbuf.size)) == PN_OVERFLOW) { |
| 99 |
app->message_buffer.size *= 2; |
| 100 |
app->message_buffer.start = (char*)realloc(app->message_buffer.start, app->message_buffer.size); |
| 101 |
mbuf.size = app->message_buffer.size; |
| 102 |
} |
| 103 |
if (status != 0) { |
| 104 |
fprintf(stderr, "error encoding message: %s\n", pn_error_text(pn_message_error(message))); |
| 105 |
exit(1); |
| 106 |
} |
| 107 |
pn_message_free(message); |
| 108 |
return pn_bytes(mbuf.size, mbuf.start); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/* Returns true to continue, false if finished */ |
| 113 |
static bool handle(app_data_t* app, pn_event_t* event) { |
| 114 |
switch (pn_event_type(event)) { |
| 115 |
|
| 116 |
case PN_CONNECTION_INIT: { |
| 117 |
pn_connection_t* c = pn_event_connection(event); |
| 118 |
pn_session_t* s = pn_session(pn_event_connection(event)); |
| 119 |
pn_connection_set_container(c, app->container_id); |
| 120 |
pn_connection_open(c); |
| 121 |
pn_session_open(s); |
| 122 |
{ |
| 123 |
pn_link_t* l = pn_sender(s, "my_sender"); |
| 124 |
pn_terminus_set_address(pn_link_target(l), app->amqp_address); |
| 125 |
pn_link_open(l); |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
case PN_LINK_FLOW: { |
| 131 |
/* The peer has given us some credit, now we can send messages */ |
| 132 |
pn_link_t *sender = pn_event_link(event); |
| 133 |
while (app->in_progress || (pn_link_credit(sender) > 0 && app->sent < app->message_count)) { |
| 134 |
if (!app->in_progress) { |
| 135 |
pn_bytes_t msgbuf = encode_message(app); |
| 136 |
/* Use sent counter as unique delivery tag. */ |
| 137 |
pn_delivery(sender, pn_dtag((const char *)&app->sent, sizeof(app->sent))); |
| 138 |
pn_link_send(sender, msgbuf.start, msgbuf.size - HOLDBACK); /* Send some part of message */ |
| 139 |
app->in_progress = true; |
| 140 |
/* Return from this link flow event and abort the message on next, */ |
| 141 |
break; |
| 142 |
} else { |
| 143 |
pn_delivery_t * pnd = pn_link_current(sender); |
| 144 |
pn_delivery_abort(pnd); |
| 145 |
/* aborted delivery is presettled and never ack'd. */ |
| 146 |
if (++app->aborted == app->message_count) { |
| 147 |
printf("%d messages started and aborted\n", app->aborted); |
| 148 |
pn_connection_close(pn_event_connection(event)); |
| 149 |
/* Continue handling events till we receive TRANSPORT_CLOSED */ |
| 150 |
} |
| 151 |
++app->sent; |
| 152 |
app->in_progress = false; |
| 153 |
} |
| 154 |
} |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
case PN_DELIVERY: { |
| 159 |
/* We received acknowledgement from the peer that a message was delivered. */ |
| 160 |
pn_delivery_t* d = pn_event_delivery(event); |
| 161 |
fprintf(stderr, "Aborted deliveries should not receive delivery events. Delivery state %d\n", (int)pn_delivery_remote_state(d)); |
| 162 |
pn_connection_close(pn_event_connection(event)); |
| 163 |
exit_code=1; |
| 164 |
break; |
| 165 |
} |
| 166 |
|
| 167 |
case PN_TRANSPORT_CLOSED: |
| 168 |
check_condition(event, pn_transport_condition(pn_event_transport(event))); |
| 169 |
break; |
| 170 |
|
| 171 |
case PN_CONNECTION_REMOTE_CLOSE: |
| 172 |
check_condition(event, pn_connection_remote_condition(pn_event_connection(event))); |
| 173 |
pn_connection_close(pn_event_connection(event)); |
| 174 |
break; |
| 175 |
|
| 176 |
case PN_SESSION_REMOTE_CLOSE: |
| 177 |
check_condition(event, pn_session_remote_condition(pn_event_session(event))); |
| 178 |
pn_connection_close(pn_event_connection(event)); |
| 179 |
break; |
| 180 |
|
| 181 |
case PN_LINK_REMOTE_CLOSE: |
| 182 |
case PN_LINK_REMOTE_DETACH: |
| 183 |
check_condition(event, pn_link_remote_condition(pn_event_link(event))); |
| 184 |
pn_connection_close(pn_event_connection(event)); |
| 185 |
break; |
| 186 |
|
| 187 |
case PN_PROACTOR_INACTIVE: |
| 188 |
return false; |
| 189 |
|
| 190 |
default: break; |
| 191 |
} |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
void run(app_data_t *app) { |
| 196 |
/* Loop and handle events */ |
| 197 |
do { |
| 198 |
pn_event_batch_t *events = pn_proactor_wait(app->proactor); |
| 199 |
pn_event_t* e; |
| 200 |
for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) { |
| 201 |
if (!handle(app, e)) { |
| 202 |
return; |
| 203 |
} |
| 204 |
} |
| 205 |
pn_proactor_done(app->proactor, events); |
| 206 |
} while(true); |
| 207 |
} |
| 208 |
|
| 209 |
int main(int argc, char **argv) { |
| 210 |
struct app_data_t app = {0}; |
| 211 |
char addr[PN_MAX_ADDR]; |
| 212 |
|
| 213 |
app.container_id = argv[0]; /* Should be unique */ |
| 214 |
app.host = (argc > 1) ? argv[1] : ""; |
| 215 |
app.port = (argc > 2) ? argv[2] : "amqp"; |
| 216 |
app.amqp_address = (argc > 3) ? argv[3] : "examples"; |
| 217 |
app.message_count = (argc > 4) ? atoi(argv[4]) : 10; |
| 218 |
|
| 219 |
app.proactor = pn_proactor(); |
| 220 |
pn_proactor_addr(addr, sizeof(addr), app.host, app.port); |
| 221 |
pn_proactor_connect2(app.proactor, NULL, NULL, addr); |
| 222 |
run(&app); |
| 223 |
pn_proactor_free(app.proactor); |
| 224 |
free(app.message_buffer.start); |
| 225 |
return exit_code; |
| 226 |
} |