| 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 "thread.h" |
| 23 |
|
| 24 |
#include <proton/condition.h> |
| 25 |
#include <proton/raw_connection.h> |
| 26 |
#include <proton/listener.h> |
| 27 |
#include <proton/netaddr.h> |
| 28 |
#include <proton/proactor.h> |
| 29 |
|
| 30 |
#include <stdbool.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
|
| 35 |
typedef struct app_data_t { |
| 36 |
const char *host, *port; |
| 37 |
const char *amqp_address; |
| 38 |
|
| 39 |
pn_proactor_t *proactor; |
| 40 |
pn_listener_t *listener; |
| 41 |
|
| 42 |
pthread_mutex_t lock; |
| 43 |
int64_t first_idle_time; |
| 44 |
int64_t wake_conn_time; |
| 45 |
int connects; |
| 46 |
int disconnects; |
| 47 |
|
| 48 |
/* Sender values */ |
| 49 |
|
| 50 |
/* Receiver values */ |
| 51 |
} app_data_t; |
| 52 |
|
| 53 |
#define MAX_CONNECTIONS 5 |
| 54 |
|
| 55 |
typedef struct conn_data_t { |
| 56 |
pn_raw_connection_t *connection; |
| 57 |
int64_t last_recv_time; |
| 58 |
int bytes; |
| 59 |
int buffers; |
| 60 |
} conn_data_t; |
| 61 |
|
| 62 |
static conn_data_t conn_data[MAX_CONNECTIONS] = {{0}}; |
| 63 |
|
| 64 |
static int exit_code = 0; |
| 65 |
|
| 66 |
/* Close the connection and the listener so so we will get a |
| 67 |
* PN_PROACTOR_INACTIVE event and exit, once all outstanding events |
| 68 |
* are processed. |
| 69 |
*/ |
| 70 |
static void close_all(pn_raw_connection_t *c, app_data_t *app) { |
| 71 |
if (c) pn_raw_connection_close(c); |
| 72 |
if (app->listener) pn_listener_close(app->listener); |
| 73 |
} |
| 74 |
|
| 75 |
static bool check_condition(pn_event_t *e, pn_condition_t *cond, app_data_t *app) { |
| 76 |
if (pn_condition_is_set(cond)) { |
| 77 |
fprintf(stderr, "%s: %s: %s\n", pn_event_type_name(pn_event_type(e)), |
| 78 |
pn_condition_get_name(cond), pn_condition_get_description(cond)); |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
static void check_condition_fatal(pn_event_t *e, pn_condition_t *cond, app_data_t *app) { |
| 86 |
if (check_condition(e, cond, app)) { |
| 87 |
close_all(pn_event_raw_connection(e), app); |
| 88 |
exit_code = 1; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
static void send_message(pn_raw_connection_t *c, const char* msg) { |
| 93 |
pn_raw_buffer_t buffer; |
| 94 |
uint32_t len = strlen(msg); |
| 95 |
char *buf = (char*) malloc(1024); |
| 96 |
memcpy(buf, msg, len); |
| 97 |
buffer.bytes = buf; |
| 98 |
buffer.capacity = 1024; |
| 99 |
buffer.offset = 0; |
| 100 |
buffer.size = len; |
| 101 |
// If message not accepted just throw it away! |
| 102 |
if (pn_raw_connection_write_buffers(c, &buffer, 1) < 1) { |
| 103 |
printf("**Couldn't send message: write not accepted**\n"); |
| 104 |
free(buf); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
static void recv_message(pn_raw_buffer_t buf) { |
| 109 |
fwrite(buf.bytes, buf.size, 1, stdout); |
| 110 |
} |
| 111 |
|
| 112 |
conn_data_t *make_conn_data(pn_raw_connection_t *c) { |
| 113 |
int i; |
| 114 |
for (i = 0; i < MAX_CONNECTIONS; ++i) { |
| 115 |
if (!conn_data[i].connection) { |
| 116 |
conn_data[i].connection = c; |
| 117 |
return &conn_data[i]; |
| 118 |
} |
| 119 |
} |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
void free_conn_data(conn_data_t *c) { |
| 124 |
if (!c) return; |
| 125 |
c->connection = NULL; |
| 126 |
} |
| 127 |
|
| 128 |
#define READ_BUFFERS 4 |
| 129 |
|
| 130 |
static void free_buffers(pn_raw_buffer_t buffs[], size_t n) { |
| 131 |
unsigned i; |
| 132 |
for (i=0; i<n; ++i) { |
| 133 |
free(buffs[i].bytes); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/* This function handles events when we are acting as the receiver */ |
| 138 |
static void handle_receive(app_data_t *app, pn_event_t* event) { |
| 139 |
switch (pn_event_type(event)) { |
| 140 |
|
| 141 |
case PN_RAW_CONNECTION_CONNECTED: { |
| 142 |
pn_raw_connection_t *c = pn_event_raw_connection(event); |
| 143 |
conn_data_t *cd = (conn_data_t *) pn_raw_connection_get_context(c); |
| 144 |
pn_raw_buffer_t buffers[READ_BUFFERS] = {{0}}; |
| 145 |
if (cd) { |
| 146 |
int i = READ_BUFFERS; |
| 147 |
printf("**raw connection %tu connected\n", cd-conn_data); |
| 148 |
pthread_mutex_lock(&app->lock); |
| 149 |
app->connects++; |
| 150 |
pthread_mutex_unlock(&app->lock); |
| 151 |
for (; i; --i) { |
| 152 |
pn_raw_buffer_t *buff = &buffers[READ_BUFFERS-i]; |
| 153 |
buff->bytes = (char*) malloc(1024); |
| 154 |
buff->capacity = 1024; |
| 155 |
buff->size = 0; |
| 156 |
buff->offset = 0; |
| 157 |
} |
| 158 |
pn_raw_connection_give_read_buffers(c, buffers, READ_BUFFERS); |
| 159 |
} else { |
| 160 |
printf("**too many raw connections connected: closing\n"); |
| 161 |
pn_raw_connection_close(c); |
| 162 |
} |
| 163 |
} break; |
| 164 |
|
| 165 |
case PN_RAW_CONNECTION_WAKE: { |
| 166 |
pn_raw_connection_t *c = pn_event_raw_connection(event); |
| 167 |
conn_data_t *cd = (conn_data_t *) pn_raw_connection_get_context(c); |
| 168 |
printf("**raw connection %tu woken\n", cd-conn_data); |
| 169 |
} break; |
| 170 |
|
| 171 |
case PN_RAW_CONNECTION_DISCONNECTED: { |
| 172 |
pn_raw_connection_t *c = pn_event_raw_connection(event); |
| 173 |
conn_data_t *cd = (conn_data_t *) pn_raw_connection_get_context(c); |
| 174 |
if (cd) { |
| 175 |
pthread_mutex_lock(&app->lock); |
| 176 |
app->disconnects++; |
| 177 |
pthread_mutex_unlock(&app->lock); |
| 178 |
printf("**raw connection %tu disconnected: bytes: %d, buffers: %d\n", cd-conn_data, cd->bytes, cd->buffers); |
| 179 |
} else { |
| 180 |
printf("**raw connection disconnected: not connected\n"); |
| 181 |
} |
| 182 |
check_condition(event, pn_raw_connection_condition(c), app); |
| 183 |
pn_raw_connection_wake(c); |
| 184 |
free_conn_data(cd); |
| 185 |
} break; |
| 186 |
|
| 187 |
case PN_RAW_CONNECTION_DRAIN_BUFFERS: { |
| 188 |
pn_raw_connection_t *c = pn_event_raw_connection(event); |
| 189 |
pn_raw_buffer_t buffs[READ_BUFFERS]; |
| 190 |
size_t n; |
| 191 |
while ( (n = pn_raw_connection_take_read_buffers(c, buffs, READ_BUFFERS)) ) { |
| 192 |
free_buffers(buffs, n); |
| 193 |
} |
| 194 |
while ( (n = pn_raw_connection_take_written_buffers(c, buffs, READ_BUFFERS)) ) { |
| 195 |
free_buffers(buffs, n); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
case PN_RAW_CONNECTION_NEED_READ_BUFFERS: { |
| 200 |
} break; |
| 201 |
|
| 202 |
case PN_RAW_CONNECTION_READ: { |
| 203 |
pn_raw_connection_t *c = pn_event_raw_connection(event); |
| 204 |
conn_data_t *cd = (conn_data_t *) pn_raw_connection_get_context(c); |
| 205 |
pn_raw_buffer_t buffs[READ_BUFFERS]; |
| 206 |
size_t n; |
| 207 |
cd->last_recv_time = pn_proactor_now_64(); |
| 208 |
while ( (n = pn_raw_connection_take_read_buffers(c, buffs, READ_BUFFERS)) ) { |
| 209 |
unsigned i; |
| 210 |
for (i=0; i<n && buffs[i].bytes; ++i) { |
| 211 |
cd->bytes += buffs[i].size; |
| 212 |
recv_message(buffs[i]); |
| 213 |
} |
| 214 |
cd->buffers += n; |
| 215 |
|
| 216 |
// Echo back if we can |
| 217 |
if (!pn_raw_connection_is_write_closed(c)) { |
| 218 |
pn_raw_connection_write_buffers(c, buffs, n); |
| 219 |
} else if (!pn_raw_connection_is_read_closed(c)) { |
| 220 |
pn_raw_connection_give_read_buffers(c, buffs, n); |
| 221 |
} else { |
| 222 |
free_buffers(buffs, n); |
| 223 |
} |
| 224 |
} |
| 225 |
} break; |
| 226 |
|
| 227 |
case PN_RAW_CONNECTION_CLOSED_READ: { |
| 228 |
pn_raw_connection_t *c = pn_event_raw_connection(event); |
| 229 |
if (!pn_raw_connection_is_write_closed(c)) { |
| 230 |
send_message(c, "** Goodbye **"); |
| 231 |
} |
| 232 |
} |
| 233 |
case PN_RAW_CONNECTION_CLOSED_WRITE:{ |
| 234 |
pn_raw_connection_t *c = pn_event_raw_connection(event); |
| 235 |
pn_raw_connection_close(c); |
| 236 |
} break; |
| 237 |
case PN_RAW_CONNECTION_WRITTEN: { |
| 238 |
pn_raw_connection_t *c = pn_event_raw_connection(event); |
| 239 |
pn_raw_buffer_t buffs[READ_BUFFERS]; |
| 240 |
size_t n; |
| 241 |
while ( (n = pn_raw_connection_take_written_buffers(c, buffs, READ_BUFFERS)) ) { |
| 242 |
if (!pn_raw_connection_is_read_closed(c)) { |
| 243 |
pn_raw_connection_give_read_buffers(c, buffs, n); |
| 244 |
} else { |
| 245 |
free_buffers(buffs, n); |
| 246 |
} |
| 247 |
}; |
| 248 |
} break; |
| 249 |
default: |
| 250 |
break; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
#define WRITE_BUFFERS 4 |
| 255 |
|
| 256 |
/* Handle all events, delegate to handle_send or handle_receive |
| 257 |
Return true to continue, false to exit |
| 258 |
*/ |
| 259 |
static bool handle(app_data_t* app, pn_event_t* event) { |
| 260 |
switch (pn_event_type(event)) { |
| 261 |
|
| 262 |
case PN_LISTENER_OPEN: { |
| 263 |
char port[256]; /* Get the listening port */ |
| 264 |
pn_netaddr_host_port(pn_listener_addr(pn_event_listener(event)), NULL, 0, port, sizeof(port)); |
| 265 |
printf("**listening on %s\n", port); |
| 266 |
fflush(stdout); |
| 267 |
break; |
| 268 |
} |
| 269 |
case PN_LISTENER_ACCEPT: { |
| 270 |
pn_listener_t *listener = pn_event_listener(event); |
| 271 |
pn_raw_connection_t *c = pn_raw_connection(); |
| 272 |
void *cd = make_conn_data(c); |
| 273 |
int64_t now = pn_proactor_now_64(); |
| 274 |
|
| 275 |
if (cd) { |
| 276 |
pthread_mutex_lock(&app->lock); |
| 277 |
app->first_idle_time = 0; |
| 278 |
if (app->wake_conn_time < now) { |
| 279 |
app->wake_conn_time = now + 5000; |
| 280 |
pn_proactor_set_timeout(pn_listener_proactor(listener), 5000); |
| 281 |
} |
| 282 |
pn_raw_connection_set_context(c, cd); |
| 283 |
|
| 284 |
pn_listener_raw_accept(listener, c); |
| 285 |
pthread_mutex_unlock(&app->lock); |
| 286 |
} else { |
| 287 |
printf("**too many connections, trying again later...\n"); |
| 288 |
|
| 289 |
/* No other sensible/correct way to reject connection - have to defer closing to event handler */ |
| 290 |
pn_raw_connection_set_context(c, 0); |
| 291 |
pn_listener_raw_accept(listener, c); |
| 292 |
} |
| 293 |
|
| 294 |
} break; |
| 295 |
|
| 296 |
case PN_LISTENER_CLOSE: { |
| 297 |
pn_listener_t *listener = pn_event_listener(event); |
| 298 |
app->listener = NULL; /* Listener is closed */ |
| 299 |
printf("**listener closed\n"); |
| 300 |
check_condition_fatal(event, pn_listener_condition(listener), app); |
| 301 |
} break; |
| 302 |
|
| 303 |
case PN_PROACTOR_TIMEOUT: { |
| 304 |
pn_proactor_t *proactor = pn_event_proactor(event); |
| 305 |
pthread_mutex_lock(&app->lock); |
| 306 |
int64_t now = pn_proactor_now_64(); |
| 307 |
pn_millis_t timeout = 5000; |
| 308 |
if (app->connects - app->disconnects == 0) { |
| 309 |
timeout = 20000; |
| 310 |
if (app->first_idle_time == 0) { |
| 311 |
printf("**idle detected, shutting down in %dms\n", timeout); |
| 312 |
app->first_idle_time = now; |
| 313 |
} else if (app->first_idle_time + 20000 <= now) { |
| 314 |
printf("**no activity for %dms: shutting down now\n", timeout); |
| 315 |
pn_listener_close(app->listener); |
| 316 |
break; // No more timeouts |
| 317 |
} |
| 318 |
} else if (now >= app->wake_conn_time) { |
| 319 |
int i; |
| 320 |
for (i = 0; i < MAX_CONNECTIONS; ++i) { |
| 321 |
if (conn_data[i].connection) pn_raw_connection_wake(conn_data[i].connection); |
| 322 |
} |
| 323 |
app->wake_conn_time = now + 5000; |
| 324 |
} |
| 325 |
pn_proactor_set_timeout(proactor, timeout); |
| 326 |
pthread_mutex_unlock(&app->lock); |
| 327 |
} break; |
| 328 |
|
| 329 |
case PN_PROACTOR_INACTIVE: |
| 330 |
case PN_PROACTOR_INTERRUPT: { |
| 331 |
pn_proactor_t *proactor = pn_event_proactor(event); |
| 332 |
pn_proactor_interrupt(proactor); |
| 333 |
return false; |
| 334 |
} break; |
| 335 |
|
| 336 |
default: { |
| 337 |
pn_raw_connection_t *c = pn_event_raw_connection(event); |
| 338 |
if (c) { |
| 339 |
handle_receive(app, event); |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
return exit_code == 0; |
| 344 |
} |
| 345 |
|
| 346 |
void* run(void *arg) { |
| 347 |
app_data_t *app = arg; |
| 348 |
|
| 349 |
/* Loop and handle events */ |
| 350 |
bool again = true; |
| 351 |
do { |
| 352 |
pn_event_batch_t *events = pn_proactor_wait(app->proactor); |
| 353 |
pn_event_t *e; |
| 354 |
for (e = pn_event_batch_next(events); e && again; e = pn_event_batch_next(events)) { |
| 355 |
again = handle(app, e); |
| 356 |
} |
| 357 |
pn_proactor_done(app->proactor, events); |
| 358 |
} while(again); |
| 359 |
return NULL; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
int main(int argc, char **argv) { |
| 364 |
struct app_data_t app = {0}; |
| 365 |
pthread_mutex_init(&app.lock, NULL); |
| 366 |
|
| 367 |
char addr[PN_MAX_ADDR]; |
| 368 |
app.host = (argc > 1) ? argv[1] : ""; |
| 369 |
app.port = (argc > 2) ? argv[2] : "amqp"; |
| 370 |
|
| 371 |
/* Create the proactor and connect */ |
| 372 |
app.proactor = pn_proactor(); |
| 373 |
app.listener = pn_listener(); |
| 374 |
pn_proactor_addr(addr, sizeof(addr), app.host, app.port); |
| 375 |
pn_proactor_listen(app.proactor, app.listener, addr, 16); |
| 376 |
|
| 377 |
size_t thread_count = 3; |
| 378 |
pthread_t* threads = (pthread_t*)calloc(sizeof(pthread_t), thread_count); |
| 379 |
int n; |
| 380 |
for (n=0; n<thread_count; n++) { |
| 381 |
int rc = pthread_create(&threads[n], 0, run, (void*)&app); |
| 382 |
if (rc) { |
| 383 |
fprintf(stderr, "Failed to create thread\n"); |
| 384 |
exit(-1); |
| 385 |
} |
| 386 |
} |
| 387 |
run(&app); |
| 388 |
|
| 389 |
for (n=0; n<thread_count; n++) { |
| 390 |
pthread_join(threads[n], 0); |
| 391 |
} |
| 392 |
|
| 393 |
pn_proactor_free(app.proactor); |
| 394 |
return exit_code; |
| 395 |
} |