| 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/object.h> |
| 23 |
#include <proton/reactor.h> |
| 24 |
#include <proton/event.h> |
| 25 |
#include <string.h> |
| 26 |
#include <assert.h> |
| 27 |
|
| 28 |
struct pn_handler_t { |
| 29 |
void (*dispatch) (pn_handler_t *, pn_event_t *, pn_event_type_t); |
| 30 |
void (*finalize) (pn_handler_t *); |
| 31 |
pn_list_t *children; |
| 32 |
}; |
| 33 |
|
| 34 |
void pn_handler_initialize(void *object) { |
| 35 |
pn_handler_t *handler = (pn_handler_t *) object; |
| 36 |
handler->dispatch = NULL; |
| 37 |
handler->children = NULL; |
| 38 |
} |
| 39 |
|
| 40 |
void pn_handler_finalize(void *object) { |
| 41 |
pn_handler_t *handler = (pn_handler_t *) object; |
| 42 |
if (handler->finalize) { |
| 43 |
handler->finalize(handler); |
| 44 |
} |
| 45 |
pn_free(handler->children); |
| 46 |
} |
| 47 |
|
| 48 |
#define pn_handler_hashcode NULL |
| 49 |
#define pn_handler_compare NULL |
| 50 |
#define pn_handler_inspect NULL |
| 51 |
|
| 52 |
pn_handler_t *pn_handler(void (*dispatch)(pn_handler_t *, pn_event_t *, pn_event_type_t)) { |
| 53 |
return pn_handler_new(dispatch, 0, NULL); |
| 54 |
} |
| 55 |
|
| 56 |
pn_handler_t *pn_handler_new(void (*dispatch)(pn_handler_t *, pn_event_t *, pn_event_type_t), size_t size, |
| 57 |
void (*finalize)(pn_handler_t *)) { |
| 58 |
static const pn_class_t clazz = PN_CLASS(pn_handler); |
| 59 |
pn_handler_t *handler = (pn_handler_t *) pn_class_new(&clazz, sizeof(pn_handler_t) + size); |
| 60 |
handler->dispatch = dispatch; |
| 61 |
handler->finalize = finalize; |
| 62 |
memset(pn_handler_mem(handler), 0, size); |
| 63 |
return handler; |
| 64 |
} |
| 65 |
|
| 66 |
void pn_handler_free(pn_handler_t *handler) { |
| 67 |
if (handler) { |
| 68 |
if (handler->children) { |
| 69 |
size_t n = pn_list_size(handler->children); |
| 70 |
for (size_t i = 0; i < n; i++) { |
| 71 |
void *child = pn_list_get(handler->children, i); |
| 72 |
pn_decref(child); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
pn_decref(handler); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void *pn_handler_mem(pn_handler_t *handler) { |
| 81 |
return (void *) (handler + 1); |
| 82 |
} |
| 83 |
|
| 84 |
void pn_handler_add(pn_handler_t *handler, pn_handler_t *child) { |
| 85 |
assert(handler); |
| 86 |
if (!handler->children) { |
| 87 |
handler->children = pn_list(PN_OBJECT, 0); |
| 88 |
} |
| 89 |
pn_list_add(handler->children, child); |
| 90 |
} |
| 91 |
|
| 92 |
void pn_handler_clear(pn_handler_t *handler) { |
| 93 |
assert(handler); |
| 94 |
if (handler->children) { |
| 95 |
pn_list_clear(handler->children); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void pn_handler_dispatch(pn_handler_t *handler, pn_event_t *event, pn_event_type_t type) { |
| 100 |
assert(handler); |
| 101 |
if (handler->dispatch) { |
| 102 |
handler->dispatch(handler, event, type); |
| 103 |
} |
| 104 |
if (handler->children) { |
| 105 |
size_t n = pn_list_size(handler->children); |
| 106 |
for (size_t i = 0; i < n; i++) { |
| 107 |
pn_handler_t *child = (pn_handler_t *) pn_list_get(handler->children, i); |
| 108 |
pn_handler_dispatch(child, event, type); |
| 109 |
} |
| 110 |
} |
| 111 |
} |