| 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 |
/* for pn_work_head and related deprecations */ |
| 23 |
#define PN_USE_DEPRECATED_API 1 |
| 24 |
|
| 25 |
#include "engine-internal.h" |
| 26 |
|
| 27 |
#include "framing.h" |
| 28 |
#include "memory.h" |
| 29 |
#include "platform/platform.h" |
| 30 |
#include "platform/platform_fmt.h" |
| 31 |
#include "protocol.h" |
| 32 |
#include "transport.h" |
| 33 |
|
| 34 |
#include <assert.h> |
| 35 |
#include <stdarg.h> |
| 36 |
#include <stddef.h> |
| 37 |
#include <stdio.h> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
|
| 41 |
static void pni_session_bound(pn_session_t *ssn); |
| 42 |
static void pni_link_bound(pn_link_t *link); |
| 43 |
|
| 44 |
|
| 45 |
// endpoints |
| 46 |
|
| 47 |
static pn_connection_t *pni_ep_get_connection(pn_endpoint_t *endpoint) |
| 48 |
{ |
| 49 |
switch (endpoint->type) { |
| 50 |
case CONNECTION: |
| 51 |
return (pn_connection_t *) endpoint; |
| 52 |
case SESSION: |
| 53 |
return ((pn_session_t *) endpoint)->connection; |
| 54 |
case SENDER: |
| 55 |
case RECEIVER: |
| 56 |
return ((pn_link_t *) endpoint)->session->connection; |
| 57 |
} |
| 58 |
|
| 59 |
return NULL; |
| 60 |
} |
| 61 |
|
| 62 |
static pn_event_type_t endpoint_event(pn_endpoint_type_t type, bool open) { |
| 63 |
switch (type) { |
| 64 |
case CONNECTION: |
| 65 |
return open ? PN_CONNECTION_LOCAL_OPEN : PN_CONNECTION_LOCAL_CLOSE; |
| 66 |
case SESSION: |
| 67 |
return open ? PN_SESSION_LOCAL_OPEN : PN_SESSION_LOCAL_CLOSE; |
| 68 |
case SENDER: |
| 69 |
case RECEIVER: |
| 70 |
return open ? PN_LINK_LOCAL_OPEN : PN_LINK_LOCAL_CLOSE; |
| 71 |
default: |
| 72 |
assert(false); |
| 73 |
return PN_EVENT_NONE; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
static void pn_endpoint_open(pn_endpoint_t *endpoint) |
| 78 |
{ |
| 79 |
if (!(endpoint->state & PN_LOCAL_ACTIVE)) { |
| 80 |
PN_SET_LOCAL(endpoint->state, PN_LOCAL_ACTIVE); |
| 81 |
pn_connection_t *conn = pni_ep_get_connection(endpoint); |
| 82 |
pn_collector_put(conn->collector, PN_OBJECT, endpoint, |
| 83 |
endpoint_event((pn_endpoint_type_t) endpoint->type, true)); |
| 84 |
pn_modified(conn, endpoint, true); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
static void pn_endpoint_close(pn_endpoint_t *endpoint) |
| 89 |
{ |
| 90 |
if (!(endpoint->state & PN_LOCAL_CLOSED)) { |
| 91 |
PN_SET_LOCAL(endpoint->state, PN_LOCAL_CLOSED); |
| 92 |
pn_connection_t *conn = pni_ep_get_connection(endpoint); |
| 93 |
pn_collector_put(conn->collector, PN_OBJECT, endpoint, |
| 94 |
endpoint_event((pn_endpoint_type_t) endpoint->type, false)); |
| 95 |
pn_modified(conn, endpoint, true); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void pn_connection_reset(pn_connection_t *connection) |
| 100 |
{ |
| 101 |
assert(connection); |
| 102 |
pn_endpoint_t *endpoint = &connection->endpoint; |
| 103 |
endpoint->state = PN_LOCAL_UNINIT | PN_REMOTE_UNINIT; |
| 104 |
} |
| 105 |
|
| 106 |
void pn_connection_open(pn_connection_t *connection) |
| 107 |
{ |
| 108 |
assert(connection); |
| 109 |
pn_endpoint_open(&connection->endpoint); |
| 110 |
} |
| 111 |
|
| 112 |
void pn_connection_close(pn_connection_t *connection) |
| 113 |
{ |
| 114 |
assert(connection); |
| 115 |
pn_endpoint_close(&connection->endpoint); |
| 116 |
} |
| 117 |
|
| 118 |
static void pni_endpoint_tini(pn_endpoint_t *endpoint); |
| 119 |
|
| 120 |
void pn_connection_release(pn_connection_t *connection) |
| 121 |
{ |
| 122 |
assert(!connection->endpoint.freed); |
| 123 |
// free those endpoints that haven't been freed by the application |
| 124 |
LL_REMOVE(connection, endpoint, &connection->endpoint); |
| 125 |
while (connection->endpoint_head) { |
| 126 |
pn_endpoint_t *ep = connection->endpoint_head; |
| 127 |
switch (ep->type) { |
| 128 |
case SESSION: |
| 129 |
// note: this will free all child links: |
| 130 |
pn_session_free((pn_session_t *)ep); |
| 131 |
break; |
| 132 |
case SENDER: |
| 133 |
case RECEIVER: |
| 134 |
pn_link_free((pn_link_t *)ep); |
| 135 |
break; |
| 136 |
default: |
| 137 |
assert(false); |
| 138 |
} |
| 139 |
} |
| 140 |
connection->endpoint.freed = true; |
| 141 |
if (!connection->transport) { |
| 142 |
// no transport available to consume transport work items, |
| 143 |
// so manually clear them: |
| 144 |
pn_ep_incref(&connection->endpoint); |
| 145 |
pn_connection_unbound(connection); |
| 146 |
} |
| 147 |
pn_ep_decref(&connection->endpoint); |
| 148 |
} |
| 149 |
|
| 150 |
void pn_connection_free(pn_connection_t *connection) { |
| 151 |
pn_connection_release(connection); |
| 152 |
pn_decref(connection); |
| 153 |
} |
| 154 |
|
| 155 |
void pn_connection_bound(pn_connection_t *connection) |
| 156 |
{ |
| 157 |
pn_collector_put(connection->collector, PN_OBJECT, connection, PN_CONNECTION_BOUND); |
| 158 |
pn_ep_incref(&connection->endpoint); |
| 159 |
|
| 160 |
size_t nsessions = pn_list_size(connection->sessions); |
| 161 |
for (size_t i = 0; i < nsessions; i++) { |
| 162 |
pni_session_bound((pn_session_t *) pn_list_get(connection->sessions, i)); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// invoked when transport has been removed: |
| 167 |
void pn_connection_unbound(pn_connection_t *connection) |
| 168 |
{ |
| 169 |
connection->transport = NULL; |
| 170 |
if (connection->endpoint.freed) { |
| 171 |
// connection has been freed prior to unbinding, thus it |
| 172 |
// cannot be re-assigned to a new transport. Clear the |
| 173 |
// transport work lists to allow the connection to be freed. |
| 174 |
while (connection->transport_head) { |
| 175 |
pn_clear_modified(connection, connection->transport_head); |
| 176 |
} |
| 177 |
while (connection->tpwork_head) { |
| 178 |
pn_clear_tpwork(connection->tpwork_head); |
| 179 |
} |
| 180 |
} |
| 181 |
pn_ep_decref(&connection->endpoint); |
| 182 |
} |
| 183 |
|
| 184 |
pn_record_t *pn_connection_attachments(pn_connection_t *connection) |
| 185 |
{ |
| 186 |
assert(connection); |
| 187 |
return connection->context; |
| 188 |
} |
| 189 |
|
| 190 |
void *pn_connection_get_context(pn_connection_t *conn) |
| 191 |
{ |
| 192 |
// XXX: we should really assert on conn here, but this causes |
| 193 |
// messenger tests to fail |
| 194 |
return conn ? pn_record_get(conn->context, PN_LEGCTX) : NULL; |
| 195 |
} |
| 196 |
|
| 197 |
void pn_connection_set_context(pn_connection_t *conn, void *context) |
| 198 |
{ |
| 199 |
assert(conn); |
| 200 |
pn_record_set(conn->context, PN_LEGCTX, context); |
| 201 |
} |
| 202 |
|
| 203 |
pn_transport_t *pn_connection_transport(pn_connection_t *connection) |
| 204 |
{ |
| 205 |
assert(connection); |
| 206 |
return connection->transport; |
| 207 |
} |
| 208 |
|
| 209 |
void pn_condition_init(pn_condition_t *condition) |
| 210 |
{ |
| 211 |
condition->name = NULL; |
| 212 |
condition->description = NULL; |
| 213 |
condition->info = NULL; |
| 214 |
} |
| 215 |
|
| 216 |
pn_condition_t *pn_condition() { |
| 217 |
pn_condition_t *c = (pn_condition_t*)pni_mem_allocate(PN_VOID, sizeof(pn_condition_t)); |
| 218 |
pn_condition_init(c); |
| 219 |
return c; |
| 220 |
} |
| 221 |
|
| 222 |
void pn_condition_tini(pn_condition_t *condition) |
| 223 |
{ |
| 224 |
pn_data_free(condition->info); |
| 225 |
pn_free(condition->description); |
| 226 |
pn_free(condition->name); |
| 227 |
} |
| 228 |
|
| 229 |
void pn_condition_free(pn_condition_t *c) { |
| 230 |
if (c) { |
| 231 |
pn_condition_clear(c); |
| 232 |
pn_condition_tini(c); |
| 233 |
pni_mem_deallocate(PN_VOID, c); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
static void pni_add_session(pn_connection_t *conn, pn_session_t *ssn) |
| 238 |
{ |
| 239 |
pn_list_add(conn->sessions, ssn); |
| 240 |
ssn->connection = conn; |
| 241 |
pn_incref(conn); // keep around until finalized |
| 242 |
pn_ep_incref(&conn->endpoint); |
| 243 |
} |
| 244 |
|
| 245 |
static void pni_remove_session(pn_connection_t *conn, pn_session_t *ssn) |
| 246 |
{ |
| 247 |
if (pn_list_remove(conn->sessions, ssn)) { |
| 248 |
pn_ep_decref(&conn->endpoint); |
| 249 |
LL_REMOVE(conn, endpoint, &ssn->endpoint); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
pn_connection_t *pn_session_connection(pn_session_t *session) |
| 254 |
{ |
| 255 |
if (!session) return NULL; |
| 256 |
return session->connection; |
| 257 |
} |
| 258 |
|
| 259 |
void pn_session_open(pn_session_t *session) |
| 260 |
{ |
| 261 |
assert(session); |
| 262 |
pn_endpoint_open(&session->endpoint); |
| 263 |
} |
| 264 |
|
| 265 |
void pn_session_close(pn_session_t *session) |
| 266 |
{ |
| 267 |
assert(session); |
| 268 |
pn_endpoint_close(&session->endpoint); |
| 269 |
} |
| 270 |
|
| 271 |
void pn_session_free(pn_session_t *session) |
| 272 |
{ |
| 273 |
assert(!session->endpoint.freed); |
| 274 |
while(pn_list_size(session->links)) { |
| 275 |
pn_link_t *link = (pn_link_t *)pn_list_get(session->links, 0); |
| 276 |
pn_link_free(link); |
| 277 |
} |
| 278 |
pni_remove_session(session->connection, session); |
| 279 |
pn_list_add(session->connection->freed, session); |
| 280 |
session->endpoint.freed = true; |
| 281 |
pn_ep_decref(&session->endpoint); |
| 282 |
|
| 283 |
// the finalize logic depends on endpoint.freed, so we incref/decref |
| 284 |
// to give it a chance to rerun |
| 285 |
pn_incref(session); |
| 286 |
pn_decref(session); |
| 287 |
} |
| 288 |
|
| 289 |
pn_record_t *pn_session_attachments(pn_session_t *session) |
| 290 |
{ |
| 291 |
assert(session); |
| 292 |
return session->context; |
| 293 |
} |
| 294 |
|
| 295 |
void *pn_session_get_context(pn_session_t *session) |
| 296 |
{ |
| 297 |
return session ? pn_record_get(session->context, PN_LEGCTX) : 0; |
| 298 |
} |
| 299 |
|
| 300 |
void pn_session_set_context(pn_session_t *session, void *context) |
| 301 |
{ |
| 302 |
assert(session); |
| 303 |
pn_record_set(session->context, PN_LEGCTX, context); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
static void pni_add_link(pn_session_t *ssn, pn_link_t *link) |
| 308 |
{ |
| 309 |
pn_list_add(ssn->links, link); |
| 310 |
link->session = ssn; |
| 311 |
pn_ep_incref(&ssn->endpoint); |
| 312 |
} |
| 313 |
|
| 314 |
static void pni_remove_link(pn_session_t *ssn, pn_link_t *link) |
| 315 |
{ |
| 316 |
if (pn_list_remove(ssn->links, link)) { |
| 317 |
pn_ep_decref(&ssn->endpoint); |
| 318 |
LL_REMOVE(ssn->connection, endpoint, &link->endpoint); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
void pn_link_open(pn_link_t *link) |
| 323 |
{ |
| 324 |
assert(link); |
| 325 |
pn_endpoint_open(&link->endpoint); |
| 326 |
} |
| 327 |
|
| 328 |
void pn_link_close(pn_link_t *link) |
| 329 |
{ |
| 330 |
assert(link); |
| 331 |
pn_endpoint_close(&link->endpoint); |
| 332 |
} |
| 333 |
|
| 334 |
void pn_link_detach(pn_link_t *link) |
| 335 |
{ |
| 336 |
assert(link); |
| 337 |
if (link->detached) return; |
| 338 |
|
| 339 |
link->detached = true; |
| 340 |
pn_collector_put(link->session->connection->collector, PN_OBJECT, link, PN_LINK_LOCAL_DETACH); |
| 341 |
pn_modified(link->session->connection, &link->endpoint, true); |
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
static void pni_terminus_free(pn_terminus_t *terminus) |
| 346 |
{ |
| 347 |
pn_free(terminus->address); |
| 348 |
pn_free(terminus->properties); |
| 349 |
pn_free(terminus->capabilities); |
| 350 |
pn_free(terminus->outcomes); |
| 351 |
pn_free(terminus->filter); |
| 352 |
} |
| 353 |
|
| 354 |
void pn_link_free(pn_link_t *link) |
| 355 |
{ |
| 356 |
assert(!link->endpoint.freed); |
| 357 |
pni_remove_link(link->session, link); |
| 358 |
pn_list_add(link->session->freed, link); |
| 359 |
pn_delivery_t *delivery = link->unsettled_head; |
| 360 |
while (delivery) { |
| 361 |
pn_delivery_t *next = delivery->unsettled_next; |
| 362 |
pn_delivery_settle(delivery); |
| 363 |
delivery = next; |
| 364 |
} |
| 365 |
link->endpoint.freed = true; |
| 366 |
pn_ep_decref(&link->endpoint); |
| 367 |
|
| 368 |
// the finalize logic depends on endpoint.freed (modified above), so |
| 369 |
// we incref/decref to give it a chance to rerun |
| 370 |
pn_incref(link); |
| 371 |
pn_decref(link); |
| 372 |
} |
| 373 |
|
| 374 |
void *pn_link_get_context(pn_link_t *link) |
| 375 |
{ |
| 376 |
assert(link); |
| 377 |
return pn_record_get(link->context, PN_LEGCTX); |
| 378 |
} |
| 379 |
|
| 380 |
void pn_link_set_context(pn_link_t *link, void *context) |
| 381 |
{ |
| 382 |
assert(link); |
| 383 |
pn_record_set(link->context, PN_LEGCTX, context); |
| 384 |
} |
| 385 |
|
| 386 |
pn_record_t *pn_link_attachments(pn_link_t *link) |
| 387 |
{ |
| 388 |
assert(link); |
| 389 |
return link->context; |
| 390 |
} |
| 391 |
|
| 392 |
void pn_endpoint_init(pn_endpoint_t *endpoint, int type, pn_connection_t *conn) |
| 393 |
{ |
| 394 |
endpoint->type = (pn_endpoint_type_t) type; |
| 395 |
endpoint->referenced = true; |
| 396 |
endpoint->state = PN_LOCAL_UNINIT | PN_REMOTE_UNINIT; |
| 397 |
pn_condition_init(&endpoint->condition); |
| 398 |
pn_condition_init(&endpoint->remote_condition); |
| 399 |
endpoint->endpoint_next = NULL; |
| 400 |
endpoint->endpoint_prev = NULL; |
| 401 |
endpoint->transport_next = NULL; |
| 402 |
endpoint->transport_prev = NULL; |
| 403 |
endpoint->modified = false; |
| 404 |
endpoint->freed = false; |
| 405 |
endpoint->refcount = 1; |
| 406 |
//fprintf(stderr, "initting 0x%lx\n", (uintptr_t) endpoint); |
| 407 |
|
| 408 |
LL_ADD(conn, endpoint, endpoint); |
| 409 |
} |
| 410 |
|
| 411 |
void pn_ep_incref(pn_endpoint_t *endpoint) |
| 412 |
{ |
| 413 |
endpoint->refcount++; |
| 414 |
} |
| 415 |
|
| 416 |
static pn_event_type_t pn_final_type(pn_endpoint_type_t type) { |
| 417 |
switch (type) { |
| 418 |
case CONNECTION: |
| 419 |
return PN_CONNECTION_FINAL; |
| 420 |
case SESSION: |
| 421 |
return PN_SESSION_FINAL; |
| 422 |
case SENDER: |
| 423 |
case RECEIVER: |
| 424 |
return PN_LINK_FINAL; |
| 425 |
default: |
| 426 |
assert(false); |
| 427 |
return PN_EVENT_NONE; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
static pn_endpoint_t *pn_ep_parent(pn_endpoint_t *endpoint) { |
| 432 |
switch (endpoint->type) { |
| 433 |
case CONNECTION: |
| 434 |
return NULL; |
| 435 |
case SESSION: |
| 436 |
return &((pn_session_t *) endpoint)->connection->endpoint; |
| 437 |
case SENDER: |
| 438 |
case RECEIVER: |
| 439 |
return &((pn_link_t *) endpoint)->session->endpoint; |
| 440 |
default: |
| 441 |
assert(false); |
| 442 |
return NULL; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
void pn_ep_decref(pn_endpoint_t *endpoint) |
| 447 |
{ |
| 448 |
assert(endpoint->refcount > 0); |
| 449 |
endpoint->refcount--; |
| 450 |
if (endpoint->refcount == 0) { |
| 451 |
pn_connection_t *conn = pni_ep_get_connection(endpoint); |
| 452 |
pn_collector_put(conn->collector, PN_OBJECT, endpoint, pn_final_type((pn_endpoint_type_t) endpoint->type)); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
static void pni_endpoint_tini(pn_endpoint_t *endpoint) |
| 457 |
{ |
| 458 |
pn_condition_tini(&endpoint->remote_condition); |
| 459 |
pn_condition_tini(&endpoint->condition); |
| 460 |
} |
| 461 |
|
| 462 |
static void pni_free_children(pn_list_t *children, pn_list_t *freed) |
| 463 |
{ |
| 464 |
while (pn_list_size(children) > 0) { |
| 465 |
pn_endpoint_t *endpoint = (pn_endpoint_t *) pn_list_get(children, 0); |
| 466 |
assert(!endpoint->referenced); |
| 467 |
pn_free(endpoint); |
| 468 |
} |
| 469 |
|
| 470 |
while (pn_list_size(freed) > 0) { |
| 471 |
pn_endpoint_t *endpoint = (pn_endpoint_t *) pn_list_get(freed, 0); |
| 472 |
assert(!endpoint->referenced); |
| 473 |
pn_free(endpoint); |
| 474 |
} |
| 475 |
|
| 476 |
pn_free(children); |
| 477 |
pn_free(freed); |
| 478 |
} |
| 479 |
|
| 480 |
static void pn_connection_finalize(void *object) |
| 481 |
{ |
| 482 |
pn_connection_t *conn = (pn_connection_t *) object; |
| 483 |
pn_endpoint_t *endpoint = &conn->endpoint; |
| 484 |
|
| 485 |
if (conn->transport) { |
| 486 |
assert(!conn->transport->referenced); |
| 487 |
pn_free(conn->transport); |
| 488 |
} |
| 489 |
|
| 490 |
// freeing the transport could post events |
| 491 |
if (pn_refcount(conn) > 0) { |
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
pni_free_children(conn->sessions, conn->freed); |
| 496 |
pn_free(conn->context); |
| 497 |
pn_decref(conn->collector); |
| 498 |
|
| 499 |
pn_free(conn->container); |
| 500 |
pn_free(conn->hostname); |
| 501 |
pn_free(conn->auth_user); |
| 502 |
pn_free(conn->authzid); |
| 503 |
pn_free(conn->auth_password); |
| 504 |
pn_free(conn->offered_capabilities); |
| 505 |
pn_free(conn->desired_capabilities); |
| 506 |
pn_free(conn->properties); |
| 507 |
pni_endpoint_tini(endpoint); |
| 508 |
pn_free(conn->delivery_pool); |
| 509 |
} |
| 510 |
|
| 511 |
#define pn_connection_initialize NULL |
| 512 |
#define pn_connection_hashcode NULL |
| 513 |
#define pn_connection_compare NULL |
| 514 |
#define pn_connection_inspect NULL |
| 515 |
|
| 516 |
pn_connection_t *pn_connection() |
| 517 |
{ |
| 518 |
static const pn_class_t clazz = PN_CLASS(pn_connection); |
| 519 |
pn_connection_t *conn = (pn_connection_t *) pn_class_new(&clazz, sizeof(pn_connection_t)); |
| 520 |
if (!conn) return NULL; |
| 521 |
|
| 522 |
conn->endpoint_head = NULL; |
| 523 |
conn->endpoint_tail = NULL; |
| 524 |
pn_endpoint_init(&conn->endpoint, CONNECTION, conn); |
| 525 |
conn->transport_head = NULL; |
| 526 |
conn->transport_tail = NULL; |
| 527 |
conn->sessions = pn_list(PN_WEAKREF, 0); |
| 528 |
conn->freed = pn_list(PN_WEAKREF, 0); |
| 529 |
conn->transport = NULL; |
| 530 |
conn->work_head = NULL; |
| 531 |
conn->work_tail = NULL; |
| 532 |
conn->tpwork_head = NULL; |
| 533 |
conn->tpwork_tail = NULL; |
| 534 |
conn->container = pn_string(NULL); |
| 535 |
conn->hostname = pn_string(NULL); |
| 536 |
conn->auth_user = pn_string(NULL); |
| 537 |
conn->authzid = pn_string(NULL); |
| 538 |
conn->auth_password = pn_string(NULL); |
| 539 |
conn->offered_capabilities = pn_data(0); |
| 540 |
conn->desired_capabilities = pn_data(0); |
| 541 |
conn->properties = pn_data(0); |
| 542 |
conn->collector = NULL; |
| 543 |
conn->context = pn_record(); |
| 544 |
conn->delivery_pool = pn_list(PN_OBJECT, 0); |
| 545 |
conn->driver = NULL; |
| 546 |
|
| 547 |
return conn; |
| 548 |
} |
| 549 |
|
| 550 |
static const pn_event_type_t endpoint_init_event_map[] = { |
| 551 |
PN_CONNECTION_INIT, /* CONNECTION */ |
| 552 |
PN_SESSION_INIT, /* SESSION */ |
| 553 |
PN_LINK_INIT, /* SENDER */ |
| 554 |
PN_LINK_INIT}; /* RECEIVER */ |
| 555 |
|
| 556 |
void pn_connection_collect(pn_connection_t *connection, pn_collector_t *collector) |
| 557 |
{ |
| 558 |
pn_decref(connection->collector); |
| 559 |
connection->collector = collector; |
| 560 |
pn_incref(connection->collector); |
| 561 |
pn_endpoint_t *endpoint = connection->endpoint_head; |
| 562 |
while (endpoint) { |
| 563 |
pn_collector_put(connection->collector, PN_OBJECT, endpoint, endpoint_init_event_map[endpoint->type]); |
| 564 |
endpoint = endpoint->endpoint_next; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
pn_collector_t* pn_connection_collector(pn_connection_t *connection) { |
| 569 |
return connection->collector; |
| 570 |
} |
| 571 |
|
| 572 |
pn_state_t pn_connection_state(pn_connection_t *connection) |
| 573 |
{ |
| 574 |
return connection ? connection->endpoint.state : 0; |
| 575 |
} |
| 576 |
|
| 577 |
const char *pn_connection_get_container(pn_connection_t *connection) |
| 578 |
{ |
| 579 |
assert(connection); |
| 580 |
return pn_string_get(connection->container); |
| 581 |
} |
| 582 |
|
| 583 |
void pn_connection_set_container(pn_connection_t *connection, const char *container) |
| 584 |
{ |
| 585 |
assert(connection); |
| 586 |
pn_string_set(connection->container, container); |
| 587 |
} |
| 588 |
|
| 589 |
const char *pn_connection_get_hostname(pn_connection_t *connection) |
| 590 |
{ |
| 591 |
assert(connection); |
| 592 |
return pn_string_get(connection->hostname); |
| 593 |
} |
| 594 |
|
| 595 |
void pn_connection_set_hostname(pn_connection_t *connection, const char *hostname) |
| 596 |
{ |
| 597 |
assert(connection); |
| 598 |
pn_string_set(connection->hostname, hostname); |
| 599 |
} |
| 600 |
|
| 601 |
const char *pn_connection_get_user(pn_connection_t *connection) |
| 602 |
{ |
| 603 |
assert(connection); |
| 604 |
return pn_string_get(connection->auth_user); |
| 605 |
} |
| 606 |
|
| 607 |
void pn_connection_set_user(pn_connection_t *connection, const char *user) |
| 608 |
{ |
| 609 |
assert(connection); |
| 610 |
pn_string_set(connection->auth_user, user); |
| 611 |
} |
| 612 |
|
| 613 |
const char *pn_connection_get_authorization(pn_connection_t *connection) |
| 614 |
{ |
| 615 |
assert(connection); |
| 616 |
return pn_string_get(connection->authzid); |
| 617 |
} |
| 618 |
|
| 619 |
void pn_connection_set_authorization(pn_connection_t *connection, const char *authzid) |
| 620 |
{ |
| 621 |
assert(connection); |
| 622 |
pn_string_set(connection->authzid, authzid); |
| 623 |
} |
| 624 |
|
| 625 |
void pn_connection_set_password(pn_connection_t *connection, const char *password) |
| 626 |
{ |
| 627 |
assert(connection); |
| 628 |
// Make sure the previous password is erased, if there was one. |
| 629 |
size_t n = pn_string_size(connection->auth_password); |
| 630 |
const char* s = pn_string_get(connection->auth_password); |
| 631 |
if (n > 0 && s) memset((void*)s, 0, n); |
| 632 |
pn_string_set(connection->auth_password, password); |
| 633 |
} |
| 634 |
|
| 635 |
pn_data_t *pn_connection_offered_capabilities(pn_connection_t *connection) |
| 636 |
{ |
| 637 |
assert(connection); |
| 638 |
return connection->offered_capabilities; |
| 639 |
} |
| 640 |
|
| 641 |
pn_data_t *pn_connection_desired_capabilities(pn_connection_t *connection) |
| 642 |
{ |
| 643 |
assert(connection); |
| 644 |
return connection->desired_capabilities; |
| 645 |
} |
| 646 |
|
| 647 |
pn_data_t *pn_connection_properties(pn_connection_t *connection) |
| 648 |
{ |
| 649 |
assert(connection); |
| 650 |
return connection->properties; |
| 651 |
} |
| 652 |
|
| 653 |
pn_data_t *pn_connection_remote_offered_capabilities(pn_connection_t *connection) |
| 654 |
{ |
| 655 |
assert(connection); |
| 656 |
return connection->transport ? connection->transport->remote_offered_capabilities : NULL; |
| 657 |
} |
| 658 |
|
| 659 |
pn_data_t *pn_connection_remote_desired_capabilities(pn_connection_t *connection) |
| 660 |
{ |
| 661 |
assert(connection); |
| 662 |
return connection->transport ? connection->transport->remote_desired_capabilities : NULL; |
| 663 |
} |
| 664 |
|
| 665 |
pn_data_t *pn_connection_remote_properties(pn_connection_t *connection) |
| 666 |
{ |
| 667 |
assert(connection); |
| 668 |
return connection->transport ? connection->transport->remote_properties : NULL; |
| 669 |
} |
| 670 |
|
| 671 |
const char *pn_connection_remote_container(pn_connection_t *connection) |
| 672 |
{ |
| 673 |
assert(connection); |
| 674 |
return connection->transport ? connection->transport->remote_container : NULL; |
| 675 |
} |
| 676 |
|
| 677 |
const char *pn_connection_remote_hostname(pn_connection_t *connection) |
| 678 |
{ |
| 679 |
assert(connection); |
| 680 |
return connection->transport ? connection->transport->remote_hostname : NULL; |
| 681 |
} |
| 682 |
|
| 683 |
pn_delivery_t *pn_work_head(pn_connection_t *connection) |
| 684 |
{ |
| 685 |
assert(connection); |
| 686 |
return connection->work_head; |
| 687 |
} |
| 688 |
|
| 689 |
pn_delivery_t *pn_work_next(pn_delivery_t *delivery) |
| 690 |
{ |
| 691 |
assert(delivery); |
| 692 |
|
| 693 |
if (delivery->work) |
| 694 |
return delivery->work_next; |
| 695 |
else |
| 696 |
return pn_work_head(delivery->link->session->connection); |
| 697 |
} |
| 698 |
|
| 699 |
static void pni_add_work(pn_connection_t *connection, pn_delivery_t *delivery) |
| 700 |
{ |
| 701 |
if (!delivery->work) |
| 702 |
{ |
| 703 |
LL_ADD(connection, work, delivery); |
| 704 |
delivery->work = true; |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
static void pni_clear_work(pn_connection_t *connection, pn_delivery_t *delivery) |
| 709 |
{ |
| 710 |
if (delivery->work) |
| 711 |
{ |
| 712 |
LL_REMOVE(connection, work, delivery); |
| 713 |
delivery->work = false; |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
void pn_work_update(pn_connection_t *connection, pn_delivery_t *delivery) |
| 718 |
{ |
| 719 |
pn_link_t *link = pn_delivery_link(delivery); |
| 720 |
pn_delivery_t *current = pn_link_current(link); |
| 721 |
if (delivery->updated && !delivery->local.settled) { |
| 722 |
pni_add_work(connection, delivery); |
| 723 |
} else if (delivery == current) { |
| 724 |
if (link->endpoint.type == SENDER) { |
| 725 |
if (pn_link_credit(link) > 0) { |
| 726 |
pni_add_work(connection, delivery); |
| 727 |
} else { |
| 728 |
pni_clear_work(connection, delivery); |
| 729 |
} |
| 730 |
} else { |
| 731 |
pni_add_work(connection, delivery); |
| 732 |
} |
| 733 |
} else { |
| 734 |
pni_clear_work(connection, delivery); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
static void pni_add_tpwork(pn_delivery_t *delivery) |
| 739 |
{ |
| 740 |
pn_connection_t *connection = delivery->link->session->connection; |
| 741 |
if (!delivery->tpwork) |
| 742 |
{ |
| 743 |
LL_ADD(connection, tpwork, delivery); |
| 744 |
delivery->tpwork = true; |
| 745 |
} |
| 746 |
pn_modified(connection, &connection->endpoint, true); |
| 747 |
} |
| 748 |
|
| 749 |
void pn_clear_tpwork(pn_delivery_t *delivery) |
| 750 |
{ |
| 751 |
pn_connection_t *connection = delivery->link->session->connection; |
| 752 |
if (delivery->tpwork) |
| 753 |
{ |
| 754 |
LL_REMOVE(connection, tpwork, delivery); |
| 755 |
delivery->tpwork = false; |
| 756 |
if (pn_refcount(delivery) > 0) { |
| 757 |
pn_incref(delivery); |
| 758 |
pn_decref(delivery); |
| 759 |
} |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
void pn_dump(pn_connection_t *conn) |
| 764 |
{ |
| 765 |
pn_endpoint_t *endpoint = conn->transport_head; |
| 766 |
while (endpoint) |
| 767 |
{ |
| 768 |
printf("%p", (void *) endpoint); |
| 769 |
endpoint = endpoint->transport_next; |
| 770 |
if (endpoint) |
| 771 |
printf(" -> "); |
| 772 |
} |
| 773 |
printf("\n"); |
| 774 |
} |
| 775 |
|
| 776 |
void pn_modified(pn_connection_t *connection, pn_endpoint_t *endpoint, bool emit) |
| 777 |
{ |
| 778 |
if (!endpoint->modified) { |
| 779 |
LL_ADD(connection, transport, endpoint); |
| 780 |
endpoint->modified = true; |
| 781 |
} |
| 782 |
|
| 783 |
if (emit && connection->transport) { |
| 784 |
pn_collector_put(connection->collector, PN_OBJECT, connection->transport, |
| 785 |
PN_TRANSPORT); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
void pn_clear_modified(pn_connection_t *connection, pn_endpoint_t *endpoint) |
| 790 |
{ |
| 791 |
if (endpoint->modified) { |
| 792 |
LL_REMOVE(connection, transport, endpoint); |
| 793 |
endpoint->transport_next = NULL; |
| 794 |
endpoint->transport_prev = NULL; |
| 795 |
endpoint->modified = false; |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
static bool pni_matches(pn_endpoint_t *endpoint, pn_endpoint_type_t type, pn_state_t state) |
| 800 |
{ |
| 801 |
if (endpoint->type != type) return false; |
| 802 |
|
| 803 |
if (!state) return true; |
| 804 |
|
| 805 |
int st = endpoint->state; |
| 806 |
if ((state & PN_REMOTE_MASK) == 0 || (state & PN_LOCAL_MASK) == 0) |
| 807 |
return st & state; |
| 808 |
else |
| 809 |
return st == state; |
| 810 |
} |
| 811 |
|
| 812 |
pn_endpoint_t *pn_find(pn_endpoint_t *endpoint, pn_endpoint_type_t type, pn_state_t state) |
| 813 |
{ |
| 814 |
while (endpoint) |
| 815 |
{ |
| 816 |
if (pni_matches(endpoint, type, state)) |
| 817 |
return endpoint; |
| 818 |
endpoint = endpoint->endpoint_next; |
| 819 |
} |
| 820 |
return NULL; |
| 821 |
} |
| 822 |
|
| 823 |
pn_session_t *pn_session_head(pn_connection_t *conn, pn_state_t state) |
| 824 |
{ |
| 825 |
if (conn) |
| 826 |
return (pn_session_t *) pn_find(conn->endpoint_head, SESSION, state); |
| 827 |
else |
| 828 |
return NULL; |
| 829 |
} |
| 830 |
|
| 831 |
pn_session_t *pn_session_next(pn_session_t *ssn, pn_state_t state) |
| 832 |
{ |
| 833 |
if (ssn) |
| 834 |
return (pn_session_t *) pn_find(ssn->endpoint.endpoint_next, SESSION, state); |
| 835 |
else |
| 836 |
return NULL; |
| 837 |
} |
| 838 |
|
| 839 |
pn_link_t *pn_link_head(pn_connection_t *conn, pn_state_t state) |
| 840 |
{ |
| 841 |
if (!conn) return NULL; |
| 842 |
|
| 843 |
pn_endpoint_t *endpoint = conn->endpoint_head; |
| 844 |
|
| 845 |
while (endpoint) |
| 846 |
{ |
| 847 |
if (pni_matches(endpoint, SENDER, state) || pni_matches(endpoint, RECEIVER, state)) |
| 848 |
return (pn_link_t *) endpoint; |
| 849 |
endpoint = endpoint->endpoint_next; |
| 850 |
} |
| 851 |
|
| 852 |
return NULL; |
| 853 |
} |
| 854 |
|
| 855 |
pn_link_t *pn_link_next(pn_link_t *link, pn_state_t state) |
| 856 |
{ |
| 857 |
if (!link) return NULL; |
| 858 |
|
| 859 |
pn_endpoint_t *endpoint = link->endpoint.endpoint_next; |
| 860 |
|
| 861 |
while (endpoint) |
| 862 |
{ |
| 863 |
if (pni_matches(endpoint, SENDER, state) || pni_matches(endpoint, RECEIVER, state)) |
| 864 |
return (pn_link_t *) endpoint; |
| 865 |
endpoint = endpoint->endpoint_next; |
| 866 |
} |
| 867 |
|
| 868 |
return NULL; |
| 869 |
} |
| 870 |
|
| 871 |
static void pn_session_incref(void *object) |
| 872 |
{ |
| 873 |
pn_session_t *session = (pn_session_t *) object; |
| 874 |
if (!session->endpoint.referenced) { |
| 875 |
session->endpoint.referenced = true; |
| 876 |
pn_incref(session->connection); |
| 877 |
} else { |
| 878 |
pn_object_incref(object); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
static bool pn_ep_bound(pn_endpoint_t *endpoint) |
| 883 |
{ |
| 884 |
pn_connection_t *conn = pni_ep_get_connection(endpoint); |
| 885 |
pn_session_t *ssn; |
| 886 |
pn_link_t *lnk; |
| 887 |
|
| 888 |
if (!conn->transport) return false; |
| 889 |
if (endpoint->modified) return true; |
| 890 |
|
| 891 |
switch (endpoint->type) { |
| 892 |
case CONNECTION: |
| 893 |
return ((pn_connection_t *)endpoint)->transport; |
| 894 |
case SESSION: |
| 895 |
ssn = (pn_session_t *) endpoint; |
| 896 |
return (((int16_t) ssn->state.local_channel) >= 0 || ((int16_t) ssn->state.remote_channel) >= 0); |
| 897 |
case SENDER: |
| 898 |
case RECEIVER: |
| 899 |
lnk = (pn_link_t *) endpoint; |
| 900 |
return ((int32_t) lnk->state.local_handle) >= 0 || ((int32_t) lnk->state.remote_handle) >= 0; |
| 901 |
default: |
| 902 |
assert(false); |
| 903 |
return false; |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
static bool pni_connection_live(pn_connection_t *conn) { |
| 908 |
return pn_refcount(conn) > 1; |
| 909 |
} |
| 910 |
|
| 911 |
static bool pni_session_live(pn_session_t *ssn) { |
| 912 |
return pni_connection_live(ssn->connection) || pn_refcount(ssn) > 1; |
| 913 |
} |
| 914 |
|
| 915 |
static bool pni_link_live(pn_link_t *link) { |
| 916 |
return pni_session_live(link->session) || pn_refcount(link) > 1; |
| 917 |
} |
| 918 |
|
| 919 |
static bool pni_endpoint_live(pn_endpoint_t *endpoint) { |
| 920 |
switch (endpoint->type) { |
| 921 |
case CONNECTION: |
| 922 |
return pni_connection_live((pn_connection_t *)endpoint); |
| 923 |
case SESSION: |
| 924 |
return pni_session_live((pn_session_t *) endpoint); |
| 925 |
case SENDER: |
| 926 |
case RECEIVER: |
| 927 |
return pni_link_live((pn_link_t *) endpoint); |
| 928 |
default: |
| 929 |
assert(false); |
| 930 |
return false; |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
static bool pni_preserve_child(pn_endpoint_t *endpoint) |
| 935 |
{ |
| 936 |
pn_connection_t *conn = pni_ep_get_connection(endpoint); |
| 937 |
pn_endpoint_t *parent = pn_ep_parent(endpoint); |
| 938 |
if (pni_endpoint_live(parent) && (!endpoint->freed || (pn_ep_bound(endpoint))) |
| 939 |
&& endpoint->referenced) { |
| 940 |
pn_object_incref(endpoint); |
| 941 |
endpoint->referenced = false; |
| 942 |
pn_decref(parent); |
| 943 |
return true; |
| 944 |
} else { |
| 945 |
LL_REMOVE(conn, transport, endpoint); |
| 946 |
return false; |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
static void pn_session_finalize(void *object) |
| 951 |
{ |
| 952 |
pn_session_t *session = (pn_session_t *) object; |
| 953 |
pn_endpoint_t *endpoint = &session->endpoint; |
| 954 |
|
| 955 |
if (pni_preserve_child(endpoint)) { |
| 956 |
return; |
| 957 |
} |
| 958 |
|
| 959 |
pn_free(session->context); |
| 960 |
pni_free_children(session->links, session->freed); |
| 961 |
pni_endpoint_tini(endpoint); |
| 962 |
pn_delivery_map_free(&session->state.incoming); |
| 963 |
pn_delivery_map_free(&session->state.outgoing); |
| 964 |
pn_free(session->state.local_handles); |
| 965 |
pn_free(session->state.remote_handles); |
| 966 |
pni_remove_session(session->connection, session); |
| 967 |
pn_list_remove(session->connection->freed, session); |
| 968 |
|
| 969 |
if (session->connection->transport) { |
| 970 |
pn_transport_t *transport = session->connection->transport; |
| 971 |
pn_hash_del(transport->local_channels, session->state.local_channel); |
| 972 |
pn_hash_del(transport->remote_channels, session->state.remote_channel); |
| 973 |
} |
| 974 |
|
| 975 |
if (endpoint->referenced) { |
| 976 |
pn_decref(session->connection); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
#define pn_session_new pn_object_new |
| 981 |
#define pn_session_refcount pn_object_refcount |
| 982 |
#define pn_session_decref pn_object_decref |
| 983 |
#define pn_session_reify pn_object_reify |
| 984 |
#define pn_session_initialize NULL |
| 985 |
#define pn_session_hashcode NULL |
| 986 |
#define pn_session_compare NULL |
| 987 |
#define pn_session_inspect NULL |
| 988 |
|
| 989 |
pn_session_t *pn_session(pn_connection_t *conn) |
| 990 |
{ |
| 991 |
assert(conn); |
| 992 |
#define pn_session_free pn_object_free |
| 993 |
static const pn_class_t clazz = PN_METACLASS(pn_session); |
| 994 |
#undef pn_session_free |
| 995 |
pn_session_t *ssn = (pn_session_t *) pn_class_new(&clazz, sizeof(pn_session_t)); |
| 996 |
if (!ssn) return NULL; |
| 997 |
pn_endpoint_init(&ssn->endpoint, SESSION, conn); |
| 998 |
pni_add_session(conn, ssn); |
| 999 |
ssn->links = pn_list(PN_WEAKREF, 0); |
| 1000 |
ssn->freed = pn_list(PN_WEAKREF, 0); |
| 1001 |
ssn->context = pn_record(); |
| 1002 |
ssn->incoming_capacity = 0; |
| 1003 |
ssn->incoming_bytes = 0; |
| 1004 |
ssn->outgoing_bytes = 0; |
| 1005 |
ssn->incoming_deliveries = 0; |
| 1006 |
ssn->outgoing_deliveries = 0; |
| 1007 |
ssn->outgoing_window = AMQP_MAX_WINDOW_SIZE; |
| 1008 |
|
| 1009 |
// begin transport state |
| 1010 |
memset(&ssn->state, 0, sizeof(ssn->state)); |
| 1011 |
ssn->state.local_channel = (uint16_t)-1; |
| 1012 |
ssn->state.remote_channel = (uint16_t)-1; |
| 1013 |
pn_delivery_map_init(&ssn->state.incoming, 0); |
| 1014 |
pn_delivery_map_init(&ssn->state.outgoing, 0); |
| 1015 |
ssn->state.local_handles = pn_hash(PN_WEAKREF, 0, 0.75); |
| 1016 |
ssn->state.remote_handles = pn_hash(PN_WEAKREF, 0, 0.75); |
| 1017 |
// end transport state |
| 1018 |
|
| 1019 |
pn_collector_put(conn->collector, PN_OBJECT, ssn, PN_SESSION_INIT); |
| 1020 |
if (conn->transport) { |
| 1021 |
pni_session_bound(ssn); |
| 1022 |
} |
| 1023 |
pn_decref(ssn); |
| 1024 |
return ssn; |
| 1025 |
} |
| 1026 |
|
| 1027 |
static void pni_session_bound(pn_session_t *ssn) |
| 1028 |
{ |
| 1029 |
assert(ssn); |
| 1030 |
size_t nlinks = pn_list_size(ssn->links); |
| 1031 |
for (size_t i = 0; i < nlinks; i++) { |
| 1032 |
pni_link_bound((pn_link_t *) pn_list_get(ssn->links, i)); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
void pn_session_unbound(pn_session_t* ssn) |
| 1037 |
{ |
| 1038 |
assert(ssn); |
| 1039 |
ssn->state.local_channel = (uint16_t)-1; |
| 1040 |
ssn->state.remote_channel = (uint16_t)-1; |
| 1041 |
ssn->incoming_bytes = 0; |
| 1042 |
ssn->outgoing_bytes = 0; |
| 1043 |
ssn->incoming_deliveries = 0; |
| 1044 |
ssn->outgoing_deliveries = 0; |
| 1045 |
} |
| 1046 |
|
| 1047 |
size_t pn_session_get_incoming_capacity(pn_session_t *ssn) |
| 1048 |
{ |
| 1049 |
assert(ssn); |
| 1050 |
return ssn->incoming_capacity; |
| 1051 |
} |
| 1052 |
|
| 1053 |
void pn_session_set_incoming_capacity(pn_session_t *ssn, size_t capacity) |
| 1054 |
{ |
| 1055 |
assert(ssn); |
| 1056 |
// XXX: should this trigger a flow? |
| 1057 |
ssn->incoming_capacity = capacity; |
| 1058 |
} |
| 1059 |
|
| 1060 |
size_t pn_session_get_outgoing_window(pn_session_t *ssn) |
| 1061 |
{ |
| 1062 |
assert(ssn); |
| 1063 |
return ssn->outgoing_window; |
| 1064 |
} |
| 1065 |
|
| 1066 |
void pn_session_set_outgoing_window(pn_session_t *ssn, size_t window) |
| 1067 |
{ |
| 1068 |
assert(ssn); |
| 1069 |
ssn->outgoing_window = window; |
| 1070 |
} |
| 1071 |
|
| 1072 |
size_t pn_session_outgoing_bytes(pn_session_t *ssn) |
| 1073 |
{ |
| 1074 |
assert(ssn); |
| 1075 |
return ssn->outgoing_bytes; |
| 1076 |
} |
| 1077 |
|
| 1078 |
size_t pn_session_incoming_bytes(pn_session_t *ssn) |
| 1079 |
{ |
| 1080 |
assert(ssn); |
| 1081 |
return ssn->incoming_bytes; |
| 1082 |
} |
| 1083 |
|
| 1084 |
pn_state_t pn_session_state(pn_session_t *session) |
| 1085 |
{ |
| 1086 |
return session->endpoint.state; |
| 1087 |
} |
| 1088 |
|
| 1089 |
static void pni_terminus_init(pn_terminus_t *terminus, pn_terminus_type_t type) |
| 1090 |
{ |
| 1091 |
terminus->type = type; |
| 1092 |
terminus->address = pn_string(NULL); |
| 1093 |
terminus->durability = PN_NONDURABLE; |
| 1094 |
terminus->has_expiry_policy = false; |
| 1095 |
terminus->expiry_policy = PN_EXPIRE_WITH_SESSION; |
| 1096 |
terminus->timeout = 0; |
| 1097 |
terminus->dynamic = false; |
| 1098 |
terminus->distribution_mode = PN_DIST_MODE_UNSPECIFIED; |
| 1099 |
terminus->properties = pn_data(0); |
| 1100 |
terminus->capabilities = pn_data(0); |
| 1101 |
terminus->outcomes = pn_data(0); |
| 1102 |
terminus->filter = pn_data(0); |
| 1103 |
} |
| 1104 |
|
| 1105 |
static void pn_link_incref(void *object) |
| 1106 |
{ |
| 1107 |
pn_link_t *link = (pn_link_t *) object; |
| 1108 |
if (!link->endpoint.referenced) { |
| 1109 |
link->endpoint.referenced = true; |
| 1110 |
pn_incref(link->session); |
| 1111 |
} else { |
| 1112 |
pn_object_incref(object); |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
static void pn_link_finalize(void *object) |
| 1117 |
{ |
| 1118 |
pn_link_t *link = (pn_link_t *) object; |
| 1119 |
pn_endpoint_t *endpoint = &link->endpoint; |
| 1120 |
|
| 1121 |
if (pni_preserve_child(endpoint)) { |
| 1122 |
return; |
| 1123 |
} |
| 1124 |
|
| 1125 |
while (link->unsettled_head) { |
| 1126 |
assert(!link->unsettled_head->referenced); |
| 1127 |
pn_free(link->unsettled_head); |
| 1128 |
} |
| 1129 |
|
| 1130 |
pn_free(link->context); |
| 1131 |
pni_terminus_free(&link->source); |
| 1132 |
pni_terminus_free(&link->target); |
| 1133 |
pni_terminus_free(&link->remote_source); |
| 1134 |
pni_terminus_free(&link->remote_target); |
| 1135 |
pn_free(link->name); |
| 1136 |
pni_endpoint_tini(endpoint); |
| 1137 |
pni_remove_link(link->session, link); |
| 1138 |
pn_hash_del(link->session->state.local_handles, link->state.local_handle); |
| 1139 |
pn_hash_del(link->session->state.remote_handles, link->state.remote_handle); |
| 1140 |
pn_list_remove(link->session->freed, link); |
| 1141 |
if (endpoint->referenced) { |
| 1142 |
pn_decref(link->session); |
| 1143 |
} |
| 1144 |
pn_free(link->properties); |
| 1145 |
pn_free(link->remote_properties); |
| 1146 |
} |
| 1147 |
|
| 1148 |
#define pn_link_refcount pn_object_refcount |
| 1149 |
#define pn_link_decref pn_object_decref |
| 1150 |
#define pn_link_reify pn_object_reify |
| 1151 |
#define pn_link_initialize NULL |
| 1152 |
#define pn_link_hashcode NULL |
| 1153 |
#define pn_link_compare NULL |
| 1154 |
#define pn_link_inspect NULL |
| 1155 |
|
| 1156 |
pn_link_t *pn_link_new(int type, pn_session_t *session, const char *name) |
| 1157 |
{ |
| 1158 |
#define pn_link_new pn_object_new |
| 1159 |
#define pn_link_free pn_object_free |
| 1160 |
static const pn_class_t clazz = PN_METACLASS(pn_link); |
| 1161 |
#undef pn_link_new |
| 1162 |
#undef pn_link_free |
| 1163 |
pn_link_t *link = (pn_link_t *) pn_class_new(&clazz, sizeof(pn_link_t)); |
| 1164 |
|
| 1165 |
pn_endpoint_init(&link->endpoint, type, session->connection); |
| 1166 |
pni_add_link(session, link); |
| 1167 |
pn_incref(session); // keep session until link finalized |
| 1168 |
link->name = pn_string(name); |
| 1169 |
pni_terminus_init(&link->source, PN_SOURCE); |
| 1170 |
pni_terminus_init(&link->target, PN_TARGET); |
| 1171 |
pni_terminus_init(&link->remote_source, PN_UNSPECIFIED); |
| 1172 |
pni_terminus_init(&link->remote_target, PN_UNSPECIFIED); |
| 1173 |
link->unsettled_head = link->unsettled_tail = link->current = NULL; |
| 1174 |
link->unsettled_count = 0; |
| 1175 |
link->max_message_size = 0; |
| 1176 |
link->remote_max_message_size = 0; |
| 1177 |
link->available = 0; |
| 1178 |
link->credit = 0; |
| 1179 |
link->queued = 0; |
| 1180 |
link->more_id = 0; |
| 1181 |
link->drain = false; |
| 1182 |
link->drain_flag_mode = true; |
| 1183 |
link->drained = 0; |
| 1184 |
link->context = pn_record(); |
| 1185 |
link->snd_settle_mode = PN_SND_MIXED; |
| 1186 |
link->rcv_settle_mode = PN_RCV_FIRST; |
| 1187 |
link->remote_snd_settle_mode = PN_SND_MIXED; |
| 1188 |
link->remote_rcv_settle_mode = PN_RCV_FIRST; |
| 1189 |
link->detached = false; |
| 1190 |
link->more_pending = false; |
| 1191 |
link->properties = 0; |
| 1192 |
link->remote_properties = 0; |
| 1193 |
|
| 1194 |
// begin transport state |
| 1195 |
link->state.local_handle = -1; |
| 1196 |
link->state.remote_handle = -1; |
| 1197 |
link->state.delivery_count = 0; |
| 1198 |
link->state.link_credit = 0; |
| 1199 |
// end transport state |
| 1200 |
|
| 1201 |
pn_collector_put(session->connection->collector, PN_OBJECT, link, PN_LINK_INIT); |
| 1202 |
if (session->connection->transport) { |
| 1203 |
pni_link_bound(link); |
| 1204 |
} |
| 1205 |
pn_decref(link); |
| 1206 |
return link; |
| 1207 |
} |
| 1208 |
|
| 1209 |
static void pni_link_bound(pn_link_t *link) |
| 1210 |
{ |
| 1211 |
} |
| 1212 |
|
| 1213 |
void pn_link_unbound(pn_link_t* link) |
| 1214 |
{ |
| 1215 |
assert(link); |
| 1216 |
link->state.local_handle = -1; |
| 1217 |
link->state.remote_handle = -1; |
| 1218 |
link->state.delivery_count = 0; |
| 1219 |
link->state.link_credit = 0; |
| 1220 |
} |
| 1221 |
|
| 1222 |
pn_terminus_t *pn_link_source(pn_link_t *link) |
| 1223 |
{ |
| 1224 |
return link ? &link->source : NULL; |
| 1225 |
} |
| 1226 |
|
| 1227 |
pn_terminus_t *pn_link_target(pn_link_t *link) |
| 1228 |
{ |
| 1229 |
return link ? &link->target : NULL; |
| 1230 |
} |
| 1231 |
|
| 1232 |
pn_terminus_t *pn_link_remote_source(pn_link_t *link) |
| 1233 |
{ |
| 1234 |
return link ? &link->remote_source : NULL; |
| 1235 |
} |
| 1236 |
|
| 1237 |
pn_terminus_t *pn_link_remote_target(pn_link_t *link) |
| 1238 |
{ |
| 1239 |
return link ? &link->remote_target : NULL; |
| 1240 |
} |
| 1241 |
|
| 1242 |
int pn_terminus_set_type(pn_terminus_t *terminus, pn_terminus_type_t type) |
| 1243 |
{ |
| 1244 |
if (!terminus) return PN_ARG_ERR; |
| 1245 |
terminus->type = type; |
| 1246 |
return 0; |
| 1247 |
} |
| 1248 |
|
| 1249 |
pn_terminus_type_t pn_terminus_get_type(pn_terminus_t *terminus) |
| 1250 |
{ |
| 1251 |
return (pn_terminus_type_t) (terminus ? terminus->type : 0); |
| 1252 |
} |
| 1253 |
|
| 1254 |
const char *pn_terminus_get_address(pn_terminus_t *terminus) |
| 1255 |
{ |
| 1256 |
assert(terminus); |
| 1257 |
return pn_string_get(terminus->address); |
| 1258 |
} |
| 1259 |
|
| 1260 |
int pn_terminus_set_address(pn_terminus_t *terminus, const char *address) |
| 1261 |
{ |
| 1262 |
assert(terminus); |
| 1263 |
return pn_string_set(terminus->address, address); |
| 1264 |
} |
| 1265 |
|
| 1266 |
pn_durability_t pn_terminus_get_durability(pn_terminus_t *terminus) |
| 1267 |
{ |
| 1268 |
return (pn_durability_t) (terminus ? terminus->durability : 0); |
| 1269 |
} |
| 1270 |
|
| 1271 |
int pn_terminus_set_durability(pn_terminus_t *terminus, pn_durability_t durability) |
| 1272 |
{ |
| 1273 |
if (!terminus) return PN_ARG_ERR; |
| 1274 |
terminus->durability = durability; |
| 1275 |
return 0; |
| 1276 |
} |
| 1277 |
|
| 1278 |
pn_expiry_policy_t pn_terminus_get_expiry_policy(pn_terminus_t *terminus) |
| 1279 |
{ |
| 1280 |
return (pn_expiry_policy_t) (terminus ? terminus->expiry_policy : 0); |
| 1281 |
} |
| 1282 |
|
| 1283 |
bool pn_terminus_has_expiry_policy(const pn_terminus_t *terminus) |
| 1284 |
{ |
| 1285 |
return terminus && terminus->has_expiry_policy; |
| 1286 |
} |
| 1287 |
|
| 1288 |
int pn_terminus_set_expiry_policy(pn_terminus_t *terminus, pn_expiry_policy_t expiry_policy) |
| 1289 |
{ |
| 1290 |
if (!terminus) return PN_ARG_ERR; |
| 1291 |
terminus->expiry_policy = expiry_policy; |
| 1292 |
terminus->has_expiry_policy = true; |
| 1293 |
return 0; |
| 1294 |
} |
| 1295 |
|
| 1296 |
pn_seconds_t pn_terminus_get_timeout(pn_terminus_t *terminus) |
| 1297 |
{ |
| 1298 |
return terminus ? terminus->timeout : 0; |
| 1299 |
} |
| 1300 |
|
| 1301 |
int pn_terminus_set_timeout(pn_terminus_t *terminus, pn_seconds_t timeout) |
| 1302 |
{ |
| 1303 |
if (!terminus) return PN_ARG_ERR; |
| 1304 |
terminus->timeout = timeout; |
| 1305 |
return 0; |
| 1306 |
} |
| 1307 |
|
| 1308 |
bool pn_terminus_is_dynamic(pn_terminus_t *terminus) |
| 1309 |
{ |
| 1310 |
return terminus ? terminus->dynamic : false; |
| 1311 |
} |
| 1312 |
|
| 1313 |
int pn_terminus_set_dynamic(pn_terminus_t *terminus, bool dynamic) |
| 1314 |
{ |
| 1315 |
if (!terminus) return PN_ARG_ERR; |
| 1316 |
terminus->dynamic = dynamic; |
| 1317 |
return 0; |
| 1318 |
} |
| 1319 |
|
| 1320 |
pn_data_t *pn_terminus_properties(pn_terminus_t *terminus) |
| 1321 |
{ |
| 1322 |
return terminus ? terminus->properties : NULL; |
| 1323 |
} |
| 1324 |
|
| 1325 |
pn_data_t *pn_terminus_capabilities(pn_terminus_t *terminus) |
| 1326 |
{ |
| 1327 |
return terminus ? terminus->capabilities : NULL; |
| 1328 |
} |
| 1329 |
|
| 1330 |
pn_data_t *pn_terminus_outcomes(pn_terminus_t *terminus) |
| 1331 |
{ |
| 1332 |
return terminus ? terminus->outcomes : NULL; |
| 1333 |
} |
| 1334 |
|
| 1335 |
pn_data_t *pn_terminus_filter(pn_terminus_t *terminus) |
| 1336 |
{ |
| 1337 |
return terminus ? terminus->filter : NULL; |
| 1338 |
} |
| 1339 |
|
| 1340 |
pn_distribution_mode_t pn_terminus_get_distribution_mode(const pn_terminus_t *terminus) |
| 1341 |
{ |
| 1342 |
return terminus ? (pn_distribution_mode_t) terminus->distribution_mode : PN_DIST_MODE_UNSPECIFIED; |
| 1343 |
} |
| 1344 |
|
| 1345 |
int pn_terminus_set_distribution_mode(pn_terminus_t *terminus, pn_distribution_mode_t m) |
| 1346 |
{ |
| 1347 |
if (!terminus) return PN_ARG_ERR; |
| 1348 |
terminus->distribution_mode = m; |
| 1349 |
return 0; |
| 1350 |
} |
| 1351 |
|
| 1352 |
int pn_terminus_copy(pn_terminus_t *terminus, pn_terminus_t *src) |
| 1353 |
{ |
| 1354 |
if (!terminus || !src) { |
| 1355 |
return PN_ARG_ERR; |
| 1356 |
} |
| 1357 |
|
| 1358 |
terminus->type = src->type; |
| 1359 |
int err = pn_terminus_set_address(terminus, pn_terminus_get_address(src)); |
| 1360 |
if (err) return err; |
| 1361 |
terminus->durability = src->durability; |
| 1362 |
terminus->has_expiry_policy = src->has_expiry_policy; |
| 1363 |
terminus->expiry_policy = src->expiry_policy; |
| 1364 |
terminus->timeout = src->timeout; |
| 1365 |
terminus->dynamic = src->dynamic; |
| 1366 |
terminus->distribution_mode = src->distribution_mode; |
| 1367 |
err = pn_data_copy(terminus->properties, src->properties); |
| 1368 |
if (err) return err; |
| 1369 |
err = pn_data_copy(terminus->capabilities, src->capabilities); |
| 1370 |
if (err) return err; |
| 1371 |
err = pn_data_copy(terminus->outcomes, src->outcomes); |
| 1372 |
if (err) return err; |
| 1373 |
err = pn_data_copy(terminus->filter, src->filter); |
| 1374 |
if (err) return err; |
| 1375 |
return 0; |
| 1376 |
} |
| 1377 |
|
| 1378 |
pn_link_t *pn_sender(pn_session_t *session, const char *name) |
| 1379 |
{ |
| 1380 |
return pn_link_new(SENDER, session, name); |
| 1381 |
} |
| 1382 |
|
| 1383 |
pn_link_t *pn_receiver(pn_session_t *session, const char *name) |
| 1384 |
{ |
| 1385 |
return pn_link_new(RECEIVER, session, name); |
| 1386 |
} |
| 1387 |
|
| 1388 |
pn_state_t pn_link_state(pn_link_t *link) |
| 1389 |
{ |
| 1390 |
return link->endpoint.state; |
| 1391 |
} |
| 1392 |
|
| 1393 |
const char *pn_link_name(pn_link_t *link) |
| 1394 |
{ |
| 1395 |
assert(link); |
| 1396 |
return pn_string_get(link->name); |
| 1397 |
} |
| 1398 |
|
| 1399 |
bool pn_link_is_sender(pn_link_t *link) |
| 1400 |
{ |
| 1401 |
return link->endpoint.type == SENDER; |
| 1402 |
} |
| 1403 |
|
| 1404 |
bool pn_link_is_receiver(pn_link_t *link) |
| 1405 |
{ |
| 1406 |
return link->endpoint.type == RECEIVER; |
| 1407 |
} |
| 1408 |
|
| 1409 |
pn_session_t *pn_link_session(pn_link_t *link) |
| 1410 |
{ |
| 1411 |
assert(link); |
| 1412 |
return link->session; |
| 1413 |
} |
| 1414 |
|
| 1415 |
static void pn_disposition_finalize(pn_disposition_t *ds) |
| 1416 |
{ |
| 1417 |
pn_free(ds->data); |
| 1418 |
pn_free(ds->annotations); |
| 1419 |
pn_condition_tini(&ds->condition); |
| 1420 |
} |
| 1421 |
|
| 1422 |
static void pn_delivery_incref(void *object) |
| 1423 |
{ |
| 1424 |
pn_delivery_t *delivery = (pn_delivery_t *) object; |
| 1425 |
if (delivery->link && !delivery->referenced) { |
| 1426 |
delivery->referenced = true; |
| 1427 |
pn_incref(delivery->link); |
| 1428 |
} else { |
| 1429 |
pn_object_incref(object); |
| 1430 |
} |
| 1431 |
} |
| 1432 |
|
| 1433 |
static bool pni_preserve_delivery(pn_delivery_t *delivery) |
| 1434 |
{ |
| 1435 |
pn_connection_t *conn = delivery->link->session->connection; |
| 1436 |
return !delivery->local.settled || (conn->transport && (delivery->state.init || delivery->tpwork)); |
| 1437 |
} |
| 1438 |
|
| 1439 |
static void pn_delivery_finalize(void *object) |
| 1440 |
{ |
| 1441 |
pn_delivery_t *delivery = (pn_delivery_t *) object; |
| 1442 |
pn_link_t *link = delivery->link; |
| 1443 |
// assert(!delivery->state.init); |
| 1444 |
|
| 1445 |
bool pooled = false; |
| 1446 |
bool referenced = true; |
| 1447 |
if (link) { |
| 1448 |
if (pni_link_live(link) && pni_preserve_delivery(delivery) && delivery->referenced) { |
| 1449 |
delivery->referenced = false; |
| 1450 |
pn_object_incref(delivery); |
| 1451 |
pn_decref(link); |
| 1452 |
return; |
| 1453 |
} |
| 1454 |
referenced = delivery->referenced; |
| 1455 |
|
| 1456 |
pn_clear_tpwork(delivery); |
| 1457 |
LL_REMOVE(link, unsettled, delivery); |
| 1458 |
pn_delivery_map_del(pn_link_is_sender(link) |
| 1459 |
? &link->session->state.outgoing |
| 1460 |
: &link->session->state.incoming, |
| 1461 |
delivery); |
| 1462 |
pn_buffer_clear(delivery->tag); |
| 1463 |
pn_buffer_clear(delivery->bytes); |
| 1464 |
pn_record_clear(delivery->context); |
| 1465 |
delivery->settled = true; |
| 1466 |
pn_connection_t *conn = link->session->connection; |
| 1467 |
assert(pn_refcount(delivery) == 0); |
| 1468 |
if (pni_connection_live(conn)) { |
| 1469 |
pn_list_t *pool = link->session->connection->delivery_pool; |
| 1470 |
delivery->link = NULL; |
| 1471 |
pn_list_add(pool, delivery); |
| 1472 |
pooled = true; |
| 1473 |
assert(pn_refcount(delivery) == 1); |
| 1474 |
} |
| 1475 |
} |
| 1476 |
|
| 1477 |
if (!pooled) { |
| 1478 |
pn_free(delivery->context); |
| 1479 |
pn_buffer_free(delivery->tag); |
| 1480 |
pn_buffer_free(delivery->bytes); |
| 1481 |
pn_disposition_finalize(&delivery->local); |
| 1482 |
pn_disposition_finalize(&delivery->remote); |
| 1483 |
} |
| 1484 |
|
| 1485 |
if (referenced) { |
| 1486 |
pn_decref(link); |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
static void pn_disposition_init(pn_disposition_t *ds) |
| 1491 |
{ |
| 1492 |
ds->data = pn_data(0); |
| 1493 |
ds->annotations = pn_data(0); |
| 1494 |
pn_condition_init(&ds->condition); |
| 1495 |
} |
| 1496 |
|
| 1497 |
static void pn_disposition_clear(pn_disposition_t *ds) |
| 1498 |
{ |
| 1499 |
ds->type = 0; |
| 1500 |
ds->section_number = 0; |
| 1501 |
ds->section_offset = 0; |
| 1502 |
ds->failed = false; |
| 1503 |
ds->undeliverable = false; |
| 1504 |
ds->settled = false; |
| 1505 |
pn_data_clear(ds->data); |
| 1506 |
pn_data_clear(ds->annotations); |
| 1507 |
pn_condition_clear(&ds->condition); |
| 1508 |
} |
| 1509 |
|
| 1510 |
#define pn_delivery_new pn_object_new |
| 1511 |
#define pn_delivery_refcount pn_object_refcount |
| 1512 |
#define pn_delivery_decref pn_object_decref |
| 1513 |
#define pn_delivery_free pn_object_free |
| 1514 |
#define pn_delivery_reify pn_object_reify |
| 1515 |
#define pn_delivery_initialize NULL |
| 1516 |
#define pn_delivery_hashcode NULL |
| 1517 |
#define pn_delivery_compare NULL |
| 1518 |
|
| 1519 |
int pn_delivery_inspect(void *obj, pn_string_t *dst) { |
| 1520 |
pn_delivery_t *d = (pn_delivery_t*)obj; |
| 1521 |
const char* dir = pn_link_is_sender(d->link) ? "sending" : "receiving"; |
| 1522 |
pn_bytes_t bytes = pn_buffer_bytes(d->tag); |
| 1523 |
int err = |
| 1524 |
pn_string_addf(dst, "pn_delivery<%p>{%s, tag=b\"", obj, dir) || |
| 1525 |
pn_quote(dst, bytes.start, bytes.size) || |
| 1526 |
pn_string_addf(dst, "\", local=%s, remote=%s}", |
| 1527 |
pn_disposition_type_name(d->local.type), |
| 1528 |
pn_disposition_type_name(d->remote.type)); |
| 1529 |
return err; |
| 1530 |
} |
| 1531 |
|
| 1532 |
pn_delivery_tag_t pn_dtag(const char *bytes, size_t size) { |
| 1533 |
pn_delivery_tag_t dtag = {size, bytes}; |
| 1534 |
return dtag; |
| 1535 |
} |
| 1536 |
|
| 1537 |
pn_delivery_t *pn_delivery(pn_link_t *link, pn_delivery_tag_t tag) |
| 1538 |
{ |
| 1539 |
assert(link); |
| 1540 |
pn_list_t *pool = link->session->connection->delivery_pool; |
| 1541 |
pn_delivery_t *delivery = (pn_delivery_t *) pn_list_pop(pool); |
| 1542 |
if (!delivery) { |
| 1543 |
static const pn_class_t clazz = PN_METACLASS(pn_delivery); |
| 1544 |
delivery = (pn_delivery_t *) pn_class_new(&clazz, sizeof(pn_delivery_t)); |
| 1545 |
if (!delivery) return NULL; |
| 1546 |
delivery->tag = pn_buffer(16); |
| 1547 |
delivery->bytes = pn_buffer(64); |
| 1548 |
pn_disposition_init(&delivery->local); |
| 1549 |
pn_disposition_init(&delivery->remote); |
| 1550 |
delivery->context = pn_record(); |
| 1551 |
} else { |
| 1552 |
assert(!delivery->state.init); |
| 1553 |
} |
| 1554 |
delivery->link = link; |
| 1555 |
pn_incref(delivery->link); // keep link until finalized |
| 1556 |
pn_buffer_clear(delivery->tag); |
| 1557 |
pn_buffer_append(delivery->tag, tag.start, tag.size); |
| 1558 |
pn_disposition_clear(&delivery->local); |
| 1559 |
pn_disposition_clear(&delivery->remote); |
| 1560 |
delivery->updated = false; |
| 1561 |
delivery->settled = false; |
| 1562 |
LL_ADD(link, unsettled, delivery); |
| 1563 |
delivery->referenced = true; |
| 1564 |
delivery->work_next = NULL; |
| 1565 |
delivery->work_prev = NULL; |
| 1566 |
delivery->work = false; |
| 1567 |
delivery->tpwork_next = NULL; |
| 1568 |
delivery->tpwork_prev = NULL; |
| 1569 |
delivery->tpwork = false; |
| 1570 |
pn_buffer_clear(delivery->bytes); |
| 1571 |
delivery->done = false; |
| 1572 |
delivery->aborted = false; |
| 1573 |
pn_record_clear(delivery->context); |
| 1574 |
|
| 1575 |
// begin delivery state |
| 1576 |
delivery->state.init = false; |
| 1577 |
delivery->state.sending = false; /* True if we have sent at least 1 frame */ |
| 1578 |
delivery->state.sent = false; /* True if we have sent the entire delivery */ |
| 1579 |
// end delivery state |
| 1580 |
|
| 1581 |
if (!link->current) |
| 1582 |
link->current = delivery; |
| 1583 |
|
| 1584 |
link->unsettled_count++; |
| 1585 |
|
| 1586 |
pn_work_update(link->session->connection, delivery); |
| 1587 |
|
| 1588 |
// XXX: could just remove incref above |
| 1589 |
pn_decref(delivery); |
| 1590 |
|
| 1591 |
return delivery; |
| 1592 |
} |
| 1593 |
|
| 1594 |
bool pn_delivery_buffered(pn_delivery_t *delivery) |
| 1595 |
{ |
| 1596 |
assert(delivery); |
| 1597 |
if (delivery->settled) return false; |
| 1598 |
if (pn_link_is_sender(delivery->link)) { |
| 1599 |
pn_delivery_state_t *state = &delivery->state; |
| 1600 |
if (state->sent) { |
| 1601 |
return false; |
| 1602 |
} else { |
| 1603 |
return delivery->done || (pn_buffer_size(delivery->bytes) > 0); |
| 1604 |
} |
| 1605 |
} else { |
| 1606 |
return false; |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
int pn_link_unsettled(pn_link_t *link) |
| 1611 |
{ |
| 1612 |
return link->unsettled_count; |
| 1613 |
} |
| 1614 |
|
| 1615 |
pn_delivery_t *pn_unsettled_head(pn_link_t *link) |
| 1616 |
{ |
| 1617 |
pn_delivery_t *d = link->unsettled_head; |
| 1618 |
while (d && d->local.settled) { |
| 1619 |
d = d->unsettled_next; |
| 1620 |
} |
| 1621 |
return d; |
| 1622 |
} |
| 1623 |
|
| 1624 |
pn_delivery_t *pn_unsettled_next(pn_delivery_t *delivery) |
| 1625 |
{ |
| 1626 |
pn_delivery_t *d = delivery->unsettled_next; |
| 1627 |
while (d && d->local.settled) { |
| 1628 |
d = d->unsettled_next; |
| 1629 |
} |
| 1630 |
return d; |
| 1631 |
} |
| 1632 |
|
| 1633 |
bool pn_delivery_current(pn_delivery_t *delivery) |
| 1634 |
{ |
| 1635 |
pn_link_t *link = delivery->link; |
| 1636 |
return pn_link_current(link) == delivery; |
| 1637 |
} |
| 1638 |
|
| 1639 |
void pn_delivery_dump(pn_delivery_t *d) |
| 1640 |
{ |
| 1641 |
char tag[1024]; |
| 1642 |
pn_bytes_t bytes = pn_buffer_bytes(d->tag); |
| 1643 |
pn_quote_data(tag, 1024, bytes.start, bytes.size); |
| 1644 |
printf("{tag=%s, local.type=%" PRIu64 ", remote.type=%" PRIu64 ", local.settled=%u, " |
| 1645 |
"remote.settled=%u, updated=%u, current=%u, writable=%u, readable=%u, " |
| 1646 |
"work=%u}", |
| 1647 |
tag, d->local.type, d->remote.type, d->local.settled, |
| 1648 |
d->remote.settled, d->updated, pn_delivery_current(d), |
| 1649 |
pn_delivery_writable(d), pn_delivery_readable(d), d->work); |
| 1650 |
} |
| 1651 |
|
| 1652 |
void *pn_delivery_get_context(pn_delivery_t *delivery) |
| 1653 |
{ |
| 1654 |
assert(delivery); |
| 1655 |
return pn_record_get(delivery->context, PN_LEGCTX); |
| 1656 |
} |
| 1657 |
|
| 1658 |
void pn_delivery_set_context(pn_delivery_t *delivery, void *context) |
| 1659 |
{ |
| 1660 |
assert(delivery); |
| 1661 |
pn_record_set(delivery->context, PN_LEGCTX, context); |
| 1662 |
} |
| 1663 |
|
| 1664 |
pn_record_t *pn_delivery_attachments(pn_delivery_t *delivery) |
| 1665 |
{ |
| 1666 |
assert(delivery); |
| 1667 |
return delivery->context; |
| 1668 |
} |
| 1669 |
|
| 1670 |
uint64_t pn_disposition_type(pn_disposition_t *disposition) |
| 1671 |
{ |
| 1672 |
assert(disposition); |
| 1673 |
return disposition->type; |
| 1674 |
} |
| 1675 |
|
| 1676 |
pn_data_t *pn_disposition_data(pn_disposition_t *disposition) |
| 1677 |
{ |
| 1678 |
assert(disposition); |
| 1679 |
return disposition->data; |
| 1680 |
} |
| 1681 |
|
| 1682 |
uint32_t pn_disposition_get_section_number(pn_disposition_t *disposition) |
| 1683 |
{ |
| 1684 |
assert(disposition); |
| 1685 |
return disposition->section_number; |
| 1686 |
} |
| 1687 |
|
| 1688 |
void pn_disposition_set_section_number(pn_disposition_t *disposition, uint32_t section_number) |
| 1689 |
{ |
| 1690 |
assert(disposition); |
| 1691 |
disposition->section_number = section_number; |
| 1692 |
} |
| 1693 |
|
| 1694 |
uint64_t pn_disposition_get_section_offset(pn_disposition_t *disposition) |
| 1695 |
{ |
| 1696 |
assert(disposition); |
| 1697 |
return disposition->section_offset; |
| 1698 |
} |
| 1699 |
|
| 1700 |
void pn_disposition_set_section_offset(pn_disposition_t *disposition, uint64_t section_offset) |
| 1701 |
{ |
| 1702 |
assert(disposition); |
| 1703 |
disposition->section_offset = section_offset; |
| 1704 |
} |
| 1705 |
|
| 1706 |
bool pn_disposition_is_failed(pn_disposition_t *disposition) |
| 1707 |
{ |
| 1708 |
assert(disposition); |
| 1709 |
return disposition->failed; |
| 1710 |
} |
| 1711 |
|
| 1712 |
void pn_disposition_set_failed(pn_disposition_t *disposition, bool failed) |
| 1713 |
{ |
| 1714 |
assert(disposition); |
| 1715 |
disposition->failed = failed; |
| 1716 |
} |
| 1717 |
|
| 1718 |
bool pn_disposition_is_undeliverable(pn_disposition_t *disposition) |
| 1719 |
{ |
| 1720 |
assert(disposition); |
| 1721 |
return disposition->undeliverable; |
| 1722 |
} |
| 1723 |
|
| 1724 |
void pn_disposition_set_undeliverable(pn_disposition_t *disposition, bool undeliverable) |
| 1725 |
{ |
| 1726 |
assert(disposition); |
| 1727 |
disposition->undeliverable = undeliverable; |
| 1728 |
} |
| 1729 |
|
| 1730 |
pn_data_t *pn_disposition_annotations(pn_disposition_t *disposition) |
| 1731 |
{ |
| 1732 |
assert(disposition); |
| 1733 |
return disposition->annotations; |
| 1734 |
} |
| 1735 |
|
| 1736 |
pn_condition_t *pn_disposition_condition(pn_disposition_t *disposition) |
| 1737 |
{ |
| 1738 |
assert(disposition); |
| 1739 |
return &disposition->condition; |
| 1740 |
} |
| 1741 |
|
| 1742 |
pn_delivery_tag_t pn_delivery_tag(pn_delivery_t *delivery) |
| 1743 |
{ |
| 1744 |
if (delivery) { |
| 1745 |
pn_bytes_t tag = pn_buffer_bytes(delivery->tag); |
| 1746 |
return pn_dtag(tag.start, tag.size); |
| 1747 |
} else { |
| 1748 |
return pn_dtag(0, 0); |
| 1749 |
} |
| 1750 |
} |
| 1751 |
|
| 1752 |
pn_delivery_t *pn_link_current(pn_link_t *link) |
| 1753 |
{ |
| 1754 |
if (!link) return NULL; |
| 1755 |
return link->current; |
| 1756 |
} |
| 1757 |
|
| 1758 |
static void pni_advance_sender(pn_link_t *link) |
| 1759 |
{ |
| 1760 |
link->current->done = true; |
| 1761 |
/* Skip accounting if the link is aborted and has not sent any frames. |
| 1762 |
A delivery that was aborted before sending the first frame was not accounted |
| 1763 |
for in pni_process_tpwork_sender() so we don't need to account for it being sent here. |
| 1764 |
*/ |
| 1765 |
bool skip = link->current->aborted && !link->current->state.sending; |
| 1766 |
if (!skip) { |
| 1767 |
link->queued++; |
| 1768 |
link->credit--; |
| 1769 |
link->session->outgoing_deliveries++; |
| 1770 |
} |
| 1771 |
pni_add_tpwork(link->current); |
| 1772 |
link->current = link->current->unsettled_next; |
| 1773 |
} |
| 1774 |
|
| 1775 |
static void pni_advance_receiver(pn_link_t *link) |
| 1776 |
{ |
| 1777 |
link->credit--; |
| 1778 |
link->queued--; |
| 1779 |
link->session->incoming_deliveries--; |
| 1780 |
|
| 1781 |
pn_delivery_t *current = link->current; |
| 1782 |
link->session->incoming_bytes -= pn_buffer_size(current->bytes); |
| 1783 |
pn_buffer_clear(current->bytes); |
| 1784 |
|
| 1785 |
if (!link->session->state.incoming_window) { |
| 1786 |
pni_add_tpwork(current); |
| 1787 |
} |
| 1788 |
|
| 1789 |
link->current = link->current->unsettled_next; |
| 1790 |
} |
| 1791 |
|
| 1792 |
bool pn_link_advance(pn_link_t *link) |
| 1793 |
{ |
| 1794 |
if (link && link->current) { |
| 1795 |
pn_delivery_t *prev = link->current; |
| 1796 |
if (link->endpoint.type == SENDER) { |
| 1797 |
pni_advance_sender(link); |
| 1798 |
} else { |
| 1799 |
pni_advance_receiver(link); |
| 1800 |
} |
| 1801 |
pn_delivery_t *next = link->current; |
| 1802 |
pn_work_update(link->session->connection, prev); |
| 1803 |
if (next) pn_work_update(link->session->connection, next); |
| 1804 |
return prev != next; |
| 1805 |
} else { |
| 1806 |
return false; |
| 1807 |
} |
| 1808 |
} |
| 1809 |
|
| 1810 |
int pn_link_credit(pn_link_t *link) |
| 1811 |
{ |
| 1812 |
return link ? link->credit : 0; |
| 1813 |
} |
| 1814 |
|
| 1815 |
int pn_link_available(pn_link_t *link) |
| 1816 |
{ |
| 1817 |
return link ? link->available : 0; |
| 1818 |
} |
| 1819 |
|
| 1820 |
int pn_link_queued(pn_link_t *link) |
| 1821 |
{ |
| 1822 |
return link ? link->queued : 0; |
| 1823 |
} |
| 1824 |
|
| 1825 |
int pn_link_remote_credit(pn_link_t *link) |
| 1826 |
{ |
| 1827 |
assert(link); |
| 1828 |
return link->credit - link->queued; |
| 1829 |
} |
| 1830 |
|
| 1831 |
bool pn_link_get_drain(pn_link_t *link) |
| 1832 |
{ |
| 1833 |
assert(link); |
| 1834 |
return link->drain; |
| 1835 |
} |
| 1836 |
|
| 1837 |
pn_snd_settle_mode_t pn_link_snd_settle_mode(pn_link_t *link) |
| 1838 |
{ |
| 1839 |
return link ? (pn_snd_settle_mode_t)link->snd_settle_mode |
| 1840 |
: PN_SND_MIXED; |
| 1841 |
} |
| 1842 |
|
| 1843 |
pn_rcv_settle_mode_t pn_link_rcv_settle_mode(pn_link_t *link) |
| 1844 |
{ |
| 1845 |
return link ? (pn_rcv_settle_mode_t)link->rcv_settle_mode |
| 1846 |
: PN_RCV_FIRST; |
| 1847 |
} |
| 1848 |
|
| 1849 |
pn_snd_settle_mode_t pn_link_remote_snd_settle_mode(pn_link_t *link) |
| 1850 |
{ |
| 1851 |
return link ? (pn_snd_settle_mode_t)link->remote_snd_settle_mode |
| 1852 |
: PN_SND_MIXED; |
| 1853 |
} |
| 1854 |
|
| 1855 |
pn_rcv_settle_mode_t pn_link_remote_rcv_settle_mode(pn_link_t *link) |
| 1856 |
{ |
| 1857 |
return link ? (pn_rcv_settle_mode_t)link->remote_rcv_settle_mode |
| 1858 |
: PN_RCV_FIRST; |
| 1859 |
} |
| 1860 |
void pn_link_set_snd_settle_mode(pn_link_t *link, pn_snd_settle_mode_t mode) |
| 1861 |
{ |
| 1862 |
if (link) |
| 1863 |
link->snd_settle_mode = (uint8_t)mode; |
| 1864 |
} |
| 1865 |
void pn_link_set_rcv_settle_mode(pn_link_t *link, pn_rcv_settle_mode_t mode) |
| 1866 |
{ |
| 1867 |
if (link) |
| 1868 |
link->rcv_settle_mode = (uint8_t)mode; |
| 1869 |
} |
| 1870 |
|
| 1871 |
void pn_delivery_settle(pn_delivery_t *delivery) |
| 1872 |
{ |
| 1873 |
assert(delivery); |
| 1874 |
if (!delivery->local.settled) { |
| 1875 |
pn_link_t *link = delivery->link; |
| 1876 |
if (pn_delivery_current(delivery)) { |
| 1877 |
pn_link_advance(link); |
| 1878 |
} |
| 1879 |
|
| 1880 |
link->unsettled_count--; |
| 1881 |
delivery->local.settled = true; |
| 1882 |
pni_add_tpwork(delivery); |
| 1883 |
pn_work_update(delivery->link->session->connection, delivery); |
| 1884 |
pn_incref(delivery); |
| 1885 |
pn_decref(delivery); |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
void pn_link_offered(pn_link_t *sender, int credit) |
| 1890 |
{ |
| 1891 |
sender->available = credit; |
| 1892 |
} |
| 1893 |
|
| 1894 |
ssize_t pn_link_send(pn_link_t *sender, const char *bytes, size_t n) |
| 1895 |
{ |
| 1896 |
pn_delivery_t *current = pn_link_current(sender); |
| 1897 |
if (!current) return PN_EOS; |
| 1898 |
if (!bytes || !n) return 0; |
| 1899 |
pn_buffer_append(current->bytes, bytes, n); |
| 1900 |
sender->session->outgoing_bytes += n; |
| 1901 |
pni_add_tpwork(current); |
| 1902 |
return n; |
| 1903 |
} |
| 1904 |
|
| 1905 |
int pn_link_drained(pn_link_t *link) |
| 1906 |
{ |
| 1907 |
assert(link); |
| 1908 |
int drained = 0; |
| 1909 |
|
| 1910 |
if (pn_link_is_sender(link)) { |
| 1911 |
if (link->drain && link->credit > 0) { |
| 1912 |
link->drained = link->credit; |
| 1913 |
link->credit = 0; |
| 1914 |
pn_modified(link->session->connection, &link->endpoint, true); |
| 1915 |
drained = link->drained; |
| 1916 |
} |
| 1917 |
} else { |
| 1918 |
drained = link->drained; |
| 1919 |
link->drained = 0; |
| 1920 |
} |
| 1921 |
|
| 1922 |
return drained; |
| 1923 |
} |
| 1924 |
|
| 1925 |
ssize_t pn_link_recv(pn_link_t *receiver, char *bytes, size_t n) |
| 1926 |
{ |
| 1927 |
if (!receiver) return PN_ARG_ERR; |
| 1928 |
pn_delivery_t *delivery = receiver->current; |
| 1929 |
if (!delivery) return PN_STATE_ERR; |
| 1930 |
if (delivery->aborted) return PN_ABORTED; |
| 1931 |
size_t size = pn_buffer_get(delivery->bytes, 0, n, bytes); |
| 1932 |
pn_buffer_trim(delivery->bytes, size, 0); |
| 1933 |
if (size) { |
| 1934 |
receiver->session->incoming_bytes -= size; |
| 1935 |
if (!receiver->session->state.incoming_window) { |
| 1936 |
pni_add_tpwork(delivery); |
| 1937 |
} |
| 1938 |
return size; |
| 1939 |
} else { |
| 1940 |
return delivery->done ? PN_EOS : 0; |
| 1941 |
} |
| 1942 |
} |
| 1943 |
|
| 1944 |
|
| 1945 |
void pn_link_flow(pn_link_t *receiver, int credit) |
| 1946 |
{ |
| 1947 |
assert(receiver); |
| 1948 |
assert(pn_link_is_receiver(receiver)); |
| 1949 |
receiver->credit += credit; |
| 1950 |
pn_modified(receiver->session->connection, &receiver->endpoint, true); |
| 1951 |
if (!receiver->drain_flag_mode) { |
| 1952 |
pn_link_set_drain(receiver, false); |
| 1953 |
receiver->drain_flag_mode = false; |
| 1954 |
} |
| 1955 |
} |
| 1956 |
|
| 1957 |
void pn_link_drain(pn_link_t *receiver, int credit) |
| 1958 |
{ |
| 1959 |
assert(receiver); |
| 1960 |
assert(pn_link_is_receiver(receiver)); |
| 1961 |
pn_link_set_drain(receiver, true); |
| 1962 |
pn_link_flow(receiver, credit); |
| 1963 |
receiver->drain_flag_mode = false; |
| 1964 |
} |
| 1965 |
|
| 1966 |
void pn_link_set_drain(pn_link_t *receiver, bool drain) |
| 1967 |
{ |
| 1968 |
assert(receiver); |
| 1969 |
assert(pn_link_is_receiver(receiver)); |
| 1970 |
receiver->drain = drain; |
| 1971 |
pn_modified(receiver->session->connection, &receiver->endpoint, true); |
| 1972 |
receiver->drain_flag_mode = true; |
| 1973 |
} |
| 1974 |
|
| 1975 |
bool pn_link_draining(pn_link_t *receiver) |
| 1976 |
{ |
| 1977 |
assert(receiver); |
| 1978 |
assert(pn_link_is_receiver(receiver)); |
| 1979 |
return receiver->drain && (pn_link_credit(receiver) > pn_link_queued(receiver)); |
| 1980 |
} |
| 1981 |
|
| 1982 |
uint64_t pn_link_max_message_size(pn_link_t *link) |
| 1983 |
{ |
| 1984 |
return link->max_message_size; |
| 1985 |
} |
| 1986 |
|
| 1987 |
void pn_link_set_max_message_size(pn_link_t *link, uint64_t size) |
| 1988 |
{ |
| 1989 |
link->max_message_size = size; |
| 1990 |
} |
| 1991 |
|
| 1992 |
uint64_t pn_link_remote_max_message_size(pn_link_t *link) |
| 1993 |
{ |
| 1994 |
return link->remote_max_message_size; |
| 1995 |
} |
| 1996 |
|
| 1997 |
pn_data_t *pn_link_properties(pn_link_t *link) |
| 1998 |
{ |
| 1999 |
assert(link); |
| 2000 |
if (!link->properties) |
| 2001 |
link->properties = pn_data(0); |
| 2002 |
return link->properties; |
| 2003 |
} |
| 2004 |
|
| 2005 |
pn_data_t *pn_link_remote_properties(pn_link_t *link) |
| 2006 |
{ |
| 2007 |
assert(link); |
| 2008 |
return link->remote_properties; |
| 2009 |
} |
| 2010 |
|
| 2011 |
|
| 2012 |
pn_link_t *pn_delivery_link(pn_delivery_t *delivery) |
| 2013 |
{ |
| 2014 |
assert(delivery); |
| 2015 |
return delivery->link; |
| 2016 |
} |
| 2017 |
|
| 2018 |
pn_disposition_t *pn_delivery_local(pn_delivery_t *delivery) |
| 2019 |
{ |
| 2020 |
assert(delivery); |
| 2021 |
return &delivery->local; |
| 2022 |
} |
| 2023 |
|
| 2024 |
uint64_t pn_delivery_local_state(pn_delivery_t *delivery) |
| 2025 |
{ |
| 2026 |
assert(delivery); |
| 2027 |
return delivery->local.type; |
| 2028 |
} |
| 2029 |
|
| 2030 |
pn_disposition_t *pn_delivery_remote(pn_delivery_t *delivery) |
| 2031 |
{ |
| 2032 |
assert(delivery); |
| 2033 |
return &delivery->remote; |
| 2034 |
} |
| 2035 |
|
| 2036 |
uint64_t pn_delivery_remote_state(pn_delivery_t *delivery) |
| 2037 |
{ |
| 2038 |
assert(delivery); |
| 2039 |
return delivery->remote.type; |
| 2040 |
} |
| 2041 |
|
| 2042 |
bool pn_delivery_settled(pn_delivery_t *delivery) |
| 2043 |
{ |
| 2044 |
return delivery ? delivery->remote.settled : false; |
| 2045 |
} |
| 2046 |
|
| 2047 |
bool pn_delivery_updated(pn_delivery_t *delivery) |
| 2048 |
{ |
| 2049 |
return delivery ? delivery->updated : false; |
| 2050 |
} |
| 2051 |
|
| 2052 |
void pn_delivery_clear(pn_delivery_t *delivery) |
| 2053 |
{ |
| 2054 |
delivery->updated = false; |
| 2055 |
pn_work_update(delivery->link->session->connection, delivery); |
| 2056 |
} |
| 2057 |
|
| 2058 |
void pn_delivery_update(pn_delivery_t *delivery, uint64_t state) |
| 2059 |
{ |
| 2060 |
if (!delivery) return; |
| 2061 |
delivery->local.type = state; |
| 2062 |
pni_add_tpwork(delivery); |
| 2063 |
} |
| 2064 |
|
| 2065 |
bool pn_delivery_writable(pn_delivery_t *delivery) |
| 2066 |
{ |
| 2067 |
if (!delivery) return false; |
| 2068 |
|
| 2069 |
pn_link_t *link = delivery->link; |
| 2070 |
return pn_link_is_sender(link) && pn_delivery_current(delivery) && pn_link_credit(link) > 0; |
| 2071 |
} |
| 2072 |
|
| 2073 |
bool pn_delivery_readable(pn_delivery_t *delivery) |
| 2074 |
{ |
| 2075 |
if (delivery) { |
| 2076 |
pn_link_t *link = delivery->link; |
| 2077 |
return pn_link_is_receiver(link) && pn_delivery_current(delivery); |
| 2078 |
} else { |
| 2079 |
return false; |
| 2080 |
} |
| 2081 |
} |
| 2082 |
|
| 2083 |
size_t pn_delivery_pending(pn_delivery_t *delivery) |
| 2084 |
{ |
| 2085 |
/* Aborted deliveries: for clients that don't check pn_delivery_aborted(), |
| 2086 |
return 1 rather than 0. This will force them to call pn_link_recv() and get |
| 2087 |
the PN_ABORTED error return code. |
| 2088 |
*/ |
| 2089 |
if (delivery->aborted) return 1; |
| 2090 |
return pn_buffer_size(delivery->bytes); |
| 2091 |
} |
| 2092 |
|
| 2093 |
bool pn_delivery_partial(pn_delivery_t *delivery) |
| 2094 |
{ |
| 2095 |
return !delivery->done; |
| 2096 |
} |
| 2097 |
|
| 2098 |
void pn_delivery_abort(pn_delivery_t *delivery) { |
| 2099 |
if (!delivery->local.settled) { /* Can't abort a settled delivery */ |
| 2100 |
delivery->aborted = true; |
| 2101 |
pn_delivery_settle(delivery); |
| 2102 |
delivery->link->session->outgoing_bytes -= pn_buffer_size(delivery->bytes); |
| 2103 |
pn_buffer_clear(delivery->bytes); |
| 2104 |
} |
| 2105 |
} |
| 2106 |
|
| 2107 |
bool pn_delivery_aborted(pn_delivery_t *delivery) { |
| 2108 |
return delivery->aborted; |
| 2109 |
} |
| 2110 |
|
| 2111 |
pn_condition_t *pn_connection_condition(pn_connection_t *connection) |
| 2112 |
{ |
| 2113 |
assert(connection); |
| 2114 |
return &connection->endpoint.condition; |
| 2115 |
} |
| 2116 |
|
| 2117 |
pn_condition_t *pn_connection_remote_condition(pn_connection_t *connection) |
| 2118 |
{ |
| 2119 |
assert(connection); |
| 2120 |
pn_transport_t *transport = connection->transport; |
| 2121 |
return transport ? &transport->remote_condition : NULL; |
| 2122 |
} |
| 2123 |
|
| 2124 |
pn_condition_t *pn_session_condition(pn_session_t *session) |
| 2125 |
{ |
| 2126 |
assert(session); |
| 2127 |
return &session->endpoint.condition; |
| 2128 |
} |
| 2129 |
|
| 2130 |
pn_condition_t *pn_session_remote_condition(pn_session_t *session) |
| 2131 |
{ |
| 2132 |
assert(session); |
| 2133 |
return &session->endpoint.remote_condition; |
| 2134 |
} |
| 2135 |
|
| 2136 |
pn_condition_t *pn_link_condition(pn_link_t *link) |
| 2137 |
{ |
| 2138 |
assert(link); |
| 2139 |
return &link->endpoint.condition; |
| 2140 |
} |
| 2141 |
|
| 2142 |
pn_condition_t *pn_link_remote_condition(pn_link_t *link) |
| 2143 |
{ |
| 2144 |
assert(link); |
| 2145 |
return &link->endpoint.remote_condition; |
| 2146 |
} |
| 2147 |
|
| 2148 |
bool pn_condition_is_set(pn_condition_t *condition) |
| 2149 |
{ |
| 2150 |
return condition && condition->name && pn_string_get(condition->name); |
| 2151 |
} |
| 2152 |
|
| 2153 |
void pn_condition_clear(pn_condition_t *condition) |
| 2154 |
{ |
| 2155 |
assert(condition); |
| 2156 |
if (condition->name) pn_string_clear(condition->name); |
| 2157 |
if (condition->description) pn_string_clear(condition->description); |
| 2158 |
if (condition->info) pn_data_clear(condition->info); |
| 2159 |
} |
| 2160 |
|
| 2161 |
const char *pn_condition_get_name(pn_condition_t *condition) |
| 2162 |
{ |
| 2163 |
assert(condition); |
| 2164 |
if (condition->name == NULL) { |
| 2165 |
return NULL; |
| 2166 |
} else { |
| 2167 |
return pn_string_get(condition->name); |
| 2168 |
} |
| 2169 |
} |
| 2170 |
|
| 2171 |
int pn_condition_set_name(pn_condition_t *condition, const char *name) |
| 2172 |
{ |
| 2173 |
assert(condition); |
| 2174 |
if (condition->name == NULL) { |
| 2175 |
condition->name = pn_string(name); |
| 2176 |
return 0; |
| 2177 |
} else { |
| 2178 |
return pn_string_set(condition->name, name); |
| 2179 |
} |
| 2180 |
} |
| 2181 |
|
| 2182 |
const char *pn_condition_get_description(pn_condition_t *condition) |
| 2183 |
{ |
| 2184 |
assert(condition); |
| 2185 |
if (condition->description == NULL) { |
| 2186 |
return NULL; |
| 2187 |
} else { |
| 2188 |
return pn_string_get(condition->description); |
| 2189 |
} |
| 2190 |
} |
| 2191 |
|
| 2192 |
int pn_condition_set_description(pn_condition_t *condition, const char *description) |
| 2193 |
{ |
| 2194 |
assert(condition); |
| 2195 |
if (condition->description == NULL) { |
| 2196 |
condition->description = pn_string(description); |
| 2197 |
return 0; |
| 2198 |
} else { |
| 2199 |
return pn_string_set(condition->description, description); |
| 2200 |
} |
| 2201 |
} |
| 2202 |
|
| 2203 |
int pn_condition_vformat(pn_condition_t *condition, const char *name, const char *fmt, va_list ap) |
| 2204 |
{ |
| 2205 |
assert(condition); |
| 2206 |
int err = pn_condition_set_name(condition, name); |
| 2207 |
if (err) |
| 2208 |
return err; |
| 2209 |
|
| 2210 |
char text[1024]; |
| 2211 |
size_t n = vsnprintf(text, 1024, fmt, ap); |
| 2212 |
if (n >= sizeof(text)) |
| 2213 |
text[sizeof(text)-1] = '\0'; |
| 2214 |
err = pn_condition_set_description(condition, text); |
| 2215 |
return err; |
| 2216 |
} |
| 2217 |
|
| 2218 |
int pn_condition_format(pn_condition_t *condition, const char *name, const char *fmt, ...) |
| 2219 |
{ |
| 2220 |
assert(condition); |
| 2221 |
va_list ap; |
| 2222 |
va_start(ap, fmt); |
| 2223 |
int err = pn_condition_vformat(condition, name, fmt, ap); |
| 2224 |
va_end(ap); |
| 2225 |
return err; |
| 2226 |
} |
| 2227 |
|
| 2228 |
pn_data_t *pn_condition_info(pn_condition_t *condition) |
| 2229 |
{ |
| 2230 |
assert(condition); |
| 2231 |
if (condition->info == NULL) { |
| 2232 |
condition->info = pn_data(0); |
| 2233 |
} |
| 2234 |
return condition->info; |
| 2235 |
} |
| 2236 |
|
| 2237 |
bool pn_condition_is_redirect(pn_condition_t *condition) |
| 2238 |
{ |
| 2239 |
const char *name = pn_condition_get_name(condition); |
| 2240 |
return name && (!strcmp(name, "amqp:connection:redirect") || |
| 2241 |
!strcmp(name, "amqp:link:redirect")); |
| 2242 |
} |
| 2243 |
|
| 2244 |
const char *pn_condition_redirect_host(pn_condition_t *condition) |
| 2245 |
{ |
| 2246 |
pn_data_t *data = pn_condition_info(condition); |
| 2247 |
pn_data_rewind(data); |
| 2248 |
pn_data_next(data); |
| 2249 |
pn_data_enter(data); |
| 2250 |
pn_data_lookup(data, "network-host"); |
| 2251 |
pn_bytes_t host = pn_data_get_bytes(data); |
| 2252 |
pn_data_rewind(data); |
| 2253 |
return host.start; |
| 2254 |
} |
| 2255 |
|
| 2256 |
int pn_condition_redirect_port(pn_condition_t *condition) |
| 2257 |
{ |
| 2258 |
pn_data_t *data = pn_condition_info(condition); |
| 2259 |
pn_data_rewind(data); |
| 2260 |
pn_data_next(data); |
| 2261 |
pn_data_enter(data); |
| 2262 |
pn_data_lookup(data, "port"); |
| 2263 |
int port = pn_data_get_int(data); |
| 2264 |
pn_data_rewind(data); |
| 2265 |
return port; |
| 2266 |
} |
| 2267 |
|
| 2268 |
pn_connection_t *pn_event_connection(pn_event_t *event) |
| 2269 |
{ |
| 2270 |
pn_session_t *ssn; |
| 2271 |
pn_transport_t *transport; |
| 2272 |
|
| 2273 |
switch (pn_class_id(pn_event_class(event))) { |
| 2274 |
case CID_pn_connection: |
| 2275 |
return (pn_connection_t *) pn_event_context(event); |
| 2276 |
case CID_pn_transport: |
| 2277 |
transport = pn_event_transport(event); |
| 2278 |
if (transport) |
| 2279 |
return transport->connection; |
| 2280 |
return NULL; |
| 2281 |
default: |
| 2282 |
ssn = pn_event_session(event); |
| 2283 |
if (ssn) |
| 2284 |
return pn_session_connection(ssn); |
| 2285 |
} |
| 2286 |
return NULL; |
| 2287 |
} |
| 2288 |
|
| 2289 |
pn_session_t *pn_event_session(pn_event_t *event) |
| 2290 |
{ |
| 2291 |
pn_link_t *link; |
| 2292 |
switch (pn_class_id(pn_event_class(event))) { |
| 2293 |
case CID_pn_session: |
| 2294 |
return (pn_session_t *) pn_event_context(event); |
| 2295 |
default: |
| 2296 |
link = pn_event_link(event); |
| 2297 |
if (link) |
| 2298 |
return pn_link_session(link); |
| 2299 |
} |
| 2300 |
return NULL; |
| 2301 |
} |
| 2302 |
|
| 2303 |
pn_link_t *pn_event_link(pn_event_t *event) |
| 2304 |
{ |
| 2305 |
pn_delivery_t *dlv; |
| 2306 |
switch (pn_class_id(pn_event_class(event))) { |
| 2307 |
case CID_pn_link: |
| 2308 |
return (pn_link_t *) pn_event_context(event); |
| 2309 |
default: |
| 2310 |
dlv = pn_event_delivery(event); |
| 2311 |
if (dlv) |
| 2312 |
return pn_delivery_link(dlv); |
| 2313 |
} |
| 2314 |
return NULL; |
| 2315 |
} |
| 2316 |
|
| 2317 |
pn_delivery_t *pn_event_delivery(pn_event_t *event) |
| 2318 |
{ |
| 2319 |
switch (pn_class_id(pn_event_class(event))) { |
| 2320 |
case CID_pn_delivery: |
| 2321 |
return (pn_delivery_t *) pn_event_context(event); |
| 2322 |
default: |
| 2323 |
return NULL; |
| 2324 |
} |
| 2325 |
} |
| 2326 |
|
| 2327 |
pn_transport_t *pn_event_transport(pn_event_t *event) |
| 2328 |
{ |
| 2329 |
switch (pn_class_id(pn_event_class(event))) { |
| 2330 |
case CID_pn_transport: |
| 2331 |
return (pn_transport_t *) pn_event_context(event); |
| 2332 |
default: |
| 2333 |
{ |
| 2334 |
pn_connection_t *conn = pn_event_connection(event); |
| 2335 |
if (conn) |
| 2336 |
return pn_connection_transport(conn); |
| 2337 |
return NULL; |
| 2338 |
} |
| 2339 |
} |
| 2340 |
} |
| 2341 |
|
| 2342 |
int pn_condition_copy(pn_condition_t *dest, pn_condition_t *src) { |
| 2343 |
assert(dest); |
| 2344 |
assert(src); |
| 2345 |
int err = 0; |
| 2346 |
if (src != dest) { |
| 2347 |
if (!(src->name == NULL && dest->name == NULL)) { |
| 2348 |
if (src->name == NULL) { |
| 2349 |
pn_free(dest->name); |
| 2350 |
dest->name = NULL; |
| 2351 |
} else { |
| 2352 |
if (dest->name == NULL) { |
| 2353 |
dest->name = pn_string(NULL); |
| 2354 |
} |
| 2355 |
err = pn_string_copy(dest->name, src->name); |
| 2356 |
} |
| 2357 |
} |
| 2358 |
if (!err && !(src->description == NULL && dest->description == NULL)) { |
| 2359 |
if (src->description == NULL) { |
| 2360 |
pn_free(dest->description); |
| 2361 |
dest->description = NULL; |
| 2362 |
} else { |
| 2363 |
if (dest->description == NULL) { |
| 2364 |
dest->description = pn_string(NULL); |
| 2365 |
} |
| 2366 |
err = pn_string_copy(dest->description, src->description); |
| 2367 |
} |
| 2368 |
} |
| 2369 |
if (!err && !(src->info == NULL && dest->info == NULL)) { |
| 2370 |
if (src->info == NULL) { |
| 2371 |
pn_data_free(dest->info); |
| 2372 |
dest->info = NULL; |
| 2373 |
} else { |
| 2374 |
if (dest->info == NULL) { |
| 2375 |
dest->info = pn_data(0); |
| 2376 |
} |
| 2377 |
err = pn_data_copy(dest->info, src->info); |
| 2378 |
} |
| 2379 |
} |
| 2380 |
} |
| 2381 |
return err; |
| 2382 |
} |
| 2383 |
|
| 2384 |
|
| 2385 |
static pn_condition_t *cond_set(pn_condition_t *cond) { |
| 2386 |
return cond && pn_condition_is_set(cond) ? cond : NULL; |
| 2387 |
} |
| 2388 |
|
| 2389 |
static pn_condition_t *cond2_set(pn_condition_t *cond1, pn_condition_t *cond2) { |
| 2390 |
pn_condition_t *cond = cond_set(cond1); |
| 2391 |
if (!cond) cond = cond_set(cond2); |
| 2392 |
return cond; |
| 2393 |
} |
| 2394 |
|
| 2395 |
pn_condition_t *pn_event_condition(pn_event_t *e) { |
| 2396 |
void *ctx = pn_event_context(e); |
| 2397 |
switch (pn_class_id(pn_event_class(e))) { |
| 2398 |
case CID_pn_connection: { |
| 2399 |
pn_connection_t *c = (pn_connection_t*)ctx; |
| 2400 |
return cond2_set(pn_connection_remote_condition(c), pn_connection_condition(c)); |
| 2401 |
} |
| 2402 |
case CID_pn_session: { |
| 2403 |
pn_session_t *s = (pn_session_t*)ctx; |
| 2404 |
return cond2_set(pn_session_remote_condition(s), pn_session_condition(s)); |
| 2405 |
} |
| 2406 |
case CID_pn_link: { |
| 2407 |
pn_link_t *l = (pn_link_t*)ctx; |
| 2408 |
return cond2_set(pn_link_remote_condition(l), pn_link_condition(l)); |
| 2409 |
} |
| 2410 |
case CID_pn_transport: |
| 2411 |
return cond_set(pn_transport_condition((pn_transport_t*)ctx)); |
| 2412 |
|
| 2413 |
default: |
| 2414 |
return NULL; |
| 2415 |
} |
| 2416 |
} |
| 2417 |
|
| 2418 |
const char *pn_disposition_type_name(uint64_t d) { |
| 2419 |
switch(d) { |
| 2420 |
case PN_RECEIVED: return "received"; |
| 2421 |
case PN_ACCEPTED: return "accepted"; |
| 2422 |
case PN_REJECTED: return "rejected"; |
| 2423 |
case PN_RELEASED: return "released"; |
| 2424 |
case PN_MODIFIED: return "modified"; |
| 2425 |
default: return "unknown"; |
| 2426 |
} |
| 2427 |
} |
| 2428 |
|
| 2429 |
#undef PN_USE_DEPRECATED_API |