static void pn_handshaker_dispatch(pn_handler_t *handler, pn_event_t *event, pn_event_type_t type) { switch (type) { case PN_CONNECTION_REMOTE_OPEN: { pn_connection_t *conn = pn_event_connection(event); if (pn_connection_state(conn) & PN_LOCAL_UNINIT) { pn_connection_open(conn); } } break; case PN_SESSION_REMOTE_OPEN: { pn_session_t *ssn = pn_event_session(event); if (pn_session_state(ssn) & PN_LOCAL_UNINIT) { pn_session_open(ssn); } } break; case PN_LINK_REMOTE_OPEN: { pn_link_t *link = pn_event_link(event); if (pn_link_state(link) & PN_LOCAL_UNINIT) { pn_terminus_copy(pn_link_source(link), pn_link_remote_source(link)); pn_terminus_copy(pn_link_target(link), pn_link_remote_target(link)); pn_link_open(link); } } break; case PN_CONNECTION_REMOTE_CLOSE: { pn_connection_t *conn = pn_event_connection(event); if (!(pn_connection_state(conn) & PN_LOCAL_CLOSED)) { pn_connection_close(conn); } } break; case PN_SESSION_REMOTE_CLOSE: { pn_session_t *ssn = pn_event_session(event); if (!(pn_session_state(ssn) & PN_LOCAL_CLOSED)) { pn_session_close(ssn); } } break; case PN_LINK_REMOTE_CLOSE: { pn_link_t *link = pn_event_link(event); if (!(pn_link_state(link) & PN_LOCAL_CLOSED)) { pn_link_close(link); } } break; default: break; } }
void messaging_adapter::on_link_flow(proton_event &pe) { pn_event_t *pne = pe.pn_event(); pn_link_t *lnk = pn_event_link(pne); // TODO: process session flow data, if no link-specific data, just return. if (!lnk) return; link_context& lctx = link_context::get(lnk); int state = pn_link_state(lnk); if ((state&PN_LOCAL_ACTIVE) && (state&PN_REMOTE_ACTIVE)) { if (pn_link_is_sender(lnk)) { if (pn_link_credit(lnk) > 0) { sender s(make_wrapper<sender>(lnk)); if (pn_link_get_drain(lnk)) { if (!lctx.draining) { lctx.draining = true; delegate_.on_sender_drain_start(s); } } else { lctx.draining = false; } // create on_message extended event delegate_.on_sendable(s); } } else { // receiver if (!pn_link_credit(lnk) && lctx.draining) { lctx.draining = false; receiver r(make_wrapper<receiver>(lnk)); delegate_.on_receiver_drain_finish(r); } } } credit_topup(lnk); }
void process_flow(ldp_connection_t *conn, pn_event_t *event) { fprintf(stderr, "flow event %s\n", pn_event_type_name(pn_event_type(event))); pn_link_t *sender = pn_event_link(event); pn_message_t *message = pn_message(); pn_message_set_address(message, "amqp://foo/bar"); pn_data_t *body = pn_message_body(message); char *msgtext = "hello world!"; pn_data_put_string(body, pn_bytes(strlen(msgtext), msgtext)); pn_buffer_t *buffer = pn_buffer(1000); char *encoded = pn_buffer_bytes(buffer).start; size_t size = pn_buffer_capacity(buffer); int err = pn_message_encode(message, encoded, &size); if (err) { fprintf(stderr, "trouble encoding message\n"); } else { char tag[8]; static uint64_t next_tag; *((uint64_t*)tag) = ++next_tag; pn_delivery_t *d = pn_delivery(sender, pn_dtag(tag, 8)); pn_link_send(sender, encoded, size); pn_link_advance(sender); } pn_buffer_free(buffer); pn_message_free(message); }
void replyto_dispatch(pn_handler_t *h, pn_event_t *event, pn_event_type_t type) { sender_context_t *sc = replyto_sender_context(h); switch (type) { case PN_DELIVERY: { check(sc->opts->get_replies, "Unexpected reply message"); pn_link_t *recv_link = pn_event_link(event); pn_delivery_t *dlv = pn_event_delivery(event); if (pn_link_is_receiver(recv_link) && !pn_delivery_partial(dlv)) { size_t encoded_size = pn_delivery_pending(dlv); check(encoded_size <= sc->encoded_data_size, "decoding buffer too small"); ssize_t n = pn_link_recv(recv_link, sc->encoded_data, encoded_size); check(n == (ssize_t)encoded_size, "read fail on reply link"); pn_message_t *msg = sc->reply_message; int err = pn_message_decode(msg, sc->encoded_data, n); check(err == 0, "message decode error"); statistics_msg_received(sc->stats, msg); sc->received++; pn_delivery_settle(dlv); } if (sc->received == sc->opts->msg_count) { pn_link_close(recv_link); pn_connection_t *conn = pn_event_connection(event); pn_connection_close(conn); } } break; default: break; } }
void messaging_adapter::on_delivery(event &e) { proton_event *pe = dynamic_cast<proton_event*>(&e); if (pe) { pn_event_t *cevent = pe->pn_event(); pn_link_t *lnk = pn_event_link(cevent); delivery dlv = pe->delivery(); if (pn_link_is_receiver(lnk)) { if (!dlv.partial() && dlv.readable()) { // generate on_message messaging_event mevent(messaging_event::MESSAGE, *pe); pn_connection_t *pnc = pn_session_connection(pn_link_session(lnk)); connection_context& ctx = connection_context::get(pnc); // Reusable per-connection message. // Avoid expensive heap malloc/free overhead. // See PROTON-998 class message &msg(ctx.event_message); mevent.message_ = &msg; mevent.message_->decode(lnk, dlv); if (pn_link_state(lnk) & PN_LOCAL_CLOSED) { if (auto_accept_) dlv.release(); } else { delegate_.on_message(mevent); if (auto_accept_ && !dlv.settled()) dlv.accept(); } } else if (dlv.updated() && dlv.settled()) { messaging_event mevent(messaging_event::DELIVERY_SETTLE, *pe); delegate_.on_delivery_settle(mevent); } } else { // sender if (dlv.updated()) { amqp_ulong rstate = dlv.remote_state(); if (rstate == PN_ACCEPTED) { messaging_event mevent(messaging_event::DELIVERY_ACCEPT, *pe); delegate_.on_delivery_accept(mevent); } else if (rstate == PN_REJECTED) { messaging_event mevent(messaging_event::DELIVERY_REJECT, *pe); delegate_.on_delivery_reject(mevent); } else if (rstate == PN_RELEASED || rstate == PN_MODIFIED) { messaging_event mevent(messaging_event::DELIVERY_RELEASE, *pe); delegate_.on_delivery_release(mevent); } if (dlv.settled()) { messaging_event mevent(messaging_event::DELIVERY_SETTLE, *pe); delegate_.on_delivery_settle(mevent); } if (auto_settle_) dlv.settle(); } } } }
void messaging_adapter::on_link_error(event &e) { delegate_.on_link_error(e); proton_event *pe = dynamic_cast<proton_event*>(&e); if (pe) { pn_link_t *link = pn_event_link(pe->pn_event()); pn_link_close(link); } }
/* Common handler for simple client/server interactions, */ static pn_event_type_t common_handler(test_handler_t *th, pn_event_t *e) { pn_connection_t *c = pn_event_connection(e); pn_listener_t *l = pn_event_listener(e); switch (pn_event_type(e)) { /* Stop on these events */ case PN_TRANSPORT_CLOSED: case PN_PROACTOR_INACTIVE: case PN_PROACTOR_TIMEOUT: case PN_LISTENER_OPEN: return pn_event_type(e); case PN_LISTENER_ACCEPT: last_accepted = pn_connection(); pn_listener_accept2(l, last_accepted, NULL); pn_listener_close(l); /* Only accept one connection */ return PN_EVENT_NONE; case PN_CONNECTION_REMOTE_OPEN: pn_connection_open(c); /* Return the open (no-op if already open) */ return PN_EVENT_NONE; case PN_SESSION_REMOTE_OPEN: pn_session_open(pn_event_session(e)); return PN_EVENT_NONE; case PN_LINK_REMOTE_OPEN: pn_link_open(pn_event_link(e)); return PN_EVENT_NONE; case PN_CONNECTION_REMOTE_CLOSE: pn_connection_close(c); /* Return the close */ return PN_EVENT_NONE; /* Ignore these events */ case PN_CONNECTION_BOUND: case PN_CONNECTION_INIT: case PN_CONNECTION_LOCAL_CLOSE: case PN_CONNECTION_LOCAL_OPEN: case PN_LINK_INIT: case PN_LINK_LOCAL_OPEN: case PN_LISTENER_CLOSE: case PN_SESSION_INIT: case PN_SESSION_LOCAL_OPEN: case PN_TRANSPORT: case PN_TRANSPORT_ERROR: case PN_TRANSPORT_HEAD_CLOSED: case PN_TRANSPORT_TAIL_CLOSED: return PN_EVENT_NONE; default: TEST_ERRORF(th->t, "unexpected event %s", pn_event_type_name(pn_event_type(e))); return PN_EVENT_NONE; /* Fail the test but keep going */ } }
void messaging_adapter::on_link_local_open(event &e) { proton_event *pe = dynamic_cast<proton_event*>(&e); if (pe) { pn_link_t *link = pn_event_link(pe->pn_event()); if (is_remote_open(pn_link_state(link))) { messaging_event mevent(messaging_event::LINK_OPENED, *pe); on_link_opened(mevent); } } }
void messaging_adapter::on_link_remote_open(event &e) { proton_event *pe = dynamic_cast<proton_event*>(&e); if (pe) { messaging_event mevent(messaging_event::LINK_OPEN, *pe); on_link_open(mevent); pn_link_t *link = pn_event_link(pe->pn_event()); if (!is_local_open(pn_link_state(link)) && is_local_unititialised(pn_link_state(link))) { pn_link_open(link); } } }
void messaging_adapter::on_link_flow(event &e) { proton_event *pe = dynamic_cast<proton_event*>(&e); if (pe) { pn_event_t *pne = pe->pn_event(); pn_link_t *lnk = pn_event_link(pne); if (lnk && pn_link_is_sender(lnk) && pn_link_credit(lnk) > 0) { // create on_message extended event messaging_event mevent(messaging_event::SENDABLE, *pe); delegate_.on_sendable(mevent);; } } }
void messaging_adapter::on_link_remote_close(event &e) { proton_event *pe = dynamic_cast<proton_event*>(&e); if (pe) { pn_event_t *cevent = pe->pn_event(); pn_link_t *lnk = pn_event_link(cevent); if (pn_condition_is_set(pn_link_remote_condition(lnk))) { messaging_event mevent(messaging_event::LINK_ERROR, *pe); on_link_error(mevent); } else { messaging_event mevent(messaging_event::LINK_CLOSE, *pe); on_link_close(mevent); } pn_link_close(lnk); } }
static pn_event_type_t message_stream_handler(test_handler_t *th, pn_event_t *e) { struct message_stream_context *ctx = (struct message_stream_context*)th->context; switch (pn_event_type(e)) { case PN_CONNECTION_BOUND: pn_transport_set_max_frame(pn_event_transport(e), FRAME); return PN_EVENT_NONE; case PN_SESSION_INIT: pn_session_set_incoming_capacity(pn_event_session(e), FRAME); /* Single frame incoming */ pn_session_set_outgoing_window(pn_event_session(e), 1); /* Single frame outgoing */ return PN_EVENT_NONE; case PN_LINK_REMOTE_OPEN: common_handler(th, e); if (pn_link_is_receiver(pn_event_link(e))) { pn_link_flow(pn_event_link(e), 1); } else { ctx->sender = pn_event_link(e); } return PN_EVENT_NONE; case PN_LINK_FLOW: /* Start a delivery */ if (pn_link_is_sender(pn_event_link(e)) && !ctx->dlv) { ctx->dlv = pn_delivery(pn_event_link(e), pn_dtag("x", 1)); } return PN_LINK_FLOW; case PN_CONNECTION_WAKE: { /* Send a chunk */ ssize_t remains = ctx->size - ctx->sent; ssize_t n = (CHUNK < remains) ? CHUNK : remains; TEST_CHECK(th->t, n == pn_link_send(ctx->sender, ctx->send_buf.start + ctx->sent, n)); ctx->sent += n; if (ctx->sent == ctx->size) { TEST_CHECK(th->t, pn_link_advance(ctx->sender)); } return PN_CONNECTION_WAKE; } case PN_DELIVERY: { /* Receive a delivery - smaller than a chunk? */ pn_delivery_t *dlv = pn_event_delivery(e); if (pn_delivery_readable(dlv)) { ssize_t n = pn_delivery_pending(dlv); rwbytes_ensure(&ctx->recv_buf, ctx->received + n); TEST_ASSERT(n == pn_link_recv(pn_event_link(e), ctx->recv_buf.start + ctx->received, n)); ctx->received += n; } ctx->complete = !pn_delivery_partial(dlv); return PN_DELIVERY; } default: return common_handler(th, e); } }
void messaging_adapter::on_link_remote_open(proton_event &pe) { pn_link_t *lnk = pn_event_link(pe.pn_event()); container& c = pe.container(); if (pn_link_is_receiver(lnk)) { receiver r(make_wrapper<receiver>(lnk)); delegate_.on_receiver_open(r); if (is_local_unititialised(pn_link_state(lnk))) { r.open(c.receiver_options()); } } else { sender s(make_wrapper<sender>(lnk)); delegate_.on_sender_open(s); if (is_local_unititialised(pn_link_state(lnk))) { s.open(c.sender_options()); } } credit_topup(lnk); }
void messaging_adapter::on_link_remote_close(proton_event &pe) { pn_event_t *cevent = pe.pn_event(); pn_link_t *lnk = pn_event_link(cevent); if (pn_link_is_receiver(lnk)) { receiver r(make_wrapper<receiver>(lnk)); if (pn_condition_is_set(pn_link_remote_condition(lnk))) { delegate_.on_receiver_error(r); } delegate_.on_receiver_close(r); } else { sender s(make_wrapper<sender>(lnk)); if (pn_condition_is_set(pn_link_remote_condition(lnk))) { delegate_.on_sender_error(s); } delegate_.on_sender_close(s); } pn_link_close(lnk); }
void messaging_adapter::on_link_local_open(proton_event &pe) { credit_topup(pn_event_link(pe.pn_event())); }
void messaging_adapter::on_delivery(proton_event &pe) { pn_event_t *cevent = pe.pn_event(); pn_link_t *lnk = pn_event_link(cevent); pn_delivery_t *dlv = pn_event_delivery(cevent); link_context& lctx = link_context::get(lnk); if (pn_link_is_receiver(lnk)) { delivery d(make_wrapper<delivery>(dlv)); if (!pn_delivery_partial(dlv) && pn_delivery_readable(dlv)) { // generate on_message pn_connection_t *pnc = pn_session_connection(pn_link_session(lnk)); connection_context& ctx = connection_context::get(pnc); // Reusable per-connection message. // Avoid expensive heap malloc/free overhead. // See PROTON-998 class message &msg(ctx.event_message); msg.decode(d); if (pn_link_state(lnk) & PN_LOCAL_CLOSED) { if (lctx.auto_accept) d.release(); } else { delegate_.on_message(d, msg); if (lctx.auto_accept && !d.settled()) d.accept(); if (lctx.draining && !pn_link_credit(lnk)) { lctx.draining = false; receiver r(make_wrapper<receiver>(lnk)); delegate_.on_receiver_drain_finish(r); } } } else if (pn_delivery_updated(dlv) && d.settled()) { delegate_.on_delivery_settle(d); } if (lctx.draining && pn_link_credit(lnk) == 0) { lctx.draining = false; pn_link_set_drain(lnk, false); receiver r(make_wrapper<receiver>(lnk)); delegate_.on_receiver_drain_finish(r); if (lctx.pending_credit) { pn_link_flow(lnk, lctx.pending_credit); lctx.pending_credit = 0; } } credit_topup(lnk); } else { tracker t(make_wrapper<tracker>(dlv)); // sender if (pn_delivery_updated(dlv)) { uint64_t rstate = pn_delivery_remote_state(dlv); if (rstate == PN_ACCEPTED) { delegate_.on_tracker_accept(t); } else if (rstate == PN_REJECTED) { delegate_.on_tracker_reject(t); } else if (rstate == PN_RELEASED || rstate == PN_MODIFIED) { delegate_.on_tracker_release(t); } if (t.settled()) { delegate_.on_tracker_settle(t); } if (lctx.auto_settle) t.settle(); } } }
void connection_dispatch(pn_handler_t *h, pn_event_t *event, pn_event_type_t type) { connection_context_t *cc = connection_context(h); bool replying = cc->global->opts->reply; switch (type) { case PN_LINK_REMOTE_OPEN: { pn_link_t *link = pn_event_link(event); if (pn_link_is_receiver(link)) { check(cc->recv_link == NULL, "Multiple incomming links on one connection"); cc->recv_link = link; pn_connection_t *conn = pn_event_connection(event); pn_list_add(cc->global->active_connections, conn); if (cc->global->shutting_down) { pn_connection_close(conn); break; } if (replying) { // Set up a reply link and defer granting credit to the incoming link pn_connection_t *conn = pn_session_connection(pn_link_session(link)); pn_session_t *ssn = pn_session(conn); pn_session_open(ssn); char name[100]; // prefer a multiplatform uuid generator sprintf(name, "reply_sender_%d", cc->connection_id); cc->reply_link = pn_sender(ssn, name); pn_link_open(cc->reply_link); } else { pn_flowcontroller_t *fc = pn_flowcontroller(1024); pn_handler_add(h, fc); pn_decref(fc); } } } break; case PN_LINK_FLOW: { if (replying) { pn_link_t *reply_link = pn_event_link(event); // pn_flowcontroller handles the non-reply case check(reply_link == cc->reply_link, "internal error"); // Grant the sender as much credit as just given to us for replies int delta = pn_link_credit(reply_link) - pn_link_credit(cc->recv_link); if (delta > 0) pn_link_flow(cc->recv_link, delta); } } break; case PN_DELIVERY: { pn_link_t *recv_link = pn_event_link(event); pn_delivery_t *dlv = pn_event_delivery(event); if (pn_link_is_receiver(recv_link) && !pn_delivery_partial(dlv)) { if (cc->global->received == 0) statistics_start(cc->global->stats); size_t encoded_size = pn_delivery_pending(dlv); cc->global->encoded_data = ensure_buffer(cc->global->encoded_data, encoded_size, &cc->global->encoded_data_size); check(cc->global->encoded_data, "decoding buffer realloc failure"); ssize_t n = pn_link_recv(recv_link, cc->global->encoded_data, encoded_size); check(n == (ssize_t) encoded_size, "message data read fail"); pn_message_t *msg = cc->global->message; int err = pn_message_decode(msg, cc->global->encoded_data, n); check(err == 0, "message decode error"); cc->global->received++; pn_delivery_settle(dlv); statistics_msg_received(cc->global->stats, msg); if (replying) { const char *reply_addr = pn_message_get_reply_to(msg); if (reply_addr) { pn_link_t *rl = cc->reply_link; check(pn_link_credit(rl) > 0, "message received without corresponding reply credit"); LOG("Replying to: %s\n", reply_addr ); pn_message_set_address(msg, reply_addr); pn_message_set_creation_time(msg, msgr_now()); char tag[8]; void *ptr = &tag; *((uint64_t *) ptr) = cc->global->sent; pn_delivery_t *dlv = pn_delivery(rl, pn_dtag(tag, 8)); size_t size = cc->global->encoded_data_size; int err = pn_message_encode(msg, cc->global->encoded_data, &size); check(err == 0, "message encoding error"); pn_link_send(rl, cc->global->encoded_data, size); pn_delivery_settle(dlv); cc->global->sent++; } } } if (cc->global->received >= cc->global->opts->msg_count) { global_shutdown(cc->global); } } break; case PN_CONNECTION_UNBOUND: { pn_connection_t *conn = pn_event_connection(event); pn_list_remove(cc->global->active_connections, conn); pn_connection_release(conn); } break; default: break; } }
int pn_event_handler(void *handler_context, void *conn_context, pn_event_t *event, qd_connection_t *qd_conn) { qd_container_t *container = (qd_container_t*) handler_context; pn_connection_t *conn = qd_connection_pn(qd_conn); pn_session_t *ssn; pn_link_t *pn_link; qd_link_t *qd_link; pn_delivery_t *delivery; switch (pn_event_type(event)) { case PN_CONNECTION_REMOTE_OPEN : qd_connection_set_user(qd_conn); if (pn_connection_state(conn) & PN_LOCAL_UNINIT) { // This Open is an externally initiated connection // Let policy engine decide qd_connection_set_event_stall(qd_conn, true); qd_conn->open_container = (void *)container; qd_connection_invoke_deferred(qd_conn, qd_policy_amqp_open, qd_conn); } else { // This Open is in response to an internally initiated connection notify_opened(container, qd_conn, conn_context); } break; case PN_CONNECTION_REMOTE_CLOSE : if (pn_connection_state(conn) == (PN_LOCAL_ACTIVE | PN_REMOTE_CLOSED)) pn_connection_close(conn); break; case PN_SESSION_REMOTE_OPEN : if (!(pn_connection_state(conn) & PN_LOCAL_CLOSED)) { ssn = pn_event_session(event); if (pn_session_state(ssn) & PN_LOCAL_UNINIT) { if (qd_conn->policy_settings) { if (!qd_policy_approve_amqp_session(ssn, qd_conn)) { break; } qd_conn->n_sessions++; } qd_policy_apply_session_settings(ssn, qd_conn); pn_session_open(ssn); } } break; case PN_SESSION_REMOTE_CLOSE : if (!(pn_connection_state(conn) & PN_LOCAL_CLOSED)) { ssn = pn_event_session(event); if (pn_session_state(ssn) == (PN_LOCAL_ACTIVE | PN_REMOTE_CLOSED)) { // remote has nuked our session. Check for any links that were // left open and forcibly detach them, since no detaches will // arrive on this session. pn_connection_t *conn = pn_session_connection(ssn); pn_link_t *pn_link = pn_link_head(conn, PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE); while (pn_link) { if (pn_link_session(pn_link) == ssn) { qd_link_t *qd_link = (qd_link_t*) pn_link_get_context(pn_link); if (qd_link && qd_link->node) { if (qd_conn->policy_settings) { if (qd_link->direction == QD_OUTGOING) { qd_conn->n_receivers--; assert(qd_conn->n_receivers >= 0); } else { qd_conn->n_senders--; assert(qd_conn->n_senders >= 0); } } qd_log(container->log_source, QD_LOG_NOTICE, "Aborting link '%s' due to parent session end", pn_link_name(pn_link)); qd_link->node->ntype->link_detach_handler(qd_link->node->context, qd_link, QD_LOST); } } pn_link = pn_link_next(pn_link, PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE); } if (qd_conn->policy_settings) { qd_conn->n_sessions--; } pn_session_close(ssn); } } break; case PN_LINK_REMOTE_OPEN : if (!(pn_connection_state(conn) & PN_LOCAL_CLOSED)) { pn_link = pn_event_link(event); if (pn_link_state(pn_link) & PN_LOCAL_UNINIT) { if (pn_link_is_sender(pn_link)) { if (qd_conn->policy_settings) { if (!qd_policy_approve_amqp_receiver_link(pn_link, qd_conn)) { break; } qd_conn->n_receivers++; } setup_outgoing_link(container, pn_link); } else { if (qd_conn->policy_settings) { if (!qd_policy_approve_amqp_sender_link(pn_link, qd_conn)) { break; } qd_conn->n_senders++; } setup_incoming_link(container, pn_link); } } else if (pn_link_state(pn_link) & PN_LOCAL_ACTIVE) handle_link_open(container, pn_link); } break; case PN_LINK_REMOTE_CLOSE : case PN_LINK_REMOTE_DETACH : if (!(pn_connection_state(conn) & PN_LOCAL_CLOSED)) { pn_link = pn_event_link(event); qd_link = (qd_link_t*) pn_link_get_context(pn_link); if (qd_link) { qd_node_t *node = qd_link->node; qd_detach_type_t dt = pn_event_type(event) == PN_LINK_REMOTE_CLOSE ? QD_CLOSED : QD_DETACHED; if (node) node->ntype->link_detach_handler(node->context, qd_link, dt); else if (qd_link->pn_link == pn_link) { pn_link_close(pn_link); } if (qd_conn->policy_counted && qd_conn->policy_settings) { if (pn_link_is_sender(pn_link)) { qd_conn->n_receivers--; qd_log(container->log_source, QD_LOG_TRACE, "Closed receiver link %s. n_receivers: %d", pn_link_name(pn_link), qd_conn->n_receivers); assert (qd_conn->n_receivers >= 0); } else { qd_conn->n_senders--; qd_log(container->log_source, QD_LOG_TRACE, "Closed sender link %s. n_senders: %d", pn_link_name(pn_link), qd_conn->n_senders); assert (qd_conn->n_senders >= 0); } } if (qd_link->close_sess_with_link && qd_link->pn_sess && pn_link_state(pn_link) == (PN_LOCAL_CLOSED | PN_REMOTE_CLOSED)) pn_session_close(qd_link->pn_sess); } } break; case PN_LINK_FLOW : pn_link = pn_event_link(event); qd_link = (qd_link_t*) pn_link_get_context(pn_link); if (qd_link && qd_link->node && qd_link->node->ntype->link_flow_handler) qd_link->node->ntype->link_flow_handler(qd_link->node->context, qd_link); break; case PN_DELIVERY : delivery = pn_event_delivery(event); if (pn_delivery_readable(delivery)) do_receive(delivery); if (pn_delivery_updated(delivery)) { do_updated(delivery); pn_delivery_clear(delivery); } break; case PN_EVENT_NONE : case PN_REACTOR_INIT : case PN_REACTOR_QUIESCED : case PN_REACTOR_FINAL : case PN_TIMER_TASK : case PN_CONNECTION_INIT : case PN_CONNECTION_BOUND : case PN_CONNECTION_UNBOUND : case PN_CONNECTION_LOCAL_OPEN : case PN_CONNECTION_LOCAL_CLOSE : case PN_CONNECTION_FINAL : case PN_SESSION_INIT : case PN_SESSION_LOCAL_OPEN : case PN_SESSION_LOCAL_CLOSE : case PN_SESSION_FINAL : case PN_LINK_INIT : case PN_LINK_LOCAL_OPEN : case PN_LINK_LOCAL_CLOSE : case PN_LINK_LOCAL_DETACH : case PN_LINK_FINAL : case PN_TRANSPORT : case PN_TRANSPORT_ERROR : case PN_TRANSPORT_HEAD_CLOSED : case PN_TRANSPORT_TAIL_CLOSED : case PN_TRANSPORT_CLOSED : case PN_TRANSPORT_AUTHENTICATED : case PN_SELECTABLE_INIT : case PN_SELECTABLE_UPDATED : case PN_SELECTABLE_READABLE : case PN_SELECTABLE_WRITABLE : case PN_SELECTABLE_ERROR : case PN_SELECTABLE_EXPIRED : case PN_SELECTABLE_FINAL : break; } return 1; }
void sender_dispatch(pn_handler_t *h, pn_event_t *event, pn_event_type_t type) { sender_context_t *sc = sender_context(h); switch (type) { case PN_CONNECTION_INIT: { pn_connection_t *conn = pn_event_connection(event); pn_connection_set_container(conn, pn_string_get(sc->container_id)); pn_connection_set_hostname(conn, pn_string_get(sc->hostname)); pn_connection_open(conn); pn_session_t *ssn = pn_session(conn); pn_session_open(ssn); pn_link_t *snd = pn_sender(ssn, "sender"); const char *path = pn_url_get_path(sc->send_url); if (path && strlen(path)) { pn_terminus_set_address(pn_link_target(snd), path); pn_terminus_set_address(pn_link_source(snd), path); } pn_link_open(snd); } break; case PN_LINK_FLOW: { pn_link_t *snd = pn_event_link(event); while (pn_link_credit(snd) > 0 && sc->sent < sc->opts->msg_count) { if (sc->sent == 0) statistics_start(sc->stats); char tag[8]; void *ptr = &tag; *((uint64_t *) ptr) = sc->sent; pn_delivery_t *dlv = pn_delivery(snd, pn_dtag(tag, 8)); // setup the message to send pn_message_t *msg = sc->message; pn_message_set_address(msg, sc->opts->targets.addresses[0]); sc->id.u.as_ulong = sc->sent; pn_message_set_correlation_id(msg, sc->id); pn_message_set_creation_time(msg, msgr_now()); size_t size = sc->encoded_data_size; int err = pn_message_encode(msg, sc->encoded_data, &size); check(err == 0, "message encoding error"); pn_link_send(snd, sc->encoded_data, size); pn_delivery_settle(dlv); sc->sent++; } if (sc->sent == sc->opts->msg_count && !sc->opts->get_replies) { pn_link_close(snd); pn_connection_t *conn = pn_event_connection(event); pn_connection_close(conn); } } break; case PN_LINK_INIT: { pn_link_t *link = pn_event_link(event); if (pn_link_is_receiver(link)) { // Response messages link. Could manage credit and deliveries in this handler but // a dedicated handler also works. pn_handler_t *replyto = replyto_handler(sc); pn_flowcontroller_t *fc = pn_flowcontroller(1024); pn_handler_add(replyto, fc); pn_decref(fc); pn_handshaker_t *handshaker = pn_handshaker(); pn_handler_add(replyto, handshaker); pn_decref(handshaker); pn_record_t *record = pn_link_attachments(link); pn_record_set_handler(record, replyto); pn_decref(replyto); } } break; case PN_CONNECTION_LOCAL_CLOSE: { statistics_report(sc->stats, sc->sent, sc->received); } break; default: break; } }
void connection_dispatch ( pn_handler_t *h, pn_event_t *event, pn_event_type_t type ) { connection_context_t *cc = connection_context(h); switch ( type ) { case PN_LINK_REMOTE_OPEN: { pn_link_t *link = pn_event_link(event); if (pn_link_is_receiver(link)) { check(cc->recv_link == NULL, "Multiple incomming links on one connection"); cc->recv_link = link; pn_connection_t *conn = pn_event_connection(event); pn_list_add(cc->global->active_connections, conn); if (cc->global->shutting_down) { pn_connection_close(conn); break; } pn_flowcontroller_t *fc = pn_flowcontroller(1024); pn_handler_add(h, fc); pn_decref(fc); } } break; case PN_DELIVERY: { pn_link_t *recv_link = pn_event_link(event); pn_delivery_t *dlv = pn_event_delivery(event); if (pn_link_is_receiver(recv_link) && !pn_delivery_partial(dlv)) { if (cc->global->received == 0) statistics_start(cc->global->stats); size_t encoded_size = pn_delivery_pending(dlv); cc->global->encoded_data = ensure_buffer(cc->global->encoded_data, encoded_size, &cc->global->encoded_data_size); check(cc->global->encoded_data, "decoding buffer realloc failure"); /* If this was the first message received, initialize our reporting. */ if ( ! cc->global->received ) rr_init ( & cc->global->resource_reporter ); ssize_t n = pn_link_recv(recv_link, cc->global->encoded_data, encoded_size); check(n == (ssize_t) encoded_size, "message data read fail"); //fprintf ( stderr, "MDEBUG encoded_size == %d\n", encoded_size ); pn_message_t *msg = cc->global->message; int err = pn_message_decode ( msg, cc->global->encoded_data, n ); check ( err == 0, "message decode error" ); /* MICK -- annotate! ================================ */ if ( cc->global->opts->timestamping ) { double message_timestamp; if ( get_message_timestamp ( msg, & message_timestamp ) ) { double now = now_timestamp ( ); cc->global->total_latency += (now - message_timestamp); } else { fprintf ( stderr, "receiver: no timestamp at msg count %d.\n", cc->global->received ); exit ( 1 ); } } /* MICK -- end annotate! ============================= */ cc->global->received++; /*--------------------------------------- Do a report ---------------------------------------*/ if ( ! ( cc->global->received % cc->global->opts->report_frequency ) ) { static bool first_time = true; double cpu_percentage; int rss; double sslr = rr_seconds_since_last_report ( & cc->global->resource_reporter ); rr_report ( & cc->global->resource_reporter, & cpu_percentage, & rss ); double throughput = (double)(cc->global->opts->report_frequency) / sslr; if ( first_time ) { if ( cc->global->opts->timestamping ) { if ( cc->global->opts->print_message_size ) fprintf(cc->global->report_fp, "msg_size\trecv_msgs\tcpu\trss\tthroughput\tlatency\n"); else fprintf(cc->global->report_fp, "recv_msgs\tcpu\trss\tthroughput\tlatency\n"); } else { if ( cc->global->opts->print_message_size ) fprintf(cc->global->report_fp, "msg_size\trecv_msgs\tcpu\trss\tthroughput\n"); else fprintf(cc->global->report_fp, "recv_msgs\tcpu\trss\tthroughput\n"); } first_time = false; } if ( cc->global->opts->timestamping ) { double average_latency = cc->global->total_latency / cc->global->opts->report_frequency; average_latency *= 1000.0; // in msec. cc->global->total_latency = 0; fprintf ( cc->global->report_fp, "%d\t%lf\t%d\t%lf\t%lf\n", cc->global->received, cpu_percentage, rss, throughput, average_latency ); } else { // was: // "recv_msgs: %10d cpu: %5.1lf rss: %6d throughput: %8.0lf\n" if ( cc->global->opts->print_message_size ) { fprintf ( cc->global->report_fp, "%d\t%d\t%lf\t%d\t%lf\n", cc->global->opts->message_size, cc->global->received, cpu_percentage, rss, throughput ); } else { fprintf ( cc->global->report_fp, "%d\t%lf\t%d\t%lf\n", cc->global->received, cpu_percentage, rss, throughput ); } } } pn_delivery_settle(dlv); // move this up statistics_msg_received(cc->global->stats, msg); } if (cc->global->received >= cc->global->opts->msg_count) { global_shutdown(cc->global); } } break; case PN_CONNECTION_UNBOUND: { pn_connection_t *conn = pn_event_connection(event); pn_list_remove(cc->global->active_connections, conn); pn_connection_release(conn); } break; default: break; } }
/* Process each event posted by the reactor. */ static void event_handler(pn_handler_t *handler, pn_event_t *event, pn_event_type_t type) { app_data_t *data = GET_APP_DATA(handler); switch (type) { case PN_CONNECTION_INIT: { // Create and open all the endpoints needed to send a message // pn_connection_t *conn; pn_session_t *ssn; pn_link_t *sender; conn = pn_event_connection(event); pn_connection_open(conn); ssn = pn_session(conn); pn_session_open(ssn); sender = pn_sender(ssn, "MySender"); // we do not wait for ack until the last message pn_link_set_snd_settle_mode(sender, PN_SND_MIXED); if (!data->anon) { pn_terminus_set_address(pn_link_target(sender), data->target); } pn_link_open(sender); } break; case PN_LINK_FLOW: { // the remote has given us some credit, now we can send messages // static long tag = 0; // a simple tag generator pn_delivery_t *delivery; pn_link_t *sender = pn_event_link(event); int credit = pn_link_credit(sender); while (credit > 0 && data->count > 0) { --credit; --data->count; ++tag; delivery = pn_delivery(sender, pn_dtag((const char *)&tag, sizeof(tag))); pn_link_send(sender, data->msg_data, data->msg_len); pn_link_advance(sender); if (data->count > 0) { // send pre-settled until the last one, then wait for an ack on // the last sent message. This allows the sender to send // messages as fast as possible and then exit when the consumer // has dealt with the last one. // pn_delivery_settle(delivery); } } } break; case PN_DELIVERY: { // Since the example sends all messages but the last pre-settled // (pre-acked), only the last message's delivery will get updated with // the remote state (acked/nacked). // pn_delivery_t *dlv = pn_event_delivery(event); if (pn_delivery_updated(dlv) && pn_delivery_remote_state(dlv)) { uint64_t rs = pn_delivery_remote_state(dlv); int done = 1; switch (rs) { case PN_RECEIVED: // This is not a terminal state - it is informational, and the // peer is still processing the message. done = 0; break; case PN_ACCEPTED: pn_delivery_settle(dlv); if (!quiet) fprintf(stdout, "Send complete!\n"); break; case PN_REJECTED: case PN_RELEASED: case PN_MODIFIED: pn_delivery_settle(dlv); fprintf(stderr, "Message not accepted - code:%lu\n", (unsigned long)rs); break; default: // ??? no other terminal states defined, so ignore anything else pn_delivery_settle(dlv); fprintf(stderr, "Unknown delivery failure - code=%lu\n", (unsigned long)rs); break; } if (done) { // initiate clean shutdown of the endpoints pn_link_t *link = pn_delivery_link(dlv); pn_session_t *ssn = pn_link_session(link); pn_link_close(link); pn_session_close(ssn); pn_connection_close(pn_session_connection(ssn)); } } } break; case PN_TRANSPORT_ERROR: { // The connection to the peer failed. // pn_transport_t *tport = pn_event_transport(event); pn_condition_t *cond = pn_transport_condition(tport); fprintf(stderr, "Network transport failed!\n"); if (pn_condition_is_set(cond)) { const char *name = pn_condition_get_name(cond); const char *desc = pn_condition_get_description(cond); fprintf(stderr, " Error: %s Description: %s\n", (name) ? name : "<error name not provided>", (desc) ? desc : "<no description provided>"); } // pn_reactor_process() will exit with a false return value, stopping // the main loop. } break; default: break; } }
link proton_event::link() const { class link lnk = pn_event_link(pn_event()); if (!lnk) throw error(MSG("No link context for this event")); return lnk; }