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
):
31 def _handle_status(self
, status
, gen_error_msg
):
32 if status
== native_bt
.MESSAGE_ITERATOR_STATUS_AGAIN
:
34 elif status
== native_bt
.MESSAGE_ITERATOR_STATUS_END
:
37 raise bt2
.Error(gen_error_msg
)
40 raise NotImplementedError
43 class _GenericMessageIterator(object._SharedObject
, _MessageIterator
):
44 def __init__(self
, ptr
):
45 self
._current
_msgs
= []
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 class _PrivateConnectionMessageIterator(_GenericMessageIterator
):
66 comp_ptr
= native_bt
.private_connection_message_iterator_get_component(self
._ptr
)
68 return bt2
.component
._create
_generic
_component
_from
_ptr
(comp_ptr
)
71 class _OutputPortMessageIterator(_GenericMessageIterator
):
72 _get_msg_range
= staticmethod(native_bt
.py3_port_output_get_msg_range
)
73 _get_ref
= staticmethod(native_bt
.port_output_message_iterator_get_ref
)
74 _put_ref
= staticmethod(native_bt
.port_output_message_iterator_put_ref
)
77 class _UserMessageIterator(_MessageIterator
):
78 def __new__(cls
, ptr
):
79 # User iterator objects are always created by the native side,
80 # that is, never instantiated directly by Python code.
82 # The native code calls this, then manually calls
83 # self.__init__() without the `ptr` argument. The user has
84 # access to self.component during this call, thanks to this
85 # self._ptr argument being set.
87 # self._ptr is NOT owned by this object here, so there's nothing
89 self
= super().__new
__(cls
)
98 return native_bt
.py3_get_user_component_from_user_msg_iter(self
._ptr
)
102 return int(self
._ptr
)
110 def _next_from_native(self
):
111 # this can raise anything: it's catched by the native part
114 except StopIteration:
119 utils
._check
_type
(msg
, bt2
.message
._Message
)
121 # Release the reference to the native part.
125 # Validate that the presence or lack of presence of a
126 # `default_clock_snapshot` value is valid in the context of `stream_class`.
128 def _validate_default_clock_snapshot(stream_class
, default_clock_snapshot
):
129 stream_class_has_default_clock_class
= stream_class
.default_clock_class
is not None
131 if stream_class_has_default_clock_class
and default_clock_snapshot
is None:
133 'stream class has a default clock class, default_clock_snapshot should not be None')
135 if not stream_class_has_default_clock_class
and default_clock_snapshot
is not None:
137 'stream class has no default clock class, default_clock_snapshot should be None')
139 def _create_event_message(self
, event_class
, packet
, default_clock_snapshot
=None):
140 utils
._check
_type
(event_class
, bt2
.event_class
.EventClass
)
141 utils
._check
_type
(packet
, bt2
.packet
._Packet
)
142 self
._validate
_default
_clock
_snapshot
(packet
.stream
.stream_class
, default_clock_snapshot
)
144 if default_clock_snapshot
is not None:
145 utils
._check
_uint
64(default_clock_snapshot
)
146 ptr
= native_bt
.message_event_create_with_default_clock_snapshot(
147 self
._ptr
, event_class
._ptr
, packet
._ptr
, default_clock_snapshot
)
149 ptr
= native_bt
.message_event_create(
150 self
._ptr
, event_class
._ptr
, packet
._ptr
)
153 raise bt2
.CreationError('cannot create event message object')
155 return bt2
.message
._EventMessage
(ptr
)
157 def _create_stream_beginning_message(self
, stream
):
158 utils
._check
_type
(stream
, bt2
.stream
._Stream
)
160 ptr
= native_bt
.message_stream_beginning_create(self
._ptr
, stream
._ptr
)
162 raise bt2
.CreationError('cannot create stream beginning message object')
164 return bt2
.message
._StreamBeginningMessage
(ptr
)
166 def _create_packet_beginning_message(self
, packet
, default_clock_snapshot
=None):
167 utils
._check
_type
(packet
, bt2
.packet
._Packet
)
169 if packet
.stream
.stream_class
.packets_have_default_beginning_clock_snapshot
:
170 if default_clock_snapshot
is None:
171 raise bt2
.CreationError("packet beginning messages in this stream must have a default clock snapshots")
173 utils
._check
_uint
64(default_clock_snapshot
)
174 ptr
= native_bt
.message_packet_beginning_create_with_default_clock_snapshot(
175 self
._ptr
, packet
._ptr
, default_clock_snapshot
)
177 if default_clock_snapshot
is not None:
178 raise bt2
.CreationError("packet beginning messages in this stream must not have a default clock snapshots")
180 ptr
= native_bt
.message_packet_beginning_create(self
._ptr
, packet
._ptr
)
183 raise bt2
.CreationError('cannot create packet beginning message object')
185 return bt2
.message
._PacketBeginningMessage
(ptr
)