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
):
22 _get_ref
= staticmethod(native_bt
.message_iterator_get_ref
)
23 _put_ref
= staticmethod(native_bt
.message_iterator_put_ref
)
25 def __init__(self
, ptr
):
26 self
._current
_msgs
= []
31 if len(self
._current
_msgs
) == self
._at
:
32 status
, msgs
= native_bt
.bt2_self_component_port_input_get_msg_range(
35 utils
._handle
_func
_status
(
36 status
, 'unexpected error: cannot advance the message iterator'
38 self
._current
_msgs
= msgs
41 msg_ptr
= self
._current
_msgs
[self
._at
]
44 return bt2_message
._create
_from
_ptr
(msg_ptr
)
46 def can_seek_beginning(self
):
47 (status
, res
) = native_bt
.message_iterator_can_seek_beginning(self
._ptr
)
48 utils
._handle
_func
_status
(
50 'cannot check whether or not message iterator can seek its beginning',
54 def seek_beginning(self
):
55 # Forget about buffered messages, they won't be valid after seeking.
56 self
._current
_msgs
.clear()
59 status
= native_bt
.message_iterator_seek_beginning(self
._ptr
)
60 utils
._handle
_func
_status
(status
, 'cannot seek message iterator beginning')
62 def can_seek_ns_from_origin(self
, ns_from_origin
):
63 utils
._check
_int
64(ns_from_origin
)
64 (status
, res
) = native_bt
.message_iterator_can_seek_ns_from_origin(
65 self
._ptr
, ns_from_origin
67 utils
._handle
_func
_status
(
69 'cannot check whether or not message iterator can seek given ns from origin',
73 def seek_ns_from_origin(self
, ns_from_origin
):
74 utils
._check
_int
64(ns_from_origin
)
76 # Forget about buffered messages, they won't be valid after seeking.
77 self
._current
_msgs
.clear()
80 status
= native_bt
.message_iterator_seek_ns_from_origin(
81 self
._ptr
, ns_from_origin
83 utils
._handle
_func
_status
(
84 status
, 'message iterator cannot seek given ns from origin'
88 def can_seek_forward(self
):
89 return native_bt
.message_iterator_can_seek_forward(self
._ptr
)
92 class _MessageIteratorConfiguration
:
93 def __init__(self
, ptr
):
96 def can_seek_forward(self
, value
):
97 utils
._check
_bool
(value
)
98 native_bt
.self_message_iterator_configuration_set_can_seek_forward(
102 can_seek_forward
= property(fset
=can_seek_forward
)
105 # This is extended by the user to implement component classes in Python. It
106 # is created for a given output port when an input port message iterator is
107 # created on the input port on the other side of the connection. It is also
108 # created when an output port message iterator is created on this output port.
110 # Its purpose is to feed the messages that should go out through this output
112 class _UserMessageIterator(_MessageIterator
):
113 def __new__(cls
, ptr
):
114 # User iterator objects are always created by the native side,
115 # that is, never instantiated directly by Python code.
117 # The native code calls this, then manually calls
118 # self.__init__() without the `ptr` argument. The user has
119 # access to self.component during this call, thanks to this
120 # self._bt_ptr argument being set.
122 # self._bt_ptr is NOT owned by this object here, so there's nothing
123 # to do in __del__().
124 self
= super().__new
__(cls
)
128 def _bt_init_from_native(self
, config_ptr
, self_output_port_ptr
):
129 self_output_port
= bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(
130 self_output_port_ptr
, native_bt
.PORT_TYPE_OUTPUT
132 config
= _MessageIteratorConfiguration(config_ptr
)
133 self
.__init
__(config
, self_output_port
)
135 def __init__(self
, config
, self_output_port
):
139 def _component(self
):
140 return native_bt
.bt2_get_user_component_from_user_msg_iter(self
._bt
_ptr
)
144 port_ptr
= native_bt
.self_message_iterator_borrow_port(self
._bt
_ptr
)
145 assert port_ptr
is not None
146 return bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(
147 port_ptr
, native_bt
.PORT_TYPE_OUTPUT
152 return int(self
._bt
_ptr
)
155 def _is_interrupted(self
):
156 return bool(native_bt
.self_message_iterator_is_interrupted(self
._bt
_ptr
))
158 def _user_finalize(self
):
164 def _bt_next_from_native(self
):
165 # this can raise anything: it's catched by the native part
168 except StopIteration:
173 utils
._check
_type
(msg
, bt2_message
._MessageConst
)
175 # The reference we return will be given to the message array.
176 # However, the `msg` Python object may stay alive, if the user has kept
177 # a reference to it. Acquire a new reference to account for that.
178 msg
._get
_ref
(msg
._ptr
)
181 def _bt_can_seek_beginning_from_native(self
):
182 # Here, we mimic the behavior of the C API:
184 # - If the iterator has a _user_can_seek_beginning method,
185 # read it and use that result.
186 # - Otherwise, the presence or absence of a `_user_seek_beginning`
187 # method indicates whether the iterator can seek beginning.
188 if hasattr(self
, '_user_can_seek_beginning'):
189 can_seek_beginning
= self
._user
_can
_seek
_beginning
()
190 utils
._check
_bool
(can_seek_beginning
)
191 return can_seek_beginning
193 return hasattr(self
, '_user_seek_beginning')
195 def _bt_seek_beginning_from_native(self
):
196 self
._user
_seek
_beginning
()
198 def _bt_can_seek_ns_from_origin_from_native(self
, ns_from_origin
):
199 # Return whether the iterator can seek ns from origin using the
200 # user-implemented seek_ns_from_origin method. We mimic the behavior
203 # - If the iterator has a _user_can_seek_ns_from_origin method,
204 # call it and use its return value.
205 # - Otherwise, if there is a `_user_seek_ns_from_origin` method,
206 # we presume it's possible.
208 if hasattr(self
, '_user_can_seek_ns_from_origin'):
209 can_seek_ns_from_origin
= self
._user
_can
_seek
_ns
_from
_origin
(ns_from_origin
)
210 utils
._check
_bool
(can_seek_ns_from_origin
)
211 return can_seek_ns_from_origin
213 return hasattr(self
, '_user_seek_ns_from_origin')
215 def _bt_seek_ns_from_origin_from_native(self
, ns_from_origin
):
216 self
._user
_seek
_ns
_from
_origin
(ns_from_origin
)
218 def _create_message_iterator(self
, input_port
):
219 utils
._check
_type
(input_port
, bt2_port
._UserComponentInputPort
)
221 if not input_port
.is_connected
:
222 raise ValueError('input port is not connected')
227 ) = native_bt
.bt2_message_iterator_create_from_message_iterator(
228 self
._bt
_ptr
, input_port
._ptr
230 utils
._handle
_func
_status
(status
, 'cannot create message iterator object')
231 assert msg_iter_ptr
is not None
233 return _UserComponentInputPortMessageIterator(msg_iter_ptr
)
235 def _create_event_message(self
, event_class
, parent
, default_clock_snapshot
=None):
236 utils
._check
_type
(event_class
, bt2_event_class
._EventClass
)
238 if event_class
.stream_class
.supports_packets
:
239 utils
._check
_type
(parent
, bt2_packet
._Packet
)
241 utils
._check
_type
(parent
, bt2_stream
._Stream
)
243 if default_clock_snapshot
is not None:
244 if event_class
.stream_class
.default_clock_class
is None:
246 'event messages in this stream must not have a default clock snapshot'
249 utils
._check
_uint
64(default_clock_snapshot
)
251 if event_class
.stream_class
.supports_packets
:
252 ptr
= native_bt
.message_event_create_with_packet_and_default_clock_snapshot(
253 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
, default_clock_snapshot
256 ptr
= native_bt
.message_event_create_with_default_clock_snapshot(
257 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
, default_clock_snapshot
260 if event_class
.stream_class
.default_clock_class
is not None:
262 'event messages in this stream must have a default clock snapshot'
265 if event_class
.stream_class
.supports_packets
:
266 ptr
= native_bt
.message_event_create_with_packet(
267 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
270 ptr
= native_bt
.message_event_create(
271 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
275 raise bt2
._MemoryError('cannot create event message object')
277 return bt2_message
._EventMessage
(ptr
)
279 def _create_message_iterator_inactivity_message(self
, clock_class
, clock_snapshot
):
280 utils
._check
_type
(clock_class
, bt2_clock_class
._ClockClass
)
281 ptr
= native_bt
.message_message_iterator_inactivity_create(
282 self
._bt
_ptr
, clock_class
._ptr
, clock_snapshot
286 raise bt2
._MemoryError('cannot create inactivity message object')
288 return bt2_message
._MessageIteratorInactivityMessage
(ptr
)
290 def _create_stream_beginning_message(self
, stream
, default_clock_snapshot
=None):
291 utils
._check
_type
(stream
, bt2_stream
._Stream
)
293 ptr
= native_bt
.message_stream_beginning_create(self
._bt
_ptr
, stream
._ptr
)
295 raise bt2
._MemoryError('cannot create stream beginning message object')
297 msg
= bt2_message
._StreamBeginningMessage
(ptr
)
299 if default_clock_snapshot
is not None:
300 msg
._default
_clock
_snapshot
= default_clock_snapshot
304 def _create_stream_end_message(self
, stream
, default_clock_snapshot
=None):
305 utils
._check
_type
(stream
, bt2_stream
._Stream
)
307 ptr
= native_bt
.message_stream_end_create(self
._bt
_ptr
, stream
._ptr
)
309 raise bt2
._MemoryError('cannot create stream end message object')
311 msg
= bt2_message
._StreamEndMessage
(ptr
)
313 if default_clock_snapshot
is not None:
314 msg
._default
_clock
_snapshot
= default_clock_snapshot
318 def _create_packet_beginning_message(self
, packet
, default_clock_snapshot
=None):
319 utils
._check
_type
(packet
, bt2_packet
._Packet
)
321 if packet
.stream
.cls
.packets_have_beginning_default_clock_snapshot
:
322 if default_clock_snapshot
is None:
324 "packet beginning messages in this stream must have a default clock snapshot"
327 utils
._check
_uint
64(default_clock_snapshot
)
328 ptr
= native_bt
.message_packet_beginning_create_with_default_clock_snapshot(
329 self
._bt
_ptr
, packet
._ptr
, default_clock_snapshot
332 if default_clock_snapshot
is not None:
334 "packet beginning messages in this stream must not have a default clock snapshot"
337 ptr
= native_bt
.message_packet_beginning_create(self
._bt
_ptr
, packet
._ptr
)
340 raise bt2
._MemoryError('cannot create packet beginning message object')
342 return bt2_message
._PacketBeginningMessage
(ptr
)
344 def _create_packet_end_message(self
, packet
, default_clock_snapshot
=None):
345 utils
._check
_type
(packet
, bt2_packet
._Packet
)
347 if packet
.stream
.cls
.packets_have_end_default_clock_snapshot
:
348 if default_clock_snapshot
is None:
350 "packet end messages in this stream must have a default clock snapshot"
353 utils
._check
_uint
64(default_clock_snapshot
)
354 ptr
= native_bt
.message_packet_end_create_with_default_clock_snapshot(
355 self
._bt
_ptr
, packet
._ptr
, default_clock_snapshot
358 if default_clock_snapshot
is not None:
360 "packet end messages in this stream must not have a default clock snapshot"
363 ptr
= native_bt
.message_packet_end_create(self
._bt
_ptr
, packet
._ptr
)
366 raise bt2
._MemoryError('cannot create packet end message object')
368 return bt2_message
._PacketEndMessage
(ptr
)
370 def _create_discarded_events_message(
371 self
, stream
, count
=None, beg_clock_snapshot
=None, end_clock_snapshot
=None
373 utils
._check
_type
(stream
, bt2_stream
._Stream
)
375 if not stream
.cls
.supports_discarded_events
:
376 raise ValueError('stream class does not support discarded events')
378 if stream
.cls
.discarded_events_have_default_clock_snapshots
:
379 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
381 'discarded events have default clock snapshots for this stream class'
384 utils
._check
_uint
64(beg_clock_snapshot
)
385 utils
._check
_uint
64(end_clock_snapshot
)
387 if beg_clock_snapshot
> end_clock_snapshot
:
389 'beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})'.format(
390 beg_clock_snapshot
, end_clock_snapshot
394 ptr
= native_bt
.message_discarded_events_create_with_default_clock_snapshots(
395 self
._bt
_ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
398 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
400 'discarded events have no default clock snapshots for this stream class'
403 ptr
= native_bt
.message_discarded_events_create(self
._bt
_ptr
, stream
._ptr
)
406 raise bt2
._MemoryError('cannot discarded events message object')
408 msg
= bt2_message
._DiscardedEventsMessage
(ptr
)
410 if count
is not None:
415 def _create_discarded_packets_message(
416 self
, stream
, count
=None, beg_clock_snapshot
=None, end_clock_snapshot
=None
418 utils
._check
_type
(stream
, bt2_stream
._Stream
)
420 if not stream
.cls
.supports_discarded_packets
:
421 raise ValueError('stream class does not support discarded packets')
423 if stream
.cls
.discarded_packets_have_default_clock_snapshots
:
424 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
426 'discarded packets have default clock snapshots for this stream class'
429 utils
._check
_uint
64(beg_clock_snapshot
)
430 utils
._check
_uint
64(end_clock_snapshot
)
432 if beg_clock_snapshot
> end_clock_snapshot
:
434 'beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})'.format(
435 beg_clock_snapshot
, end_clock_snapshot
439 ptr
= native_bt
.message_discarded_packets_create_with_default_clock_snapshots(
440 self
._bt
_ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
443 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
445 'discarded packets have no default clock snapshots for this stream class'
448 ptr
= native_bt
.message_discarded_packets_create(self
._bt
_ptr
, stream
._ptr
)
451 raise bt2
._MemoryError('cannot discarded packets message object')
453 msg
= bt2_message
._DiscardedPacketsMessage
(ptr
)
455 if count
is not None: