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
)
224 ) = native_bt
.bt2_message_iterator_create_from_message_iterator(
225 self
._bt
_ptr
, input_port
._ptr
227 utils
._handle
_func
_status
(status
, 'cannot create message iterator object')
228 assert msg_iter_ptr
is not None
230 return _UserComponentInputPortMessageIterator(msg_iter_ptr
)
232 def _create_event_message(self
, event_class
, parent
, default_clock_snapshot
=None):
233 utils
._check
_type
(event_class
, bt2_event_class
._EventClass
)
235 if event_class
.stream_class
.supports_packets
:
236 utils
._check
_type
(parent
, bt2_packet
._Packet
)
238 utils
._check
_type
(parent
, bt2_stream
._Stream
)
240 if default_clock_snapshot
is not None:
241 if event_class
.stream_class
.default_clock_class
is None:
243 'event messages in this stream must not have a default clock snapshot'
246 utils
._check
_uint
64(default_clock_snapshot
)
248 if event_class
.stream_class
.supports_packets
:
249 ptr
= native_bt
.message_event_create_with_packet_and_default_clock_snapshot(
250 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
, default_clock_snapshot
253 ptr
= native_bt
.message_event_create_with_default_clock_snapshot(
254 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
, default_clock_snapshot
257 if event_class
.stream_class
.default_clock_class
is not None:
259 'event messages in this stream must have a default clock snapshot'
262 if event_class
.stream_class
.supports_packets
:
263 ptr
= native_bt
.message_event_create_with_packet(
264 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
267 ptr
= native_bt
.message_event_create(
268 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
272 raise bt2
._MemoryError('cannot create event message object')
274 return bt2_message
._EventMessage
(ptr
)
276 def _create_message_iterator_inactivity_message(self
, clock_class
, clock_snapshot
):
277 utils
._check
_type
(clock_class
, bt2_clock_class
._ClockClass
)
278 ptr
= native_bt
.message_message_iterator_inactivity_create(
279 self
._bt
_ptr
, clock_class
._ptr
, clock_snapshot
283 raise bt2
._MemoryError('cannot create inactivity message object')
285 return bt2_message
._MessageIteratorInactivityMessage
(ptr
)
287 def _create_stream_beginning_message(self
, stream
, default_clock_snapshot
=None):
288 utils
._check
_type
(stream
, bt2_stream
._Stream
)
290 ptr
= native_bt
.message_stream_beginning_create(self
._bt
_ptr
, stream
._ptr
)
292 raise bt2
._MemoryError('cannot create stream beginning message object')
294 msg
= bt2_message
._StreamBeginningMessage
(ptr
)
296 if default_clock_snapshot
is not None:
297 msg
._default
_clock
_snapshot
= default_clock_snapshot
301 def _create_stream_end_message(self
, stream
, default_clock_snapshot
=None):
302 utils
._check
_type
(stream
, bt2_stream
._Stream
)
304 ptr
= native_bt
.message_stream_end_create(self
._bt
_ptr
, stream
._ptr
)
306 raise bt2
._MemoryError('cannot create stream end message object')
308 msg
= bt2_message
._StreamEndMessage
(ptr
)
310 if default_clock_snapshot
is not None:
311 msg
._default
_clock
_snapshot
= default_clock_snapshot
315 def _create_packet_beginning_message(self
, packet
, default_clock_snapshot
=None):
316 utils
._check
_type
(packet
, bt2_packet
._Packet
)
318 if packet
.stream
.cls
.packets_have_beginning_default_clock_snapshot
:
319 if default_clock_snapshot
is None:
321 "packet beginning messages in this stream must have a default clock snapshot"
324 utils
._check
_uint
64(default_clock_snapshot
)
325 ptr
= native_bt
.message_packet_beginning_create_with_default_clock_snapshot(
326 self
._bt
_ptr
, packet
._ptr
, default_clock_snapshot
329 if default_clock_snapshot
is not None:
331 "packet beginning messages in this stream must not have a default clock snapshot"
334 ptr
= native_bt
.message_packet_beginning_create(self
._bt
_ptr
, packet
._ptr
)
337 raise bt2
._MemoryError('cannot create packet beginning message object')
339 return bt2_message
._PacketBeginningMessage
(ptr
)
341 def _create_packet_end_message(self
, packet
, default_clock_snapshot
=None):
342 utils
._check
_type
(packet
, bt2_packet
._Packet
)
344 if packet
.stream
.cls
.packets_have_end_default_clock_snapshot
:
345 if default_clock_snapshot
is None:
347 "packet end messages in this stream must have a default clock snapshot"
350 utils
._check
_uint
64(default_clock_snapshot
)
351 ptr
= native_bt
.message_packet_end_create_with_default_clock_snapshot(
352 self
._bt
_ptr
, packet
._ptr
, default_clock_snapshot
355 if default_clock_snapshot
is not None:
357 "packet end messages in this stream must not have a default clock snapshot"
360 ptr
= native_bt
.message_packet_end_create(self
._bt
_ptr
, packet
._ptr
)
363 raise bt2
._MemoryError('cannot create packet end message object')
365 return bt2_message
._PacketEndMessage
(ptr
)
367 def _create_discarded_events_message(
368 self
, stream
, count
=None, beg_clock_snapshot
=None, end_clock_snapshot
=None
370 utils
._check
_type
(stream
, bt2_stream
._Stream
)
372 if not stream
.cls
.supports_discarded_events
:
373 raise ValueError('stream class does not support discarded events')
375 if stream
.cls
.discarded_events_have_default_clock_snapshots
:
376 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
378 'discarded events have default clock snapshots for this stream class'
381 utils
._check
_uint
64(beg_clock_snapshot
)
382 utils
._check
_uint
64(end_clock_snapshot
)
383 ptr
= native_bt
.message_discarded_events_create_with_default_clock_snapshots(
384 self
._bt
_ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
387 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
389 'discarded events have no default clock snapshots for this stream class'
392 ptr
= native_bt
.message_discarded_events_create(self
._bt
_ptr
, stream
._ptr
)
395 raise bt2
._MemoryError('cannot discarded events message object')
397 msg
= bt2_message
._DiscardedEventsMessage
(ptr
)
399 if count
is not None:
404 def _create_discarded_packets_message(
405 self
, stream
, count
=None, beg_clock_snapshot
=None, end_clock_snapshot
=None
407 utils
._check
_type
(stream
, bt2_stream
._Stream
)
409 if not stream
.cls
.supports_discarded_packets
:
410 raise ValueError('stream class does not support discarded packets')
412 if stream
.cls
.discarded_packets_have_default_clock_snapshots
:
413 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
415 'discarded packets have default clock snapshots for this stream class'
418 utils
._check
_uint
64(beg_clock_snapshot
)
419 utils
._check
_uint
64(end_clock_snapshot
)
420 ptr
= native_bt
.message_discarded_packets_create_with_default_clock_snapshots(
421 self
._bt
_ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
424 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
426 'discarded packets have no default clock snapshots for this stream class'
429 ptr
= native_bt
.message_discarded_packets_create(self
._bt
_ptr
, stream
._ptr
)
432 raise bt2
._MemoryError('cannot discarded packets message object')
434 msg
= bt2_message
._DiscardedPacketsMessage
(ptr
)
436 if count
is not None: