1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
7 from bt2
import port
as bt2_port
8 from bt2
import error
as bt2_error
9 from bt2
import utils
as bt2_utils
10 from bt2
import object as bt2_object
11 from bt2
import packet
as bt2_packet
12 from bt2
import stream
as bt2_stream
13 from bt2
import message
as bt2_message
14 from bt2
import native_bt
15 from bt2
import clock_class
as bt2_clock_class
16 from bt2
import event_class
as bt2_event_class
19 class _MessageIterator(collections
.abc
.Iterator
):
21 raise NotImplementedError
24 class _UserComponentInputPortMessageIterator(
25 bt2_object
._SharedObject
, _MessageIterator
29 native_bt
.message_iterator_get_ref(ptr
)
33 native_bt
.message_iterator_put_ref(ptr
)
35 def __init__(self
, ptr
):
36 self
._current
_msgs
= []
41 if len(self
._current
_msgs
) == self
._at
:
42 status
, msgs
= native_bt
.bt2_self_component_port_input_get_msg_range(
45 bt2_utils
._handle
_func
_status
(
46 status
, "unexpected error: cannot advance the message iterator"
48 self
._current
_msgs
= msgs
51 msg_ptr
= self
._current
_msgs
[self
._at
]
54 return bt2_message
._create
_from
_ptr
(msg_ptr
)
56 def can_seek_beginning(self
):
57 (status
, res
) = native_bt
.message_iterator_can_seek_beginning(self
._ptr
)
58 bt2_utils
._handle
_func
_status
(
60 "cannot check whether or not message iterator can seek its beginning",
64 def seek_beginning(self
):
65 # Forget about buffered messages, they won't be valid after seeking.
66 self
._current
_msgs
.clear()
69 status
= native_bt
.message_iterator_seek_beginning(self
._ptr
)
70 bt2_utils
._handle
_func
_status
(status
, "cannot seek message iterator beginning")
72 def can_seek_ns_from_origin(self
, ns_from_origin
):
73 bt2_utils
._check
_int
64(ns_from_origin
)
74 (status
, res
) = native_bt
.message_iterator_can_seek_ns_from_origin(
75 self
._ptr
, ns_from_origin
77 bt2_utils
._handle
_func
_status
(
79 "cannot check whether or not message iterator can seek given ns from origin",
83 def seek_ns_from_origin(self
, ns_from_origin
):
84 bt2_utils
._check
_int
64(ns_from_origin
)
86 # Forget about buffered messages, they won't be valid after seeking.
87 self
._current
_msgs
.clear()
90 status
= native_bt
.message_iterator_seek_ns_from_origin(
91 self
._ptr
, ns_from_origin
93 bt2_utils
._handle
_func
_status
(
94 status
, "message iterator cannot seek given ns from origin"
98 def can_seek_forward(self
):
99 return native_bt
.message_iterator_can_seek_forward(self
._ptr
)
102 class _MessageIteratorConfiguration
:
103 def __init__(self
, ptr
):
106 def can_seek_forward(self
, value
):
107 bt2_utils
._check
_bool
(value
)
108 native_bt
.self_message_iterator_configuration_set_can_seek_forward(
112 can_seek_forward
= property(fset
=can_seek_forward
)
115 # This is extended by the user to implement component classes in Python. It
116 # is created for a given output port when an input port message iterator is
117 # created on the input port on the other side of the connection.
119 # Its purpose is to feed the messages that should go out through this output
121 class _UserMessageIterator(_MessageIterator
):
122 def __new__(cls
, ptr
):
123 # User iterator objects are always created by the native side,
124 # that is, never instantiated directly by Python code.
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
129 # self._bt_ptr argument being set.
131 # self._bt_ptr is NOT owned by this object here, so there's nothing
132 # to do in __del__().
133 self
= super().__new
__(cls
)
137 def _bt_init_from_native(self
, config_ptr
, self_output_port_ptr
):
138 self_output_port
= bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(
139 self_output_port_ptr
, native_bt
.PORT_TYPE_OUTPUT
141 config
= _MessageIteratorConfiguration(config_ptr
)
142 self
.__init
__(config
, self_output_port
)
144 def __init__(self
, config
, self_output_port
):
148 def _component(self
):
149 return native_bt
.bt2_get_user_component_from_user_msg_iter(self
._bt
_ptr
)
153 port_ptr
= native_bt
.self_message_iterator_borrow_port(self
._bt
_ptr
)
154 assert port_ptr
is not None
155 return bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(
156 port_ptr
, native_bt
.PORT_TYPE_OUTPUT
161 return int(self
._bt
_ptr
)
164 def _is_interrupted(self
):
165 return bool(native_bt
.self_message_iterator_is_interrupted(self
._bt
_ptr
))
167 def _user_finalize(self
):
173 def _bt_next_from_native(self
):
174 # this can raise anything: it's caught by the native part
177 except StopIteration:
182 bt2_utils
._check
_type
(msg
, bt2_message
._MessageConst
)
184 # The reference we return will be given to the message array.
185 # However, the `msg` Python object may stay alive, if the user has kept
186 # a reference to it. Acquire a new reference to account for that.
187 msg
._get
_ref
(msg
._ptr
)
190 def _bt_can_seek_beginning_from_native(self
):
191 # Here, we mimic the behavior of the C API:
193 # - If the iterator has a _user_can_seek_beginning method,
194 # read it and use that result.
195 # - Otherwise, the presence or absence of a `_user_seek_beginning`
196 # method indicates whether the iterator can seek beginning.
197 if hasattr(self
, "_user_can_seek_beginning"):
198 can_seek_beginning
= self
._user
_can
_seek
_beginning
()
199 bt2_utils
._check
_bool
(can_seek_beginning
)
200 return can_seek_beginning
202 return hasattr(self
, "_user_seek_beginning")
204 def _bt_seek_beginning_from_native(self
):
205 self
._user
_seek
_beginning
()
207 def _bt_can_seek_ns_from_origin_from_native(self
, ns_from_origin
):
208 # Return whether the iterator can seek ns from origin using the
209 # user-implemented seek_ns_from_origin method. We mimic the behavior
212 # - If the iterator has a _user_can_seek_ns_from_origin method,
213 # call it and use its return value.
214 # - Otherwise, if there is a `_user_seek_ns_from_origin` method,
215 # we presume it's possible.
217 if hasattr(self
, "_user_can_seek_ns_from_origin"):
218 can_seek_ns_from_origin
= self
._user
_can
_seek
_ns
_from
_origin
(ns_from_origin
)
219 bt2_utils
._check
_bool
(can_seek_ns_from_origin
)
220 return can_seek_ns_from_origin
222 return hasattr(self
, "_user_seek_ns_from_origin")
224 def _bt_seek_ns_from_origin_from_native(self
, ns_from_origin
):
225 self
._user
_seek
_ns
_from
_origin
(ns_from_origin
)
227 def _create_message_iterator(self
, input_port
):
228 bt2_utils
._check
_type
(input_port
, bt2_port
._UserComponentInputPort
)
230 if not input_port
.is_connected
:
231 raise ValueError("input port is not connected")
236 ) = native_bt
.bt2_message_iterator_create_from_message_iterator(
237 self
._bt
_ptr
, input_port
._ptr
239 bt2_utils
._handle
_func
_status
(status
, "cannot create message iterator object")
240 assert msg_iter_ptr
is not None
242 return _UserComponentInputPortMessageIterator(msg_iter_ptr
)
244 def _create_event_message(self
, event_class
, parent
, default_clock_snapshot
=None):
245 bt2_utils
._check
_type
(event_class
, bt2_event_class
._EventClass
)
247 if event_class
.stream_class
.supports_packets
:
248 bt2_utils
._check
_type
(parent
, bt2_packet
._Packet
)
250 bt2_utils
._check
_type
(parent
, bt2_stream
._Stream
)
252 if default_clock_snapshot
is not None:
253 if event_class
.stream_class
.default_clock_class
is None:
255 "event messages in this stream must not have a default clock snapshot"
258 bt2_utils
._check
_uint
64(default_clock_snapshot
)
260 if event_class
.stream_class
.supports_packets
:
261 ptr
= native_bt
.message_event_create_with_packet_and_default_clock_snapshot(
262 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
, default_clock_snapshot
265 ptr
= native_bt
.message_event_create_with_default_clock_snapshot(
266 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
, default_clock_snapshot
269 if event_class
.stream_class
.default_clock_class
is not None:
271 "event messages in this stream must have a default clock snapshot"
274 if event_class
.stream_class
.supports_packets
:
275 ptr
= native_bt
.message_event_create_with_packet(
276 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
279 ptr
= native_bt
.message_event_create(
280 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
284 raise bt2_error
._MemoryError
("cannot create event message object")
286 return bt2_message
._EventMessage
(ptr
)
288 def _create_message_iterator_inactivity_message(self
, clock_class
, clock_snapshot
):
289 bt2_utils
._check
_type
(clock_class
, bt2_clock_class
._ClockClass
)
290 ptr
= native_bt
.message_message_iterator_inactivity_create(
291 self
._bt
_ptr
, clock_class
._ptr
, clock_snapshot
295 raise bt2_error
._MemoryError
("cannot create inactivity message object")
297 return bt2_message
._MessageIteratorInactivityMessage
(ptr
)
299 def _create_stream_beginning_message(self
, stream
, default_clock_snapshot
=None):
300 bt2_utils
._check
_type
(stream
, bt2_stream
._Stream
)
302 ptr
= native_bt
.message_stream_beginning_create(self
._bt
_ptr
, stream
._ptr
)
304 raise bt2_error
._MemoryError
(
305 "cannot create stream beginning message object"
308 msg
= bt2_message
._StreamBeginningMessage
(ptr
)
310 if default_clock_snapshot
is not None:
311 msg
._default
_clock
_snapshot
= default_clock_snapshot
315 def _create_stream_end_message(self
, stream
, default_clock_snapshot
=None):
316 bt2_utils
._check
_type
(stream
, bt2_stream
._Stream
)
318 ptr
= native_bt
.message_stream_end_create(self
._bt
_ptr
, stream
._ptr
)
320 raise bt2_error
._MemoryError
("cannot create stream end message object")
322 msg
= bt2_message
._StreamEndMessage
(ptr
)
324 if default_clock_snapshot
is not None:
325 msg
._default
_clock
_snapshot
= default_clock_snapshot
329 def _create_packet_beginning_message(self
, packet
, default_clock_snapshot
=None):
330 bt2_utils
._check
_type
(packet
, bt2_packet
._Packet
)
332 if packet
.stream
.cls
.packets_have_beginning_default_clock_snapshot
:
333 if default_clock_snapshot
is None:
335 "packet beginning messages in this stream must have a default clock snapshot"
338 bt2_utils
._check
_uint
64(default_clock_snapshot
)
339 ptr
= native_bt
.message_packet_beginning_create_with_default_clock_snapshot(
340 self
._bt
_ptr
, packet
._ptr
, default_clock_snapshot
343 if default_clock_snapshot
is not None:
345 "packet beginning messages in this stream must not have a default clock snapshot"
348 ptr
= native_bt
.message_packet_beginning_create(self
._bt
_ptr
, packet
._ptr
)
351 raise bt2_error
._MemoryError
(
352 "cannot create packet beginning message object"
355 return bt2_message
._PacketBeginningMessage
(ptr
)
357 def _create_packet_end_message(self
, packet
, default_clock_snapshot
=None):
358 bt2_utils
._check
_type
(packet
, bt2_packet
._Packet
)
360 if packet
.stream
.cls
.packets_have_end_default_clock_snapshot
:
361 if default_clock_snapshot
is None:
363 "packet end messages in this stream must have a default clock snapshot"
366 bt2_utils
._check
_uint
64(default_clock_snapshot
)
367 ptr
= native_bt
.message_packet_end_create_with_default_clock_snapshot(
368 self
._bt
_ptr
, packet
._ptr
, default_clock_snapshot
371 if default_clock_snapshot
is not None:
373 "packet end messages in this stream must not have a default clock snapshot"
376 ptr
= native_bt
.message_packet_end_create(self
._bt
_ptr
, packet
._ptr
)
379 raise bt2_error
._MemoryError
("cannot create packet end message object")
381 return bt2_message
._PacketEndMessage
(ptr
)
383 def _create_discarded_events_message(
384 self
, stream
, count
=None, beg_clock_snapshot
=None, end_clock_snapshot
=None
386 bt2_utils
._check
_type
(stream
, bt2_stream
._Stream
)
388 if not stream
.cls
.supports_discarded_events
:
389 raise ValueError("stream class does not support discarded events")
391 if stream
.cls
.discarded_events_have_default_clock_snapshots
:
392 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
394 "discarded events have default clock snapshots for this stream class"
397 bt2_utils
._check
_uint
64(beg_clock_snapshot
)
398 bt2_utils
._check
_uint
64(end_clock_snapshot
)
400 if beg_clock_snapshot
> end_clock_snapshot
:
402 "beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})".format(
403 beg_clock_snapshot
, end_clock_snapshot
408 native_bt
.message_discarded_events_create_with_default_clock_snapshots(
409 self
._bt
_ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
413 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
415 "discarded events have no default clock snapshots for this stream class"
418 ptr
= native_bt
.message_discarded_events_create(self
._bt
_ptr
, stream
._ptr
)
421 raise bt2_error
._MemoryError
("cannot discarded events message object")
423 msg
= bt2_message
._DiscardedEventsMessage
(ptr
)
425 if count
is not None:
430 def _create_discarded_packets_message(
431 self
, stream
, count
=None, beg_clock_snapshot
=None, end_clock_snapshot
=None
433 bt2_utils
._check
_type
(stream
, bt2_stream
._Stream
)
435 if not stream
.cls
.supports_discarded_packets
:
436 raise ValueError("stream class does not support discarded packets")
438 if stream
.cls
.discarded_packets_have_default_clock_snapshots
:
439 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
441 "discarded packets have default clock snapshots for this stream class"
444 bt2_utils
._check
_uint
64(beg_clock_snapshot
)
445 bt2_utils
._check
_uint
64(end_clock_snapshot
)
447 if beg_clock_snapshot
> end_clock_snapshot
:
449 "beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})".format(
450 beg_clock_snapshot
, end_clock_snapshot
455 native_bt
.message_discarded_packets_create_with_default_clock_snapshots(
456 self
._bt
_ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
460 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
462 "discarded packets have no default clock snapshots for this stream class"
465 ptr
= native_bt
.message_discarded_packets_create(self
._bt
_ptr
, stream
._ptr
)
468 raise bt2_error
._MemoryError
("cannot discarded packets message object")
470 msg
= bt2_message
._DiscardedPacketsMessage
(ptr
)
472 if count
is not None: