| 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 "messaging_adapter.hpp" |
| 23 |
|
| 24 |
#include "proton/connection.hpp" |
| 25 |
#include "proton/container.hpp" |
| 26 |
#include "proton/delivery.hpp" |
| 27 |
#include "proton/error.hpp" |
| 28 |
#include "proton/messaging_handler.hpp" |
| 29 |
#include "proton/receiver.hpp" |
| 30 |
#include "proton/receiver_options.hpp" |
| 31 |
#include "proton/sender.hpp" |
| 32 |
#include "proton/sender_options.hpp" |
| 33 |
#include "proton/session.hpp" |
| 34 |
#include "proton/tracker.hpp" |
| 35 |
#include "proton/transport.hpp" |
| 36 |
|
| 37 |
#include "contexts.hpp" |
| 38 |
#include "msg.hpp" |
| 39 |
#include "proton_bits.hpp" |
| 40 |
|
| 41 |
#include <proton/connection.h> |
| 42 |
#include <proton/delivery.h> |
| 43 |
#include <proton/handlers.h> |
| 44 |
#include <proton/link.h> |
| 45 |
#include <proton/message.h> |
| 46 |
#include <proton/session.h> |
| 47 |
#include <proton/transport.h> |
| 48 |
|
| 49 |
#include <assert.h> |
| 50 |
|
| 51 |
namespace proton { |
| 52 |
|
| 53 |
namespace { |
| 54 |
// This must only be called for receiver links |
| 55 |
void credit_topup(pn_link_t *link) { |
| 56 |
assert(pn_link_is_receiver(link)); |
| 57 |
int window = link_context::get(link).credit_window; |
| 58 |
if (window) { |
| 59 |
int delta = window - pn_link_credit(link); |
| 60 |
pn_link_flow(link, delta); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
void on_link_flow(messaging_handler& handler, pn_event_t* event) { |
| 66 |
pn_link_t *lnk = pn_event_link(event); |
| 67 |
// TODO: process session flow data, if no link-specific data, just return. |
| 68 |
if (!lnk) return; |
| 69 |
int state = pn_link_state(lnk); |
| 70 |
if ((state&PN_LOCAL_ACTIVE) && (state&PN_REMOTE_ACTIVE)) { |
| 71 |
link_context& lctx = link_context::get(lnk); |
| 72 |
if (pn_link_is_sender(lnk)) { |
| 73 |
if (pn_link_credit(lnk) > 0) { |
| 74 |
sender s(make_wrapper<sender>(lnk)); |
| 75 |
bool draining = pn_link_get_drain(lnk); |
| 76 |
if ( draining && !lctx.draining) { |
| 77 |
handler.on_sender_drain_start(s); |
| 78 |
} |
| 79 |
lctx.draining = draining; |
| 80 |
// create on_message extended event |
| 81 |
handler.on_sendable(s); |
| 82 |
} |
| 83 |
} else { |
| 84 |
// receiver |
| 85 |
if (!pn_link_credit(lnk) && lctx.draining) { |
| 86 |
lctx.draining = false; |
| 87 |
pn_link_set_drain(lnk, false); |
| 88 |
receiver r(make_wrapper<receiver>(lnk)); |
| 89 |
handler.on_receiver_drain_finish(r); |
| 90 |
} |
| 91 |
credit_topup(lnk); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// Decode the message corresponding to a delivery from a link. |
| 97 |
void message_decode(message& msg, proton::delivery delivery) { |
| 98 |
std::vector<char> buf; |
| 99 |
buf.resize(pn_delivery_pending(unwrap(delivery))); |
| 100 |
if (buf.empty()) |
| 101 |
throw error("message decode: no delivery pending on link"); |
| 102 |
proton::receiver link = delivery.receiver(); |
| 103 |
assert(!buf.empty()); |
| 104 |
ssize_t n = pn_link_recv(unwrap(link), const_cast<char *>(&buf[0]), buf.size()); |
| 105 |
if (n != ssize_t(buf.size())) throw error(MSG("receiver read failure")); |
| 106 |
msg.clear(); |
| 107 |
msg.decode(buf); |
| 108 |
pn_link_advance(unwrap(link)); |
| 109 |
} |
| 110 |
|
| 111 |
void on_delivery(messaging_handler& handler, pn_event_t* event) { |
| 112 |
pn_link_t *lnk = pn_event_link(event); |
| 113 |
pn_delivery_t *dlv = pn_event_delivery(event); |
| 114 |
link_context& lctx = link_context::get(lnk); |
| 115 |
|
| 116 |
if (pn_link_is_receiver(lnk)) { |
| 117 |
delivery d(make_wrapper<delivery>(dlv)); |
| 118 |
if (!pn_delivery_partial(dlv) && pn_delivery_readable(dlv)) { |
| 119 |
// generate on_message |
| 120 |
pn_connection_t *pnc = pn_session_connection(pn_link_session(lnk)); |
| 121 |
connection_context& ctx = connection_context::get(pnc); |
| 122 |
// Reusable per-connection message. |
| 123 |
// Avoid expensive heap malloc/free overhead. |
| 124 |
// See PROTON-998 |
| 125 |
class message &msg(ctx.event_message); |
| 126 |
message_decode(msg, d); |
| 127 |
if (pn_link_state(lnk) & PN_LOCAL_CLOSED) { |
| 128 |
if (lctx.auto_accept) |
| 129 |
d.release(); |
| 130 |
} else { |
| 131 |
handler.on_message(d, msg); |
| 132 |
if (lctx.auto_accept && pn_delivery_local_state(dlv) == 0) // Not set by handler |
| 133 |
d.accept(); |
| 134 |
if (lctx.draining && !pn_link_credit(lnk)) { |
| 135 |
lctx.draining = false; |
| 136 |
pn_link_set_drain(lnk, false); |
| 137 |
receiver r(make_wrapper<receiver>(lnk)); |
| 138 |
handler.on_receiver_drain_finish(r); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
else if (pn_delivery_updated(dlv) && d.settled()) { |
| 143 |
handler.on_delivery_settle(d); |
| 144 |
} |
| 145 |
if (lctx.draining && pn_link_credit(lnk) == 0) { |
| 146 |
lctx.draining = false; |
| 147 |
pn_link_set_drain(lnk, false); |
| 148 |
receiver r(make_wrapper<receiver>(lnk)); |
| 149 |
handler.on_receiver_drain_finish(r); |
| 150 |
if (lctx.pending_credit) { |
| 151 |
pn_link_flow(lnk, lctx.pending_credit); |
| 152 |
lctx.pending_credit = 0; |
| 153 |
} |
| 154 |
} |
| 155 |
credit_topup(lnk); |
| 156 |
} else { |
| 157 |
// sender |
| 158 |
if (pn_delivery_updated(dlv)) { |
| 159 |
tracker t(make_wrapper<tracker>(dlv)); |
| 160 |
switch(pn_delivery_remote_state(dlv)) { |
| 161 |
case PN_ACCEPTED: |
| 162 |
handler.on_tracker_accept(t); |
| 163 |
break; |
| 164 |
case PN_REJECTED: |
| 165 |
handler.on_tracker_reject(t); |
| 166 |
break; |
| 167 |
case PN_RELEASED: |
| 168 |
case PN_MODIFIED: |
| 169 |
handler.on_tracker_release(t); |
| 170 |
break; |
| 171 |
} |
| 172 |
if (t.settled()) { |
| 173 |
handler.on_tracker_settle(t); |
| 174 |
if (lctx.auto_settle) |
| 175 |
t.settle(); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
bool is_remote_uninitialized(pn_state_t state) { |
| 182 |
return state & PN_REMOTE_UNINIT; |
| 183 |
} |
| 184 |
|
| 185 |
void on_link_remote_detach(messaging_handler& handler, pn_event_t* event) { |
| 186 |
pn_link_t *lnk = pn_event_link(event); |
| 187 |
if (pn_link_is_receiver(lnk)) { |
| 188 |
receiver r(make_wrapper<receiver>(lnk)); |
| 189 |
handler.on_receiver_detach(r); |
| 190 |
} else { |
| 191 |
sender s(make_wrapper<sender>(lnk)); |
| 192 |
handler.on_sender_detach(s); |
| 193 |
} |
| 194 |
pn_link_detach(lnk); |
| 195 |
} |
| 196 |
|
| 197 |
void on_link_remote_close(messaging_handler& handler, pn_event_t* event) { |
| 198 |
pn_link_t *lnk = pn_event_link(event); |
| 199 |
if (pn_link_is_receiver(lnk)) { |
| 200 |
receiver r(make_wrapper<receiver>(lnk)); |
| 201 |
if (pn_condition_is_set(pn_link_remote_condition(lnk))) { |
| 202 |
handler.on_receiver_error(r); |
| 203 |
} |
| 204 |
handler.on_receiver_close(r); |
| 205 |
} else { |
| 206 |
sender s(make_wrapper<sender>(lnk)); |
| 207 |
if (pn_condition_is_set(pn_link_remote_condition(lnk))) { |
| 208 |
handler.on_sender_error(s); |
| 209 |
} |
| 210 |
handler.on_sender_close(s); |
| 211 |
} |
| 212 |
pn_link_close(lnk); |
| 213 |
} |
| 214 |
|
| 215 |
void on_session_remote_close(messaging_handler& handler, pn_event_t* event) { |
| 216 |
pn_session_t *session = pn_event_session(event); |
| 217 |
class session s(make_wrapper(session)); |
| 218 |
if (pn_condition_is_set(pn_session_remote_condition(session))) { |
| 219 |
handler.on_session_error(s); |
| 220 |
} |
| 221 |
handler.on_session_close(s); |
| 222 |
pn_session_close(session); |
| 223 |
} |
| 224 |
|
| 225 |
void on_connection_remote_close(messaging_handler& handler, pn_event_t* event) { |
| 226 |
pn_connection_t *conn = pn_event_connection(event); |
| 227 |
connection c(make_wrapper(conn)); |
| 228 |
if (pn_condition_is_set(pn_connection_remote_condition(conn))) { |
| 229 |
handler.on_connection_error(c); |
| 230 |
} |
| 231 |
handler.on_connection_close(c); |
| 232 |
pn_connection_close(conn); |
| 233 |
} |
| 234 |
|
| 235 |
void on_connection_bound(messaging_handler& handler, pn_event_t* event) { |
| 236 |
connection c(make_wrapper(pn_event_connection(event))); |
| 237 |
} |
| 238 |
|
| 239 |
void on_connection_remote_open(messaging_handler& handler, pn_event_t* event) { |
| 240 |
// Generate on_transport_open event here until we find a better place |
| 241 |
transport t(make_wrapper(pn_event_transport(event))); |
| 242 |
handler.on_transport_open(t); |
| 243 |
|
| 244 |
pn_connection_t *conn = pn_event_connection(event); |
| 245 |
connection c(make_wrapper(conn)); |
| 246 |
handler.on_connection_open(c); |
| 247 |
} |
| 248 |
|
| 249 |
void on_session_remote_open(messaging_handler& handler, pn_event_t* event) { |
| 250 |
pn_session_t *session = pn_event_session(event); |
| 251 |
class session s(make_wrapper(session)); |
| 252 |
handler.on_session_open(s); |
| 253 |
} |
| 254 |
|
| 255 |
void on_link_local_open(messaging_handler& handler, pn_event_t* event) { |
| 256 |
pn_link_t* lnk = pn_event_link(event); |
| 257 |
if ( pn_link_is_receiver(lnk) ) { |
| 258 |
credit_topup(lnk); |
| 259 |
// We know local is active so don't check for it |
| 260 |
} else if ( pn_link_state(lnk)&PN_REMOTE_ACTIVE && pn_link_credit(lnk) > 0) { |
| 261 |
sender s(make_wrapper<sender>(lnk)); |
| 262 |
handler.on_sendable(s); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
void on_link_remote_open(messaging_handler& handler, pn_event_t* event) { |
| 267 |
pn_link_t *lnk = pn_event_link(event); |
| 268 |
if (pn_link_state(lnk) & PN_LOCAL_UNINIT) { // Incoming link |
| 269 |
// Copy source and target from remote end. |
| 270 |
pn_terminus_copy(pn_link_source(lnk), pn_link_remote_source(lnk)); |
| 271 |
pn_terminus_copy(pn_link_target(lnk), pn_link_remote_target(lnk)); |
| 272 |
} |
| 273 |
if (pn_link_is_receiver(lnk)) { |
| 274 |
receiver r(make_wrapper<receiver>(lnk)); |
| 275 |
handler.on_receiver_open(r); |
| 276 |
credit_topup(lnk); |
| 277 |
} else { |
| 278 |
sender s(make_wrapper<sender>(lnk)); |
| 279 |
handler.on_sender_open(s); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
void on_transport_closed(messaging_handler& handler, pn_event_t* event) { |
| 284 |
pn_transport_t *tspt = pn_event_transport(event); |
| 285 |
transport t(make_wrapper(tspt)); |
| 286 |
|
| 287 |
// If the connection isn't open generate on_transport_open event |
| 288 |
// because we didn't generate it yet and the events won't match. |
| 289 |
pn_connection_t *conn = pn_event_connection(event); |
| 290 |
if (!conn || is_remote_uninitialized(pn_connection_state(conn))) { |
| 291 |
handler.on_transport_open(t); |
| 292 |
} |
| 293 |
|
| 294 |
if (pn_condition_is_set(pn_transport_condition(tspt))) { |
| 295 |
handler.on_transport_error(t); |
| 296 |
} |
| 297 |
handler.on_transport_close(t); |
| 298 |
} |
| 299 |
|
| 300 |
void on_connection_wake(messaging_handler& handler, pn_event_t* event) { |
| 301 |
connection c(make_wrapper(pn_event_connection(event))); |
| 302 |
handler.on_connection_wake(c); |
| 303 |
} |
| 304 |
|
| 305 |
} |
| 306 |
|
| 307 |
void messaging_adapter::dispatch(messaging_handler& handler, pn_event_t* event) |
| 308 |
{ |
| 309 |
pn_event_type_t type = pn_event_type(event); |
| 310 |
|
| 311 |
// Only handle events we are interested in |
| 312 |
switch(type) { |
| 313 |
case PN_CONNECTION_BOUND: on_connection_bound(handler, event); break; |
| 314 |
case PN_CONNECTION_REMOTE_OPEN: on_connection_remote_open(handler, event); break; |
| 315 |
case PN_CONNECTION_REMOTE_CLOSE: on_connection_remote_close(handler, event); break; |
| 316 |
|
| 317 |
case PN_SESSION_REMOTE_OPEN: on_session_remote_open(handler, event); break; |
| 318 |
case PN_SESSION_REMOTE_CLOSE: on_session_remote_close(handler, event); break; |
| 319 |
|
| 320 |
case PN_LINK_LOCAL_OPEN: on_link_local_open(handler, event); break; |
| 321 |
case PN_LINK_REMOTE_OPEN: on_link_remote_open(handler, event); break; |
| 322 |
case PN_LINK_REMOTE_CLOSE: on_link_remote_close(handler, event); break; |
| 323 |
case PN_LINK_REMOTE_DETACH: on_link_remote_detach(handler, event); break; |
| 324 |
case PN_LINK_FLOW: on_link_flow(handler, event); break; |
| 325 |
|
| 326 |
case PN_DELIVERY: on_delivery(handler, event); break; |
| 327 |
|
| 328 |
case PN_TRANSPORT_CLOSED: on_transport_closed(handler, event); break; |
| 329 |
|
| 330 |
case PN_CONNECTION_WAKE: on_connection_wake(handler, event); break; |
| 331 |
|
| 332 |
// Ignore everything else |
| 333 |
default: break; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
} |