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
25 import collections
.abc
30 class _MessageIterator(collections
.abc
.Iterator
):
32 raise NotImplementedError
35 class _GenericMessageIterator(object._SharedObject
, _MessageIterator
):
36 def __init__(self
, ptr
):
37 self
._current
_msgs
= []
41 def _handle_status(self
, status
, gen_error_msg
):
42 if status
== native_bt
.MESSAGE_ITERATOR_STATUS_AGAIN
:
44 elif status
== native_bt
.MESSAGE_ITERATOR_STATUS_END
:
47 raise bt2
.Error(gen_error_msg
)
50 if len(self
._current
_msgs
) == self
._at
:
51 status
, msgs
= self
._get
_msg
_range
(self
._ptr
)
52 self
._handle
_status
(status
,
53 'unexpected error: cannot advance the message iterator')
54 self
._current
_msgs
= msgs
57 msg_ptr
= self
._current
_msgs
[self
._at
]
60 return bt2
.message
._create
_from
_ptr
(msg_ptr
)
63 def can_seek_beginning(self
):
64 res
= self
._can
_seek
_beginning
(self
._ptr
)
67 def seek_beginning(self
):
68 # Forget about buffered messages, they won't be valid after seeking..
69 self
._current
_msgs
.clear()
72 status
= self
._seek
_beginning
(self
._ptr
)
73 self
._handle
_status
(status
, 'cannot seek message iterator beginning')
76 # This is created when a component wants to iterate on one of its input ports.
77 class _UserComponentInputPortMessageIterator(_GenericMessageIterator
):
78 _get_msg_range
= staticmethod(native_bt
.py3_self_component_port_input_get_msg_range
)
79 _get_ref
= staticmethod(native_bt
.self_component_port_input_message_iterator_get_ref
)
80 _put_ref
= staticmethod(native_bt
.self_component_port_input_message_iterator_put_ref
)
81 _can_seek_beginning
= staticmethod(native_bt
.self_component_port_input_message_iterator_can_seek_beginning
)
82 _seek_beginning
= staticmethod(native_bt
.self_component_port_input_message_iterator_seek_beginning
)
85 # This is created when the user wants to iterate on a component's output port,
86 # from outside the graph.
87 class _OutputPortMessageIterator(_GenericMessageIterator
):
88 _get_msg_range
= staticmethod(native_bt
.py3_port_output_get_msg_range
)
89 _get_ref
= staticmethod(native_bt
.port_output_message_iterator_get_ref
)
90 _put_ref
= staticmethod(native_bt
.port_output_message_iterator_put_ref
)
91 _can_seek_beginning
= staticmethod(native_bt
.port_output_message_iterator_can_seek_beginning
)
92 _seek_beginning
= staticmethod(native_bt
.port_output_message_iterator_seek_beginning
)
95 # This is extended by the user to implement component classes in Python. It
96 # is created for a given output port when an input port message iterator is
97 # created on the input port on the other side of the connection. It is also
98 # created when an output port message iterator is created on this output port.
100 # Its purpose is to feed the messages that should go out through this output
102 class _UserMessageIterator(_MessageIterator
):
103 def __new__(cls
, ptr
):
104 # User iterator objects are always created by the native side,
105 # that is, never instantiated directly by Python code.
107 # The native code calls this, then manually calls
108 # self.__init__() without the `ptr` argument. The user has
109 # access to self.component during this call, thanks to this
110 # self._ptr argument being set.
112 # self._ptr is NOT owned by this object here, so there's nothing
113 # to do in __del__().
114 self
= super().__new
__(cls
)
118 def _init_from_native(self
, self_output_port_ptr
):
119 self_output_port
= bt2
.port
._create
_self
_from
_ptr
_and
_get
_ref
(
120 self_output_port_ptr
, native_bt
.PORT_TYPE_OUTPUT
)
121 self
.__init
__(self_output_port
)
123 def __init__(self
, output_port
):
127 def _component(self
):
128 return native_bt
.py3_get_user_component_from_user_msg_iter(self
._ptr
)
132 return int(self
._ptr
)
140 def _next_from_native(self
):
141 # this can raise anything: it's catched by the native part
144 except StopIteration:
149 utils
._check
_type
(msg
, bt2
.message
._Message
)
151 # The reference we return will be given to the message array.
152 # However, the `msg` Python object may stay alive, if the user has kept
153 # a reference to it. Acquire a new reference to account for that.
154 msg
._get
_ref
(msg
._ptr
)
158 def _can_seek_beginning_from_native(self
):
159 # Here, we mimic the behavior of the C API:
161 # - If the iterator has a _can_seek_beginning attribute, read it and use
163 # - Otherwise, the presence or absence of a `_seek_beginning`
164 # method indicates whether the iterator can seek beginning.
165 if hasattr(self
, '_can_seek_beginning'):
166 can_seek_beginning
= self
._can
_seek
_beginning
167 utils
._check
_bool
(can_seek_beginning
)
168 return can_seek_beginning
170 return hasattr(self
, '_seek_beginning')
172 def _seek_beginning_from_native(self
):
173 self
._seek
_beginning
()
175 def _create_event_message(self
, event_class
, packet
, default_clock_snapshot
=None):
176 utils
._check
_type
(event_class
, bt2
.event_class
._EventClass
)
177 utils
._check
_type
(packet
, bt2
.packet
._Packet
)
179 if default_clock_snapshot
is not None:
180 if event_class
.stream_class
.default_clock_class
is None:
181 raise ValueError('event messages in this stream must not have a default clock snapshot')
183 utils
._check
_uint
64(default_clock_snapshot
)
184 ptr
= native_bt
.message_event_create_with_default_clock_snapshot(
185 self
._ptr
, event_class
._ptr
, packet
._ptr
, default_clock_snapshot
)
187 if event_class
.stream_class
.default_clock_class
is not None:
188 raise ValueError('event messages in this stream must have a default clock snapshot')
190 ptr
= native_bt
.message_event_create(
191 self
._ptr
, event_class
._ptr
, packet
._ptr
)
194 raise bt2
.CreationError('cannot create event message object')
196 return bt2
.message
._EventMessage
(ptr
)
198 def _create_message_iterator_inactivity_message(self
, clock_class
, clock_snapshot
):
199 utils
._check
_type
(clock_class
, bt2
.clock_class
._ClockClass
)
200 ptr
= native_bt
.message_message_iterator_inactivity_create(
201 self
._ptr
, clock_class
._ptr
, clock_snapshot
)
204 raise bt2
.CreationError('cannot create inactivity message object')
206 return bt2
.message
._MessageIteratorInactivityMessage
(ptr
)
208 _unknown_clock_snapshot
= bt2
.message
._StreamActivityMessageUnknownClockSnapshot
()
209 _infinite_clock_snapshot
= bt2
.message
._StreamActivityMessageInfiniteClockSnapshot
()
212 def _validate_stream_activity_message_default_clock_snapshot(stream
, default_cs
):
213 isinst_infinite
= isinstance(default_cs
, bt2
.message
._StreamActivityMessageInfiniteClockSnapshot
)
214 isinst_unknown
= isinstance(default_cs
, bt2
.message
._StreamActivityMessageUnknownClockSnapshot
)
216 if utils
._is
_uint
64(default_cs
):
218 elif isinst_infinite
or isinst_unknown
:
219 if default_cs
is not _UserMessageIterator
._unknown
_clock
_snapshot
and default_cs
is not _UserMessageIterator
._infinite
_clock
_snapshot
:
220 raise ValueError('unexpected value for default clock snapshot')
222 raise TypeError("unexpected type '{}' for default clock snapshot".format(default_cs
.__class
__.__name
__))
224 if stream
.cls
.default_clock_class
is None:
225 if utils
._is
_uint
64(default_cs
):
226 raise ValueError('stream activity messages in this stream cannot have a known default clock snapshot')
228 def _create_stream_beginning_message(self
, stream
):
229 utils
._check
_type
(stream
, bt2
.stream
._Stream
)
231 ptr
= native_bt
.message_stream_beginning_create(self
._ptr
, stream
._ptr
)
233 raise bt2
.CreationError('cannot create stream beginning message object')
235 return bt2
.message
._StreamBeginningMessage
(ptr
)
237 def _create_stream_activity_beginning_message(self
, stream
,
238 default_clock_snapshot
=_unknown_clock_snapshot
):
239 utils
._check
_type
(stream
, bt2
.stream
._Stream
)
240 self
._validate
_stream
_activity
_message
_default
_clock
_snapshot
(stream
, default_clock_snapshot
)
241 ptr
= native_bt
.message_stream_activity_beginning_create(self
._ptr
, stream
._ptr
)
244 raise bt2
.CreationError(
245 'cannot create stream activity beginning message object')
247 msg
= bt2
.message
._StreamActivityBeginningMessage
(ptr
)
248 msg
._default
_clock
_snapshot
= default_clock_snapshot
251 def _create_stream_activity_end_message(self
, stream
,
252 default_clock_snapshot
=_unknown_clock_snapshot
):
253 utils
._check
_type
(stream
, bt2
.stream
._Stream
)
254 self
._validate
_stream
_activity
_message
_default
_clock
_snapshot
(stream
, default_clock_snapshot
)
255 ptr
= native_bt
.message_stream_activity_end_create(self
._ptr
, stream
._ptr
)
258 raise bt2
.CreationError(
259 'cannot create stream activity end message object')
261 msg
= bt2
.message
._StreamActivityEndMessage
(ptr
)
262 msg
._default
_clock
_snapshot
= default_clock_snapshot
265 def _create_stream_end_message(self
, stream
):
266 utils
._check
_type
(stream
, bt2
.stream
._Stream
)
268 ptr
= native_bt
.message_stream_end_create(self
._ptr
, stream
._ptr
)
270 raise bt2
.CreationError('cannot create stream end message object')
272 return bt2
.message
._StreamEndMessage
(ptr
)
274 def _create_packet_beginning_message(self
, packet
, default_clock_snapshot
=None):
275 utils
._check
_type
(packet
, bt2
.packet
._Packet
)
277 if packet
.stream
.cls
.packets_have_beginning_default_clock_snapshot
:
278 if default_clock_snapshot
is None:
279 raise ValueError("packet beginning messages in this stream must have a default clock snapshot")
281 utils
._check
_uint
64(default_clock_snapshot
)
282 ptr
= native_bt
.message_packet_beginning_create_with_default_clock_snapshot(
283 self
._ptr
, packet
._ptr
, default_clock_snapshot
)
285 if default_clock_snapshot
is not None:
286 raise ValueError("packet beginning messages in this stream must not have a default clock snapshot")
288 ptr
= native_bt
.message_packet_beginning_create(self
._ptr
, packet
._ptr
)
291 raise bt2
.CreationError('cannot create packet beginning message object')
293 return bt2
.message
._PacketBeginningMessage
(ptr
)
295 def _create_packet_end_message(self
, packet
, default_clock_snapshot
=None):
296 utils
._check
_type
(packet
, bt2
.packet
._Packet
)
298 if packet
.stream
.cls
.packets_have_end_default_clock_snapshot
:
299 if default_clock_snapshot
is None:
300 raise ValueError("packet end messages in this stream must have a default clock snapshot")
302 utils
._check
_uint
64(default_clock_snapshot
)
303 ptr
= native_bt
.message_packet_end_create_with_default_clock_snapshot(
304 self
._ptr
, packet
._ptr
, default_clock_snapshot
)
306 if default_clock_snapshot
is not None:
307 raise ValueError("packet end messages in this stream must not have a default clock snapshot")
309 ptr
= native_bt
.message_packet_end_create(self
._ptr
, packet
._ptr
)
312 raise bt2
.CreationError('cannot create packet end message object')
314 return bt2
.message
._PacketEndMessage
(ptr
)
316 def _create_discarded_events_message(self
, stream
, count
=None,
317 beg_clock_snapshot
=None,
318 end_clock_snapshot
=None):
319 utils
._check
_type
(stream
, bt2
.stream
._Stream
)
321 if not stream
.cls
.supports_discarded_events
:
322 raise ValueError('stream class does not support discarded events')
324 if stream
.cls
.discarded_events_have_default_clock_snapshots
:
325 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
326 raise ValueError('discarded events have default clock snapshots for this stream class')
328 utils
._check
_uint
64(beg_clock_snapshot
)
329 utils
._check
_uint
64(end_clock_snapshot
)
330 ptr
= native_bt
.message_discarded_events_create_with_default_clock_snapshots(
331 self
._ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
)
333 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
334 raise ValueError('discarded events have no default clock snapshots for this stream class')
336 ptr
= native_bt
.message_discarded_events_create(
337 self
._ptr
, stream
._ptr
)
340 raise bt2
.CreationError('cannot discarded events message object')
342 msg
= bt2
.message
._DiscardedEventsMessage
(ptr
)
344 if count
is not None:
349 def _create_discarded_packets_message(self
, stream
, count
=None, beg_clock_snapshot
=None, end_clock_snapshot
=None):
350 utils
._check
_type
(stream
, bt2
.stream
._Stream
)
352 if not stream
.cls
.supports_discarded_packets
:
353 raise ValueError('stream class does not support discarded packets')
355 if stream
.cls
.discarded_packets_have_default_clock_snapshots
:
356 if beg_clock_snapshot
is None or end_clock_snapshot
is None:
357 raise ValueError('discarded packets have default clock snapshots for this stream class')
359 utils
._check
_uint
64(beg_clock_snapshot
)
360 utils
._check
_uint
64(end_clock_snapshot
)
361 ptr
= native_bt
.message_discarded_packets_create_with_default_clock_snapshots(
362 self
._ptr
, stream
._ptr
, beg_clock_snapshot
, end_clock_snapshot
)
364 if beg_clock_snapshot
is not None or end_clock_snapshot
is not None:
365 raise ValueError('discarded packets have no default clock snapshots for this stream class')
367 ptr
= native_bt
.message_discarded_packets_create(
368 self
._ptr
, stream
._ptr
)
371 raise bt2
.CreationError('cannot discarded packets message object')
373 msg
= bt2
.message
._DiscardedPacketsMessage
(ptr
)
375 if count
is not None: