| 1 | #ifndef BABELTRACE_GRAPH_NOTIFICATION_ITERATOR_INTERNAL_H |
| 2 | #define BABELTRACE_GRAPH_NOTIFICATION_ITERATOR_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * BabelTrace - Notification Iterator Internal |
| 6 | * |
| 7 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 8 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include <babeltrace/babeltrace-internal.h> |
| 30 | #include <babeltrace/object-internal.h> |
| 31 | #include <babeltrace/ref-internal.h> |
| 32 | #include <babeltrace/graph/connection.h> |
| 33 | #include <babeltrace/graph/notification.h> |
| 34 | #include <babeltrace/graph/notification-iterator.h> |
| 35 | #include <babeltrace/graph/private-connection-private-notification-iterator.h> |
| 36 | #include <babeltrace/types.h> |
| 37 | #include <babeltrace/assert-internal.h> |
| 38 | #include <stdbool.h> |
| 39 | |
| 40 | struct bt_port; |
| 41 | struct bt_graph; |
| 42 | |
| 43 | enum bt_notification_iterator_type { |
| 44 | BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION, |
| 45 | BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT, |
| 46 | }; |
| 47 | |
| 48 | enum bt_private_connection_notification_iterator_notif_type { |
| 49 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT = (1U << 0), |
| 50 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY = (1U << 1), |
| 51 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN = (1U << 2), |
| 52 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END = (1U << 3), |
| 53 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN = (1U << 4), |
| 54 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END = (1U << 5), |
| 55 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS = (1U << 6), |
| 56 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS = (1U << 7), |
| 57 | }; |
| 58 | |
| 59 | enum bt_private_connection_notification_iterator_state { |
| 60 | /* Iterator is not initialized. */ |
| 61 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED, |
| 62 | |
| 63 | /* Iterator is active, not at the end yet, and not finalized. */ |
| 64 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE, |
| 65 | |
| 66 | /* |
| 67 | * Iterator is ended, not finalized yet: the "next" method |
| 68 | * returns BT_NOTIFICATION_ITERATOR_STATUS_END. |
| 69 | */ |
| 70 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED, |
| 71 | |
| 72 | /* |
| 73 | * Iterator is finalized, but not at the end yet. This means |
| 74 | * that the "next" method can still return queued notifications |
| 75 | * before returning the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED |
| 76 | * status. |
| 77 | */ |
| 78 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED, |
| 79 | |
| 80 | /* |
| 81 | * Iterator is finalized and ended: the "next" method always |
| 82 | * returns BT_NOTIFICATION_ITERATOR_STATUS_CANCELED. |
| 83 | */ |
| 84 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED, |
| 85 | }; |
| 86 | |
| 87 | struct bt_notification_iterator { |
| 88 | struct bt_object base; |
| 89 | enum bt_notification_iterator_type type; |
| 90 | struct bt_notification *current_notification; /* owned by this */ |
| 91 | }; |
| 92 | |
| 93 | struct bt_notification_iterator_private_connection { |
| 94 | struct bt_notification_iterator base; |
| 95 | struct bt_component *upstream_component; /* Weak */ |
| 96 | struct bt_port *upstream_port; /* Weak */ |
| 97 | struct bt_connection *connection; /* Weak */ |
| 98 | struct bt_graph *graph; /* Weak */ |
| 99 | |
| 100 | /* |
| 101 | * This hash table keeps the state of a stream as viewed by |
| 102 | * this notification iterator. This is used to, in developer |
| 103 | * mode: |
| 104 | * |
| 105 | * * Automatically enqueue "stream begin", "packet begin", |
| 106 | * "packet end", and "stream end" notifications depending |
| 107 | * on the stream's state and on the next notification returned |
| 108 | * by the upstream component. |
| 109 | * |
| 110 | * * Make sure that, once the notification iterator has seen a |
| 111 | * "stream end" notification for a given stream, no other |
| 112 | * notifications which refer to this stream can be delivered |
| 113 | * by this iterator. |
| 114 | * |
| 115 | * The key (struct bt_stream *) is not owned by this. The |
| 116 | * value is an allocated state structure. |
| 117 | */ |
| 118 | GHashTable *stream_states; |
| 119 | |
| 120 | enum bt_private_connection_notification_iterator_state state; |
| 121 | void *user_data; |
| 122 | }; |
| 123 | |
| 124 | struct bt_notification_iterator_output_port { |
| 125 | struct bt_notification_iterator base; |
| 126 | struct bt_graph *graph; /* Owned by this */ |
| 127 | struct bt_component *colander; /* Owned by this */ |
| 128 | struct bt_port *output_port; /* Owned by this */ |
| 129 | }; |
| 130 | |
| 131 | static inline |
| 132 | struct bt_notification *bt_notification_iterator_borrow_current_notification( |
| 133 | struct bt_notification_iterator *iterator) |
| 134 | { |
| 135 | BT_ASSERT(iterator); |
| 136 | return iterator->current_notification; |
| 137 | } |
| 138 | |
| 139 | static inline |
| 140 | void bt_notification_iterator_replace_current_notification( |
| 141 | struct bt_notification_iterator *iterator, |
| 142 | struct bt_notification *notification) |
| 143 | { |
| 144 | BT_ASSERT(iterator); |
| 145 | bt_put(iterator->current_notification); |
| 146 | iterator->current_notification = bt_get(notification); |
| 147 | } |
| 148 | |
| 149 | static inline |
| 150 | struct bt_private_connection_private_notification_iterator * |
| 151 | bt_private_connection_private_notification_iterator_from_notification_iterator( |
| 152 | struct bt_notification_iterator_private_connection *iterator) |
| 153 | { |
| 154 | return (void *) iterator; |
| 155 | } |
| 156 | |
| 157 | BT_HIDDEN |
| 158 | enum bt_connection_status bt_private_connection_notification_iterator_create( |
| 159 | struct bt_component *upstream_comp, |
| 160 | struct bt_port *upstream_port, |
| 161 | struct bt_connection *connection, |
| 162 | struct bt_notification_iterator_private_connection **iterator); |
| 163 | |
| 164 | BT_HIDDEN |
| 165 | void bt_private_connection_notification_iterator_finalize( |
| 166 | struct bt_notification_iterator_private_connection *iterator); |
| 167 | |
| 168 | BT_HIDDEN |
| 169 | void bt_private_connection_notification_iterator_set_connection( |
| 170 | struct bt_notification_iterator_private_connection *iterator, |
| 171 | struct bt_connection *connection); |
| 172 | |
| 173 | static inline |
| 174 | const char *bt_notification_iterator_status_string( |
| 175 | enum bt_notification_iterator_status status) |
| 176 | { |
| 177 | switch (status) { |
| 178 | case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED: |
| 179 | return "BT_NOTIFICATION_ITERATOR_STATUS_CANCELED"; |
| 180 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: |
| 181 | return "BT_NOTIFICATION_ITERATOR_STATUS_AGAIN"; |
| 182 | case BT_NOTIFICATION_ITERATOR_STATUS_END: |
| 183 | return "BT_NOTIFICATION_ITERATOR_STATUS_END"; |
| 184 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: |
| 185 | return "BT_NOTIFICATION_ITERATOR_STATUS_OK"; |
| 186 | case BT_NOTIFICATION_ITERATOR_STATUS_INVALID: |
| 187 | return "BT_NOTIFICATION_ITERATOR_STATUS_INVALID"; |
| 188 | case BT_NOTIFICATION_ITERATOR_STATUS_ERROR: |
| 189 | return "BT_NOTIFICATION_ITERATOR_STATUS_ERROR"; |
| 190 | case BT_NOTIFICATION_ITERATOR_STATUS_NOMEM: |
| 191 | return "BT_NOTIFICATION_ITERATOR_STATUS_NOMEM"; |
| 192 | case BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED: |
| 193 | return "BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED"; |
| 194 | default: |
| 195 | return "(unknown)"; |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | static inline |
| 200 | const char *bt_private_connection_notification_iterator_state_string( |
| 201 | enum bt_private_connection_notification_iterator_state state) |
| 202 | { |
| 203 | switch (state) { |
| 204 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE: |
| 205 | return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE"; |
| 206 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED: |
| 207 | return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED"; |
| 208 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED: |
| 209 | return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED"; |
| 210 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED: |
| 211 | return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED"; |
| 212 | default: |
| 213 | return "(unknown)"; |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | #endif /* BABELTRACE_GRAPH_NOTIFICATION_ITERATOR_INTERNAL_H */ |