1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
, object, utils
6 from bt2
import message
as bt2_message
8 from bt2
import stream
as bt2_stream
9 from bt2
import event_class
as bt2_event_class
10 from bt2
import packet
as bt2_packet
11 from bt2
import port
as bt2_port
12 from bt2
import clock_class
as bt2_clock_class
16 class _MessageIterator(collections
.abc
.Iterator
):
18 raise NotImplementedError
21 class _UserComponentInputPortMessageIterator(object._SharedObject
, _MessageIterator
):
24 native_bt
.message_iterator_get_ref(ptr
)
28 native_bt
.message_iterator_put_ref(ptr
)
30 def __init__(self
, ptr
):
31 self
._current
_msgs
= []
36 if len(self
._current
_msgs
) == self
._at
:
37 status
, msgs
= native_bt
.bt2_self_component_port_input_get_msg_range(
40 utils
._handle
_func
_status
(
41 status
, "unexpected error: cannot advance the message iterator"
43 self
._current
_msgs
= msgs
46 msg_ptr
= self
._current
_msgs
[self
._at
]
49 return bt2_message
._create
_from
_ptr
(msg_ptr
)
51 def can_seek_beginning(self
):
52 (status
, res
) = native_bt
.message_iterator_can_seek_beginning(self
._ptr
)
53 utils
._handle
_func
_status
(
55 "cannot check whether or not message iterator can seek its beginning",
59 def seek_beginning(self
):
60 # Forget about buffered messages, they won't be valid after seeking.
61 self
._current
_msgs
.clear()
64 status
= native_bt
.message_iterator_seek_beginning(self
._ptr
)
65 utils
._handle
_func
_status
(status
, "cannot seek message iterator beginning")
67 def can_seek_ns_from_origin(self
, ns_from_origin
):
68 utils
._check
_int
64(ns_from_origin
)
69 (status
, res
) = native_bt
.message_iterator_can_seek_ns_from_origin(
70 self
._ptr
, ns_from_origin
72 utils
._handle
_func
_status
(
74 "cannot check whether or not message iterator can seek given ns from origin",
78 def seek_ns_from_origin(self
, ns_from_origin
):
79 utils
._check
_int
64(ns_from_origin
)
81 # Forget about buffered messages, they won't be valid after seeking.
82 self
._current
_msgs
.clear()
85 status
= native_bt
.message_iterator_seek_ns_from_origin(
86 self
._ptr
, ns_from_origin
88 utils
._handle
_func
_status
(
89 status
, "message iterator cannot seek given ns from origin"
93 def can_seek_forward(self
):
94 return native_bt
.message_iterator_can_seek_forward(self
._ptr
)
97 class _MessageIteratorConfiguration
:
98 def __init__(self
, ptr
):
101 def can_seek_forward(self
, value
):
102 utils
._check
_bool
(value
)
103 native_bt
.self_message_iterator_configuration_set_can_seek_forward(
107 can_seek_forward
= property(fset
=can_seek_forward
)
110 # This is extended by the user to implement component classes in Python. It
111 # is created for a given output port when an input port message iterator is
112 # created on the input port on the other side of the connection.
114 # Its purpose is to feed the messages that should go out through this output
116 class _UserMessageIterator(_MessageIterator
):
117 def __new__(cls
, ptr
):
118 # User iterator objects are always created by the native side,
119 # that is, never instantiated directly by Python code.
121 # The native code calls this, then manually calls
122 # self.__init__() without the `ptr` argument. The user has
123 # access to self.component during this call, thanks to this
124 # self._bt_ptr argument being set.
126 # self._bt_ptr is NOT owned by this object here, so there's nothing
127 # to do in __del__().
128 self
= super().__new
__(cls
)
132 def _bt_init_from_native(self
, config_ptr
, self_output_port_ptr
):
133 self_output_port
= bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(
134 self_output_port_ptr
, native_bt
.PORT_TYPE_OUTPUT
136 config
= _MessageIteratorConfiguration(config_ptr
)
137 self
.__init
__(config
, self_output_port
)
139 def __init__(self
, config
, self_output_port
):
143 def _component(self
):
144 return native_bt
.bt2_get_user_component_from_user_msg_iter(self
._bt
_ptr
)
148 port_ptr
= native_bt
.self_message_iterator_borrow_port(self
._bt
_ptr
)
149 assert port_ptr
is not None
150 return bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(
151 port_ptr
, native_bt
.PORT_TYPE_OUTPUT
156 return int(self
._bt
_ptr
)
159 def _is_interrupted(self
):
160 return bool(native_bt
.self_message_iterator_is_interrupted(self
._bt
_ptr
))
162 def _user_finalize(self
):
168 def _bt_next_from_native(self
):
169 # this can raise anything: it's catched by the native part
172 except StopIteration:
177 utils
._check
_type
(msg
, bt2_message
._MessageConst
)
179 # The reference we return will be given to the message array.
180 # However, the `msg` Python object may stay alive, if the user has kept
181 # a reference to it. Acquire a new reference to account for that.
182 msg
._get
_ref
(msg
._ptr
)
185 def _bt_can_seek_beginning_from_native(self
):
186 # Here, we mimic the behavior of the C API:
188 # - If the iterator has a _user_can_seek_beginning method,
189 # read it and use that result.
190 # - Otherwise, the presence or absence of a `_user_seek_beginning`
191 # method indicates whether the iterator can seek beginning.
192 if hasattr(self
, "_user_can_seek_beginning"):
193 can_seek_beginning
= self
._user
_can
_seek
_beginning
()
194 utils
._check
_bool
(can_seek_beginning
)
195 return can_seek_beginning
197 return hasattr(self
, "_user_seek_beginning")
199 def _bt_seek_beginning_from_native(self
):
200 self
._user
_seek
_beginning
()
202 def _bt_can_seek_ns_from_origin_from_native(self
, ns_from_origin
):
203 # Return whether the iterator can seek ns from origin using the
204 # user-implemented seek_ns_from_origin method. We mimic the behavior
207 # - If the iterator has a _user_can_seek_ns_from_origin method,
208 # call it and use its return value.
209 # - Otherwise, if there is a `_user_seek_ns_from_origin` method,
210 # we presume it's possible.
212 if hasattr(self
, "_user_can_seek_ns_from_origin"):
213 can_seek_ns_from_origin
= self
._user
_can
_seek
_ns
_from
_origin
(ns_from_origin
)
214 utils
._check
_bool
(can_seek_ns_from_origin
)
215 return can_seek_ns_from_origin
217 return hasattr(self
, "_user_seek_ns_from_origin")
219 def _bt_seek_ns_from_origin_from_native(self
, ns_from_origin
):
220 self
._user
_seek
_ns
_from
_origin
(ns_from_origin
)
222 def _create_message_iterator(self
, input_port
):
223 utils
._check
_type
(input_port
, bt2_port
._UserComponentInputPort
)
225 if not input_port
.is_connected
:
226 raise ValueError("input port is not connected")
231 ) = native_bt
.bt2_message_iterator_create_from_message_iterator(
232 self
._bt
_ptr
, input_port
._ptr
234 utils
._handle
_func
_status
(status
, "cannot create message iterator object")
235 assert msg_iter_ptr
is not None
237 return _UserComponentInputPortMessageIterator(msg_iter_ptr
)
239 def _create_event_message(self
, event_class
, parent
, default_clock_snapshot
=None):
240 utils
._check
_type
(event_class
, bt2_event_class
._EventClass
)
242 if event_class
.stream_class
.supports_packets
:
243 utils
._check
_type
(parent
, bt2_packet
._Packet
)
245 utils
._check
_type
(parent
, bt2_stream
._Stream
)
247 if default_clock_snapshot
is not None:
248 if event_class
.stream_class
.default_clock_class
is None:
250 "event messages in this stream must not have a default clock snapshot"
253 utils
._check
_uint
64(default_clock_snapshot
)
255 if event_class
.stream_class
.supports_packets
:
256 ptr
= native_bt
.message_event_create_with_packet_and_default_clock_snapshot(
257 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
, default_clock_snapshot
260 ptr
= native_bt
.message_event_create_with_default_clock_snapshot(
261 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
, default_clock_snapshot
264 if event_class
.stream_class
.default_clock_class
is not None:
266 "event messages in this stream must have a default clock snapshot"
269 if event_class
.stream_class
.supports_packets
:
270 ptr
= native_bt
.message_event_create_with_packet(
271 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
274 ptr
= native_bt
.message_event_create(
275 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
279 raise bt2
._MemoryError("cannot create event message object")
281 return bt2_message
._EventMessage
(ptr
)
283 def _create_message_iterator_inactivity_message(self
, clock_class
, clock_snapshot
):
284 utils
._check
_type
(clock_class
, bt2_clock_class
._ClockClass
)
285 ptr
= native_bt
.message_message_iterator_inactivity_create(
286 self
._bt
_ptr
, clock_class
._ptr
, clock_snapshot
290 raise bt2
._MemoryError("cannot create inactivity message object")
292 return bt2_message
._MessageIteratorInactivityMessage
(ptr
)
294 def _create_stream_beginning_message(self
, stream
, default_clock_snapshot
=None):
295 utils
._check
_type
(stream
, bt2_stream
._Stream
)
297 ptr
= native_bt
.message_stream_beginning_create(self
._bt
_ptr
, stream
._ptr
)
299 raise bt2
._MemoryError("cannot create stream beginning message object")
301 msg
= bt2_message
._StreamBeginningMessage
(ptr
)
303 if default_clock_snapshot
is not None:
304 msg
._default
_clock
_snapshot
= default_clock_snapshot
308 def _create_stream_end_message(self
, stream
, default_clock_snapshot
=None):
309 utils
._check
_type
(stream
, bt2_stream
._Stream
)
311 ptr
= native_bt
.message_stream_end_create(self
._bt
_ptr
, stream
._ptr
)
313 raise bt2
._MemoryError("cannot create stream end message object")
315 msg
= bt2_message
._StreamEndMessage
(ptr
)
317 if default_clock_snapshot
is not None:
318 msg
._default
_clock
_snapshot
= default_clock_snapshot
322 def _create_packet_beginning_message(self
, packet
, default_clock_snapshot
=None):
323 utils
._check
_type
(packet
, bt2_packet
._Packet
)
325 if packet
.stream
.cls
.packets_have_beginning_default_clock_snapshot
:
326 if default_clock_snapshot
is None:
328 "packet beginning messages in this stream must have a default clock snapshot"
331 utils
._check
_uint
64(default_clock_snapshot
)
332 ptr
= native_bt
.message_packet_beginning_create_with_default_clock_snapshot(
333 self
._bt
_ptr
, packet
._ptr
, default_clock_snapshot
336 if default_clock_snapshot
is not None:
338 "packet beginning messages in this stream must not have a default clock snapshot"
341 ptr
= native_bt
.message_packet_beginning_create(self
._bt
_ptr
, packet
._ptr
)
344 raise bt2
._MemoryError("cannot create packet beginning message object")
346 return bt2_message
._PacketBeginningMessage
(ptr
)
348 def _create_packet_end_message(self
, packet
, default_clock_snapshot
=None):
349 utils
._check
_type
(packet
, bt2_packet
._Packet
)
351 if packet
.stream
.cls
.packets_have_end_default_clock_snapshot
:
352 if default_clock_snapshot
is None:
354 "packet end messages in this stream must have a default clock snapshot"
357 utils
._check
_uint
64(default_clock_snapshot
)
358 ptr
= native_bt
.message_packet_end_create_with_default_clock_snapshot(
359 self
._bt
_ptr
, packet
._ptr
, default_clock_snapshot
362 if default_clock_snapshot
is not None:
364 "packet end messages in this stream must not have a default clock snapshot"
367 ptr
= native_bt
.message_packet_end_create(self
._bt
_ptr
, packet
._ptr
)
370 raise bt2
._MemoryError("cannot create packet end message object")
372 return bt2_message
._PacketEndMessage
(ptr
)
374 def _create_discarded_events_message(
375 self
, stream
, count
=None, beg_clock_snapshot
=None, end_clock_snapshot
=None
377 utils
._check
_type
(stream
, bt2_stream
._Stream
)
379 if not stream
.cls
.supports_discarded_events
:
380 raise ValueError("stream class does not support discarded events")
382 if stream
.cls
.discarded_events_have_default_clock_snapshots
:
383 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
385 "discarded events have default clock snapshots for this stream class"
388 utils
._check
_uint
64(beg_clock_snapshot
)
389 utils
._check
_uint
64(end_clock_snapshot
)
391 if beg_clock_snapshot
> end_clock_snapshot
:
393 "beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})".format(
394 beg_clock_snapshot
, end_clock_snapshot
399 native_bt
.message_discarded_events_create_with_default_clock_snapshots(
400 self
._bt
_ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
404 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
406 "discarded events have no default clock snapshots for this stream class"
409 ptr
= native_bt
.message_discarded_events_create(self
._bt
_ptr
, stream
._ptr
)
412 raise bt2
._MemoryError("cannot discarded events message object")
414 msg
= bt2_message
._DiscardedEventsMessage
(ptr
)
416 if count
is not None:
421 def _create_discarded_packets_message(
422 self
, stream
, count
=None, beg_clock_snapshot
=None, end_clock_snapshot
=None
424 utils
._check
_type
(stream
, bt2_stream
._Stream
)
426 if not stream
.cls
.supports_discarded_packets
:
427 raise ValueError("stream class does not support discarded packets")
429 if stream
.cls
.discarded_packets_have_default_clock_snapshots
:
430 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
432 "discarded packets have default clock snapshots for this stream class"
435 utils
._check
_uint
64(beg_clock_snapshot
)
436 utils
._check
_uint
64(end_clock_snapshot
)
438 if beg_clock_snapshot
> end_clock_snapshot
:
440 "beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})".format(
441 beg_clock_snapshot
, end_clock_snapshot
446 native_bt
.message_discarded_packets_create_with_default_clock_snapshots(
447 self
._bt
_ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
451 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
453 "discarded packets have no default clock snapshots for this stream class"
456 ptr
= native_bt
.message_discarded_packets_create(self
._bt
_ptr
, stream
._ptr
)
459 raise bt2
._MemoryError("cannot discarded packets message object")
461 msg
= bt2_message
._DiscardedPacketsMessage
(ptr
)
463 if count
is not None: