Returns true to continue, false if finished */
| 75 | |
| 76 | /* Returns true to continue, false if finished */ |
| 77 | static bool handle(app_data_t* app, pn_event_t* event) { |
| 78 | switch (pn_event_type(event)) { |
| 79 | |
| 80 | case PN_CONNECTION_INIT: { |
| 81 | pn_connection_t* c = pn_event_connection(event); |
| 82 | pn_session_t* s = pn_session(pn_event_connection(event)); |
| 83 | pn_connection_set_container(c, app->container_id); |
| 84 | pn_connection_open(c); |
| 85 | pn_session_open(s); |
| 86 | { |
| 87 | pn_link_t* l = pn_sender(s, "my_sender"); |
| 88 | pn_terminus_set_address(pn_link_target(l), app->amqp_address); |
| 89 | pn_link_open(l); |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | case PN_LINK_FLOW: { |
| 95 | /* The peer has given us some credit, now we can send messages */ |
| 96 | pn_link_t *sender = pn_event_link(event); |
| 97 | while (pn_link_credit(sender) > 0 && app->sent < app->message_count) { |
| 98 | ++app->sent; |
| 99 | /* Use sent counter as unique delivery tag. */ |
| 100 | pn_delivery(sender, pn_dtag((const char *)&app->sent, sizeof(app->sent))); |
| 101 | send_message(app, sender); |
| 102 | } |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | case PN_DELIVERY: { |
| 107 | /* We received acknowledgement from the peer that a message was delivered. */ |
| 108 | pn_delivery_t* d = pn_event_delivery(event); |
| 109 | if (pn_delivery_remote_state(d) == PN_ACCEPTED) { |
| 110 | pn_delivery_settle(d); |
| 111 | if (++app->acknowledged == app->message_count) { |
| 112 | printf("%d messages sent and acknowledged\n", app->acknowledged); |
| 113 | pn_connection_close(pn_event_connection(event)); |
| 114 | /* Continue handling events till we receive TRANSPORT_CLOSED */ |
| 115 | } |
| 116 | } else { |
| 117 | fprintf(stderr, "unexpected delivery state %d\n", (int)pn_delivery_remote_state(d)); |
| 118 | pn_connection_close(pn_event_connection(event)); |
| 119 | exit_code=1; |
| 120 | } |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | case PN_TRANSPORT_CLOSED: |
| 125 | check_condition(event, pn_transport_condition(pn_event_transport(event))); |
| 126 | break; |
| 127 | |
| 128 | case PN_CONNECTION_REMOTE_CLOSE: |
| 129 | check_condition(event, pn_connection_remote_condition(pn_event_connection(event))); |
| 130 | pn_connection_close(pn_event_connection(event)); |
| 131 | break; |
| 132 | |
| 133 | case PN_SESSION_REMOTE_CLOSE: |
| 134 | check_condition(event, pn_session_remote_condition(pn_event_session(event))); |
no test coverage detected