| 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 <proton/messenger.h> |
| 23 |
#include <proton/engine.h> |
| 24 |
#include <proton/object.h> |
| 25 |
#include <assert.h> |
| 26 |
#ifndef __cplusplus |
| 27 |
#include <stdbool.h> |
| 28 |
#endif |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include "core/util.h" |
| 32 |
#include "store.h" |
| 33 |
|
| 34 |
typedef struct pni_stream_t pni_stream_t; |
| 35 |
|
| 36 |
struct pni_store_t { |
| 37 |
pni_stream_t *streams; |
| 38 |
pni_entry_t *store_head; |
| 39 |
pni_entry_t *store_tail; |
| 40 |
pn_hash_t *tracked; |
| 41 |
size_t size; |
| 42 |
unsigned window; |
| 43 |
pn_sequence_t lwm; |
| 44 |
pn_sequence_t hwm; |
| 45 |
}; |
| 46 |
|
| 47 |
struct pni_stream_t { |
| 48 |
pni_store_t *store; |
| 49 |
pn_string_t *address; |
| 50 |
pni_entry_t *stream_head; |
| 51 |
pni_entry_t *stream_tail; |
| 52 |
pni_stream_t *next; |
| 53 |
}; |
| 54 |
|
| 55 |
struct pni_entry_t { |
| 56 |
pni_stream_t *stream; |
| 57 |
pni_entry_t *stream_next; |
| 58 |
pni_entry_t *stream_prev; |
| 59 |
pni_entry_t *store_next; |
| 60 |
pni_entry_t *store_prev; |
| 61 |
pn_buffer_t *bytes; |
| 62 |
pn_delivery_t *delivery; |
| 63 |
void *context; |
| 64 |
pn_status_t status; |
| 65 |
pn_sequence_t id; |
| 66 |
bool free; |
| 67 |
}; |
| 68 |
|
| 69 |
void pni_entry_finalize(void *object) |
| 70 |
{ |
| 71 |
pni_entry_t *entry = (pni_entry_t *) object; |
| 72 |
assert(entry->free); |
| 73 |
pn_delivery_t *d = entry->delivery; |
| 74 |
if (d) { |
| 75 |
pn_delivery_settle(d); |
| 76 |
pni_entry_set_delivery(entry, NULL); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
pni_store_t *pni_store() |
| 81 |
{ |
| 82 |
pni_store_t *store = (pni_store_t *) malloc(sizeof(pni_store_t)); |
| 83 |
if (!store) return NULL; |
| 84 |
|
| 85 |
store->size = 0; |
| 86 |
store->streams = NULL; |
| 87 |
store->store_head = NULL; |
| 88 |
store->store_tail = NULL; |
| 89 |
store->window = 0; |
| 90 |
store->lwm = 0; |
| 91 |
store->hwm = 0; |
| 92 |
store->tracked = pn_hash(PN_OBJECT, 0, 0.75); |
| 93 |
|
| 94 |
return store; |
| 95 |
} |
| 96 |
|
| 97 |
size_t pni_store_size(pni_store_t *store) |
| 98 |
{ |
| 99 |
assert(store); |
| 100 |
return store->size; |
| 101 |
} |
| 102 |
|
| 103 |
pni_stream_t *pni_stream(pni_store_t *store, const char *address, bool create) |
| 104 |
{ |
| 105 |
assert(store); |
| 106 |
assert(address); |
| 107 |
|
| 108 |
pni_stream_t *prev = NULL; |
| 109 |
pni_stream_t *stream = store->streams; |
| 110 |
while (stream) { |
| 111 |
if (!strcmp(pn_string_get(stream->address), address)) { |
| 112 |
return stream; |
| 113 |
} |
| 114 |
prev = stream; |
| 115 |
stream = stream->next; |
| 116 |
} |
| 117 |
|
| 118 |
if (create) { |
| 119 |
stream = (pni_stream_t *) malloc(sizeof(pni_stream_t)); |
| 120 |
if (stream != NULL) { |
| 121 |
stream->store = store; |
| 122 |
stream->address = pn_string(address); |
| 123 |
stream->stream_head = NULL; |
| 124 |
stream->stream_tail = NULL; |
| 125 |
stream->next = NULL; |
| 126 |
|
| 127 |
if (prev) { |
| 128 |
prev->next = stream; |
| 129 |
} else { |
| 130 |
store->streams = stream; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return stream; |
| 136 |
} |
| 137 |
|
| 138 |
pni_stream_t *pni_stream_head(pni_store_t *store) |
| 139 |
{ |
| 140 |
assert(store); |
| 141 |
return store->streams; |
| 142 |
} |
| 143 |
|
| 144 |
pni_stream_t *pni_stream_next(pni_stream_t *stream) |
| 145 |
{ |
| 146 |
assert(stream); |
| 147 |
return stream->next; |
| 148 |
} |
| 149 |
|
| 150 |
void pni_entry_free(pni_entry_t *entry) |
| 151 |
{ |
| 152 |
if (!entry) return; |
| 153 |
pni_stream_t *stream = entry->stream; |
| 154 |
pni_store_t *store = stream->store; |
| 155 |
LL_REMOVE(stream, stream, entry); |
| 156 |
LL_REMOVE(store, store, entry); |
| 157 |
entry->free = true; |
| 158 |
|
| 159 |
pn_buffer_free(entry->bytes); |
| 160 |
entry->bytes = NULL; |
| 161 |
pn_decref(entry); |
| 162 |
store->size--; |
| 163 |
} |
| 164 |
|
| 165 |
void pni_stream_free(pni_stream_t *stream) |
| 166 |
{ |
| 167 |
if (!stream) return; |
| 168 |
pni_entry_t *entry; |
| 169 |
while ((entry = LL_HEAD(stream, stream))) { |
| 170 |
pni_entry_free(entry); |
| 171 |
} |
| 172 |
pn_free(stream->address); |
| 173 |
stream->address = NULL; |
| 174 |
free(stream); |
| 175 |
} |
| 176 |
|
| 177 |
void pni_store_free(pni_store_t *store) |
| 178 |
{ |
| 179 |
if (!store) return; |
| 180 |
pn_free(store->tracked); |
| 181 |
pni_stream_t *stream = store->streams; |
| 182 |
while (stream) { |
| 183 |
pni_stream_t *next = stream->next; |
| 184 |
pni_stream_free(stream); |
| 185 |
stream = next; |
| 186 |
} |
| 187 |
free(store); |
| 188 |
} |
| 189 |
|
| 190 |
pni_stream_t *pni_stream_put(pni_store_t *store, const char *address) |
| 191 |
{ |
| 192 |
assert(store); assert(address); |
| 193 |
return pni_stream(store, address, true); |
| 194 |
} |
| 195 |
|
| 196 |
pni_stream_t *pni_stream_get(pni_store_t *store, const char *address) |
| 197 |
{ |
| 198 |
assert(store); assert(address); |
| 199 |
return pni_stream(store, address, false); |
| 200 |
} |
| 201 |
|
| 202 |
#define CID_pni_entry CID_pn_object |
| 203 |
#define pni_entry_initialize NULL |
| 204 |
#define pni_entry_hashcode NULL |
| 205 |
#define pni_entry_compare NULL |
| 206 |
#define pni_entry_inspect NULL |
| 207 |
|
| 208 |
pni_entry_t *pni_store_put(pni_store_t *store, const char *address) |
| 209 |
{ |
| 210 |
assert(store); |
| 211 |
static const pn_class_t clazz = PN_CLASS(pni_entry); |
| 212 |
|
| 213 |
if (!address) address = ""; |
| 214 |
pni_stream_t *stream = pni_stream_put(store, address); |
| 215 |
if (!stream) return NULL; |
| 216 |
pni_entry_t *entry = (pni_entry_t *) pn_class_new(&clazz, sizeof(pni_entry_t)); |
| 217 |
if (!entry) return NULL; |
| 218 |
entry->stream = stream; |
| 219 |
entry->free = false; |
| 220 |
entry->stream_next = NULL; |
| 221 |
entry->stream_prev = NULL; |
| 222 |
entry->store_next = NULL; |
| 223 |
entry->store_prev = NULL; |
| 224 |
entry->delivery = NULL; |
| 225 |
entry->bytes = pn_buffer(64); |
| 226 |
entry->status = PN_STATUS_UNKNOWN; |
| 227 |
LL_ADD(stream, stream, entry); |
| 228 |
LL_ADD(store, store, entry); |
| 229 |
store->size++; |
| 230 |
return entry; |
| 231 |
} |
| 232 |
|
| 233 |
pni_entry_t *pni_store_get(pni_store_t *store, const char *address) |
| 234 |
{ |
| 235 |
assert(store); |
| 236 |
if (address) { |
| 237 |
pni_stream_t *stream = pni_stream_get(store, address); |
| 238 |
if (!stream) return NULL; |
| 239 |
return LL_HEAD(stream, stream); |
| 240 |
} else { |
| 241 |
return LL_HEAD(store, store); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
pn_buffer_t *pni_entry_bytes(pni_entry_t *entry) |
| 246 |
{ |
| 247 |
assert(entry); |
| 248 |
return entry->bytes; |
| 249 |
} |
| 250 |
|
| 251 |
pn_status_t pni_entry_get_status(pni_entry_t *entry) |
| 252 |
{ |
| 253 |
assert(entry); |
| 254 |
return entry->status; |
| 255 |
} |
| 256 |
|
| 257 |
void pni_entry_set_status(pni_entry_t *entry, pn_status_t status) |
| 258 |
{ |
| 259 |
assert(entry); |
| 260 |
entry->status = status; |
| 261 |
} |
| 262 |
|
| 263 |
pn_delivery_t *pni_entry_get_delivery(pni_entry_t *entry) |
| 264 |
{ |
| 265 |
assert(entry); |
| 266 |
return entry->delivery; |
| 267 |
} |
| 268 |
|
| 269 |
void pni_entry_set_delivery(pni_entry_t *entry, pn_delivery_t *delivery) |
| 270 |
{ |
| 271 |
assert(entry); |
| 272 |
if (entry->delivery) { |
| 273 |
pn_delivery_set_context(entry->delivery, NULL); |
| 274 |
} |
| 275 |
entry->delivery = delivery; |
| 276 |
if (delivery) { |
| 277 |
pn_delivery_set_context(delivery, entry); |
| 278 |
} |
| 279 |
pni_entry_updated(entry); |
| 280 |
} |
| 281 |
|
| 282 |
void pni_entry_set_context(pni_entry_t *entry, void *context) |
| 283 |
{ |
| 284 |
assert(entry); |
| 285 |
entry->context = context; |
| 286 |
} |
| 287 |
|
| 288 |
void *pni_entry_get_context(pni_entry_t *entry) |
| 289 |
{ |
| 290 |
assert(entry); |
| 291 |
return entry->context; |
| 292 |
} |
| 293 |
|
| 294 |
static pn_status_t disp2status(uint64_t disp) |
| 295 |
{ |
| 296 |
if (!disp) return PN_STATUS_PENDING; |
| 297 |
|
| 298 |
switch (disp) { |
| 299 |
case PN_RECEIVED: |
| 300 |
return PN_STATUS_PENDING; |
| 301 |
case PN_ACCEPTED: |
| 302 |
return PN_STATUS_ACCEPTED; |
| 303 |
case PN_REJECTED: |
| 304 |
return PN_STATUS_REJECTED; |
| 305 |
case PN_RELEASED: |
| 306 |
return PN_STATUS_RELEASED; |
| 307 |
case PN_MODIFIED: |
| 308 |
return PN_STATUS_MODIFIED; |
| 309 |
default: |
| 310 |
assert(0); |
| 311 |
} |
| 312 |
|
| 313 |
return (pn_status_t) 0; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
void pni_entry_updated(pni_entry_t *entry) |
| 318 |
{ |
| 319 |
assert(entry); |
| 320 |
pn_delivery_t *d = entry->delivery; |
| 321 |
if (d) { |
| 322 |
if (pn_delivery_remote_state(d)) { |
| 323 |
entry->status = disp2status(pn_delivery_remote_state(d)); |
| 324 |
} else if (pn_delivery_settled(d)) { |
| 325 |
uint64_t disp = pn_delivery_local_state(d); |
| 326 |
if (disp) { |
| 327 |
entry->status = disp2status(disp); |
| 328 |
} else { |
| 329 |
entry->status = PN_STATUS_SETTLED; |
| 330 |
} |
| 331 |
} else { |
| 332 |
entry->status = PN_STATUS_PENDING; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
pn_sequence_t pni_entry_id(pni_entry_t *entry) |
| 338 |
{ |
| 339 |
assert(entry); |
| 340 |
return entry->id; |
| 341 |
} |
| 342 |
|
| 343 |
pni_entry_t *pni_store_entry(pni_store_t *store, pn_sequence_t id) |
| 344 |
{ |
| 345 |
assert(store); |
| 346 |
return (pni_entry_t *) pn_hash_get(store->tracked, id); |
| 347 |
} |
| 348 |
|
| 349 |
bool pni_store_tracking(pni_store_t *store, pn_sequence_t id) |
| 350 |
{ |
| 351 |
return (id - store->lwm < INT32_MAX) && (store->hwm - id < INT32_MAX + 1u); |
| 352 |
} |
| 353 |
|
| 354 |
pn_sequence_t pni_entry_track(pni_entry_t *entry) |
| 355 |
{ |
| 356 |
assert(entry); |
| 357 |
|
| 358 |
pni_store_t *store = entry->stream->store; |
| 359 |
entry->id = store->hwm++; |
| 360 |
pn_hash_put(store->tracked, entry->id, entry); |
| 361 |
|
| 362 |
if (store->window < INT32_MAX) { |
| 363 |
while (store->hwm - store->lwm > store->window) { |
| 364 |
pni_entry_t *e = pni_store_entry(store, store->lwm); |
| 365 |
if (e) { |
| 366 |
pn_hash_del(store->tracked, store->lwm); |
| 367 |
} |
| 368 |
store->lwm++; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
return entry->id; |
| 373 |
} |
| 374 |
|
| 375 |
int pni_store_update(pni_store_t *store, pn_sequence_t id, pn_status_t status, |
| 376 |
int flags, bool settle, bool match) |
| 377 |
{ |
| 378 |
assert(store); |
| 379 |
|
| 380 |
if (!pni_store_tracking(store, id)) { |
| 381 |
return 0; |
| 382 |
} |
| 383 |
|
| 384 |
size_t start; |
| 385 |
if (PN_CUMULATIVE & flags) { |
| 386 |
start = store->lwm; |
| 387 |
} else { |
| 388 |
start = id; |
| 389 |
} |
| 390 |
|
| 391 |
for (pn_sequence_t i = start; i <= id; i++) { |
| 392 |
pni_entry_t *e = pni_store_entry(store, i); |
| 393 |
if (e) { |
| 394 |
pn_delivery_t *d = e->delivery; |
| 395 |
if (d) { |
| 396 |
if (!pn_delivery_local_state(d)) { |
| 397 |
if (match) { |
| 398 |
pn_delivery_update(d, pn_delivery_remote_state(d)); |
| 399 |
} else { |
| 400 |
switch (status) { |
| 401 |
case PN_STATUS_ACCEPTED: |
| 402 |
pn_delivery_update(d, PN_ACCEPTED); |
| 403 |
break; |
| 404 |
case PN_STATUS_REJECTED: |
| 405 |
pn_delivery_update(d, PN_REJECTED); |
| 406 |
break; |
| 407 |
default: |
| 408 |
break; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
pni_entry_updated(e); |
| 413 |
} |
| 414 |
} |
| 415 |
if (settle) { |
| 416 |
if (d) { |
| 417 |
pn_delivery_settle(d); |
| 418 |
} |
| 419 |
pn_hash_del(store->tracked, e->id); |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
while (store->hwm - store->lwm > 0 && |
| 425 |
!pn_hash_get(store->tracked, store->lwm)) { |
| 426 |
store->lwm++; |
| 427 |
} |
| 428 |
|
| 429 |
return 0; |
| 430 |
} |
| 431 |
|
| 432 |
int pni_store_get_window(pni_store_t *store) |
| 433 |
{ |
| 434 |
assert(store); |
| 435 |
return store->window; |
| 436 |
} |
| 437 |
|
| 438 |
void pni_store_set_window(pni_store_t *store, int window) |
| 439 |
{ |
| 440 |
assert(store); |
| 441 |
store->window = window; |
| 442 |
} |