bt2: expose seek_ns_from_origin and can_seek_ns_from_origin
[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 def can_seek_ns_from_origin(self, ns_from_origin):
89 utils._check_int64(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
92 )
93 utils._handle_func_status(
94 status,
95 'cannot check whether or not message iterator can seek given ns from origin',
96 )
97 return res != 0
98
99 def seek_ns_from_origin(self, ns_from_origin):
100 utils._check_int64(ns_from_origin)
101
102 # Forget about buffered messages, they won't be valid after seeking.
103 self._current_msgs.clear()
104 self._at = 0
105
106 status = native_bt.self_component_port_input_message_iterator_seek_ns_from_origin(
107 self._ptr, ns_from_origin
108 )
109 utils._handle_func_status(
110 status, 'message iterator cannot seek given ns from origin'
111 )
112
113
114 # This is extended by the user to implement component classes in Python. It
115 # is created for a given output port when an input port message iterator is
116 # created on the input port on the other side of the connection. It is also
117 # created when an output port message iterator is created on this output port.
118 #
119 # Its purpose is to feed the messages that should go out through this output
120 # port.
121 class _UserMessageIterator(_MessageIterator):
122 def __new__(cls, ptr):
123 # User iterator objects are always created by the native side,
124 # that is, never instantiated directly by Python code.
125 #
126 # The native code calls this, then manually calls
127 # self.__init__() without the `ptr` argument. The user has
128 # access to self.component during this call, thanks to this
129 # self._bt_ptr argument being set.
130 #
131 # self._bt_ptr is NOT owned by this object here, so there's nothing
132 # to do in __del__().
133 self = super().__new__(cls)
134 self._bt_ptr = ptr
135 return self
136
137 def _bt_init_from_native(self, self_output_port_ptr):
138 self_output_port = bt2_port._create_self_from_ptr_and_get_ref(
139 self_output_port_ptr, native_bt.PORT_TYPE_OUTPUT
140 )
141 self.__init__(self_output_port)
142
143 def __init__(self, output_port):
144 pass
145
146 @property
147 def _component(self):
148 return native_bt.bt2_get_user_component_from_user_msg_iter(self._bt_ptr)
149
150 @property
151 def _port(self):
152 port_ptr = native_bt.self_message_iterator_borrow_port(self._bt_ptr)
153 assert port_ptr is not None
154 return bt2_port._create_self_from_ptr_and_get_ref(
155 port_ptr, native_bt.PORT_TYPE_OUTPUT
156 )
157
158 @property
159 def addr(self):
160 return int(self._bt_ptr)
161
162 @property
163 def _is_interrupted(self):
164 return bool(native_bt.self_message_iterator_is_interrupted(self._bt_ptr))
165
166 def _user_finalize(self):
167 pass
168
169 def __next__(self):
170 raise bt2.Stop
171
172 def _bt_next_from_native(self):
173 # this can raise anything: it's catched by the native part
174 try:
175 msg = next(self)
176 except StopIteration:
177 raise bt2.Stop
178 except Exception:
179 raise
180
181 utils._check_type(msg, bt2_message._MessageConst)
182
183 # The reference we return will be given to the message array.
184 # However, the `msg` Python object may stay alive, if the user has kept
185 # a reference to it. Acquire a new reference to account for that.
186 msg._get_ref(msg._ptr)
187 return int(msg._ptr)
188
189 def _bt_can_seek_beginning_from_native(self):
190 # Here, we mimic the behavior of the C API:
191 #
192 # - If the iterator has a _user_can_seek_beginning method,
193 # read it and use that result.
194 # - Otherwise, the presence or absence of a `_user_seek_beginning`
195 # method indicates whether the iterator can seek beginning.
196 if hasattr(self, '_user_can_seek_beginning'):
197 can_seek_beginning = self._user_can_seek_beginning()
198 utils._check_bool(can_seek_beginning)
199 return can_seek_beginning
200 else:
201 return hasattr(self, '_user_seek_beginning')
202
203 def _bt_seek_beginning_from_native(self):
204 self._user_seek_beginning()
205
206 def _bt_can_seek_ns_from_origin_from_native(self, ns_from_origin):
207 # Here, we mimic the behavior of the C API:
208 #
209 # - If the iterator has a _user_can_seek_ns_from_origin method,
210 # call it and use its return value.
211 # - Otherwise, if there is a `_user_seek_ns_from_origin` method,
212 # we presume it's possible.
213 # - Otherwise, check if we can seek to beginning (which allows us to
214 # seek to beginning and then fast forward - aka auto-seek).
215 if hasattr(self, '_user_can_seek_ns_from_origin'):
216 can_seek_ns_from_origin = self._user_can_seek_ns_from_origin(ns_from_origin)
217 utils._check_bool(can_seek_ns_from_origin)
218 return can_seek_ns_from_origin
219 elif hasattr(self, '_user_seek_ns_from_origin'):
220 return True
221 else:
222 return self._bt_can_seek_beginning_from_native()
223
224 def _bt_seek_ns_from_origin_from_native(self, ns_from_origin):
225 self._user_seek_ns_from_origin(ns_from_origin)
226
227 def _create_input_port_message_iterator(self, input_port):
228 utils._check_type(input_port, bt2_port._UserComponentInputPort)
229
230 status, msg_iter_ptr = native_bt.bt2_self_component_port_input_message_iterator_create_from_message_iterator(
231 self._bt_ptr, input_port._ptr
232 )
233 utils._handle_func_status(status, 'cannot create message iterator object')
234 assert msg_iter_ptr is not None
235
236 return _UserComponentInputPortMessageIterator(msg_iter_ptr)
237
238 def _create_event_message(self, event_class, parent, default_clock_snapshot=None):
239 utils._check_type(event_class, bt2_event_class._EventClass)
240
241 if event_class.stream_class.supports_packets:
242 utils._check_type(parent, bt2_packet._Packet)
243 else:
244 utils._check_type(parent, bt2_stream._Stream)
245
246 if default_clock_snapshot is not None:
247 if event_class.stream_class.default_clock_class is None:
248 raise ValueError(
249 'event messages in this stream must not have a default clock snapshot'
250 )
251
252 utils._check_uint64(default_clock_snapshot)
253
254 if event_class.stream_class.supports_packets:
255 ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot(
256 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
257 )
258 else:
259 ptr = native_bt.message_event_create_with_default_clock_snapshot(
260 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
261 )
262 else:
263 if event_class.stream_class.default_clock_class is not None:
264 raise ValueError(
265 'event messages in this stream must have a default clock snapshot'
266 )
267
268 if event_class.stream_class.supports_packets:
269 ptr = native_bt.message_event_create_with_packet(
270 self._bt_ptr, event_class._ptr, parent._ptr
271 )
272 else:
273 ptr = native_bt.message_event_create(
274 self._bt_ptr, event_class._ptr, parent._ptr
275 )
276
277 if ptr is None:
278 raise bt2._MemoryError('cannot create event message object')
279
280 return bt2_message._EventMessage(ptr)
281
282 def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot):
283 utils._check_type(clock_class, bt2_clock_class._ClockClass)
284 ptr = native_bt.message_message_iterator_inactivity_create(
285 self._bt_ptr, clock_class._ptr, clock_snapshot
286 )
287
288 if ptr is None:
289 raise bt2._MemoryError('cannot create inactivity message object')
290
291 return bt2_message._MessageIteratorInactivityMessage(ptr)
292
293 def _create_stream_beginning_message(self, stream, default_clock_snapshot=None):
294 utils._check_type(stream, bt2_stream._Stream)
295
296 ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr)
297 if ptr is None:
298 raise bt2._MemoryError('cannot create stream beginning message object')
299
300 msg = bt2_message._StreamBeginningMessage(ptr)
301
302 if default_clock_snapshot is not None:
303 msg._default_clock_snapshot = default_clock_snapshot
304
305 return msg
306
307 def _create_stream_end_message(self, stream, default_clock_snapshot=None):
308 utils._check_type(stream, bt2_stream._Stream)
309
310 ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr)
311 if ptr is None:
312 raise bt2._MemoryError('cannot create stream end message object')
313
314 msg = bt2_message._StreamEndMessage(ptr)
315
316 if default_clock_snapshot is not None:
317 msg._default_clock_snapshot = default_clock_snapshot
318
319 return msg
320
321 def _create_packet_beginning_message(self, packet, default_clock_snapshot=None):
322 utils._check_type(packet, bt2_packet._Packet)
323
324 if packet.stream.cls.packets_have_beginning_default_clock_snapshot:
325 if default_clock_snapshot is None:
326 raise ValueError(
327 "packet beginning messages in this stream must have a default clock snapshot"
328 )
329
330 utils._check_uint64(default_clock_snapshot)
331 ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot(
332 self._bt_ptr, packet._ptr, default_clock_snapshot
333 )
334 else:
335 if default_clock_snapshot is not None:
336 raise ValueError(
337 "packet beginning messages in this stream must not have a default clock snapshot"
338 )
339
340 ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr)
341
342 if ptr is None:
343 raise bt2._MemoryError('cannot create packet beginning message object')
344
345 return bt2_message._PacketBeginningMessage(ptr)
346
347 def _create_packet_end_message(self, packet, default_clock_snapshot=None):
348 utils._check_type(packet, bt2_packet._Packet)
349
350 if packet.stream.cls.packets_have_end_default_clock_snapshot:
351 if default_clock_snapshot is None:
352 raise ValueError(
353 "packet end messages in this stream must have a default clock snapshot"
354 )
355
356 utils._check_uint64(default_clock_snapshot)
357 ptr = native_bt.message_packet_end_create_with_default_clock_snapshot(
358 self._bt_ptr, packet._ptr, default_clock_snapshot
359 )
360 else:
361 if default_clock_snapshot is not None:
362 raise ValueError(
363 "packet end messages in this stream must not have a default clock snapshot"
364 )
365
366 ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr)
367
368 if ptr is None:
369 raise bt2._MemoryError('cannot create packet end message object')
370
371 return bt2_message._PacketEndMessage(ptr)
372
373 def _create_discarded_events_message(
374 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
375 ):
376 utils._check_type(stream, bt2_stream._Stream)
377
378 if not stream.cls.supports_discarded_events:
379 raise ValueError('stream class does not support discarded events')
380
381 if stream.cls.discarded_events_have_default_clock_snapshots:
382 if beg_clock_snapshot is None or end_clock_snapshot is None:
383 raise ValueError(
384 'discarded events have default clock snapshots for this stream class'
385 )
386
387 utils._check_uint64(beg_clock_snapshot)
388 utils._check_uint64(end_clock_snapshot)
389 ptr = native_bt.message_discarded_events_create_with_default_clock_snapshots(
390 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
391 )
392 else:
393 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
394 raise ValueError(
395 'discarded events have no default clock snapshots for this stream class'
396 )
397
398 ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr)
399
400 if ptr is None:
401 raise bt2._MemoryError('cannot discarded events message object')
402
403 msg = bt2_message._DiscardedEventsMessage(ptr)
404
405 if count is not None:
406 msg._count = count
407
408 return msg
409
410 def _create_discarded_packets_message(
411 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
412 ):
413 utils._check_type(stream, bt2_stream._Stream)
414
415 if not stream.cls.supports_discarded_packets:
416 raise ValueError('stream class does not support discarded packets')
417
418 if stream.cls.discarded_packets_have_default_clock_snapshots:
419 if beg_clock_snapshot is None or end_clock_snapshot is None:
420 raise ValueError(
421 'discarded packets have default clock snapshots for this stream class'
422 )
423
424 utils._check_uint64(beg_clock_snapshot)
425 utils._check_uint64(end_clock_snapshot)
426 ptr = native_bt.message_discarded_packets_create_with_default_clock_snapshots(
427 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
428 )
429 else:
430 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
431 raise ValueError(
432 'discarded packets have no default clock snapshots for this stream class'
433 )
434
435 ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr)
436
437 if ptr is None:
438 raise bt2._MemoryError('cannot discarded packets message object')
439
440 msg = bt2_message._DiscardedPacketsMessage(ptr)
441
442 if count is not None:
443 msg._count = count
444
445 return msg
This page took 0.038758 seconds and 4 git commands to generate.