Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
4 | # | |
5 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | # of this software and associated documentation files (the "Software"), to deal | |
7 | # in the Software without restriction, including without limitation the rights | |
8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | # copies of the Software, and to permit persons to whom the Software is | |
10 | # furnished to do so, subject to the following conditions: | |
11 | # | |
12 | # The above copyright notice and this permission notice shall be included in | |
13 | # all copies or substantial portions of the Software. | |
14 | # | |
15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 | # THE SOFTWARE. | |
22 | ||
23 | from bt2 import native_bt, object, utils | |
3fb99a22 | 24 | from bt2 import message as bt2_message |
81447b5b | 25 | import collections.abc |
3fb99a22 PP |
26 | from bt2 import stream as bt2_stream |
27 | from bt2 import event_class as bt2_event_class | |
28 | from bt2 import packet as bt2_packet | |
29 | from bt2 import port as bt2_port | |
30 | from bt2 import clock_class as bt2_clock_class | |
81447b5b PP |
31 | import bt2 |
32 | ||
33 | ||
5602ef81 | 34 | class _MessageIterator(collections.abc.Iterator): |
81447b5b | 35 | def __next__(self): |
811644b8 | 36 | raise NotImplementedError |
81447b5b PP |
37 | |
38 | ||
1975af3d SM |
39 | class _UserComponentInputPortMessageIterator(object._SharedObject, _MessageIterator): |
40 | _get_ref = staticmethod( | |
41 | native_bt.self_component_port_input_message_iterator_get_ref | |
42 | ) | |
43 | _put_ref = staticmethod( | |
44 | native_bt.self_component_port_input_message_iterator_put_ref | |
45 | ) | |
46 | ||
2ae9f48c | 47 | def __init__(self, ptr): |
cfbd7cf3 FD |
48 | self._current_msgs = [] |
49 | self._at = 0 | |
50 | super().__init__(ptr) | |
811644b8 PP |
51 | |
52 | def __next__(self): | |
2ae9f48c | 53 | if len(self._current_msgs) == self._at: |
1975af3d SM |
54 | status, msgs = native_bt.bt2_self_component_port_input_get_msg_range( |
55 | self._ptr | |
56 | ) | |
cfbd7cf3 FD |
57 | utils._handle_func_status( |
58 | status, 'unexpected error: cannot advance the message iterator' | |
59 | ) | |
2ae9f48c SM |
60 | self._current_msgs = msgs |
61 | self._at = 0 | |
62 | ||
63 | msg_ptr = self._current_msgs[self._at] | |
64 | self._at += 1 | |
65 | ||
3fb99a22 | 66 | return bt2_message._create_from_ptr(msg_ptr) |
81447b5b | 67 | |
f00b8d40 | 68 | def can_seek_beginning(self): |
1975af3d SM |
69 | status, res = native_bt.self_component_port_input_message_iterator_can_seek_beginning( |
70 | self._ptr | |
71 | ) | |
f2fb1b32 SM |
72 | utils._handle_func_status( |
73 | status, | |
74 | 'cannot check whether or not message iterator can seek its beginning', | |
75 | ) | |
f00b8d40 SM |
76 | return res != 0 |
77 | ||
78 | def seek_beginning(self): | |
b6909b73 | 79 | # Forget about buffered messages, they won't be valid after seeking. |
f00b8d40 SM |
80 | self._current_msgs.clear() |
81 | self._at = 0 | |
82 | ||
1975af3d SM |
83 | status = native_bt.self_component_port_input_message_iterator_seek_beginning( |
84 | self._ptr | |
85 | ) | |
cfbd7cf3 | 86 | utils._handle_func_status(status, 'cannot seek message iterator beginning') |
f00b8d40 | 87 | |
811644b8 | 88 | |
c5f330cd SM |
89 | # This is extended by the user to implement component classes in Python. It |
90 | # is created for a given output port when an input port message iterator is | |
91 | # created on the input port on the other side of the connection. It is also | |
92 | # created when an output port message iterator is created on this output port. | |
93 | # | |
94 | # Its purpose is to feed the messages that should go out through this output | |
95 | # port. | |
5602ef81 | 96 | class _UserMessageIterator(_MessageIterator): |
81447b5b | 97 | def __new__(cls, ptr): |
811644b8 | 98 | # User iterator objects are always created by the native side, |
81447b5b PP |
99 | # that is, never instantiated directly by Python code. |
100 | # | |
811644b8 PP |
101 | # The native code calls this, then manually calls |
102 | # self.__init__() without the `ptr` argument. The user has | |
103 | # access to self.component during this call, thanks to this | |
85906b6b | 104 | # self._bt_ptr argument being set. |
81447b5b | 105 | # |
85906b6b | 106 | # self._bt_ptr is NOT owned by this object here, so there's nothing |
81447b5b PP |
107 | # to do in __del__(). |
108 | self = super().__new__(cls) | |
85906b6b | 109 | self._bt_ptr = ptr |
81447b5b PP |
110 | return self |
111 | ||
85906b6b | 112 | def _bt_init_from_native(self, self_output_port_ptr): |
3fb99a22 | 113 | self_output_port = bt2_port._create_self_from_ptr_and_get_ref( |
cfbd7cf3 FD |
114 | self_output_port_ptr, native_bt.PORT_TYPE_OUTPUT |
115 | ) | |
c5f330cd SM |
116 | self.__init__(self_output_port) |
117 | ||
118 | def __init__(self, output_port): | |
81447b5b PP |
119 | pass |
120 | ||
121 | @property | |
811644b8 | 122 | def _component(self): |
85906b6b | 123 | return native_bt.bt2_get_user_component_from_user_msg_iter(self._bt_ptr) |
81447b5b | 124 | |
14503fb1 SM |
125 | @property |
126 | def _port(self): | |
127 | port_ptr = native_bt.self_message_iterator_borrow_port(self._bt_ptr) | |
128 | assert port_ptr is not None | |
129 | return bt2_port._create_self_from_ptr_and_get_ref( | |
130 | port_ptr, native_bt.PORT_TYPE_OUTPUT | |
131 | ) | |
132 | ||
81447b5b PP |
133 | @property |
134 | def addr(self): | |
85906b6b | 135 | return int(self._bt_ptr) |
81447b5b | 136 | |
9b4f9b42 PP |
137 | @property |
138 | def _is_interrupted(self): | |
139 | return bool(native_bt.self_message_iterator_is_interrupted(self._bt_ptr)) | |
140 | ||
6a91742b | 141 | def _user_finalize(self): |
81447b5b PP |
142 | pass |
143 | ||
811644b8 PP |
144 | def __next__(self): |
145 | raise bt2.Stop | |
146 | ||
85906b6b | 147 | def _bt_next_from_native(self): |
811644b8 PP |
148 | # this can raise anything: it's catched by the native part |
149 | try: | |
5602ef81 | 150 | msg = next(self) |
811644b8 PP |
151 | except StopIteration: |
152 | raise bt2.Stop | |
4c4935bf | 153 | except Exception: |
811644b8 PP |
154 | raise |
155 | ||
f0a42b33 | 156 | utils._check_type(msg, bt2_message._MessageConst) |
81447b5b | 157 | |
d79a8353 SM |
158 | # The reference we return will be given to the message array. |
159 | # However, the `msg` Python object may stay alive, if the user has kept | |
160 | # a reference to it. Acquire a new reference to account for that. | |
161 | msg._get_ref(msg._ptr) | |
162 | return int(msg._ptr) | |
2ae9f48c | 163 | |
85906b6b | 164 | def _bt_can_seek_beginning_from_native(self): |
f00b8d40 SM |
165 | # Here, we mimic the behavior of the C API: |
166 | # | |
14cfc8ce | 167 | # - If the iterator has a _user_can_seek_beginning method, |
6a91742b | 168 | # read it and use that result. |
5a096c63 | 169 | # - Otherwise, the presence or absence of a `_user_seek_beginning` |
f00b8d40 | 170 | # method indicates whether the iterator can seek beginning. |
6a91742b | 171 | if hasattr(self, '_user_can_seek_beginning'): |
14cfc8ce | 172 | can_seek_beginning = self._user_can_seek_beginning() |
f00b8d40 SM |
173 | utils._check_bool(can_seek_beginning) |
174 | return can_seek_beginning | |
175 | else: | |
6a91742b | 176 | return hasattr(self, '_user_seek_beginning') |
f00b8d40 | 177 | |
85906b6b | 178 | def _bt_seek_beginning_from_native(self): |
6a91742b | 179 | self._user_seek_beginning() |
f00b8d40 | 180 | |
ca02df0a | 181 | def _create_input_port_message_iterator(self, input_port): |
3fb99a22 | 182 | utils._check_type(input_port, bt2_port._UserComponentInputPort) |
ca02df0a | 183 | |
e803df70 | 184 | status, msg_iter_ptr = native_bt.bt2_self_component_port_input_message_iterator_create_from_message_iterator( |
ca02df0a PP |
185 | self._bt_ptr, input_port._ptr |
186 | ) | |
e803df70 SM |
187 | utils._handle_func_status(status, 'cannot create message iterator object') |
188 | assert msg_iter_ptr is not None | |
ca02df0a PP |
189 | |
190 | return _UserComponentInputPortMessageIterator(msg_iter_ptr) | |
191 | ||
36d9460d | 192 | def _create_event_message(self, event_class, parent, default_clock_snapshot=None): |
3fb99a22 | 193 | utils._check_type(event_class, bt2_event_class._EventClass) |
26fc5aed PP |
194 | |
195 | if event_class.stream_class.supports_packets: | |
3fb99a22 | 196 | utils._check_type(parent, bt2_packet._Packet) |
26fc5aed | 197 | else: |
3fb99a22 | 198 | utils._check_type(parent, bt2_stream._Stream) |
2ae9f48c SM |
199 | |
200 | if default_clock_snapshot is not None: | |
c6af194f | 201 | if event_class.stream_class.default_clock_class is None: |
cfbd7cf3 FD |
202 | raise ValueError( |
203 | 'event messages in this stream must not have a default clock snapshot' | |
204 | ) | |
c6af194f | 205 | |
2ae9f48c | 206 | utils._check_uint64(default_clock_snapshot) |
26fc5aed PP |
207 | |
208 | if event_class.stream_class.supports_packets: | |
209 | ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot( | |
cfbd7cf3 FD |
210 | self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot |
211 | ) | |
26fc5aed PP |
212 | else: |
213 | ptr = native_bt.message_event_create_with_default_clock_snapshot( | |
cfbd7cf3 FD |
214 | self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot |
215 | ) | |
2ae9f48c | 216 | else: |
c6af194f | 217 | if event_class.stream_class.default_clock_class is not None: |
cfbd7cf3 FD |
218 | raise ValueError( |
219 | 'event messages in this stream must have a default clock snapshot' | |
220 | ) | |
c6af194f | 221 | |
26fc5aed PP |
222 | if event_class.stream_class.supports_packets: |
223 | ptr = native_bt.message_event_create_with_packet( | |
cfbd7cf3 FD |
224 | self._bt_ptr, event_class._ptr, parent._ptr |
225 | ) | |
26fc5aed PP |
226 | else: |
227 | ptr = native_bt.message_event_create( | |
cfbd7cf3 FD |
228 | self._bt_ptr, event_class._ptr, parent._ptr |
229 | ) | |
2ae9f48c SM |
230 | |
231 | if ptr is None: | |
694c792b | 232 | raise bt2._MemoryError('cannot create event message object') |
2ae9f48c | 233 | |
3fb99a22 | 234 | return bt2_message._EventMessage(ptr) |
2ae9f48c | 235 | |
9ec609ec | 236 | def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot): |
3fb99a22 | 237 | utils._check_type(clock_class, bt2_clock_class._ClockClass) |
9ec609ec | 238 | ptr = native_bt.message_message_iterator_inactivity_create( |
cfbd7cf3 FD |
239 | self._bt_ptr, clock_class._ptr, clock_snapshot |
240 | ) | |
9ec609ec SM |
241 | |
242 | if ptr is None: | |
694c792b | 243 | raise bt2._MemoryError('cannot create inactivity message object') |
9ec609ec | 244 | |
3fb99a22 | 245 | return bt2_message._MessageIteratorInactivityMessage(ptr) |
9ec609ec | 246 | |
188edac1 | 247 | def _create_stream_beginning_message(self, stream, default_clock_snapshot=None): |
3fb99a22 | 248 | utils._check_type(stream, bt2_stream._Stream) |
2ae9f48c | 249 | |
85906b6b | 250 | ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr) |
2ae9f48c | 251 | if ptr is None: |
694c792b | 252 | raise bt2._MemoryError('cannot create stream beginning message object') |
2ae9f48c | 253 | |
3fb99a22 | 254 | msg = bt2_message._StreamBeginningMessage(ptr) |
9ec609ec | 255 | |
188edac1 SM |
256 | if default_clock_snapshot is not None: |
257 | msg._default_clock_snapshot = default_clock_snapshot | |
9ec609ec | 258 | |
9ec609ec SM |
259 | return msg |
260 | ||
188edac1 | 261 | def _create_stream_end_message(self, stream, default_clock_snapshot=None): |
3fb99a22 | 262 | utils._check_type(stream, bt2_stream._Stream) |
5f25509b | 263 | |
85906b6b | 264 | ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr) |
5f25509b | 265 | if ptr is None: |
694c792b | 266 | raise bt2._MemoryError('cannot create stream end message object') |
5f25509b | 267 | |
3fb99a22 | 268 | msg = bt2_message._StreamEndMessage(ptr) |
188edac1 SM |
269 | |
270 | if default_clock_snapshot is not None: | |
271 | msg._default_clock_snapshot = default_clock_snapshot | |
272 | ||
273 | return msg | |
5f25509b | 274 | |
2ae9f48c | 275 | def _create_packet_beginning_message(self, packet, default_clock_snapshot=None): |
3fb99a22 | 276 | utils._check_type(packet, bt2_packet._Packet) |
2ae9f48c | 277 | |
e8ac1aae | 278 | if packet.stream.cls.packets_have_beginning_default_clock_snapshot: |
2ae9f48c | 279 | if default_clock_snapshot is None: |
cfbd7cf3 FD |
280 | raise ValueError( |
281 | "packet beginning messages in this stream must have a default clock snapshot" | |
282 | ) | |
2ae9f48c SM |
283 | |
284 | utils._check_uint64(default_clock_snapshot) | |
285 | ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot( | |
cfbd7cf3 FD |
286 | self._bt_ptr, packet._ptr, default_clock_snapshot |
287 | ) | |
2ae9f48c SM |
288 | else: |
289 | if default_clock_snapshot is not None: | |
cfbd7cf3 FD |
290 | raise ValueError( |
291 | "packet beginning messages in this stream must not have a default clock snapshot" | |
292 | ) | |
2ae9f48c | 293 | |
85906b6b | 294 | ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr) |
2ae9f48c SM |
295 | |
296 | if ptr is None: | |
694c792b | 297 | raise bt2._MemoryError('cannot create packet beginning message object') |
2ae9f48c | 298 | |
3fb99a22 | 299 | return bt2_message._PacketBeginningMessage(ptr) |
5f25509b SM |
300 | |
301 | def _create_packet_end_message(self, packet, default_clock_snapshot=None): | |
3fb99a22 | 302 | utils._check_type(packet, bt2_packet._Packet) |
5f25509b | 303 | |
e8ac1aae | 304 | if packet.stream.cls.packets_have_end_default_clock_snapshot: |
9ec609ec | 305 | if default_clock_snapshot is None: |
cfbd7cf3 FD |
306 | raise ValueError( |
307 | "packet end messages in this stream must have a default clock snapshot" | |
308 | ) | |
9ec609ec | 309 | |
5f25509b SM |
310 | utils._check_uint64(default_clock_snapshot) |
311 | ptr = native_bt.message_packet_end_create_with_default_clock_snapshot( | |
cfbd7cf3 FD |
312 | self._bt_ptr, packet._ptr, default_clock_snapshot |
313 | ) | |
5f25509b | 314 | else: |
9ec609ec | 315 | if default_clock_snapshot is not None: |
cfbd7cf3 FD |
316 | raise ValueError( |
317 | "packet end messages in this stream must not have a default clock snapshot" | |
318 | ) | |
9ec609ec | 319 | |
85906b6b | 320 | ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr) |
5f25509b SM |
321 | |
322 | if ptr is None: | |
694c792b | 323 | raise bt2._MemoryError('cannot create packet end message object') |
5f25509b | 324 | |
3fb99a22 | 325 | return bt2_message._PacketEndMessage(ptr) |
9ec609ec | 326 | |
cfbd7cf3 FD |
327 | def _create_discarded_events_message( |
328 | self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None | |
329 | ): | |
3fb99a22 | 330 | utils._check_type(stream, bt2_stream._Stream) |
9ec609ec | 331 | |
e8ac1aae | 332 | if not stream.cls.supports_discarded_events: |
2e90378a PP |
333 | raise ValueError('stream class does not support discarded events') |
334 | ||
e8ac1aae | 335 | if stream.cls.discarded_events_have_default_clock_snapshots: |
2e90378a | 336 | if beg_clock_snapshot is None or end_clock_snapshot is None: |
cfbd7cf3 FD |
337 | raise ValueError( |
338 | 'discarded events have default clock snapshots for this stream class' | |
339 | ) | |
2e90378a | 340 | |
9ec609ec SM |
341 | utils._check_uint64(beg_clock_snapshot) |
342 | utils._check_uint64(end_clock_snapshot) | |
343 | ptr = native_bt.message_discarded_events_create_with_default_clock_snapshots( | |
cfbd7cf3 FD |
344 | self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot |
345 | ) | |
9ec609ec | 346 | else: |
2e90378a | 347 | if beg_clock_snapshot is not None or end_clock_snapshot is not None: |
cfbd7cf3 FD |
348 | raise ValueError( |
349 | 'discarded events have no default clock snapshots for this stream class' | |
350 | ) | |
2e90378a | 351 | |
cfbd7cf3 | 352 | ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr) |
9ec609ec SM |
353 | |
354 | if ptr is None: | |
694c792b | 355 | raise bt2._MemoryError('cannot discarded events message object') |
9ec609ec | 356 | |
3fb99a22 | 357 | msg = bt2_message._DiscardedEventsMessage(ptr) |
9ec609ec SM |
358 | |
359 | if count is not None: | |
360 | msg._count = count | |
361 | ||
362 | return msg | |
363 | ||
cfbd7cf3 FD |
364 | def _create_discarded_packets_message( |
365 | self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None | |
366 | ): | |
3fb99a22 | 367 | utils._check_type(stream, bt2_stream._Stream) |
9ec609ec | 368 | |
e8ac1aae | 369 | if not stream.cls.supports_discarded_packets: |
2e90378a PP |
370 | raise ValueError('stream class does not support discarded packets') |
371 | ||
e8ac1aae | 372 | if stream.cls.discarded_packets_have_default_clock_snapshots: |
2e90378a | 373 | if beg_clock_snapshot is None or end_clock_snapshot is None: |
cfbd7cf3 FD |
374 | raise ValueError( |
375 | 'discarded packets have default clock snapshots for this stream class' | |
376 | ) | |
2e90378a | 377 | |
9ec609ec SM |
378 | utils._check_uint64(beg_clock_snapshot) |
379 | utils._check_uint64(end_clock_snapshot) | |
380 | ptr = native_bt.message_discarded_packets_create_with_default_clock_snapshots( | |
cfbd7cf3 FD |
381 | self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot |
382 | ) | |
9ec609ec | 383 | else: |
2e90378a | 384 | if beg_clock_snapshot is not None or end_clock_snapshot is not None: |
cfbd7cf3 FD |
385 | raise ValueError( |
386 | 'discarded packets have no default clock snapshots for this stream class' | |
387 | ) | |
2e90378a | 388 | |
cfbd7cf3 | 389 | ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr) |
9ec609ec SM |
390 | |
391 | if ptr is None: | |
694c792b | 392 | raise bt2._MemoryError('cannot discarded packets message object') |
9ec609ec | 393 | |
3fb99a22 | 394 | msg = bt2_message._DiscardedPacketsMessage(ptr) |
9ec609ec SM |
395 | |
396 | if count is not None: | |
397 | msg._count = count | |
398 | ||
399 | return msg |