| 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 |
#include <stdio.h> |
| 22 |
#include <proton/object.h> |
| 23 |
#include <proton/event.h> |
| 24 |
#include <proton/reactor.h> |
| 25 |
#include <assert.h> |
| 26 |
|
| 27 |
struct pn_collector_t { |
| 28 |
pn_list_t *pool; |
| 29 |
pn_event_t *head; |
| 30 |
pn_event_t *tail; |
| 31 |
pn_event_t *prev; /* event returned by previous call to pn_collector_next() */ |
| 32 |
bool freed; |
| 33 |
}; |
| 34 |
|
| 35 |
struct pn_event_t { |
| 36 |
pn_list_t *pool; |
| 37 |
const pn_class_t *clazz; |
| 38 |
void *context; // depends on clazz |
| 39 |
pn_record_t *attachments; |
| 40 |
pn_event_t *next; |
| 41 |
pn_event_type_t type; |
| 42 |
}; |
| 43 |
|
| 44 |
static void pn_collector_initialize(pn_collector_t *collector) |
| 45 |
{ |
| 46 |
collector->pool = pn_list(PN_OBJECT, 0); |
| 47 |
collector->head = NULL; |
| 48 |
collector->tail = NULL; |
| 49 |
collector->prev = NULL; |
| 50 |
collector->freed = false; |
| 51 |
} |
| 52 |
|
| 53 |
void pn_collector_drain(pn_collector_t *collector) |
| 54 |
{ |
| 55 |
assert(collector); |
| 56 |
while (pn_collector_next(collector)) |
| 57 |
; |
| 58 |
assert(!collector->head); |
| 59 |
assert(!collector->tail); |
| 60 |
} |
| 61 |
|
| 62 |
static void pn_collector_shrink(pn_collector_t *collector) |
| 63 |
{ |
| 64 |
assert(collector); |
| 65 |
pn_list_clear(collector->pool); |
| 66 |
} |
| 67 |
|
| 68 |
static void pn_collector_finalize(pn_collector_t *collector) |
| 69 |
{ |
| 70 |
pn_collector_drain(collector); |
| 71 |
pn_decref(collector->pool); |
| 72 |
} |
| 73 |
|
| 74 |
static int pn_collector_inspect(pn_collector_t *collector, pn_string_t *dst) |
| 75 |
{ |
| 76 |
assert(collector); |
| 77 |
int err = pn_string_addf(dst, "EVENTS["); |
| 78 |
if (err) return err; |
| 79 |
pn_event_t *event = collector->head; |
| 80 |
bool first = true; |
| 81 |
while (event) { |
| 82 |
if (first) { |
| 83 |
first = false; |
| 84 |
} else { |
| 85 |
err = pn_string_addf(dst, ", "); |
| 86 |
if (err) return err; |
| 87 |
} |
| 88 |
err = pn_inspect(event, dst); |
| 89 |
if (err) return err; |
| 90 |
event = event->next; |
| 91 |
} |
| 92 |
return pn_string_addf(dst, "]"); |
| 93 |
} |
| 94 |
|
| 95 |
#define pn_collector_hashcode NULL |
| 96 |
#define pn_collector_compare NULL |
| 97 |
|
| 98 |
PN_CLASSDEF(pn_collector) |
| 99 |
|
| 100 |
pn_collector_t *pn_collector(void) |
| 101 |
{ |
| 102 |
return pn_collector_new(); |
| 103 |
} |
| 104 |
|
| 105 |
void pn_collector_free(pn_collector_t *collector) |
| 106 |
{ |
| 107 |
assert(collector); |
| 108 |
pn_collector_release(collector); |
| 109 |
pn_decref(collector); |
| 110 |
} |
| 111 |
|
| 112 |
void pn_collector_release(pn_collector_t *collector) |
| 113 |
{ |
| 114 |
assert(collector); |
| 115 |
if (!collector->freed) { |
| 116 |
collector->freed = true; |
| 117 |
pn_collector_drain(collector); |
| 118 |
pn_collector_shrink(collector); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
pn_event_t *pn_event(void); |
| 123 |
|
| 124 |
pn_event_t *pn_collector_put(pn_collector_t *collector, |
| 125 |
const pn_class_t *clazz, void *context, |
| 126 |
pn_event_type_t type) |
| 127 |
{ |
| 128 |
if (!collector) { |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
|
| 132 |
assert(context); |
| 133 |
|
| 134 |
if (collector->freed) { |
| 135 |
return NULL; |
| 136 |
} |
| 137 |
|
| 138 |
pn_event_t *tail = collector->tail; |
| 139 |
if (tail && tail->type == type && tail->context == context) { |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
|
| 143 |
clazz = clazz->reify(context); |
| 144 |
|
| 145 |
pn_event_t *event = (pn_event_t *) pn_list_pop(collector->pool); |
| 146 |
|
| 147 |
if (!event) { |
| 148 |
event = pn_event(); |
| 149 |
} |
| 150 |
|
| 151 |
event->pool = collector->pool; |
| 152 |
pn_incref(event->pool); |
| 153 |
|
| 154 |
if (tail) { |
| 155 |
tail->next = event; |
| 156 |
collector->tail = event; |
| 157 |
} else { |
| 158 |
collector->tail = event; |
| 159 |
collector->head = event; |
| 160 |
} |
| 161 |
|
| 162 |
event->clazz = clazz; |
| 163 |
event->context = context; |
| 164 |
event->type = type; |
| 165 |
pn_class_incref(clazz, event->context); |
| 166 |
|
| 167 |
return event; |
| 168 |
} |
| 169 |
|
| 170 |
pn_event_t *pn_collector_peek(pn_collector_t *collector) |
| 171 |
{ |
| 172 |
return collector->head; |
| 173 |
} |
| 174 |
|
| 175 |
// Advance head pointer for pop or next, return the old head. |
| 176 |
static pn_event_t *pop_internal(pn_collector_t *collector) { |
| 177 |
pn_event_t *event = collector->head; |
| 178 |
if (event) { |
| 179 |
collector->head = event->next; |
| 180 |
if (!collector->head) { |
| 181 |
collector->tail = NULL; |
| 182 |
} |
| 183 |
} |
| 184 |
return event; |
| 185 |
} |
| 186 |
|
| 187 |
bool pn_collector_pop(pn_collector_t *collector) { |
| 188 |
pn_event_t *event = pop_internal(collector); |
| 189 |
if (event) { |
| 190 |
pn_decref(event); |
| 191 |
} |
| 192 |
return event; |
| 193 |
} |
| 194 |
|
| 195 |
pn_event_t *pn_collector_next(pn_collector_t *collector) { |
| 196 |
if (collector->prev) { |
| 197 |
pn_decref(collector->prev); |
| 198 |
} |
| 199 |
collector->prev = pop_internal(collector); |
| 200 |
return collector->prev; |
| 201 |
} |
| 202 |
|
| 203 |
pn_event_t *pn_collector_prev(pn_collector_t *collector) { |
| 204 |
return collector->prev; |
| 205 |
} |
| 206 |
|
| 207 |
bool pn_collector_more(pn_collector_t *collector) |
| 208 |
{ |
| 209 |
assert(collector); |
| 210 |
return collector->head && collector->head->next; |
| 211 |
} |
| 212 |
|
| 213 |
static void pn_event_initialize(pn_event_t *event) |
| 214 |
{ |
| 215 |
event->pool = NULL; |
| 216 |
event->type = PN_EVENT_NONE; |
| 217 |
event->clazz = NULL; |
| 218 |
event->context = NULL; |
| 219 |
event->next = NULL; |
| 220 |
event->attachments = pn_record(); |
| 221 |
} |
| 222 |
|
| 223 |
static void pn_event_finalize(pn_event_t *event) { |
| 224 |
// decref before adding to the free list |
| 225 |
if (event->clazz && event->context) { |
| 226 |
pn_class_decref(event->clazz, event->context); |
| 227 |
} |
| 228 |
|
| 229 |
pn_list_t *pool = event->pool; |
| 230 |
|
| 231 |
if (pool && pn_refcount(pool) > 1) { |
| 232 |
event->pool = NULL; |
| 233 |
event->type = PN_EVENT_NONE; |
| 234 |
event->clazz = NULL; |
| 235 |
event->context = NULL; |
| 236 |
event->next = NULL; |
| 237 |
pn_record_clear(event->attachments); |
| 238 |
pn_list_add(pool, event); |
| 239 |
} else { |
| 240 |
pn_decref(event->attachments); |
| 241 |
} |
| 242 |
|
| 243 |
pn_decref(pool); |
| 244 |
} |
| 245 |
|
| 246 |
static int pn_event_inspect(pn_event_t *event, pn_string_t *dst) |
| 247 |
{ |
| 248 |
assert(event); |
| 249 |
assert(dst); |
| 250 |
const char *name = pn_event_type_name(event->type); |
| 251 |
int err; |
| 252 |
if (name) { |
| 253 |
err = pn_string_addf(dst, "(%s", pn_event_type_name(event->type)); |
| 254 |
} else { |
| 255 |
err = pn_string_addf(dst, "(<%u>", (unsigned int) event->type); |
| 256 |
} |
| 257 |
if (err) return err; |
| 258 |
if (event->context) { |
| 259 |
err = pn_string_addf(dst, ", "); |
| 260 |
if (err) return err; |
| 261 |
err = pn_class_inspect(event->clazz, event->context, dst); |
| 262 |
if (err) return err; |
| 263 |
} |
| 264 |
|
| 265 |
return pn_string_addf(dst, ")"); |
| 266 |
} |
| 267 |
|
| 268 |
#define pn_event_hashcode NULL |
| 269 |
#define pn_event_compare NULL |
| 270 |
|
| 271 |
PN_CLASSDEF(pn_event) |
| 272 |
|
| 273 |
pn_event_t *pn_event(void) |
| 274 |
{ |
| 275 |
return pn_event_new(); |
| 276 |
} |
| 277 |
|
| 278 |
pn_event_type_t pn_event_type(pn_event_t *event) |
| 279 |
{ |
| 280 |
return event ? event->type : PN_EVENT_NONE; |
| 281 |
} |
| 282 |
|
| 283 |
const pn_class_t *pn_event_class(pn_event_t *event) |
| 284 |
{ |
| 285 |
assert(event); |
| 286 |
return event->clazz; |
| 287 |
} |
| 288 |
|
| 289 |
void *pn_event_context(pn_event_t *event) |
| 290 |
{ |
| 291 |
assert(event); |
| 292 |
return event->context; |
| 293 |
} |
| 294 |
|
| 295 |
pn_record_t *pn_event_attachments(pn_event_t *event) |
| 296 |
{ |
| 297 |
assert(event); |
| 298 |
return event->attachments; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
const char *pn_event_type_name(pn_event_type_t type) |
| 303 |
{ |
| 304 |
#define CASE(X) case X: return #X |
| 305 |
switch (type) { |
| 306 |
CASE(PN_EVENT_NONE); |
| 307 |
CASE(PN_REACTOR_INIT); |
| 308 |
CASE(PN_REACTOR_QUIESCED); |
| 309 |
CASE(PN_REACTOR_FINAL); |
| 310 |
CASE(PN_TIMER_TASK); |
| 311 |
CASE(PN_CONNECTION_INIT); |
| 312 |
CASE(PN_CONNECTION_BOUND); |
| 313 |
CASE(PN_CONNECTION_UNBOUND); |
| 314 |
CASE(PN_CONNECTION_REMOTE_OPEN); |
| 315 |
CASE(PN_CONNECTION_LOCAL_OPEN); |
| 316 |
CASE(PN_CONNECTION_REMOTE_CLOSE); |
| 317 |
CASE(PN_CONNECTION_LOCAL_CLOSE); |
| 318 |
CASE(PN_CONNECTION_FINAL); |
| 319 |
CASE(PN_SESSION_INIT); |
| 320 |
CASE(PN_SESSION_REMOTE_OPEN); |
| 321 |
CASE(PN_SESSION_LOCAL_OPEN); |
| 322 |
CASE(PN_SESSION_REMOTE_CLOSE); |
| 323 |
CASE(PN_SESSION_LOCAL_CLOSE); |
| 324 |
CASE(PN_SESSION_FINAL); |
| 325 |
CASE(PN_LINK_INIT); |
| 326 |
CASE(PN_LINK_REMOTE_OPEN); |
| 327 |
CASE(PN_LINK_LOCAL_OPEN); |
| 328 |
CASE(PN_LINK_REMOTE_CLOSE); |
| 329 |
CASE(PN_LINK_LOCAL_DETACH); |
| 330 |
CASE(PN_LINK_REMOTE_DETACH); |
| 331 |
CASE(PN_LINK_LOCAL_CLOSE); |
| 332 |
CASE(PN_LINK_FLOW); |
| 333 |
CASE(PN_LINK_FINAL); |
| 334 |
CASE(PN_DELIVERY); |
| 335 |
CASE(PN_TRANSPORT); |
| 336 |
CASE(PN_TRANSPORT_AUTHENTICATED); |
| 337 |
CASE(PN_TRANSPORT_ERROR); |
| 338 |
CASE(PN_TRANSPORT_HEAD_CLOSED); |
| 339 |
CASE(PN_TRANSPORT_TAIL_CLOSED); |
| 340 |
CASE(PN_TRANSPORT_CLOSED); |
| 341 |
CASE(PN_SELECTABLE_INIT); |
| 342 |
CASE(PN_SELECTABLE_UPDATED); |
| 343 |
CASE(PN_SELECTABLE_READABLE); |
| 344 |
CASE(PN_SELECTABLE_WRITABLE); |
| 345 |
CASE(PN_SELECTABLE_ERROR); |
| 346 |
CASE(PN_SELECTABLE_EXPIRED); |
| 347 |
CASE(PN_SELECTABLE_FINAL); |
| 348 |
CASE(PN_CONNECTION_WAKE); |
| 349 |
CASE(PN_LISTENER_ACCEPT); |
| 350 |
CASE(PN_LISTENER_CLOSE); |
| 351 |
CASE(PN_PROACTOR_INTERRUPT); |
| 352 |
CASE(PN_PROACTOR_TIMEOUT); |
| 353 |
CASE(PN_PROACTOR_INACTIVE); |
| 354 |
CASE(PN_LISTENER_OPEN); |
| 355 |
CASE(PN_RAW_CONNECTION_CONNECTED); |
| 356 |
CASE(PN_RAW_CONNECTION_DISCONNECTED); |
| 357 |
CASE(PN_RAW_CONNECTION_CLOSED_READ); |
| 358 |
CASE(PN_RAW_CONNECTION_CLOSED_WRITE); |
| 359 |
CASE(PN_RAW_CONNECTION_NEED_READ_BUFFERS); |
| 360 |
CASE(PN_RAW_CONNECTION_NEED_WRITE_BUFFERS); |
| 361 |
CASE(PN_RAW_CONNECTION_READ); |
| 362 |
CASE(PN_RAW_CONNECTION_WRITTEN); |
| 363 |
CASE(PN_RAW_CONNECTION_WAKE); |
| 364 |
CASE(PN_RAW_CONNECTION_DRAIN_BUFFERS); |
| 365 |
default: |
| 366 |
return "PN_UNKNOWN"; |
| 367 |
} |
| 368 |
return NULL; |
| 369 |
#undef CASE |
| 370 |
} |