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 | |
c946c9de | 24 | from bt2 import message as bt2_message |
81447b5b | 25 | import collections.abc |
c946c9de 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 | ||
fa4c33e3 | 34 | class _MessageIterator(collections.abc.Iterator): |
81447b5b | 35 | def __next__(self): |
f6a5e476 | 36 | raise NotImplementedError |
81447b5b PP |
37 | |
38 | ||
a43da05a 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 | ||
27d97a3f | 47 | def __init__(self, ptr): |
61d96b89 FD |
48 | self._current_msgs = [] |
49 | self._at = 0 | |
50 | super().__init__(ptr) | |
f6a5e476 PP |
51 | |
52 | def __next__(self): | |
27d97a3f | 53 | if len(self._current_msgs) == self._at: |
a43da05a SM |
54 | status, msgs = native_bt.bt2_self_component_port_input_get_msg_range( |
55 | self._ptr | |
56 | ) | |
61d96b89 FD |
57 | utils._handle_func_status( |
58 | status, 'unexpected error: cannot advance the message iterator' | |
59 | ) | |
27d97a3f SM |
60 | self._current_msgs = msgs |
61 | self._at = 0 | |
62 | ||
63 | msg_ptr = self._current_msgs[self._at] | |
64 | self._at += 1 | |
65 | ||
c946c9de | 66 | return bt2_message._create_from_ptr(msg_ptr) |
81447b5b | 67 | |
39ddfa44 | 68 | def can_seek_beginning(self): |
a43da05a SM |
69 | status, res = native_bt.self_component_port_input_message_iterator_can_seek_beginning( |
70 | self._ptr | |
71 | ) | |
9e8e8b43 SM |
72 | utils._handle_func_status( |
73 | status, | |
74 | 'cannot check whether or not message iterator can seek its beginning', | |
75 | ) | |
39ddfa44 SM |
76 | return res != 0 |
77 | ||
78 | def seek_beginning(self): | |
2a02ed93 | 79 | # Forget about buffered messages, they won't be valid after seeking. |
39ddfa44 SM |
80 | self._current_msgs.clear() |
81 | self._at = 0 | |
82 | ||
a43da05a SM |
83 | status = native_bt.self_component_port_input_message_iterator_seek_beginning( |
84 | self._ptr | |
85 | ) | |
61d96b89 | 86 | utils._handle_func_status(status, 'cannot seek message iterator beginning') |
39ddfa44 | 87 | |
24a68958 SM |
88 | def can_seek_ns_from_origin(self, ns_from_origin): |
89 | utils._check_int64(ns_from_origin) | |
90 | status, res = native_bt.self_component_port_input_message_iterator_can_seek_ns_from_origin( | |
91 | self._ptr, ns_from_origin | |
92 | ) | |
93 | utils._handle_func_status( | |
94 | status, | |
95 | 'cannot check whether or not message iterator can seek given ns from origin', | |
96 | ) | |
97 | return res != 0 | |
98 | ||
99 | def seek_ns_from_origin(self, ns_from_origin): | |
100 | utils._check_int64(ns_from_origin) | |
101 | ||
102 | # Forget about buffered messages, they won't be valid after seeking. | |
103 | self._current_msgs.clear() | |
104 | self._at = 0 | |
105 | ||
106 | status = native_bt.self_component_port_input_message_iterator_seek_ns_from_origin( | |
107 | self._ptr, ns_from_origin | |
108 | ) | |
109 | utils._handle_func_status( | |
110 | status, 'message iterator cannot seek given ns from origin' | |
111 | ) | |
112 | ||
f6a5e476 | 113 | |
a4dcfa96 SM |
114 | # This is extended by the user to implement component classes in Python. It |
115 | # is created for a given output port when an input port message iterator is | |
116 | # created on the input port on the other side of the connection. It is also | |
117 | # created when an output port message iterator is created on this output port. | |
118 | # | |
119 | # Its purpose is to feed the messages that should go out through this output | |
120 | # port. | |
fa4c33e3 | 121 | class _UserMessageIterator(_MessageIterator): |
81447b5b | 122 | def __new__(cls, ptr): |
f6a5e476 | 123 | # User iterator objects are always created by the native side, |
81447b5b PP |
124 | # that is, never instantiated directly by Python code. |
125 | # | |
f6a5e476 PP |
126 | # The native code calls this, then manually calls |
127 | # self.__init__() without the `ptr` argument. The user has | |
128 | # access to self.component during this call, thanks to this | |
deec48a6 | 129 | # self._bt_ptr argument being set. |
81447b5b | 130 | # |
deec48a6 | 131 | # self._bt_ptr is NOT owned by this object here, so there's nothing |
81447b5b PP |
132 | # to do in __del__(). |
133 | self = super().__new__(cls) | |
deec48a6 | 134 | self._bt_ptr = ptr |
81447b5b PP |
135 | return self |
136 | ||
deec48a6 | 137 | def _bt_init_from_native(self, self_output_port_ptr): |
c946c9de | 138 | self_output_port = bt2_port._create_self_from_ptr_and_get_ref( |
61d96b89 FD |
139 | self_output_port_ptr, native_bt.PORT_TYPE_OUTPUT |
140 | ) | |
a4dcfa96 SM |
141 | self.__init__(self_output_port) |
142 | ||
143 | def __init__(self, output_port): | |
81447b5b PP |
144 | pass |
145 | ||
146 | @property | |
f6a5e476 | 147 | def _component(self): |
deec48a6 | 148 | return native_bt.bt2_get_user_component_from_user_msg_iter(self._bt_ptr) |
81447b5b | 149 | |
3f9a359f SM |
150 | @property |
151 | def _port(self): | |
152 | port_ptr = native_bt.self_message_iterator_borrow_port(self._bt_ptr) | |
153 | assert port_ptr is not None | |
154 | return bt2_port._create_self_from_ptr_and_get_ref( | |
155 | port_ptr, native_bt.PORT_TYPE_OUTPUT | |
156 | ) | |
157 | ||
81447b5b PP |
158 | @property |
159 | def addr(self): | |
deec48a6 | 160 | return int(self._bt_ptr) |
81447b5b | 161 | |
d73bb381 PP |
162 | @property |
163 | def _is_interrupted(self): | |
164 | return bool(native_bt.self_message_iterator_is_interrupted(self._bt_ptr)) | |
165 | ||
819d0ae7 | 166 | def _user_finalize(self): |
81447b5b PP |
167 | pass |
168 | ||
f6a5e476 PP |
169 | def __next__(self): |
170 | raise bt2.Stop | |
171 | ||
deec48a6 | 172 | def _bt_next_from_native(self): |
f6a5e476 PP |
173 | # this can raise anything: it's catched by the native part |
174 | try: | |
fa4c33e3 | 175 | msg = next(self) |
f6a5e476 PP |
176 | except StopIteration: |
177 | raise bt2.Stop | |
5760d571 | 178 | except Exception: |
f6a5e476 PP |
179 | raise |
180 | ||
9cbe0c59 | 181 | utils._check_type(msg, bt2_message._MessageConst) |
81447b5b | 182 | |
4e853135 SM |
183 | # The reference we return will be given to the message array. |
184 | # However, the `msg` Python object may stay alive, if the user has kept | |
185 | # a reference to it. Acquire a new reference to account for that. | |
186 | msg._get_ref(msg._ptr) | |
187 | return int(msg._ptr) | |
27d97a3f | 188 | |
deec48a6 | 189 | def _bt_can_seek_beginning_from_native(self): |
39ddfa44 SM |
190 | # Here, we mimic the behavior of the C API: |
191 | # | |
5c836b95 | 192 | # - If the iterator has a _user_can_seek_beginning method, |
819d0ae7 | 193 | # read it and use that result. |
9fa5c517 | 194 | # - Otherwise, the presence or absence of a `_user_seek_beginning` |
39ddfa44 | 195 | # method indicates whether the iterator can seek beginning. |
819d0ae7 | 196 | if hasattr(self, '_user_can_seek_beginning'): |
5c836b95 | 197 | can_seek_beginning = self._user_can_seek_beginning() |
39ddfa44 SM |
198 | utils._check_bool(can_seek_beginning) |
199 | return can_seek_beginning | |
200 | else: | |
819d0ae7 | 201 | return hasattr(self, '_user_seek_beginning') |
39ddfa44 | 202 | |
deec48a6 | 203 | def _bt_seek_beginning_from_native(self): |
819d0ae7 | 204 | self._user_seek_beginning() |
39ddfa44 | 205 | |
24a68958 SM |
206 | def _bt_can_seek_ns_from_origin_from_native(self, ns_from_origin): |
207 | # Here, we mimic the behavior of the C API: | |
208 | # | |
209 | # - If the iterator has a _user_can_seek_ns_from_origin method, | |
210 | # call it and use its return value. | |
211 | # - Otherwise, if there is a `_user_seek_ns_from_origin` method, | |
212 | # we presume it's possible. | |
213 | # - Otherwise, check if we can seek to beginning (which allows us to | |
214 | # seek to beginning and then fast forward - aka auto-seek). | |
215 | if hasattr(self, '_user_can_seek_ns_from_origin'): | |
216 | can_seek_ns_from_origin = self._user_can_seek_ns_from_origin(ns_from_origin) | |
217 | utils._check_bool(can_seek_ns_from_origin) | |
218 | return can_seek_ns_from_origin | |
219 | elif hasattr(self, '_user_seek_ns_from_origin'): | |
220 | return True | |
221 | else: | |
222 | return self._bt_can_seek_beginning_from_native() | |
223 | ||
224 | def _bt_seek_ns_from_origin_from_native(self, ns_from_origin): | |
225 | self._user_seek_ns_from_origin(ns_from_origin) | |
226 | ||
692f1a01 | 227 | def _create_input_port_message_iterator(self, input_port): |
c946c9de | 228 | utils._check_type(input_port, bt2_port._UserComponentInputPort) |
692f1a01 | 229 | |
ab8b2b1b | 230 | status, msg_iter_ptr = native_bt.bt2_self_component_port_input_message_iterator_create_from_message_iterator( |
692f1a01 PP |
231 | self._bt_ptr, input_port._ptr |
232 | ) | |
ab8b2b1b SM |
233 | utils._handle_func_status(status, 'cannot create message iterator object') |
234 | assert msg_iter_ptr is not None | |
692f1a01 PP |
235 | |
236 | return _UserComponentInputPortMessageIterator(msg_iter_ptr) | |
237 | ||
363f4bcf | 238 | def _create_event_message(self, event_class, parent, default_clock_snapshot=None): |
c946c9de | 239 | utils._check_type(event_class, bt2_event_class._EventClass) |
37a93d41 PP |
240 | |
241 | if event_class.stream_class.supports_packets: | |
c946c9de | 242 | utils._check_type(parent, bt2_packet._Packet) |
37a93d41 | 243 | else: |
c946c9de | 244 | utils._check_type(parent, bt2_stream._Stream) |
27d97a3f SM |
245 | |
246 | if default_clock_snapshot is not None: | |
dcd94213 | 247 | if event_class.stream_class.default_clock_class is None: |
61d96b89 FD |
248 | raise ValueError( |
249 | 'event messages in this stream must not have a default clock snapshot' | |
250 | ) | |
dcd94213 | 251 | |
27d97a3f | 252 | utils._check_uint64(default_clock_snapshot) |
37a93d41 PP |
253 | |
254 | if event_class.stream_class.supports_packets: | |
255 | ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot( | |
61d96b89 FD |
256 | self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot |
257 | ) | |
37a93d41 PP |
258 | else: |
259 | ptr = native_bt.message_event_create_with_default_clock_snapshot( | |
61d96b89 FD |
260 | self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot |
261 | ) | |
27d97a3f | 262 | else: |
dcd94213 | 263 | if event_class.stream_class.default_clock_class is not None: |
61d96b89 FD |
264 | raise ValueError( |
265 | 'event messages in this stream must have a default clock snapshot' | |
266 | ) | |
dcd94213 | 267 | |
37a93d41 PP |
268 | if event_class.stream_class.supports_packets: |
269 | ptr = native_bt.message_event_create_with_packet( | |
61d96b89 FD |
270 | self._bt_ptr, event_class._ptr, parent._ptr |
271 | ) | |
37a93d41 PP |
272 | else: |
273 | ptr = native_bt.message_event_create( | |
61d96b89 FD |
274 | self._bt_ptr, event_class._ptr, parent._ptr |
275 | ) | |
27d97a3f SM |
276 | |
277 | if ptr is None: | |
614743a5 | 278 | raise bt2._MemoryError('cannot create event message object') |
27d97a3f | 279 | |
c946c9de | 280 | return bt2_message._EventMessage(ptr) |
27d97a3f | 281 | |
0010c8b0 | 282 | def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot): |
c946c9de | 283 | utils._check_type(clock_class, bt2_clock_class._ClockClass) |
0010c8b0 | 284 | ptr = native_bt.message_message_iterator_inactivity_create( |
61d96b89 FD |
285 | self._bt_ptr, clock_class._ptr, clock_snapshot |
286 | ) | |
0010c8b0 SM |
287 | |
288 | if ptr is None: | |
614743a5 | 289 | raise bt2._MemoryError('cannot create inactivity message object') |
0010c8b0 | 290 | |
c946c9de | 291 | return bt2_message._MessageIteratorInactivityMessage(ptr) |
0010c8b0 | 292 | |
b7cbc799 | 293 | def _create_stream_beginning_message(self, stream, default_clock_snapshot=None): |
c946c9de | 294 | utils._check_type(stream, bt2_stream._Stream) |
27d97a3f | 295 | |
deec48a6 | 296 | ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr) |
27d97a3f | 297 | if ptr is None: |
614743a5 | 298 | raise bt2._MemoryError('cannot create stream beginning message object') |
27d97a3f | 299 | |
c946c9de | 300 | msg = bt2_message._StreamBeginningMessage(ptr) |
0010c8b0 | 301 | |
b7cbc799 SM |
302 | if default_clock_snapshot is not None: |
303 | msg._default_clock_snapshot = default_clock_snapshot | |
0010c8b0 | 304 | |
0010c8b0 SM |
305 | return msg |
306 | ||
b7cbc799 | 307 | def _create_stream_end_message(self, stream, default_clock_snapshot=None): |
c946c9de | 308 | utils._check_type(stream, bt2_stream._Stream) |
871a292a | 309 | |
deec48a6 | 310 | ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr) |
871a292a | 311 | if ptr is None: |
614743a5 | 312 | raise bt2._MemoryError('cannot create stream end message object') |
871a292a | 313 | |
c946c9de | 314 | msg = bt2_message._StreamEndMessage(ptr) |
b7cbc799 SM |
315 | |
316 | if default_clock_snapshot is not None: | |
317 | msg._default_clock_snapshot = default_clock_snapshot | |
318 | ||
319 | return msg | |
871a292a | 320 | |
27d97a3f | 321 | def _create_packet_beginning_message(self, packet, default_clock_snapshot=None): |
c946c9de | 322 | utils._check_type(packet, bt2_packet._Packet) |
27d97a3f | 323 | |
c88be1c8 | 324 | if packet.stream.cls.packets_have_beginning_default_clock_snapshot: |
27d97a3f | 325 | if default_clock_snapshot is None: |
61d96b89 FD |
326 | raise ValueError( |
327 | "packet beginning messages in this stream must have a default clock snapshot" | |
328 | ) | |
27d97a3f SM |
329 | |
330 | utils._check_uint64(default_clock_snapshot) | |
331 | ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot( | |
61d96b89 FD |
332 | self._bt_ptr, packet._ptr, default_clock_snapshot |
333 | ) | |
27d97a3f SM |
334 | else: |
335 | if default_clock_snapshot is not None: | |
61d96b89 FD |
336 | raise ValueError( |
337 | "packet beginning messages in this stream must not have a default clock snapshot" | |
338 | ) | |
27d97a3f | 339 | |
deec48a6 | 340 | ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr) |
27d97a3f SM |
341 | |
342 | if ptr is None: | |
614743a5 | 343 | raise bt2._MemoryError('cannot create packet beginning message object') |
27d97a3f | 344 | |
c946c9de | 345 | return bt2_message._PacketBeginningMessage(ptr) |
871a292a SM |
346 | |
347 | def _create_packet_end_message(self, packet, default_clock_snapshot=None): | |
c946c9de | 348 | utils._check_type(packet, bt2_packet._Packet) |
871a292a | 349 | |
c88be1c8 | 350 | if packet.stream.cls.packets_have_end_default_clock_snapshot: |
0010c8b0 | 351 | if default_clock_snapshot is None: |
61d96b89 FD |
352 | raise ValueError( |
353 | "packet end messages in this stream must have a default clock snapshot" | |
354 | ) | |
0010c8b0 | 355 | |
871a292a SM |
356 | utils._check_uint64(default_clock_snapshot) |
357 | ptr = native_bt.message_packet_end_create_with_default_clock_snapshot( | |
61d96b89 FD |
358 | self._bt_ptr, packet._ptr, default_clock_snapshot |
359 | ) | |
871a292a | 360 | else: |
0010c8b0 | 361 | if default_clock_snapshot is not None: |
61d96b89 FD |
362 | raise ValueError( |
363 | "packet end messages in this stream must not have a default clock snapshot" | |
364 | ) | |
0010c8b0 | 365 | |
deec48a6 | 366 | ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr) |
871a292a SM |
367 | |
368 | if ptr is None: | |
614743a5 | 369 | raise bt2._MemoryError('cannot create packet end message object') |
871a292a | 370 | |
c946c9de | 371 | return bt2_message._PacketEndMessage(ptr) |
0010c8b0 | 372 | |
61d96b89 FD |
373 | def _create_discarded_events_message( |
374 | self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None | |
375 | ): | |
c946c9de | 376 | utils._check_type(stream, bt2_stream._Stream) |
0010c8b0 | 377 | |
c88be1c8 | 378 | if not stream.cls.supports_discarded_events: |
77037b2b PP |
379 | raise ValueError('stream class does not support discarded events') |
380 | ||
c88be1c8 | 381 | if stream.cls.discarded_events_have_default_clock_snapshots: |
77037b2b | 382 | if beg_clock_snapshot is None or end_clock_snapshot is None: |
61d96b89 FD |
383 | raise ValueError( |
384 | 'discarded events have default clock snapshots for this stream class' | |
385 | ) | |
77037b2b | 386 | |
0010c8b0 SM |
387 | utils._check_uint64(beg_clock_snapshot) |
388 | utils._check_uint64(end_clock_snapshot) | |
389 | ptr = native_bt.message_discarded_events_create_with_default_clock_snapshots( | |
61d96b89 FD |
390 | self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot |
391 | ) | |
0010c8b0 | 392 | else: |
77037b2b | 393 | if beg_clock_snapshot is not None or end_clock_snapshot is not None: |
61d96b89 FD |
394 | raise ValueError( |
395 | 'discarded events have no default clock snapshots for this stream class' | |
396 | ) | |
77037b2b | 397 | |
61d96b89 | 398 | ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr) |
0010c8b0 SM |
399 | |
400 | if ptr is None: | |
614743a5 | 401 | raise bt2._MemoryError('cannot discarded events message object') |
0010c8b0 | 402 | |
c946c9de | 403 | msg = bt2_message._DiscardedEventsMessage(ptr) |
0010c8b0 SM |
404 | |
405 | if count is not None: | |
406 | msg._count = count | |
407 | ||
408 | return msg | |
409 | ||
61d96b89 FD |
410 | def _create_discarded_packets_message( |
411 | self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None | |
412 | ): | |
c946c9de | 413 | utils._check_type(stream, bt2_stream._Stream) |
0010c8b0 | 414 | |
c88be1c8 | 415 | if not stream.cls.supports_discarded_packets: |
77037b2b PP |
416 | raise ValueError('stream class does not support discarded packets') |
417 | ||
c88be1c8 | 418 | if stream.cls.discarded_packets_have_default_clock_snapshots: |
77037b2b | 419 | if beg_clock_snapshot is None or end_clock_snapshot is None: |
61d96b89 FD |
420 | raise ValueError( |
421 | 'discarded packets have default clock snapshots for this stream class' | |
422 | ) | |
77037b2b | 423 | |
0010c8b0 SM |
424 | utils._check_uint64(beg_clock_snapshot) |
425 | utils._check_uint64(end_clock_snapshot) | |
426 | ptr = native_bt.message_discarded_packets_create_with_default_clock_snapshots( | |
61d96b89 FD |
427 | self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot |
428 | ) | |
0010c8b0 | 429 | else: |
77037b2b | 430 | if beg_clock_snapshot is not None or end_clock_snapshot is not None: |
61d96b89 FD |
431 | raise ValueError( |
432 | 'discarded packets have no default clock snapshots for this stream class' | |
433 | ) | |
77037b2b | 434 | |
61d96b89 | 435 | ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr) |
0010c8b0 SM |
436 | |
437 | if ptr is None: | |
614743a5 | 438 | raise bt2._MemoryError('cannot discarded packets message object') |
0010c8b0 | 439 | |
c946c9de | 440 | msg = bt2_message._DiscardedPacketsMessage(ptr) |
0010c8b0 SM |
441 | |
442 | if count is not None: | |
443 | msg._count = count | |
444 | ||
445 | return msg |