bt2: remove _GenericMessageIterator abstraction level
[babeltrace.git] / src / bindings / python / bt2 / bt2 / message_iterator.py
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
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
31 import bt2
32
33
34 class _MessageIterator(collections.abc.Iterator):
35 def __next__(self):
36 raise NotImplementedError
37
38
39 class _UserComponentInputPortMessageIterator(object._SharedObject, _MessageIterator):
40 _get_ref = staticmethod(
41 native_bt.self_component_port_input_message_iterator_get_ref
42 )
43 _put_ref = staticmethod(
44 native_bt.self_component_port_input_message_iterator_put_ref
45 )
46
47 def __init__(self, ptr):
48 self._current_msgs = []
49 self._at = 0
50 super().__init__(ptr)
51
52 def __next__(self):
53 if len(self._current_msgs) == self._at:
54 status, msgs = native_bt.bt2_self_component_port_input_get_msg_range(
55 self._ptr
56 )
57 utils._handle_func_status(
58 status, 'unexpected error: cannot advance the message iterator'
59 )
60 self._current_msgs = msgs
61 self._at = 0
62
63 msg_ptr = self._current_msgs[self._at]
64 self._at += 1
65
66 return bt2_message._create_from_ptr(msg_ptr)
67
68 def can_seek_beginning(self):
69 status, res = native_bt.self_component_port_input_message_iterator_can_seek_beginning(
70 self._ptr
71 )
72 utils._handle_func_status(
73 status,
74 'cannot check whether or not message iterator can seek its beginning',
75 )
76 return res != 0
77
78 def seek_beginning(self):
79 # Forget about buffered messages, they won't be valid after seeking.
80 self._current_msgs.clear()
81 self._at = 0
82
83 status = native_bt.self_component_port_input_message_iterator_seek_beginning(
84 self._ptr
85 )
86 utils._handle_func_status(status, 'cannot seek message iterator beginning')
87
88
89 # This is extended by the user to implement component classes in Python. It
90 # is created for a given output port when an input port message iterator is
91 # created on the input port on the other side of the connection. It is also
92 # created when an output port message iterator is created on this output port.
93 #
94 # Its purpose is to feed the messages that should go out through this output
95 # port.
96 class _UserMessageIterator(_MessageIterator):
97 def __new__(cls, ptr):
98 # User iterator objects are always created by the native side,
99 # that is, never instantiated directly by Python code.
100 #
101 # The native code calls this, then manually calls
102 # self.__init__() without the `ptr` argument. The user has
103 # access to self.component during this call, thanks to this
104 # self._bt_ptr argument being set.
105 #
106 # self._bt_ptr is NOT owned by this object here, so there's nothing
107 # to do in __del__().
108 self = super().__new__(cls)
109 self._bt_ptr = ptr
110 return self
111
112 def _bt_init_from_native(self, self_output_port_ptr):
113 self_output_port = bt2_port._create_self_from_ptr_and_get_ref(
114 self_output_port_ptr, native_bt.PORT_TYPE_OUTPUT
115 )
116 self.__init__(self_output_port)
117
118 def __init__(self, output_port):
119 pass
120
121 @property
122 def _component(self):
123 return native_bt.bt2_get_user_component_from_user_msg_iter(self._bt_ptr)
124
125 @property
126 def _port(self):
127 port_ptr = native_bt.self_message_iterator_borrow_port(self._bt_ptr)
128 assert port_ptr is not None
129 return bt2_port._create_self_from_ptr_and_get_ref(
130 port_ptr, native_bt.PORT_TYPE_OUTPUT
131 )
132
133 @property
134 def addr(self):
135 return int(self._bt_ptr)
136
137 @property
138 def _is_interrupted(self):
139 return bool(native_bt.self_message_iterator_is_interrupted(self._bt_ptr))
140
141 def _user_finalize(self):
142 pass
143
144 def __next__(self):
145 raise bt2.Stop
146
147 def _bt_next_from_native(self):
148 # this can raise anything: it's catched by the native part
149 try:
150 msg = next(self)
151 except StopIteration:
152 raise bt2.Stop
153 except Exception:
154 raise
155
156 utils._check_type(msg, bt2_message._MessageConst)
157
158 # The reference we return will be given to the message array.
159 # However, the `msg` Python object may stay alive, if the user has kept
160 # a reference to it. Acquire a new reference to account for that.
161 msg._get_ref(msg._ptr)
162 return int(msg._ptr)
163
164 def _bt_can_seek_beginning_from_native(self):
165 # Here, we mimic the behavior of the C API:
166 #
167 # - If the iterator has a _user_can_seek_beginning method,
168 # read it and use that result.
169 # - Otherwise, the presence or absence of a `_user_seek_beginning`
170 # method indicates whether the iterator can seek beginning.
171 if hasattr(self, '_user_can_seek_beginning'):
172 can_seek_beginning = self._user_can_seek_beginning()
173 utils._check_bool(can_seek_beginning)
174 return can_seek_beginning
175 else:
176 return hasattr(self, '_user_seek_beginning')
177
178 def _bt_seek_beginning_from_native(self):
179 self._user_seek_beginning()
180
181 def _create_input_port_message_iterator(self, input_port):
182 utils._check_type(input_port, bt2_port._UserComponentInputPort)
183
184 status, msg_iter_ptr = native_bt.bt2_self_component_port_input_message_iterator_create_from_message_iterator(
185 self._bt_ptr, input_port._ptr
186 )
187 utils._handle_func_status(status, 'cannot create message iterator object')
188 assert msg_iter_ptr is not None
189
190 return _UserComponentInputPortMessageIterator(msg_iter_ptr)
191
192 def _create_event_message(self, event_class, parent, default_clock_snapshot=None):
193 utils._check_type(event_class, bt2_event_class._EventClass)
194
195 if event_class.stream_class.supports_packets:
196 utils._check_type(parent, bt2_packet._Packet)
197 else:
198 utils._check_type(parent, bt2_stream._Stream)
199
200 if default_clock_snapshot is not None:
201 if event_class.stream_class.default_clock_class is None:
202 raise ValueError(
203 'event messages in this stream must not have a default clock snapshot'
204 )
205
206 utils._check_uint64(default_clock_snapshot)
207
208 if event_class.stream_class.supports_packets:
209 ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot(
210 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
211 )
212 else:
213 ptr = native_bt.message_event_create_with_default_clock_snapshot(
214 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
215 )
216 else:
217 if event_class.stream_class.default_clock_class is not None:
218 raise ValueError(
219 'event messages in this stream must have a default clock snapshot'
220 )
221
222 if event_class.stream_class.supports_packets:
223 ptr = native_bt.message_event_create_with_packet(
224 self._bt_ptr, event_class._ptr, parent._ptr
225 )
226 else:
227 ptr = native_bt.message_event_create(
228 self._bt_ptr, event_class._ptr, parent._ptr
229 )
230
231 if ptr is None:
232 raise bt2._MemoryError('cannot create event message object')
233
234 return bt2_message._EventMessage(ptr)
235
236 def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot):
237 utils._check_type(clock_class, bt2_clock_class._ClockClass)
238 ptr = native_bt.message_message_iterator_inactivity_create(
239 self._bt_ptr, clock_class._ptr, clock_snapshot
240 )
241
242 if ptr is None:
243 raise bt2._MemoryError('cannot create inactivity message object')
244
245 return bt2_message._MessageIteratorInactivityMessage(ptr)
246
247 def _create_stream_beginning_message(self, stream, default_clock_snapshot=None):
248 utils._check_type(stream, bt2_stream._Stream)
249
250 ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr)
251 if ptr is None:
252 raise bt2._MemoryError('cannot create stream beginning message object')
253
254 msg = bt2_message._StreamBeginningMessage(ptr)
255
256 if default_clock_snapshot is not None:
257 msg._default_clock_snapshot = default_clock_snapshot
258
259 return msg
260
261 def _create_stream_end_message(self, stream, default_clock_snapshot=None):
262 utils._check_type(stream, bt2_stream._Stream)
263
264 ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr)
265 if ptr is None:
266 raise bt2._MemoryError('cannot create stream end message object')
267
268 msg = bt2_message._StreamEndMessage(ptr)
269
270 if default_clock_snapshot is not None:
271 msg._default_clock_snapshot = default_clock_snapshot
272
273 return msg
274
275 def _create_packet_beginning_message(self, packet, default_clock_snapshot=None):
276 utils._check_type(packet, bt2_packet._Packet)
277
278 if packet.stream.cls.packets_have_beginning_default_clock_snapshot:
279 if default_clock_snapshot is None:
280 raise ValueError(
281 "packet beginning messages in this stream must have a default clock snapshot"
282 )
283
284 utils._check_uint64(default_clock_snapshot)
285 ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot(
286 self._bt_ptr, packet._ptr, default_clock_snapshot
287 )
288 else:
289 if default_clock_snapshot is not None:
290 raise ValueError(
291 "packet beginning messages in this stream must not have a default clock snapshot"
292 )
293
294 ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr)
295
296 if ptr is None:
297 raise bt2._MemoryError('cannot create packet beginning message object')
298
299 return bt2_message._PacketBeginningMessage(ptr)
300
301 def _create_packet_end_message(self, packet, default_clock_snapshot=None):
302 utils._check_type(packet, bt2_packet._Packet)
303
304 if packet.stream.cls.packets_have_end_default_clock_snapshot:
305 if default_clock_snapshot is None:
306 raise ValueError(
307 "packet end messages in this stream must have a default clock snapshot"
308 )
309
310 utils._check_uint64(default_clock_snapshot)
311 ptr = native_bt.message_packet_end_create_with_default_clock_snapshot(
312 self._bt_ptr, packet._ptr, default_clock_snapshot
313 )
314 else:
315 if default_clock_snapshot is not None:
316 raise ValueError(
317 "packet end messages in this stream must not have a default clock snapshot"
318 )
319
320 ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr)
321
322 if ptr is None:
323 raise bt2._MemoryError('cannot create packet end message object')
324
325 return bt2_message._PacketEndMessage(ptr)
326
327 def _create_discarded_events_message(
328 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
329 ):
330 utils._check_type(stream, bt2_stream._Stream)
331
332 if not stream.cls.supports_discarded_events:
333 raise ValueError('stream class does not support discarded events')
334
335 if stream.cls.discarded_events_have_default_clock_snapshots:
336 if beg_clock_snapshot is None or end_clock_snapshot is None:
337 raise ValueError(
338 'discarded events have default clock snapshots for this stream class'
339 )
340
341 utils._check_uint64(beg_clock_snapshot)
342 utils._check_uint64(end_clock_snapshot)
343 ptr = native_bt.message_discarded_events_create_with_default_clock_snapshots(
344 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
345 )
346 else:
347 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
348 raise ValueError(
349 'discarded events have no default clock snapshots for this stream class'
350 )
351
352 ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr)
353
354 if ptr is None:
355 raise bt2._MemoryError('cannot discarded events message object')
356
357 msg = bt2_message._DiscardedEventsMessage(ptr)
358
359 if count is not None:
360 msg._count = count
361
362 return msg
363
364 def _create_discarded_packets_message(
365 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
366 ):
367 utils._check_type(stream, bt2_stream._Stream)
368
369 if not stream.cls.supports_discarded_packets:
370 raise ValueError('stream class does not support discarded packets')
371
372 if stream.cls.discarded_packets_have_default_clock_snapshots:
373 if beg_clock_snapshot is None or end_clock_snapshot is None:
374 raise ValueError(
375 'discarded packets have default clock snapshots for this stream class'
376 )
377
378 utils._check_uint64(beg_clock_snapshot)
379 utils._check_uint64(end_clock_snapshot)
380 ptr = native_bt.message_discarded_packets_create_with_default_clock_snapshots(
381 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
382 )
383 else:
384 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
385 raise ValueError(
386 'discarded packets have no default clock snapshots for this stream class'
387 )
388
389 ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr)
390
391 if ptr is None:
392 raise bt2._MemoryError('cannot discarded packets message object')
393
394 msg = bt2_message._DiscardedPacketsMessage(ptr)
395
396 if count is not None:
397 msg._count = count
398
399 return msg
This page took 0.052841 seconds and 4 git commands to generate.