1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
23 from bt2
import native_bt
, object, utils
24 from bt2
import message
as bt2_message
25 import collections
.abc
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
34 class _MessageIterator(collections
.abc
.Iterator
):
36 raise NotImplementedError
39 class _UserComponentInputPortMessageIterator(object._SharedObject
, _MessageIterator
):
40 _get_ref
= staticmethod(
41 native_bt
.self_component_port_input_message_iterator_get_ref
43 _put_ref
= staticmethod(
44 native_bt
.self_component_port_input_message_iterator_put_ref
47 def __init__(self
, ptr
):
48 self
._current
_msgs
= []
53 if len(self
._current
_msgs
) == self
._at
:
54 status
, msgs
= native_bt
.bt2_self_component_port_input_get_msg_range(
57 utils
._handle
_func
_status
(
58 status
, 'unexpected error: cannot advance the message iterator'
60 self
._current
_msgs
= msgs
63 msg_ptr
= self
._current
_msgs
[self
._at
]
66 return bt2_message
._create
_from
_ptr
(msg_ptr
)
68 def can_seek_beginning(self
):
69 status
, res
= native_bt
.self_component_port_input_message_iterator_can_seek_beginning(
72 utils
._handle
_func
_status
(
74 'cannot check whether or not message iterator can seek its beginning',
78 def seek_beginning(self
):
79 # Forget about buffered messages, they won't be valid after seeking.
80 self
._current
_msgs
.clear()
83 status
= native_bt
.self_component_port_input_message_iterator_seek_beginning(
86 utils
._handle
_func
_status
(status
, 'cannot seek message iterator beginning')
88 def can_seek_ns_from_origin(self
, ns_from_origin
):
89 utils
._check
_int
64(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
93 utils
._handle
_func
_status
(
95 'cannot check whether or not message iterator can seek given ns from origin',
99 def seek_ns_from_origin(self
, ns_from_origin
):
100 utils
._check
_int
64(ns_from_origin
)
102 # Forget about buffered messages, they won't be valid after seeking.
103 self
._current
_msgs
.clear()
106 status
= native_bt
.self_component_port_input_message_iterator_seek_ns_from_origin(
107 self
._ptr
, ns_from_origin
109 utils
._handle
_func
_status
(
110 status
, 'message iterator cannot seek given ns from origin'
114 def can_seek_forward(self
):
115 return native_bt
.self_component_port_input_message_iterator_can_seek_forward(
120 class _MessageIteratorConfiguration
:
121 def __init__(self
, ptr
):
124 def can_seek_forward(self
, value
):
125 utils
._check
_bool
(value
)
126 native_bt
.self_message_iterator_configuration_set_can_seek_forward(
130 can_seek_forward
= property(fset
=can_seek_forward
)
133 # This is extended by the user to implement component classes in Python. It
134 # is created for a given output port when an input port message iterator is
135 # created on the input port on the other side of the connection. It is also
136 # created when an output port message iterator is created on this output port.
138 # Its purpose is to feed the messages that should go out through this output
140 class _UserMessageIterator(_MessageIterator
):
141 def __new__(cls
, ptr
):
142 # User iterator objects are always created by the native side,
143 # that is, never instantiated directly by Python code.
145 # The native code calls this, then manually calls
146 # self.__init__() without the `ptr` argument. The user has
147 # access to self.component during this call, thanks to this
148 # self._bt_ptr argument being set.
150 # self._bt_ptr is NOT owned by this object here, so there's nothing
151 # to do in __del__().
152 self
= super().__new
__(cls
)
156 def _bt_init_from_native(self
, config_ptr
, self_output_port_ptr
):
157 self_output_port
= bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(
158 self_output_port_ptr
, native_bt
.PORT_TYPE_OUTPUT
160 config
= _MessageIteratorConfiguration(config_ptr
)
161 self
.__init
__(config
, self_output_port
)
163 def __init__(self
, config
, self_output_port
):
167 def _component(self
):
168 return native_bt
.bt2_get_user_component_from_user_msg_iter(self
._bt
_ptr
)
172 port_ptr
= native_bt
.self_message_iterator_borrow_port(self
._bt
_ptr
)
173 assert port_ptr
is not None
174 return bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(
175 port_ptr
, native_bt
.PORT_TYPE_OUTPUT
180 return int(self
._bt
_ptr
)
183 def _is_interrupted(self
):
184 return bool(native_bt
.self_message_iterator_is_interrupted(self
._bt
_ptr
))
186 def _user_finalize(self
):
192 def _bt_next_from_native(self
):
193 # this can raise anything: it's catched by the native part
196 except StopIteration:
201 utils
._check
_type
(msg
, bt2_message
._MessageConst
)
203 # The reference we return will be given to the message array.
204 # However, the `msg` Python object may stay alive, if the user has kept
205 # a reference to it. Acquire a new reference to account for that.
206 msg
._get
_ref
(msg
._ptr
)
209 def _bt_can_seek_beginning_from_native(self
):
210 # Here, we mimic the behavior of the C API:
212 # - If the iterator has a _user_can_seek_beginning method,
213 # read it and use that result.
214 # - Otherwise, the presence or absence of a `_user_seek_beginning`
215 # method indicates whether the iterator can seek beginning.
216 if hasattr(self
, '_user_can_seek_beginning'):
217 can_seek_beginning
= self
._user
_can
_seek
_beginning
()
218 utils
._check
_bool
(can_seek_beginning
)
219 return can_seek_beginning
221 return hasattr(self
, '_user_seek_beginning')
223 def _bt_seek_beginning_from_native(self
):
224 self
._user
_seek
_beginning
()
226 def _bt_can_seek_ns_from_origin_from_native(self
, ns_from_origin
):
227 # Return whether the iterator can seek ns from origin using the
228 # user-implemented seek_ns_from_origin method. We mimic the behavior
231 # - If the iterator has a _user_can_seek_ns_from_origin method,
232 # call it and use its return value.
233 # - Otherwise, if there is a `_user_seek_ns_from_origin` method,
234 # we presume it's possible.
236 if hasattr(self
, '_user_can_seek_ns_from_origin'):
237 can_seek_ns_from_origin
= self
._user
_can
_seek
_ns
_from
_origin
(ns_from_origin
)
238 utils
._check
_bool
(can_seek_ns_from_origin
)
239 return can_seek_ns_from_origin
241 return hasattr(self
, '_user_seek_ns_from_origin')
243 def _bt_seek_ns_from_origin_from_native(self
, ns_from_origin
):
244 self
._user
_seek
_ns
_from
_origin
(ns_from_origin
)
246 def _create_input_port_message_iterator(self
, input_port
):
247 utils
._check
_type
(input_port
, bt2_port
._UserComponentInputPort
)
249 status
, msg_iter_ptr
= native_bt
.bt2_self_component_port_input_message_iterator_create_from_message_iterator(
250 self
._bt
_ptr
, input_port
._ptr
252 utils
._handle
_func
_status
(status
, 'cannot create message iterator object')
253 assert msg_iter_ptr
is not None
255 return _UserComponentInputPortMessageIterator(msg_iter_ptr
)
257 def _create_event_message(self
, event_class
, parent
, default_clock_snapshot
=None):
258 utils
._check
_type
(event_class
, bt2_event_class
._EventClass
)
260 if event_class
.stream_class
.supports_packets
:
261 utils
._check
_type
(parent
, bt2_packet
._Packet
)
263 utils
._check
_type
(parent
, bt2_stream
._Stream
)
265 if default_clock_snapshot
is not None:
266 if event_class
.stream_class
.default_clock_class
is None:
268 'event messages in this stream must not have a default clock snapshot'
271 utils
._check
_uint
64(default_clock_snapshot
)
273 if event_class
.stream_class
.supports_packets
:
274 ptr
= native_bt
.message_event_create_with_packet_and_default_clock_snapshot(
275 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
, default_clock_snapshot
278 ptr
= native_bt
.message_event_create_with_default_clock_snapshot(
279 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
, default_clock_snapshot
282 if event_class
.stream_class
.default_clock_class
is not None:
284 'event messages in this stream must have a default clock snapshot'
287 if event_class
.stream_class
.supports_packets
:
288 ptr
= native_bt
.message_event_create_with_packet(
289 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
292 ptr
= native_bt
.message_event_create(
293 self
._bt
_ptr
, event_class
._ptr
, parent
._ptr
297 raise bt2
._MemoryError('cannot create event message object')
299 return bt2_message
._EventMessage
(ptr
)
301 def _create_message_iterator_inactivity_message(self
, clock_class
, clock_snapshot
):
302 utils
._check
_type
(clock_class
, bt2_clock_class
._ClockClass
)
303 ptr
= native_bt
.message_message_iterator_inactivity_create(
304 self
._bt
_ptr
, clock_class
._ptr
, clock_snapshot
308 raise bt2
._MemoryError('cannot create inactivity message object')
310 return bt2_message
._MessageIteratorInactivityMessage
(ptr
)
312 def _create_stream_beginning_message(self
, stream
, default_clock_snapshot
=None):
313 utils
._check
_type
(stream
, bt2_stream
._Stream
)
315 ptr
= native_bt
.message_stream_beginning_create(self
._bt
_ptr
, stream
._ptr
)
317 raise bt2
._MemoryError('cannot create stream beginning message object')
319 msg
= bt2_message
._StreamBeginningMessage
(ptr
)
321 if default_clock_snapshot
is not None:
322 msg
._default
_clock
_snapshot
= default_clock_snapshot
326 def _create_stream_end_message(self
, stream
, default_clock_snapshot
=None):
327 utils
._check
_type
(stream
, bt2_stream
._Stream
)
329 ptr
= native_bt
.message_stream_end_create(self
._bt
_ptr
, stream
._ptr
)
331 raise bt2
._MemoryError('cannot create stream end message object')
333 msg
= bt2_message
._StreamEndMessage
(ptr
)
335 if default_clock_snapshot
is not None:
336 msg
._default
_clock
_snapshot
= default_clock_snapshot
340 def _create_packet_beginning_message(self
, packet
, default_clock_snapshot
=None):
341 utils
._check
_type
(packet
, bt2_packet
._Packet
)
343 if packet
.stream
.cls
.packets_have_beginning_default_clock_snapshot
:
344 if default_clock_snapshot
is None:
346 "packet beginning messages in this stream must have a default clock snapshot"
349 utils
._check
_uint
64(default_clock_snapshot
)
350 ptr
= native_bt
.message_packet_beginning_create_with_default_clock_snapshot(
351 self
._bt
_ptr
, packet
._ptr
, default_clock_snapshot
354 if default_clock_snapshot
is not None:
356 "packet beginning messages in this stream must not have a default clock snapshot"
359 ptr
= native_bt
.message_packet_beginning_create(self
._bt
_ptr
, packet
._ptr
)
362 raise bt2
._MemoryError('cannot create packet beginning message object')
364 return bt2_message
._PacketBeginningMessage
(ptr
)
366 def _create_packet_end_message(self
, packet
, default_clock_snapshot
=None):
367 utils
._check
_type
(packet
, bt2_packet
._Packet
)
369 if packet
.stream
.cls
.packets_have_end_default_clock_snapshot
:
370 if default_clock_snapshot
is None:
372 "packet end messages in this stream must have a default clock snapshot"
375 utils
._check
_uint
64(default_clock_snapshot
)
376 ptr
= native_bt
.message_packet_end_create_with_default_clock_snapshot(
377 self
._bt
_ptr
, packet
._ptr
, default_clock_snapshot
380 if default_clock_snapshot
is not None:
382 "packet end messages in this stream must not have a default clock snapshot"
385 ptr
= native_bt
.message_packet_end_create(self
._bt
_ptr
, packet
._ptr
)
388 raise bt2
._MemoryError('cannot create packet end message object')
390 return bt2_message
._PacketEndMessage
(ptr
)
392 def _create_discarded_events_message(
393 self
, stream
, count
=None, beg_clock_snapshot
=None, end_clock_snapshot
=None
395 utils
._check
_type
(stream
, bt2_stream
._Stream
)
397 if not stream
.cls
.supports_discarded_events
:
398 raise ValueError('stream class does not support discarded events')
400 if stream
.cls
.discarded_events_have_default_clock_snapshots
:
401 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
403 'discarded events have default clock snapshots for this stream class'
406 utils
._check
_uint
64(beg_clock_snapshot
)
407 utils
._check
_uint
64(end_clock_snapshot
)
408 ptr
= native_bt
.message_discarded_events_create_with_default_clock_snapshots(
409 self
._bt
_ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
412 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
414 'discarded events have no default clock snapshots for this stream class'
417 ptr
= native_bt
.message_discarded_events_create(self
._bt
_ptr
, stream
._ptr
)
420 raise bt2
._MemoryError('cannot discarded events message object')
422 msg
= bt2_message
._DiscardedEventsMessage
(ptr
)
424 if count
is not None:
429 def _create_discarded_packets_message(
430 self
, stream
, count
=None, beg_clock_snapshot
=None, end_clock_snapshot
=None
432 utils
._check
_type
(stream
, bt2_stream
._Stream
)
434 if not stream
.cls
.supports_discarded_packets
:
435 raise ValueError('stream class does not support discarded packets')
437 if stream
.cls
.discarded_packets_have_default_clock_snapshots
:
438 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
440 'discarded packets have default clock snapshots for this stream class'
443 utils
._check
_uint
64(beg_clock_snapshot
)
444 utils
._check
_uint
64(end_clock_snapshot
)
445 ptr
= native_bt
.message_discarded_packets_create_with_default_clock_snapshots(
446 self
._bt
_ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
449 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
451 'discarded packets have no default clock snapshots for this stream class'
454 ptr
= native_bt
.message_discarded_packets_create(self
._bt
_ptr
, stream
._ptr
)
457 raise bt2
._MemoryError('cannot discarded packets message object')
459 msg
= bt2_message
._DiscardedPacketsMessage
(ptr
)
461 if count
is not None: