Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
4 | # | |
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: | |
11 | # | |
12 | # The above copyright notice and this permission notice shall be included in | |
13 | # all copies or substantial portions of the Software. | |
14 | # | |
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 | |
21 | # THE SOFTWARE. | |
22 | ||
23 | from bt2 import native_bt, object, utils | |
5602ef81 | 24 | import bt2.message |
81447b5b PP |
25 | import collections.abc |
26 | import bt2.component | |
27 | import bt2 | |
28 | ||
29 | ||
5602ef81 | 30 | class _MessageIterator(collections.abc.Iterator): |
81447b5b | 31 | def _handle_status(self, status, gen_error_msg): |
2ae9f48c | 32 | if status == native_bt.MESSAGE_ITERATOR_STATUS_AGAIN: |
811644b8 | 33 | raise bt2.TryAgain |
5602ef81 | 34 | elif status == native_bt.MESSAGE_ITERATOR_STATUS_END: |
81447b5b | 35 | raise bt2.Stop |
81447b5b PP |
36 | elif status < 0: |
37 | raise bt2.Error(gen_error_msg) | |
38 | ||
81447b5b | 39 | def __next__(self): |
811644b8 | 40 | raise NotImplementedError |
81447b5b PP |
41 | |
42 | ||
78288f58 | 43 | class _GenericMessageIterator(object._SharedObject, _MessageIterator): |
2ae9f48c SM |
44 | def __init__(self, ptr): |
45 | self._current_msgs = [] | |
46 | self._at = 0 | |
47 | super().__init__(ptr) | |
811644b8 PP |
48 | |
49 | def __next__(self): | |
2ae9f48c SM |
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 | |
55 | self._at = 0 | |
56 | ||
57 | msg_ptr = self._current_msgs[self._at] | |
58 | self._at += 1 | |
59 | ||
60 | return bt2.message._create_from_ptr(msg_ptr) | |
81447b5b | 61 | |
811644b8 | 62 | |
5f25509b SM |
63 | class _UserComponentInputPortMessageIterator(_GenericMessageIterator): |
64 | _get_msg_range = staticmethod(native_bt.py3_self_component_port_input_get_msg_range) | |
65 | ||
90157d89 PP |
66 | @property |
67 | def component(self): | |
5602ef81 | 68 | comp_ptr = native_bt.private_connection_message_iterator_get_component(self._ptr) |
90157d89 PP |
69 | assert(comp_ptr) |
70 | return bt2.component._create_generic_component_from_ptr(comp_ptr) | |
71 | ||
72 | ||
5602ef81 | 73 | class _OutputPortMessageIterator(_GenericMessageIterator): |
2ae9f48c SM |
74 | _get_msg_range = staticmethod(native_bt.py3_port_output_get_msg_range) |
75 | _get_ref = staticmethod(native_bt.port_output_message_iterator_get_ref) | |
76 | _put_ref = staticmethod(native_bt.port_output_message_iterator_put_ref) | |
dc43190b PP |
77 | |
78 | ||
5602ef81 | 79 | class _UserMessageIterator(_MessageIterator): |
81447b5b | 80 | def __new__(cls, ptr): |
811644b8 | 81 | # User iterator objects are always created by the native side, |
81447b5b PP |
82 | # that is, never instantiated directly by Python code. |
83 | # | |
811644b8 PP |
84 | # The native code calls this, then manually calls |
85 | # self.__init__() without the `ptr` argument. The user has | |
86 | # access to self.component during this call, thanks to this | |
87 | # self._ptr argument being set. | |
81447b5b PP |
88 | # |
89 | # self._ptr is NOT owned by this object here, so there's nothing | |
90 | # to do in __del__(). | |
91 | self = super().__new__(cls) | |
92 | self._ptr = ptr | |
93 | return self | |
94 | ||
95 | def __init__(self): | |
96 | pass | |
97 | ||
98 | @property | |
811644b8 | 99 | def _component(self): |
5602ef81 | 100 | return native_bt.py3_get_user_component_from_user_msg_iter(self._ptr) |
81447b5b PP |
101 | |
102 | @property | |
103 | def addr(self): | |
104 | return int(self._ptr) | |
105 | ||
811644b8 | 106 | def _finalize(self): |
81447b5b PP |
107 | pass |
108 | ||
811644b8 PP |
109 | def __next__(self): |
110 | raise bt2.Stop | |
111 | ||
112 | def _next_from_native(self): | |
113 | # this can raise anything: it's catched by the native part | |
114 | try: | |
5602ef81 | 115 | msg = next(self) |
811644b8 PP |
116 | except StopIteration: |
117 | raise bt2.Stop | |
118 | except: | |
119 | raise | |
120 | ||
5602ef81 | 121 | utils._check_type(msg, bt2.message._Message) |
81447b5b | 122 | |
2ae9f48c SM |
123 | # Release the reference to the native part. |
124 | ptr = msg._release() | |
125 | return int(ptr) | |
126 | ||
127 | # Validate that the presence or lack of presence of a | |
128 | # `default_clock_snapshot` value is valid in the context of `stream_class`. | |
129 | @staticmethod | |
130 | def _validate_default_clock_snapshot(stream_class, default_clock_snapshot): | |
131 | stream_class_has_default_clock_class = stream_class.default_clock_class is not None | |
132 | ||
133 | if stream_class_has_default_clock_class and default_clock_snapshot is None: | |
134 | raise bt2.Error( | |
135 | 'stream class has a default clock class, default_clock_snapshot should not be None') | |
136 | ||
137 | if not stream_class_has_default_clock_class and default_clock_snapshot is not None: | |
138 | raise bt2.Error( | |
139 | 'stream class has no default clock class, default_clock_snapshot should be None') | |
140 | ||
141 | def _create_event_message(self, event_class, packet, default_clock_snapshot=None): | |
142 | utils._check_type(event_class, bt2.event_class.EventClass) | |
143 | utils._check_type(packet, bt2.packet._Packet) | |
144 | self._validate_default_clock_snapshot(packet.stream.stream_class, default_clock_snapshot) | |
145 | ||
146 | if default_clock_snapshot is not None: | |
147 | utils._check_uint64(default_clock_snapshot) | |
148 | ptr = native_bt.message_event_create_with_default_clock_snapshot( | |
149 | self._ptr, event_class._ptr, packet._ptr, default_clock_snapshot) | |
150 | else: | |
151 | ptr = native_bt.message_event_create( | |
152 | self._ptr, event_class._ptr, packet._ptr) | |
153 | ||
154 | if ptr is None: | |
155 | raise bt2.CreationError('cannot create event message object') | |
156 | ||
157 | return bt2.message._EventMessage(ptr) | |
158 | ||
9ec609ec SM |
159 | def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot): |
160 | utils._check_type(clock_class, bt2.clock_class._ClockClass) | |
161 | ptr = native_bt.message_message_iterator_inactivity_create( | |
162 | self._ptr, clock_class._ptr, clock_snapshot) | |
163 | ||
164 | if ptr is None: | |
165 | raise bt2.CreationError('cannot create inactivity message object') | |
166 | ||
167 | return bt2.message._MessageIteratorInactivityMessage(ptr) | |
168 | ||
2ae9f48c SM |
169 | def _create_stream_beginning_message(self, stream): |
170 | utils._check_type(stream, bt2.stream._Stream) | |
171 | ||
172 | ptr = native_bt.message_stream_beginning_create(self._ptr, stream._ptr) | |
173 | if ptr is None: | |
174 | raise bt2.CreationError('cannot create stream beginning message object') | |
175 | ||
176 | return bt2.message._StreamBeginningMessage(ptr) | |
177 | ||
9ec609ec SM |
178 | def _create_stream_activity_beginning_message(self, stream, default_clock_snapshot=None): |
179 | utils._check_type(stream, bt2.stream._Stream) | |
180 | self._validate_default_clock_snapshot(stream.stream_class, default_clock_snapshot) | |
181 | ||
182 | ptr = native_bt.message_stream_activity_beginning_create(self._ptr, stream._ptr) | |
183 | ||
184 | if ptr is None: | |
185 | raise bt2.CreationError( | |
186 | 'cannot create stream activity beginning message object') | |
187 | ||
188 | msg = bt2.message._StreamActivityBeginningMessage(ptr) | |
189 | ||
190 | if default_clock_snapshot is not None: | |
191 | msg._default_clock_snapshot = default_clock_snapshot | |
192 | ||
193 | return msg | |
194 | ||
195 | def _create_stream_activity_end_message(self, stream, default_clock_snapshot=None): | |
196 | utils._check_type(stream, bt2.stream._Stream) | |
197 | self._validate_default_clock_snapshot(stream.stream_class, default_clock_snapshot) | |
198 | ||
199 | ptr = native_bt.message_stream_activity_end_create(self._ptr, stream._ptr) | |
200 | ||
201 | if ptr is None: | |
202 | raise bt2.CreationError( | |
203 | 'cannot create stream activity end message object') | |
204 | ||
205 | msg = bt2.message._StreamActivityEndMessage(ptr) | |
206 | ||
207 | if default_clock_snapshot is not None: | |
208 | msg._default_clock_snapshot = default_clock_snapshot | |
209 | ||
210 | return msg | |
211 | ||
5f25509b SM |
212 | def _create_stream_end_message(self, stream): |
213 | utils._check_type(stream, bt2.stream._Stream) | |
214 | ||
215 | ptr = native_bt.message_stream_end_create(self._ptr, stream._ptr) | |
216 | if ptr is None: | |
217 | raise bt2.CreationError('cannot create stream end message object') | |
218 | ||
219 | return bt2.message._StreamEndMessage(ptr) | |
220 | ||
2ae9f48c SM |
221 | def _create_packet_beginning_message(self, packet, default_clock_snapshot=None): |
222 | utils._check_type(packet, bt2.packet._Packet) | |
223 | ||
224 | if packet.stream.stream_class.packets_have_default_beginning_clock_snapshot: | |
225 | if default_clock_snapshot is None: | |
9ec609ec | 226 | raise ValueError("packet beginning messages in this stream must have a default clock snapshots") |
2ae9f48c SM |
227 | |
228 | utils._check_uint64(default_clock_snapshot) | |
229 | ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot( | |
230 | self._ptr, packet._ptr, default_clock_snapshot) | |
231 | else: | |
232 | if default_clock_snapshot is not None: | |
9ec609ec | 233 | raise ValueError("packet beginning messages in this stream must not have a default clock snapshots") |
2ae9f48c SM |
234 | |
235 | ptr = native_bt.message_packet_beginning_create(self._ptr, packet._ptr) | |
236 | ||
237 | if ptr is None: | |
238 | raise bt2.CreationError('cannot create packet beginning message object') | |
239 | ||
240 | return bt2.message._PacketBeginningMessage(ptr) | |
5f25509b SM |
241 | |
242 | def _create_packet_end_message(self, packet, default_clock_snapshot=None): | |
243 | utils._check_type(packet, bt2.packet._Packet) | |
5f25509b | 244 | |
9ec609ec SM |
245 | if packet.stream.stream_class.packets_have_default_end_clock_snapshot: |
246 | if default_clock_snapshot is None: | |
247 | raise ValueError("packet end messages in this stream must have a default clock snapshots") | |
248 | ||
5f25509b SM |
249 | utils._check_uint64(default_clock_snapshot) |
250 | ptr = native_bt.message_packet_end_create_with_default_clock_snapshot( | |
251 | self._ptr, packet._ptr, default_clock_snapshot) | |
252 | else: | |
9ec609ec SM |
253 | if default_clock_snapshot is not None: |
254 | raise ValueError("packet end messages in this stream must not have a default clock snapshots") | |
255 | ||
5f25509b SM |
256 | ptr = native_bt.message_packet_end_create(self._ptr, packet._ptr) |
257 | ||
258 | if ptr is None: | |
259 | raise bt2.CreationError('cannot create packet end message object') | |
260 | ||
261 | return bt2.message._PacketEndMessage(ptr) | |
9ec609ec SM |
262 | |
263 | def _create_discarded_events_message(self, stream, count=None, | |
264 | beg_clock_snapshot=None, | |
265 | end_clock_snapshot=None): | |
266 | utils._check_type(stream, bt2.stream._Stream) | |
267 | ||
268 | if beg_clock_snapshot is None and end_clock_snapshot is None: | |
269 | ptr = native_bt.message_discarded_events_create(self._ptr, stream._ptr) | |
270 | elif beg_clock_snapshot is not None and end_clock_snapshot is not None: | |
271 | utils._check_uint64(beg_clock_snapshot) | |
272 | utils._check_uint64(end_clock_snapshot) | |
273 | ptr = native_bt.message_discarded_events_create_with_default_clock_snapshots( | |
274 | self._ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot) | |
275 | else: | |
276 | raise ValueError('begin and end clock snapshots must be both provided or both omitted') | |
277 | ||
278 | if ptr is None: | |
279 | raise bt2.CreationError('cannot discarded events message object') | |
280 | ||
281 | msg = bt2.message._DiscardedEventsMessage(ptr) | |
282 | ||
283 | if count is not None: | |
284 | msg._count = count | |
285 | ||
286 | return msg | |
287 | ||
288 | def _create_discarded_packets_message(self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None): | |
289 | utils._check_type(stream, bt2.stream._Stream) | |
290 | ||
291 | if beg_clock_snapshot is None and end_clock_snapshot is None: | |
292 | ptr = native_bt.message_discarded_packets_create(self._ptr, stream._ptr) | |
293 | elif beg_clock_snapshot is not None and end_clock_snapshot is not None: | |
294 | utils._check_uint64(beg_clock_snapshot) | |
295 | utils._check_uint64(end_clock_snapshot) | |
296 | ptr = native_bt.message_discarded_packets_create_with_default_clock_snapshots( | |
297 | self._ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot) | |
298 | else: | |
299 | raise ValueError('begin and end clock snapshots must be both provided or both omitted') | |
300 | ||
301 | if ptr is None: | |
302 | raise bt2.CreationError('cannot discarded packets message object') | |
303 | ||
304 | msg = bt2.message._DiscardedPacketsMessage(ptr) | |
305 | ||
306 | if count is not None: | |
307 | msg._count = count | |
308 | ||
309 | return msg | |
310 |