lib: make can_seek_ns_from_origin logic use `can_seek_forward` property of iterator
[babeltrace.git] / src / bindings / python / bt2 / bt2 / message_iterator.py
CommitLineData
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
23from bt2 import native_bt, object, utils
c946c9de 24from bt2 import message as bt2_message
81447b5b 25import collections.abc
c946c9de
PP
26from bt2 import stream as bt2_stream
27from bt2 import event_class as bt2_event_class
28from bt2 import packet as bt2_packet
29from bt2 import port as bt2_port
30from bt2 import clock_class as bt2_clock_class
81447b5b
PP
31import bt2
32
33
fa4c33e3 34class _MessageIterator(collections.abc.Iterator):
81447b5b 35 def __next__(self):
f6a5e476 36 raise NotImplementedError
81447b5b
PP
37
38
a43da05a
SM
39class _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
27d97a3f 47 def __init__(self, ptr):
61d96b89
FD
48 self._current_msgs = []
49 self._at = 0
50 super().__init__(ptr)
f6a5e476
PP
51
52 def __next__(self):
27d97a3f 53 if len(self._current_msgs) == self._at:
a43da05a
SM
54 status, msgs = native_bt.bt2_self_component_port_input_get_msg_range(
55 self._ptr
56 )
61d96b89
FD
57 utils._handle_func_status(
58 status, 'unexpected error: cannot advance the message iterator'
59 )
27d97a3f
SM
60 self._current_msgs = msgs
61 self._at = 0
62
63 msg_ptr = self._current_msgs[self._at]
64 self._at += 1
65
c946c9de 66 return bt2_message._create_from_ptr(msg_ptr)
81447b5b 67
39ddfa44 68 def can_seek_beginning(self):
a43da05a
SM
69 status, res = native_bt.self_component_port_input_message_iterator_can_seek_beginning(
70 self._ptr
71 )
9e8e8b43
SM
72 utils._handle_func_status(
73 status,
74 'cannot check whether or not message iterator can seek its beginning',
75 )
39ddfa44
SM
76 return res != 0
77
78 def seek_beginning(self):
2a02ed93 79 # Forget about buffered messages, they won't be valid after seeking.
39ddfa44
SM
80 self._current_msgs.clear()
81 self._at = 0
82
a43da05a
SM
83 status = native_bt.self_component_port_input_message_iterator_seek_beginning(
84 self._ptr
85 )
61d96b89 86 utils._handle_func_status(status, 'cannot seek message iterator beginning')
39ddfa44 87
24a68958
SM
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
9415de1c
SM
113 @property
114 def can_seek_forward(self):
115 return native_bt.self_component_port_input_message_iterator_can_seek_forward(
116 self._ptr
117 )
118
119
120class _MessageIteratorConfiguration:
121 def __init__(self, ptr):
122 self._ptr = ptr
123
124 def can_seek_forward(self, value):
125 utils._check_bool(value)
126 native_bt.self_message_iterator_configuration_set_can_seek_forward(
127 self._ptr, value
128 )
129
130 can_seek_forward = property(fset=can_seek_forward)
131
f6a5e476 132
a4dcfa96
SM
133# This is extended by the user to implement component classes in Python. It
134# is created for a given output port when an input port message iterator is
135# created on the input port on the other side of the connection. It is also
136# created when an output port message iterator is created on this output port.
137#
138# Its purpose is to feed the messages that should go out through this output
139# port.
fa4c33e3 140class _UserMessageIterator(_MessageIterator):
81447b5b 141 def __new__(cls, ptr):
f6a5e476 142 # User iterator objects are always created by the native side,
81447b5b
PP
143 # that is, never instantiated directly by Python code.
144 #
f6a5e476
PP
145 # The native code calls this, then manually calls
146 # self.__init__() without the `ptr` argument. The user has
147 # access to self.component during this call, thanks to this
deec48a6 148 # self._bt_ptr argument being set.
81447b5b 149 #
deec48a6 150 # self._bt_ptr is NOT owned by this object here, so there's nothing
81447b5b
PP
151 # to do in __del__().
152 self = super().__new__(cls)
deec48a6 153 self._bt_ptr = ptr
81447b5b
PP
154 return self
155
9415de1c 156 def _bt_init_from_native(self, config_ptr, self_output_port_ptr):
c946c9de 157 self_output_port = bt2_port._create_self_from_ptr_and_get_ref(
61d96b89
FD
158 self_output_port_ptr, native_bt.PORT_TYPE_OUTPUT
159 )
9415de1c
SM
160 config = _MessageIteratorConfiguration(config_ptr)
161 self.__init__(config, self_output_port)
a4dcfa96 162
9415de1c 163 def __init__(self, config, self_output_port):
81447b5b
PP
164 pass
165
166 @property
f6a5e476 167 def _component(self):
deec48a6 168 return native_bt.bt2_get_user_component_from_user_msg_iter(self._bt_ptr)
81447b5b 169
3f9a359f
SM
170 @property
171 def _port(self):
172 port_ptr = native_bt.self_message_iterator_borrow_port(self._bt_ptr)
173 assert port_ptr is not None
174 return bt2_port._create_self_from_ptr_and_get_ref(
175 port_ptr, native_bt.PORT_TYPE_OUTPUT
176 )
177
81447b5b
PP
178 @property
179 def addr(self):
deec48a6 180 return int(self._bt_ptr)
81447b5b 181
d73bb381
PP
182 @property
183 def _is_interrupted(self):
184 return bool(native_bt.self_message_iterator_is_interrupted(self._bt_ptr))
185
819d0ae7 186 def _user_finalize(self):
81447b5b
PP
187 pass
188
f6a5e476
PP
189 def __next__(self):
190 raise bt2.Stop
191
deec48a6 192 def _bt_next_from_native(self):
f6a5e476
PP
193 # this can raise anything: it's catched by the native part
194 try:
fa4c33e3 195 msg = next(self)
f6a5e476
PP
196 except StopIteration:
197 raise bt2.Stop
5760d571 198 except Exception:
f6a5e476
PP
199 raise
200
9cbe0c59 201 utils._check_type(msg, bt2_message._MessageConst)
81447b5b 202
4e853135
SM
203 # The reference we return will be given to the message array.
204 # However, the `msg` Python object may stay alive, if the user has kept
205 # a reference to it. Acquire a new reference to account for that.
206 msg._get_ref(msg._ptr)
207 return int(msg._ptr)
27d97a3f 208
deec48a6 209 def _bt_can_seek_beginning_from_native(self):
39ddfa44
SM
210 # Here, we mimic the behavior of the C API:
211 #
5c836b95 212 # - If the iterator has a _user_can_seek_beginning method,
819d0ae7 213 # read it and use that result.
9fa5c517 214 # - Otherwise, the presence or absence of a `_user_seek_beginning`
39ddfa44 215 # method indicates whether the iterator can seek beginning.
819d0ae7 216 if hasattr(self, '_user_can_seek_beginning'):
5c836b95 217 can_seek_beginning = self._user_can_seek_beginning()
39ddfa44
SM
218 utils._check_bool(can_seek_beginning)
219 return can_seek_beginning
220 else:
819d0ae7 221 return hasattr(self, '_user_seek_beginning')
39ddfa44 222
deec48a6 223 def _bt_seek_beginning_from_native(self):
819d0ae7 224 self._user_seek_beginning()
39ddfa44 225
24a68958 226 def _bt_can_seek_ns_from_origin_from_native(self, ns_from_origin):
c49bf79b
SM
227 # Return whether the iterator can seek ns from origin using the
228 # user-implemented seek_ns_from_origin method. We mimic the behavior
229 # of the C API:
24a68958
SM
230 #
231 # - If the iterator has a _user_can_seek_ns_from_origin method,
232 # call it and use its return value.
233 # - Otherwise, if there is a `_user_seek_ns_from_origin` method,
234 # we presume it's possible.
c49bf79b 235
24a68958
SM
236 if hasattr(self, '_user_can_seek_ns_from_origin'):
237 can_seek_ns_from_origin = self._user_can_seek_ns_from_origin(ns_from_origin)
238 utils._check_bool(can_seek_ns_from_origin)
239 return can_seek_ns_from_origin
24a68958 240 else:
c49bf79b 241 return hasattr(self, '_user_seek_ns_from_origin')
24a68958
SM
242
243 def _bt_seek_ns_from_origin_from_native(self, ns_from_origin):
244 self._user_seek_ns_from_origin(ns_from_origin)
245
692f1a01 246 def _create_input_port_message_iterator(self, input_port):
c946c9de 247 utils._check_type(input_port, bt2_port._UserComponentInputPort)
692f1a01 248
ab8b2b1b 249 status, msg_iter_ptr = native_bt.bt2_self_component_port_input_message_iterator_create_from_message_iterator(
692f1a01
PP
250 self._bt_ptr, input_port._ptr
251 )
ab8b2b1b
SM
252 utils._handle_func_status(status, 'cannot create message iterator object')
253 assert msg_iter_ptr is not None
692f1a01
PP
254
255 return _UserComponentInputPortMessageIterator(msg_iter_ptr)
256
363f4bcf 257 def _create_event_message(self, event_class, parent, default_clock_snapshot=None):
c946c9de 258 utils._check_type(event_class, bt2_event_class._EventClass)
37a93d41
PP
259
260 if event_class.stream_class.supports_packets:
c946c9de 261 utils._check_type(parent, bt2_packet._Packet)
37a93d41 262 else:
c946c9de 263 utils._check_type(parent, bt2_stream._Stream)
27d97a3f
SM
264
265 if default_clock_snapshot is not None:
dcd94213 266 if event_class.stream_class.default_clock_class is None:
61d96b89
FD
267 raise ValueError(
268 'event messages in this stream must not have a default clock snapshot'
269 )
dcd94213 270
27d97a3f 271 utils._check_uint64(default_clock_snapshot)
37a93d41
PP
272
273 if event_class.stream_class.supports_packets:
274 ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot(
61d96b89
FD
275 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
276 )
37a93d41
PP
277 else:
278 ptr = native_bt.message_event_create_with_default_clock_snapshot(
61d96b89
FD
279 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
280 )
27d97a3f 281 else:
dcd94213 282 if event_class.stream_class.default_clock_class is not None:
61d96b89
FD
283 raise ValueError(
284 'event messages in this stream must have a default clock snapshot'
285 )
dcd94213 286
37a93d41
PP
287 if event_class.stream_class.supports_packets:
288 ptr = native_bt.message_event_create_with_packet(
61d96b89
FD
289 self._bt_ptr, event_class._ptr, parent._ptr
290 )
37a93d41
PP
291 else:
292 ptr = native_bt.message_event_create(
61d96b89
FD
293 self._bt_ptr, event_class._ptr, parent._ptr
294 )
27d97a3f
SM
295
296 if ptr is None:
614743a5 297 raise bt2._MemoryError('cannot create event message object')
27d97a3f 298
c946c9de 299 return bt2_message._EventMessage(ptr)
27d97a3f 300
0010c8b0 301 def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot):
c946c9de 302 utils._check_type(clock_class, bt2_clock_class._ClockClass)
0010c8b0 303 ptr = native_bt.message_message_iterator_inactivity_create(
61d96b89
FD
304 self._bt_ptr, clock_class._ptr, clock_snapshot
305 )
0010c8b0
SM
306
307 if ptr is None:
614743a5 308 raise bt2._MemoryError('cannot create inactivity message object')
0010c8b0 309
c946c9de 310 return bt2_message._MessageIteratorInactivityMessage(ptr)
0010c8b0 311
b7cbc799 312 def _create_stream_beginning_message(self, stream, default_clock_snapshot=None):
c946c9de 313 utils._check_type(stream, bt2_stream._Stream)
27d97a3f 314
deec48a6 315 ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr)
27d97a3f 316 if ptr is None:
614743a5 317 raise bt2._MemoryError('cannot create stream beginning message object')
27d97a3f 318
c946c9de 319 msg = bt2_message._StreamBeginningMessage(ptr)
0010c8b0 320
b7cbc799
SM
321 if default_clock_snapshot is not None:
322 msg._default_clock_snapshot = default_clock_snapshot
0010c8b0 323
0010c8b0
SM
324 return msg
325
b7cbc799 326 def _create_stream_end_message(self, stream, default_clock_snapshot=None):
c946c9de 327 utils._check_type(stream, bt2_stream._Stream)
871a292a 328
deec48a6 329 ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr)
871a292a 330 if ptr is None:
614743a5 331 raise bt2._MemoryError('cannot create stream end message object')
871a292a 332
c946c9de 333 msg = bt2_message._StreamEndMessage(ptr)
b7cbc799
SM
334
335 if default_clock_snapshot is not None:
336 msg._default_clock_snapshot = default_clock_snapshot
337
338 return msg
871a292a 339
27d97a3f 340 def _create_packet_beginning_message(self, packet, default_clock_snapshot=None):
c946c9de 341 utils._check_type(packet, bt2_packet._Packet)
27d97a3f 342
c88be1c8 343 if packet.stream.cls.packets_have_beginning_default_clock_snapshot:
27d97a3f 344 if default_clock_snapshot is None:
61d96b89
FD
345 raise ValueError(
346 "packet beginning messages in this stream must have a default clock snapshot"
347 )
27d97a3f
SM
348
349 utils._check_uint64(default_clock_snapshot)
350 ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot(
61d96b89
FD
351 self._bt_ptr, packet._ptr, default_clock_snapshot
352 )
27d97a3f
SM
353 else:
354 if default_clock_snapshot is not None:
61d96b89
FD
355 raise ValueError(
356 "packet beginning messages in this stream must not have a default clock snapshot"
357 )
27d97a3f 358
deec48a6 359 ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr)
27d97a3f
SM
360
361 if ptr is None:
614743a5 362 raise bt2._MemoryError('cannot create packet beginning message object')
27d97a3f 363
c946c9de 364 return bt2_message._PacketBeginningMessage(ptr)
871a292a
SM
365
366 def _create_packet_end_message(self, packet, default_clock_snapshot=None):
c946c9de 367 utils._check_type(packet, bt2_packet._Packet)
871a292a 368
c88be1c8 369 if packet.stream.cls.packets_have_end_default_clock_snapshot:
0010c8b0 370 if default_clock_snapshot is None:
61d96b89
FD
371 raise ValueError(
372 "packet end messages in this stream must have a default clock snapshot"
373 )
0010c8b0 374
871a292a
SM
375 utils._check_uint64(default_clock_snapshot)
376 ptr = native_bt.message_packet_end_create_with_default_clock_snapshot(
61d96b89
FD
377 self._bt_ptr, packet._ptr, default_clock_snapshot
378 )
871a292a 379 else:
0010c8b0 380 if default_clock_snapshot is not None:
61d96b89
FD
381 raise ValueError(
382 "packet end messages in this stream must not have a default clock snapshot"
383 )
0010c8b0 384
deec48a6 385 ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr)
871a292a
SM
386
387 if ptr is None:
614743a5 388 raise bt2._MemoryError('cannot create packet end message object')
871a292a 389
c946c9de 390 return bt2_message._PacketEndMessage(ptr)
0010c8b0 391
61d96b89
FD
392 def _create_discarded_events_message(
393 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
394 ):
c946c9de 395 utils._check_type(stream, bt2_stream._Stream)
0010c8b0 396
c88be1c8 397 if not stream.cls.supports_discarded_events:
77037b2b
PP
398 raise ValueError('stream class does not support discarded events')
399
c88be1c8 400 if stream.cls.discarded_events_have_default_clock_snapshots:
77037b2b 401 if beg_clock_snapshot is None or end_clock_snapshot is None:
61d96b89
FD
402 raise ValueError(
403 'discarded events have default clock snapshots for this stream class'
404 )
77037b2b 405
0010c8b0
SM
406 utils._check_uint64(beg_clock_snapshot)
407 utils._check_uint64(end_clock_snapshot)
408 ptr = native_bt.message_discarded_events_create_with_default_clock_snapshots(
61d96b89
FD
409 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
410 )
0010c8b0 411 else:
77037b2b 412 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
61d96b89
FD
413 raise ValueError(
414 'discarded events have no default clock snapshots for this stream class'
415 )
77037b2b 416
61d96b89 417 ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr)
0010c8b0
SM
418
419 if ptr is None:
614743a5 420 raise bt2._MemoryError('cannot discarded events message object')
0010c8b0 421
c946c9de 422 msg = bt2_message._DiscardedEventsMessage(ptr)
0010c8b0
SM
423
424 if count is not None:
425 msg._count = count
426
427 return msg
428
61d96b89
FD
429 def _create_discarded_packets_message(
430 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
431 ):
c946c9de 432 utils._check_type(stream, bt2_stream._Stream)
0010c8b0 433
c88be1c8 434 if not stream.cls.supports_discarded_packets:
77037b2b
PP
435 raise ValueError('stream class does not support discarded packets')
436
c88be1c8 437 if stream.cls.discarded_packets_have_default_clock_snapshots:
77037b2b 438 if beg_clock_snapshot is None or end_clock_snapshot is None:
61d96b89
FD
439 raise ValueError(
440 'discarded packets have default clock snapshots for this stream class'
441 )
77037b2b 442
0010c8b0
SM
443 utils._check_uint64(beg_clock_snapshot)
444 utils._check_uint64(end_clock_snapshot)
445 ptr = native_bt.message_discarded_packets_create_with_default_clock_snapshots(
61d96b89
FD
446 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
447 )
0010c8b0 448 else:
77037b2b 449 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
61d96b89
FD
450 raise ValueError(
451 'discarded packets have no default clock snapshots for this stream class'
452 )
77037b2b 453
61d96b89 454 ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr)
0010c8b0
SM
455
456 if ptr is None:
614743a5 457 raise bt2._MemoryError('cannot discarded packets message object')
0010c8b0 458
c946c9de 459 msg = bt2_message._DiscardedPacketsMessage(ptr)
0010c8b0
SM
460
461 if count is not None:
462 msg._count = count
463
464 return msg
This page took 0.070144 seconds and 4 git commands to generate.