Commit | Line | Data |
---|---|---|
33b34c43 PP |
1 | #ifndef BABELTRACE_COMPONENT_NOTIFICATION_ITERATOR_INTERNAL_H |
2 | #define BABELTRACE_COMPONENT_NOTIFICATION_ITERATOR_INTERNAL_H | |
47e5a032 JG |
3 | |
4 | /* | |
5 | * BabelTrace - Notification Iterator Internal | |
6 | * | |
7 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3230ee6b | 8 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
47e5a032 JG |
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> | |
890882ef | 30 | #include <babeltrace/object-internal.h> |
fb2dcc52 | 31 | #include <babeltrace/ref-internal.h> |
bd14d768 | 32 | #include <babeltrace/graph/connection.h> |
b2e0c907 PP |
33 | #include <babeltrace/graph/notification.h> |
34 | #include <babeltrace/graph/notification-iterator.h> | |
90157d89 | 35 | #include <babeltrace/graph/private-connection-private-notification-iterator.h> |
c55a9f58 | 36 | #include <babeltrace/types.h> |
088d4023 | 37 | #include <stdbool.h> |
47e5a032 | 38 | |
3230ee6b PP |
39 | struct bt_port; |
40 | ||
90157d89 PP |
41 | enum bt_notification_iterator_type { |
42 | BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION, | |
fa054faf PP |
43 | }; |
44 | ||
90157d89 PP |
45 | enum bt_private_connection_notification_iterator_notif_type { |
46 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT = (1U << 0), | |
47 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY = (1U << 1), | |
48 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN = (1U << 2), | |
49 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END = (1U << 3), | |
50 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN = (1U << 4), | |
51 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END = (1U << 5), | |
52 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS = (1U << 6), | |
53 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS = (1U << 7), | |
54 | }; | |
55 | ||
56 | enum bt_private_connection_notification_iterator_state { | |
088d4023 | 57 | /* Iterator is not initialized. */ |
90157d89 | 58 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED, |
088d4023 | 59 | |
bd14d768 | 60 | /* Iterator is active, not at the end yet, and not finalized. */ |
90157d89 | 61 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE, |
bd14d768 PP |
62 | |
63 | /* | |
64 | * Iterator is ended, not finalized yet: the "next" method | |
65 | * returns BT_NOTIFICATION_ITERATOR_STATUS_END. | |
66 | */ | |
90157d89 | 67 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED, |
bd14d768 PP |
68 | |
69 | /* | |
70 | * Iterator is finalized, but not at the end yet. This means | |
71 | * that the "next" method can still return queued notifications | |
72 | * before returning the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED | |
73 | * status. | |
74 | */ | |
90157d89 | 75 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED, |
bd14d768 PP |
76 | |
77 | /* | |
78 | * Iterator is finalized and ended: the "next" method always | |
79 | * returns BT_NOTIFICATION_ITERATOR_STATUS_CANCELED. | |
80 | */ | |
90157d89 | 81 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED, |
bd14d768 PP |
82 | }; |
83 | ||
47e5a032 | 84 | struct bt_notification_iterator { |
b8a06801 | 85 | struct bt_object base; |
90157d89 PP |
86 | enum bt_notification_iterator_type type; |
87 | }; | |
88 | ||
89 | struct bt_notification_iterator_private_connection { | |
90 | struct bt_notification_iterator base; | |
bd14d768 PP |
91 | struct bt_component *upstream_component; /* Weak */ |
92 | struct bt_port *upstream_port; /* Weak */ | |
93 | struct bt_connection *connection; /* Weak */ | |
3230ee6b PP |
94 | struct bt_notification *current_notification; /* owned by this */ |
95 | GQueue *queue; /* struct bt_notification * (owned by this) */ | |
96 | ||
97 | /* | |
98 | * This hash table keeps the state of a stream as viewed by | |
99 | * this notification iterator. This is used to: | |
100 | * | |
101 | * * Automatically enqueue "stream begin", "packet begin", | |
102 | * "packet end", and "stream end" notifications depending | |
103 | * on the stream's state and on the next notification returned | |
104 | * by the upstream component. | |
105 | * | |
106 | * * Make sure that, once the notification iterator has seen | |
107 | * a "stream end" notification for a given stream, that no | |
108 | * other notifications which refer to this stream can be | |
109 | * delivered by this iterator. | |
110 | * | |
111 | * The key (struct bt_ctf_stream *) is not owned by this. The | |
112 | * value is an allocated state structure. | |
113 | */ | |
114 | GHashTable *stream_states; | |
115 | ||
116 | /* | |
117 | * This is an array of actions which can be rolled back. It's | |
118 | * similar to the memento pattern, but it's not exactly that. It | |
119 | * is allocated once and reset for each notification to process. | |
120 | * More details near the implementation. | |
121 | */ | |
122 | GArray *actions; | |
123 | ||
fa054faf PP |
124 | /* |
125 | * This is a mask of notifications to which the user of this | |
126 | * iterator is subscribed | |
90157d89 PP |
127 | * (see enum bt_private_connection_notification_iterator_notif_type |
128 | * above). | |
fa054faf PP |
129 | */ |
130 | uint32_t subscription_mask; | |
131 | ||
90157d89 | 132 | enum bt_private_connection_notification_iterator_state state; |
47e5a032 JG |
133 | void *user_data; |
134 | }; | |
135 | ||
890882ef | 136 | static inline |
90157d89 PP |
137 | struct bt_notification_iterator_private_connection * |
138 | bt_private_connection_notification_iterator_from_private( | |
139 | struct bt_private_connection_private_notification_iterator *private_notification_iterator) | |
890882ef PP |
140 | { |
141 | return (void *) private_notification_iterator; | |
142 | } | |
143 | ||
144 | static inline | |
90157d89 PP |
145 | struct bt_private_connection_private_notification_iterator * |
146 | bt_private_connection_private_notification_iterator_from_notification_iterator( | |
147 | struct bt_notification_iterator_private_connection *iterator) | |
890882ef | 148 | { |
90157d89 | 149 | return (void *) iterator; |
890882ef PP |
150 | } |
151 | ||
47e5a032 | 152 | BT_HIDDEN |
90157d89 | 153 | enum bt_connection_status bt_private_connection_notification_iterator_create( |
73d5c1ad | 154 | struct bt_component *upstream_comp, |
fa054faf | 155 | struct bt_port *upstream_port, |
bd14d768 | 156 | const enum bt_notification_type *notification_types, |
73d5c1ad | 157 | struct bt_connection *connection, |
90157d89 | 158 | struct bt_notification_iterator_private_connection **iterator); |
47e5a032 | 159 | |
bd14d768 | 160 | BT_HIDDEN |
90157d89 PP |
161 | void bt_private_connection_notification_iterator_finalize( |
162 | struct bt_notification_iterator_private_connection *iterator); | |
bd14d768 PP |
163 | |
164 | BT_HIDDEN | |
90157d89 PP |
165 | void bt_private_connection_notification_iterator_set_connection( |
166 | struct bt_notification_iterator_private_connection *iterator, | |
bd14d768 PP |
167 | struct bt_connection *connection); |
168 | ||
a36bfb16 PP |
169 | static inline |
170 | const char *bt_notification_iterator_status_string( | |
171 | enum bt_notification_iterator_status status) | |
172 | { | |
173 | switch (status) { | |
174 | case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED: | |
175 | return "BT_NOTIFICATION_ITERATOR_STATUS_CANCELED"; | |
176 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
177 | return "BT_NOTIFICATION_ITERATOR_STATUS_AGAIN"; | |
178 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
179 | return "BT_NOTIFICATION_ITERATOR_STATUS_END"; | |
180 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: | |
181 | return "BT_NOTIFICATION_ITERATOR_STATUS_OK"; | |
182 | case BT_NOTIFICATION_ITERATOR_STATUS_INVALID: | |
183 | return "BT_NOTIFICATION_ITERATOR_STATUS_INVALID"; | |
184 | case BT_NOTIFICATION_ITERATOR_STATUS_ERROR: | |
185 | return "BT_NOTIFICATION_ITERATOR_STATUS_ERROR"; | |
186 | case BT_NOTIFICATION_ITERATOR_STATUS_NOMEM: | |
187 | return "BT_NOTIFICATION_ITERATOR_STATUS_NOMEM"; | |
188 | case BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED: | |
189 | return "BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED"; | |
190 | default: | |
191 | return "(unknown)"; | |
192 | } | |
193 | }; | |
194 | ||
8f9d7550 | 195 | static inline |
90157d89 PP |
196 | const char *bt_private_connection_notification_iterator_state_string( |
197 | enum bt_private_connection_notification_iterator_state state) | |
8f9d7550 PP |
198 | { |
199 | switch (state) { | |
90157d89 PP |
200 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE: |
201 | return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE"; | |
202 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED: | |
203 | return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED"; | |
204 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED: | |
205 | return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED"; | |
206 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED: | |
207 | return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED"; | |
8f9d7550 PP |
208 | default: |
209 | return "(unknown)"; | |
210 | } | |
211 | }; | |
212 | ||
33b34c43 | 213 | #endif /* BABELTRACE_COMPONENT_NOTIFICATION_ITERATOR_INTERNAL_H */ |