Move to kernel style SPDX license identifiers
[babeltrace.git] / src / bindings / python / bt2 / bt2 / message_iterator.py
CommitLineData
0235b0db 1# SPDX-License-Identifier: MIT
81447b5b
PP
2#
3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
81447b5b
PP
4
5from bt2 import native_bt, object, utils
3fb99a22 6from bt2 import message as bt2_message
81447b5b 7import collections.abc
3fb99a22
PP
8from bt2 import stream as bt2_stream
9from bt2 import event_class as bt2_event_class
10from bt2 import packet as bt2_packet
11from bt2 import port as bt2_port
12from bt2 import clock_class as bt2_clock_class
81447b5b
PP
13import bt2
14
15
5602ef81 16class _MessageIterator(collections.abc.Iterator):
81447b5b 17 def __next__(self):
811644b8 18 raise NotImplementedError
81447b5b
PP
19
20
1975af3d 21class _UserComponentInputPortMessageIterator(object._SharedObject, _MessageIterator):
9a2c8b8e
PP
22 _get_ref = staticmethod(native_bt.message_iterator_get_ref)
23 _put_ref = staticmethod(native_bt.message_iterator_put_ref)
1975af3d 24
2ae9f48c 25 def __init__(self, ptr):
cfbd7cf3
FD
26 self._current_msgs = []
27 self._at = 0
28 super().__init__(ptr)
811644b8
PP
29
30 def __next__(self):
2ae9f48c 31 if len(self._current_msgs) == self._at:
1975af3d
SM
32 status, msgs = native_bt.bt2_self_component_port_input_get_msg_range(
33 self._ptr
34 )
cfbd7cf3
FD
35 utils._handle_func_status(
36 status, 'unexpected error: cannot advance the message iterator'
37 )
2ae9f48c
SM
38 self._current_msgs = msgs
39 self._at = 0
40
41 msg_ptr = self._current_msgs[self._at]
42 self._at += 1
43
3fb99a22 44 return bt2_message._create_from_ptr(msg_ptr)
81447b5b 45
f00b8d40 46 def can_seek_beginning(self):
9a2c8b8e 47 (status, res) = native_bt.message_iterator_can_seek_beginning(self._ptr)
f2fb1b32
SM
48 utils._handle_func_status(
49 status,
50 'cannot check whether or not message iterator can seek its beginning',
51 )
f00b8d40
SM
52 return res != 0
53
54 def seek_beginning(self):
b6909b73 55 # Forget about buffered messages, they won't be valid after seeking.
f00b8d40
SM
56 self._current_msgs.clear()
57 self._at = 0
58
9a2c8b8e 59 status = native_bt.message_iterator_seek_beginning(self._ptr)
cfbd7cf3 60 utils._handle_func_status(status, 'cannot seek message iterator beginning')
f00b8d40 61
c182d7dd
SM
62 def can_seek_ns_from_origin(self, ns_from_origin):
63 utils._check_int64(ns_from_origin)
9a2c8b8e 64 (status, res) = native_bt.message_iterator_can_seek_ns_from_origin(
c182d7dd
SM
65 self._ptr, ns_from_origin
66 )
67 utils._handle_func_status(
68 status,
69 'cannot check whether or not message iterator can seek given ns from origin',
70 )
71 return res != 0
72
73 def seek_ns_from_origin(self, ns_from_origin):
74 utils._check_int64(ns_from_origin)
75
76 # Forget about buffered messages, they won't be valid after seeking.
77 self._current_msgs.clear()
78 self._at = 0
79
9a2c8b8e 80 status = native_bt.message_iterator_seek_ns_from_origin(
c182d7dd
SM
81 self._ptr, ns_from_origin
82 )
83 utils._handle_func_status(
84 status, 'message iterator cannot seek given ns from origin'
85 )
86
8d8b141d
SM
87 @property
88 def can_seek_forward(self):
9a2c8b8e 89 return native_bt.message_iterator_can_seek_forward(self._ptr)
8d8b141d
SM
90
91
92class _MessageIteratorConfiguration:
93 def __init__(self, ptr):
94 self._ptr = ptr
95
96 def can_seek_forward(self, value):
97 utils._check_bool(value)
98 native_bt.self_message_iterator_configuration_set_can_seek_forward(
99 self._ptr, value
100 )
101
102 can_seek_forward = property(fset=can_seek_forward)
103
811644b8 104
c5f330cd
SM
105# This is extended by the user to implement component classes in Python. It
106# is created for a given output port when an input port message iterator is
107# created on the input port on the other side of the connection. It is also
108# created when an output port message iterator is created on this output port.
109#
110# Its purpose is to feed the messages that should go out through this output
111# port.
5602ef81 112class _UserMessageIterator(_MessageIterator):
81447b5b 113 def __new__(cls, ptr):
811644b8 114 # User iterator objects are always created by the native side,
81447b5b
PP
115 # that is, never instantiated directly by Python code.
116 #
811644b8
PP
117 # The native code calls this, then manually calls
118 # self.__init__() without the `ptr` argument. The user has
119 # access to self.component during this call, thanks to this
85906b6b 120 # self._bt_ptr argument being set.
81447b5b 121 #
85906b6b 122 # self._bt_ptr is NOT owned by this object here, so there's nothing
81447b5b
PP
123 # to do in __del__().
124 self = super().__new__(cls)
85906b6b 125 self._bt_ptr = ptr
81447b5b
PP
126 return self
127
8d8b141d 128 def _bt_init_from_native(self, config_ptr, self_output_port_ptr):
3fb99a22 129 self_output_port = bt2_port._create_self_from_ptr_and_get_ref(
cfbd7cf3
FD
130 self_output_port_ptr, native_bt.PORT_TYPE_OUTPUT
131 )
8d8b141d
SM
132 config = _MessageIteratorConfiguration(config_ptr)
133 self.__init__(config, self_output_port)
c5f330cd 134
8d8b141d 135 def __init__(self, config, self_output_port):
81447b5b
PP
136 pass
137
138 @property
811644b8 139 def _component(self):
85906b6b 140 return native_bt.bt2_get_user_component_from_user_msg_iter(self._bt_ptr)
81447b5b 141
14503fb1
SM
142 @property
143 def _port(self):
144 port_ptr = native_bt.self_message_iterator_borrow_port(self._bt_ptr)
145 assert port_ptr is not None
146 return bt2_port._create_self_from_ptr_and_get_ref(
147 port_ptr, native_bt.PORT_TYPE_OUTPUT
148 )
149
81447b5b
PP
150 @property
151 def addr(self):
85906b6b 152 return int(self._bt_ptr)
81447b5b 153
9b4f9b42
PP
154 @property
155 def _is_interrupted(self):
156 return bool(native_bt.self_message_iterator_is_interrupted(self._bt_ptr))
157
6a91742b 158 def _user_finalize(self):
81447b5b
PP
159 pass
160
811644b8
PP
161 def __next__(self):
162 raise bt2.Stop
163
85906b6b 164 def _bt_next_from_native(self):
811644b8
PP
165 # this can raise anything: it's catched by the native part
166 try:
5602ef81 167 msg = next(self)
811644b8
PP
168 except StopIteration:
169 raise bt2.Stop
4c4935bf 170 except Exception:
811644b8
PP
171 raise
172
f0a42b33 173 utils._check_type(msg, bt2_message._MessageConst)
81447b5b 174
d79a8353
SM
175 # The reference we return will be given to the message array.
176 # However, the `msg` Python object may stay alive, if the user has kept
177 # a reference to it. Acquire a new reference to account for that.
178 msg._get_ref(msg._ptr)
179 return int(msg._ptr)
2ae9f48c 180
85906b6b 181 def _bt_can_seek_beginning_from_native(self):
f00b8d40
SM
182 # Here, we mimic the behavior of the C API:
183 #
14cfc8ce 184 # - If the iterator has a _user_can_seek_beginning method,
6a91742b 185 # read it and use that result.
5a096c63 186 # - Otherwise, the presence or absence of a `_user_seek_beginning`
f00b8d40 187 # method indicates whether the iterator can seek beginning.
6a91742b 188 if hasattr(self, '_user_can_seek_beginning'):
14cfc8ce 189 can_seek_beginning = self._user_can_seek_beginning()
f00b8d40
SM
190 utils._check_bool(can_seek_beginning)
191 return can_seek_beginning
192 else:
6a91742b 193 return hasattr(self, '_user_seek_beginning')
f00b8d40 194
85906b6b 195 def _bt_seek_beginning_from_native(self):
6a91742b 196 self._user_seek_beginning()
f00b8d40 197
c182d7dd 198 def _bt_can_seek_ns_from_origin_from_native(self, ns_from_origin):
c0e46a7c
SM
199 # Return whether the iterator can seek ns from origin using the
200 # user-implemented seek_ns_from_origin method. We mimic the behavior
201 # of the C API:
c182d7dd
SM
202 #
203 # - If the iterator has a _user_can_seek_ns_from_origin method,
204 # call it and use its return value.
205 # - Otherwise, if there is a `_user_seek_ns_from_origin` method,
206 # we presume it's possible.
c0e46a7c 207
c182d7dd
SM
208 if hasattr(self, '_user_can_seek_ns_from_origin'):
209 can_seek_ns_from_origin = self._user_can_seek_ns_from_origin(ns_from_origin)
210 utils._check_bool(can_seek_ns_from_origin)
211 return can_seek_ns_from_origin
c182d7dd 212 else:
c0e46a7c 213 return hasattr(self, '_user_seek_ns_from_origin')
c182d7dd
SM
214
215 def _bt_seek_ns_from_origin_from_native(self, ns_from_origin):
216 self._user_seek_ns_from_origin(ns_from_origin)
217
9a2c8b8e 218 def _create_message_iterator(self, input_port):
3fb99a22 219 utils._check_type(input_port, bt2_port._UserComponentInputPort)
ca02df0a 220
75882e97
FD
221 (
222 status,
223 msg_iter_ptr,
9a2c8b8e 224 ) = native_bt.bt2_message_iterator_create_from_message_iterator(
ca02df0a
PP
225 self._bt_ptr, input_port._ptr
226 )
e803df70
SM
227 utils._handle_func_status(status, 'cannot create message iterator object')
228 assert msg_iter_ptr is not None
ca02df0a
PP
229
230 return _UserComponentInputPortMessageIterator(msg_iter_ptr)
231
36d9460d 232 def _create_event_message(self, event_class, parent, default_clock_snapshot=None):
3fb99a22 233 utils._check_type(event_class, bt2_event_class._EventClass)
26fc5aed
PP
234
235 if event_class.stream_class.supports_packets:
3fb99a22 236 utils._check_type(parent, bt2_packet._Packet)
26fc5aed 237 else:
3fb99a22 238 utils._check_type(parent, bt2_stream._Stream)
2ae9f48c
SM
239
240 if default_clock_snapshot is not None:
c6af194f 241 if event_class.stream_class.default_clock_class is None:
cfbd7cf3
FD
242 raise ValueError(
243 'event messages in this stream must not have a default clock snapshot'
244 )
c6af194f 245
2ae9f48c 246 utils._check_uint64(default_clock_snapshot)
26fc5aed
PP
247
248 if event_class.stream_class.supports_packets:
249 ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot(
cfbd7cf3
FD
250 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
251 )
26fc5aed
PP
252 else:
253 ptr = native_bt.message_event_create_with_default_clock_snapshot(
cfbd7cf3
FD
254 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
255 )
2ae9f48c 256 else:
c6af194f 257 if event_class.stream_class.default_clock_class is not None:
cfbd7cf3
FD
258 raise ValueError(
259 'event messages in this stream must have a default clock snapshot'
260 )
c6af194f 261
26fc5aed
PP
262 if event_class.stream_class.supports_packets:
263 ptr = native_bt.message_event_create_with_packet(
cfbd7cf3
FD
264 self._bt_ptr, event_class._ptr, parent._ptr
265 )
26fc5aed
PP
266 else:
267 ptr = native_bt.message_event_create(
cfbd7cf3
FD
268 self._bt_ptr, event_class._ptr, parent._ptr
269 )
2ae9f48c
SM
270
271 if ptr is None:
694c792b 272 raise bt2._MemoryError('cannot create event message object')
2ae9f48c 273
3fb99a22 274 return bt2_message._EventMessage(ptr)
2ae9f48c 275
9ec609ec 276 def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot):
3fb99a22 277 utils._check_type(clock_class, bt2_clock_class._ClockClass)
9ec609ec 278 ptr = native_bt.message_message_iterator_inactivity_create(
cfbd7cf3
FD
279 self._bt_ptr, clock_class._ptr, clock_snapshot
280 )
9ec609ec
SM
281
282 if ptr is None:
694c792b 283 raise bt2._MemoryError('cannot create inactivity message object')
9ec609ec 284
3fb99a22 285 return bt2_message._MessageIteratorInactivityMessage(ptr)
9ec609ec 286
188edac1 287 def _create_stream_beginning_message(self, stream, default_clock_snapshot=None):
3fb99a22 288 utils._check_type(stream, bt2_stream._Stream)
2ae9f48c 289
85906b6b 290 ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr)
2ae9f48c 291 if ptr is None:
694c792b 292 raise bt2._MemoryError('cannot create stream beginning message object')
2ae9f48c 293
3fb99a22 294 msg = bt2_message._StreamBeginningMessage(ptr)
9ec609ec 295
188edac1
SM
296 if default_clock_snapshot is not None:
297 msg._default_clock_snapshot = default_clock_snapshot
9ec609ec 298
9ec609ec
SM
299 return msg
300
188edac1 301 def _create_stream_end_message(self, stream, default_clock_snapshot=None):
3fb99a22 302 utils._check_type(stream, bt2_stream._Stream)
5f25509b 303
85906b6b 304 ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr)
5f25509b 305 if ptr is None:
694c792b 306 raise bt2._MemoryError('cannot create stream end message object')
5f25509b 307
3fb99a22 308 msg = bt2_message._StreamEndMessage(ptr)
188edac1
SM
309
310 if default_clock_snapshot is not None:
311 msg._default_clock_snapshot = default_clock_snapshot
312
313 return msg
5f25509b 314
2ae9f48c 315 def _create_packet_beginning_message(self, packet, default_clock_snapshot=None):
3fb99a22 316 utils._check_type(packet, bt2_packet._Packet)
2ae9f48c 317
e8ac1aae 318 if packet.stream.cls.packets_have_beginning_default_clock_snapshot:
2ae9f48c 319 if default_clock_snapshot is None:
cfbd7cf3
FD
320 raise ValueError(
321 "packet beginning messages in this stream must have a default clock snapshot"
322 )
2ae9f48c
SM
323
324 utils._check_uint64(default_clock_snapshot)
325 ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot(
cfbd7cf3
FD
326 self._bt_ptr, packet._ptr, default_clock_snapshot
327 )
2ae9f48c
SM
328 else:
329 if default_clock_snapshot is not None:
cfbd7cf3
FD
330 raise ValueError(
331 "packet beginning messages in this stream must not have a default clock snapshot"
332 )
2ae9f48c 333
85906b6b 334 ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr)
2ae9f48c
SM
335
336 if ptr is None:
694c792b 337 raise bt2._MemoryError('cannot create packet beginning message object')
2ae9f48c 338
3fb99a22 339 return bt2_message._PacketBeginningMessage(ptr)
5f25509b
SM
340
341 def _create_packet_end_message(self, packet, default_clock_snapshot=None):
3fb99a22 342 utils._check_type(packet, bt2_packet._Packet)
5f25509b 343
e8ac1aae 344 if packet.stream.cls.packets_have_end_default_clock_snapshot:
9ec609ec 345 if default_clock_snapshot is None:
cfbd7cf3
FD
346 raise ValueError(
347 "packet end messages in this stream must have a default clock snapshot"
348 )
9ec609ec 349
5f25509b
SM
350 utils._check_uint64(default_clock_snapshot)
351 ptr = native_bt.message_packet_end_create_with_default_clock_snapshot(
cfbd7cf3
FD
352 self._bt_ptr, packet._ptr, default_clock_snapshot
353 )
5f25509b 354 else:
9ec609ec 355 if default_clock_snapshot is not None:
cfbd7cf3
FD
356 raise ValueError(
357 "packet end messages in this stream must not have a default clock snapshot"
358 )
9ec609ec 359
85906b6b 360 ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr)
5f25509b
SM
361
362 if ptr is None:
694c792b 363 raise bt2._MemoryError('cannot create packet end message object')
5f25509b 364
3fb99a22 365 return bt2_message._PacketEndMessage(ptr)
9ec609ec 366
cfbd7cf3
FD
367 def _create_discarded_events_message(
368 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
369 ):
3fb99a22 370 utils._check_type(stream, bt2_stream._Stream)
9ec609ec 371
e8ac1aae 372 if not stream.cls.supports_discarded_events:
2e90378a
PP
373 raise ValueError('stream class does not support discarded events')
374
e8ac1aae 375 if stream.cls.discarded_events_have_default_clock_snapshots:
2e90378a 376 if beg_clock_snapshot is None or end_clock_snapshot is None:
cfbd7cf3
FD
377 raise ValueError(
378 'discarded events have default clock snapshots for this stream class'
379 )
2e90378a 380
9ec609ec
SM
381 utils._check_uint64(beg_clock_snapshot)
382 utils._check_uint64(end_clock_snapshot)
383 ptr = native_bt.message_discarded_events_create_with_default_clock_snapshots(
cfbd7cf3
FD
384 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
385 )
9ec609ec 386 else:
2e90378a 387 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
cfbd7cf3
FD
388 raise ValueError(
389 'discarded events have no default clock snapshots for this stream class'
390 )
2e90378a 391
cfbd7cf3 392 ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr)
9ec609ec
SM
393
394 if ptr is None:
694c792b 395 raise bt2._MemoryError('cannot discarded events message object')
9ec609ec 396
3fb99a22 397 msg = bt2_message._DiscardedEventsMessage(ptr)
9ec609ec
SM
398
399 if count is not None:
400 msg._count = count
401
402 return msg
403
cfbd7cf3
FD
404 def _create_discarded_packets_message(
405 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
406 ):
3fb99a22 407 utils._check_type(stream, bt2_stream._Stream)
9ec609ec 408
e8ac1aae 409 if not stream.cls.supports_discarded_packets:
2e90378a
PP
410 raise ValueError('stream class does not support discarded packets')
411
e8ac1aae 412 if stream.cls.discarded_packets_have_default_clock_snapshots:
2e90378a 413 if beg_clock_snapshot is None or end_clock_snapshot is None:
cfbd7cf3
FD
414 raise ValueError(
415 'discarded packets have default clock snapshots for this stream class'
416 )
2e90378a 417
9ec609ec
SM
418 utils._check_uint64(beg_clock_snapshot)
419 utils._check_uint64(end_clock_snapshot)
420 ptr = native_bt.message_discarded_packets_create_with_default_clock_snapshots(
cfbd7cf3
FD
421 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
422 )
9ec609ec 423 else:
2e90378a 424 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
cfbd7cf3
FD
425 raise ValueError(
426 'discarded packets have no default clock snapshots for this stream class'
427 )
2e90378a 428
cfbd7cf3 429 ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr)
9ec609ec
SM
430
431 if ptr is None:
694c792b 432 raise bt2._MemoryError('cannot discarded packets message object')
9ec609ec 433
3fb99a22 434 msg = bt2_message._DiscardedPacketsMessage(ptr)
9ec609ec
SM
435
436 if count is not None:
437 msg._count = count
438
439 return msg
This page took 0.105303 seconds and 4 git commands to generate.