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> | |
35 | #include <babeltrace/graph/private-notification-iterator.h> | |
c55a9f58 | 36 | #include <babeltrace/types.h> |
47e5a032 | 37 | |
3230ee6b PP |
38 | struct bt_port; |
39 | ||
fa054faf PP |
40 | enum bt_notification_iterator_notif_type { |
41 | BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT = (1U << 0), | |
42 | BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY = (1U << 1), | |
43 | BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN = (1U << 2), | |
44 | BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END = (1U << 3), | |
45 | BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN = (1U << 4), | |
46 | BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END = (1U << 5), | |
47 | }; | |
48 | ||
bd14d768 PP |
49 | enum bt_notification_iterator_state { |
50 | /* Iterator is active, not at the end yet, and not finalized. */ | |
51 | BT_NOTIFICATION_ITERATOR_STATE_ACTIVE, | |
52 | ||
53 | /* | |
54 | * Iterator is ended, not finalized yet: the "next" method | |
55 | * returns BT_NOTIFICATION_ITERATOR_STATUS_END. | |
56 | */ | |
57 | BT_NOTIFICATION_ITERATOR_STATE_ENDED, | |
58 | ||
59 | /* | |
60 | * Iterator is finalized, but not at the end yet. This means | |
61 | * that the "next" method can still return queued notifications | |
62 | * before returning the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED | |
63 | * status. | |
64 | */ | |
65 | BT_NOTIFICATION_ITERATOR_STATE_FINALIZED, | |
66 | ||
67 | /* | |
68 | * Iterator is finalized and ended: the "next" method always | |
69 | * returns BT_NOTIFICATION_ITERATOR_STATUS_CANCELED. | |
70 | */ | |
71 | BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED, | |
72 | }; | |
73 | ||
47e5a032 | 74 | struct bt_notification_iterator { |
b8a06801 | 75 | struct bt_object base; |
bd14d768 PP |
76 | struct bt_component *upstream_component; /* Weak */ |
77 | struct bt_port *upstream_port; /* Weak */ | |
78 | struct bt_connection *connection; /* Weak */ | |
3230ee6b PP |
79 | struct bt_notification *current_notification; /* owned by this */ |
80 | GQueue *queue; /* struct bt_notification * (owned by this) */ | |
81 | ||
82 | /* | |
83 | * This hash table keeps the state of a stream as viewed by | |
84 | * this notification iterator. This is used to: | |
85 | * | |
86 | * * Automatically enqueue "stream begin", "packet begin", | |
87 | * "packet end", and "stream end" notifications depending | |
88 | * on the stream's state and on the next notification returned | |
89 | * by the upstream component. | |
90 | * | |
91 | * * Make sure that, once the notification iterator has seen | |
92 | * a "stream end" notification for a given stream, that no | |
93 | * other notifications which refer to this stream can be | |
94 | * delivered by this iterator. | |
95 | * | |
96 | * The key (struct bt_ctf_stream *) is not owned by this. The | |
97 | * value is an allocated state structure. | |
98 | */ | |
99 | GHashTable *stream_states; | |
100 | ||
101 | /* | |
102 | * This is an array of actions which can be rolled back. It's | |
103 | * similar to the memento pattern, but it's not exactly that. It | |
104 | * is allocated once and reset for each notification to process. | |
105 | * More details near the implementation. | |
106 | */ | |
107 | GArray *actions; | |
108 | ||
fa054faf PP |
109 | /* |
110 | * This is a mask of notifications to which the user of this | |
111 | * iterator is subscribed | |
112 | * (see enum bt_notification_iterator_notif_type above). | |
113 | */ | |
114 | uint32_t subscription_mask; | |
115 | ||
bd14d768 | 116 | enum bt_notification_iterator_state state; |
47e5a032 JG |
117 | void *user_data; |
118 | }; | |
119 | ||
890882ef PP |
120 | static inline |
121 | struct bt_notification_iterator *bt_notification_iterator_from_private( | |
122 | struct bt_private_notification_iterator *private_notification_iterator) | |
123 | { | |
124 | return (void *) private_notification_iterator; | |
125 | } | |
126 | ||
127 | static inline | |
128 | struct bt_private_notification_iterator * | |
129 | bt_private_notification_iterator_from_notification_iterator( | |
130 | struct bt_notification_iterator *notification_iterator) | |
131 | { | |
132 | return (void *) notification_iterator; | |
133 | } | |
134 | ||
47e5a032 JG |
135 | /** |
136 | * Allocate a notification iterator. | |
137 | * | |
138 | * @param component Component instance | |
139 | * @returns A notification iterator instance | |
140 | */ | |
141 | BT_HIDDEN | |
142 | struct bt_notification_iterator *bt_notification_iterator_create( | |
3230ee6b | 143 | struct bt_component *upstream_component, |
fa054faf | 144 | struct bt_port *upstream_port, |
bd14d768 PP |
145 | const enum bt_notification_type *notification_types, |
146 | struct bt_connection *connection); | |
47e5a032 JG |
147 | |
148 | /** | |
149 | * Validate a notification iterator. | |
150 | * | |
151 | * @param iterator Notification iterator instance | |
152 | * @returns One of #bt_component_status values | |
153 | */ | |
154 | BT_HIDDEN | |
155 | enum bt_notification_iterator_status bt_notification_iterator_validate( | |
156 | struct bt_notification_iterator *iterator); | |
157 | ||
bd14d768 PP |
158 | BT_HIDDEN |
159 | void bt_notification_iterator_finalize( | |
160 | struct bt_notification_iterator *iterator); | |
161 | ||
162 | BT_HIDDEN | |
163 | void bt_notification_iterator_set_connection( | |
164 | struct bt_notification_iterator *iterator, | |
165 | struct bt_connection *connection); | |
166 | ||
a36bfb16 PP |
167 | static inline |
168 | const char *bt_notification_iterator_status_string( | |
169 | enum bt_notification_iterator_status status) | |
170 | { | |
171 | switch (status) { | |
172 | case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED: | |
173 | return "BT_NOTIFICATION_ITERATOR_STATUS_CANCELED"; | |
174 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
175 | return "BT_NOTIFICATION_ITERATOR_STATUS_AGAIN"; | |
176 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
177 | return "BT_NOTIFICATION_ITERATOR_STATUS_END"; | |
178 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: | |
179 | return "BT_NOTIFICATION_ITERATOR_STATUS_OK"; | |
180 | case BT_NOTIFICATION_ITERATOR_STATUS_INVALID: | |
181 | return "BT_NOTIFICATION_ITERATOR_STATUS_INVALID"; | |
182 | case BT_NOTIFICATION_ITERATOR_STATUS_ERROR: | |
183 | return "BT_NOTIFICATION_ITERATOR_STATUS_ERROR"; | |
184 | case BT_NOTIFICATION_ITERATOR_STATUS_NOMEM: | |
185 | return "BT_NOTIFICATION_ITERATOR_STATUS_NOMEM"; | |
186 | case BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED: | |
187 | return "BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED"; | |
188 | default: | |
189 | return "(unknown)"; | |
190 | } | |
191 | }; | |
192 | ||
33b34c43 | 193 | #endif /* BABELTRACE_COMPONENT_NOTIFICATION_ITERATOR_INTERNAL_H */ |