lib: pass config object to message iterator init method, add can seek forward property
[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
3fb99a22 24from bt2 import message as bt2_message
81447b5b 25import collections.abc
3fb99a22
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
5602ef81 34class _MessageIterator(collections.abc.Iterator):
81447b5b 35 def __next__(self):
811644b8 36 raise NotImplementedError
81447b5b
PP
37
38
1975af3d
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
2ae9f48c 47 def __init__(self, ptr):
cfbd7cf3
FD
48 self._current_msgs = []
49 self._at = 0
50 super().__init__(ptr)
811644b8
PP
51
52 def __next__(self):
2ae9f48c 53 if len(self._current_msgs) == self._at:
1975af3d
SM
54 status, msgs = native_bt.bt2_self_component_port_input_get_msg_range(
55 self._ptr
56 )
cfbd7cf3
FD
57 utils._handle_func_status(
58 status, 'unexpected error: cannot advance the message iterator'
59 )
2ae9f48c
SM
60 self._current_msgs = msgs
61 self._at = 0
62
63 msg_ptr = self._current_msgs[self._at]
64 self._at += 1
65
3fb99a22 66 return bt2_message._create_from_ptr(msg_ptr)
81447b5b 67
f00b8d40 68 def can_seek_beginning(self):
1975af3d
SM
69 status, res = native_bt.self_component_port_input_message_iterator_can_seek_beginning(
70 self._ptr
71 )
f2fb1b32
SM
72 utils._handle_func_status(
73 status,
74 'cannot check whether or not message iterator can seek its beginning',
75 )
f00b8d40
SM
76 return res != 0
77
78 def seek_beginning(self):
b6909b73 79 # Forget about buffered messages, they won't be valid after seeking.
f00b8d40
SM
80 self._current_msgs.clear()
81 self._at = 0
82
1975af3d
SM
83 status = native_bt.self_component_port_input_message_iterator_seek_beginning(
84 self._ptr
85 )
cfbd7cf3 86 utils._handle_func_status(status, 'cannot seek message iterator beginning')
f00b8d40 87
c182d7dd
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
8d8b141d
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
811644b8 132
c5f330cd
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.
5602ef81 140class _UserMessageIterator(_MessageIterator):
81447b5b 141 def __new__(cls, ptr):
811644b8 142 # User iterator objects are always created by the native side,
81447b5b
PP
143 # that is, never instantiated directly by Python code.
144 #
811644b8
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
85906b6b 148 # self._bt_ptr argument being set.
81447b5b 149 #
85906b6b 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)
85906b6b 153 self._bt_ptr = ptr
81447b5b
PP
154 return self
155
8d8b141d 156 def _bt_init_from_native(self, config_ptr, self_output_port_ptr):
3fb99a22 157 self_output_port = bt2_port._create_self_from_ptr_and_get_ref(
cfbd7cf3
FD
158 self_output_port_ptr, native_bt.PORT_TYPE_OUTPUT
159 )
8d8b141d
SM
160 config = _MessageIteratorConfiguration(config_ptr)
161 self.__init__(config, self_output_port)
c5f330cd 162
8d8b141d 163 def __init__(self, config, self_output_port):
81447b5b
PP
164 pass
165
166 @property
811644b8 167 def _component(self):
85906b6b 168 return native_bt.bt2_get_user_component_from_user_msg_iter(self._bt_ptr)
81447b5b 169
14503fb1
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):
85906b6b 180 return int(self._bt_ptr)
81447b5b 181
9b4f9b42
PP
182 @property
183 def _is_interrupted(self):
184 return bool(native_bt.self_message_iterator_is_interrupted(self._bt_ptr))
185
6a91742b 186 def _user_finalize(self):
81447b5b
PP
187 pass
188
811644b8
PP
189 def __next__(self):
190 raise bt2.Stop
191
85906b6b 192 def _bt_next_from_native(self):
811644b8
PP
193 # this can raise anything: it's catched by the native part
194 try:
5602ef81 195 msg = next(self)
811644b8
PP
196 except StopIteration:
197 raise bt2.Stop
4c4935bf 198 except Exception:
811644b8
PP
199 raise
200
f0a42b33 201 utils._check_type(msg, bt2_message._MessageConst)
81447b5b 202
d79a8353
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)
2ae9f48c 208
85906b6b 209 def _bt_can_seek_beginning_from_native(self):
f00b8d40
SM
210 # Here, we mimic the behavior of the C API:
211 #
14cfc8ce 212 # - If the iterator has a _user_can_seek_beginning method,
6a91742b 213 # read it and use that result.
5a096c63 214 # - Otherwise, the presence or absence of a `_user_seek_beginning`
f00b8d40 215 # method indicates whether the iterator can seek beginning.
6a91742b 216 if hasattr(self, '_user_can_seek_beginning'):
14cfc8ce 217 can_seek_beginning = self._user_can_seek_beginning()
f00b8d40
SM
218 utils._check_bool(can_seek_beginning)
219 return can_seek_beginning
220 else:
6a91742b 221 return hasattr(self, '_user_seek_beginning')
f00b8d40 222
85906b6b 223 def _bt_seek_beginning_from_native(self):
6a91742b 224 self._user_seek_beginning()
f00b8d40 225
c182d7dd
SM
226 def _bt_can_seek_ns_from_origin_from_native(self, ns_from_origin):
227 # Here, we mimic the behavior of the C API:
228 #
229 # - If the iterator has a _user_can_seek_ns_from_origin method,
230 # call it and use its return value.
231 # - Otherwise, if there is a `_user_seek_ns_from_origin` method,
232 # we presume it's possible.
233 # - Otherwise, check if we can seek to beginning (which allows us to
234 # seek to beginning and then fast forward - aka auto-seek).
235 if hasattr(self, '_user_can_seek_ns_from_origin'):
236 can_seek_ns_from_origin = self._user_can_seek_ns_from_origin(ns_from_origin)
237 utils._check_bool(can_seek_ns_from_origin)
238 return can_seek_ns_from_origin
239 elif hasattr(self, '_user_seek_ns_from_origin'):
240 return True
241 else:
242 return self._bt_can_seek_beginning_from_native()
243
244 def _bt_seek_ns_from_origin_from_native(self, ns_from_origin):
245 self._user_seek_ns_from_origin(ns_from_origin)
246
ca02df0a 247 def _create_input_port_message_iterator(self, input_port):
3fb99a22 248 utils._check_type(input_port, bt2_port._UserComponentInputPort)
ca02df0a 249
e803df70 250 status, msg_iter_ptr = native_bt.bt2_self_component_port_input_message_iterator_create_from_message_iterator(
ca02df0a
PP
251 self._bt_ptr, input_port._ptr
252 )
e803df70
SM
253 utils._handle_func_status(status, 'cannot create message iterator object')
254 assert msg_iter_ptr is not None
ca02df0a
PP
255
256 return _UserComponentInputPortMessageIterator(msg_iter_ptr)
257
36d9460d 258 def _create_event_message(self, event_class, parent, default_clock_snapshot=None):
3fb99a22 259 utils._check_type(event_class, bt2_event_class._EventClass)
26fc5aed
PP
260
261 if event_class.stream_class.supports_packets:
3fb99a22 262 utils._check_type(parent, bt2_packet._Packet)
26fc5aed 263 else:
3fb99a22 264 utils._check_type(parent, bt2_stream._Stream)
2ae9f48c
SM
265
266 if default_clock_snapshot is not None:
c6af194f 267 if event_class.stream_class.default_clock_class is None:
cfbd7cf3
FD
268 raise ValueError(
269 'event messages in this stream must not have a default clock snapshot'
270 )
c6af194f 271
2ae9f48c 272 utils._check_uint64(default_clock_snapshot)
26fc5aed
PP
273
274 if event_class.stream_class.supports_packets:
275 ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot(
cfbd7cf3
FD
276 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
277 )
26fc5aed
PP
278 else:
279 ptr = native_bt.message_event_create_with_default_clock_snapshot(
cfbd7cf3
FD
280 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
281 )
2ae9f48c 282 else:
c6af194f 283 if event_class.stream_class.default_clock_class is not None:
cfbd7cf3
FD
284 raise ValueError(
285 'event messages in this stream must have a default clock snapshot'
286 )
c6af194f 287
26fc5aed
PP
288 if event_class.stream_class.supports_packets:
289 ptr = native_bt.message_event_create_with_packet(
cfbd7cf3
FD
290 self._bt_ptr, event_class._ptr, parent._ptr
291 )
26fc5aed
PP
292 else:
293 ptr = native_bt.message_event_create(
cfbd7cf3
FD
294 self._bt_ptr, event_class._ptr, parent._ptr
295 )
2ae9f48c
SM
296
297 if ptr is None:
694c792b 298 raise bt2._MemoryError('cannot create event message object')
2ae9f48c 299
3fb99a22 300 return bt2_message._EventMessage(ptr)
2ae9f48c 301
9ec609ec 302 def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot):
3fb99a22 303 utils._check_type(clock_class, bt2_clock_class._ClockClass)
9ec609ec 304 ptr = native_bt.message_message_iterator_inactivity_create(
cfbd7cf3
FD
305 self._bt_ptr, clock_class._ptr, clock_snapshot
306 )
9ec609ec
SM
307
308 if ptr is None:
694c792b 309 raise bt2._MemoryError('cannot create inactivity message object')
9ec609ec 310
3fb99a22 311 return bt2_message._MessageIteratorInactivityMessage(ptr)
9ec609ec 312
188edac1 313 def _create_stream_beginning_message(self, stream, default_clock_snapshot=None):
3fb99a22 314 utils._check_type(stream, bt2_stream._Stream)
2ae9f48c 315
85906b6b 316 ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr)
2ae9f48c 317 if ptr is None:
694c792b 318 raise bt2._MemoryError('cannot create stream beginning message object')
2ae9f48c 319
3fb99a22 320 msg = bt2_message._StreamBeginningMessage(ptr)
9ec609ec 321
188edac1
SM
322 if default_clock_snapshot is not None:
323 msg._default_clock_snapshot = default_clock_snapshot
9ec609ec 324
9ec609ec
SM
325 return msg
326
188edac1 327 def _create_stream_end_message(self, stream, default_clock_snapshot=None):
3fb99a22 328 utils._check_type(stream, bt2_stream._Stream)
5f25509b 329
85906b6b 330 ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr)
5f25509b 331 if ptr is None:
694c792b 332 raise bt2._MemoryError('cannot create stream end message object')
5f25509b 333
3fb99a22 334 msg = bt2_message._StreamEndMessage(ptr)
188edac1
SM
335
336 if default_clock_snapshot is not None:
337 msg._default_clock_snapshot = default_clock_snapshot
338
339 return msg
5f25509b 340
2ae9f48c 341 def _create_packet_beginning_message(self, packet, default_clock_snapshot=None):
3fb99a22 342 utils._check_type(packet, bt2_packet._Packet)
2ae9f48c 343
e8ac1aae 344 if packet.stream.cls.packets_have_beginning_default_clock_snapshot:
2ae9f48c 345 if default_clock_snapshot is None:
cfbd7cf3
FD
346 raise ValueError(
347 "packet beginning messages in this stream must have a default clock snapshot"
348 )
2ae9f48c
SM
349
350 utils._check_uint64(default_clock_snapshot)
351 ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot(
cfbd7cf3
FD
352 self._bt_ptr, packet._ptr, default_clock_snapshot
353 )
2ae9f48c
SM
354 else:
355 if default_clock_snapshot is not None:
cfbd7cf3
FD
356 raise ValueError(
357 "packet beginning messages in this stream must not have a default clock snapshot"
358 )
2ae9f48c 359
85906b6b 360 ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr)
2ae9f48c
SM
361
362 if ptr is None:
694c792b 363 raise bt2._MemoryError('cannot create packet beginning message object')
2ae9f48c 364
3fb99a22 365 return bt2_message._PacketBeginningMessage(ptr)
5f25509b
SM
366
367 def _create_packet_end_message(self, packet, default_clock_snapshot=None):
3fb99a22 368 utils._check_type(packet, bt2_packet._Packet)
5f25509b 369
e8ac1aae 370 if packet.stream.cls.packets_have_end_default_clock_snapshot:
9ec609ec 371 if default_clock_snapshot is None:
cfbd7cf3
FD
372 raise ValueError(
373 "packet end messages in this stream must have a default clock snapshot"
374 )
9ec609ec 375
5f25509b
SM
376 utils._check_uint64(default_clock_snapshot)
377 ptr = native_bt.message_packet_end_create_with_default_clock_snapshot(
cfbd7cf3
FD
378 self._bt_ptr, packet._ptr, default_clock_snapshot
379 )
5f25509b 380 else:
9ec609ec 381 if default_clock_snapshot is not None:
cfbd7cf3
FD
382 raise ValueError(
383 "packet end messages in this stream must not have a default clock snapshot"
384 )
9ec609ec 385
85906b6b 386 ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr)
5f25509b
SM
387
388 if ptr is None:
694c792b 389 raise bt2._MemoryError('cannot create packet end message object')
5f25509b 390
3fb99a22 391 return bt2_message._PacketEndMessage(ptr)
9ec609ec 392
cfbd7cf3
FD
393 def _create_discarded_events_message(
394 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
395 ):
3fb99a22 396 utils._check_type(stream, bt2_stream._Stream)
9ec609ec 397
e8ac1aae 398 if not stream.cls.supports_discarded_events:
2e90378a
PP
399 raise ValueError('stream class does not support discarded events')
400
e8ac1aae 401 if stream.cls.discarded_events_have_default_clock_snapshots:
2e90378a 402 if beg_clock_snapshot is None or end_clock_snapshot is None:
cfbd7cf3
FD
403 raise ValueError(
404 'discarded events have default clock snapshots for this stream class'
405 )
2e90378a 406
9ec609ec
SM
407 utils._check_uint64(beg_clock_snapshot)
408 utils._check_uint64(end_clock_snapshot)
409 ptr = native_bt.message_discarded_events_create_with_default_clock_snapshots(
cfbd7cf3
FD
410 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
411 )
9ec609ec 412 else:
2e90378a 413 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
cfbd7cf3
FD
414 raise ValueError(
415 'discarded events have no default clock snapshots for this stream class'
416 )
2e90378a 417
cfbd7cf3 418 ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr)
9ec609ec
SM
419
420 if ptr is None:
694c792b 421 raise bt2._MemoryError('cannot discarded events message object')
9ec609ec 422
3fb99a22 423 msg = bt2_message._DiscardedEventsMessage(ptr)
9ec609ec
SM
424
425 if count is not None:
426 msg._count = count
427
428 return msg
429
cfbd7cf3
FD
430 def _create_discarded_packets_message(
431 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
432 ):
3fb99a22 433 utils._check_type(stream, bt2_stream._Stream)
9ec609ec 434
e8ac1aae 435 if not stream.cls.supports_discarded_packets:
2e90378a
PP
436 raise ValueError('stream class does not support discarded packets')
437
e8ac1aae 438 if stream.cls.discarded_packets_have_default_clock_snapshots:
2e90378a 439 if beg_clock_snapshot is None or end_clock_snapshot is None:
cfbd7cf3
FD
440 raise ValueError(
441 'discarded packets have default clock snapshots for this stream class'
442 )
2e90378a 443
9ec609ec
SM
444 utils._check_uint64(beg_clock_snapshot)
445 utils._check_uint64(end_clock_snapshot)
446 ptr = native_bt.message_discarded_packets_create_with_default_clock_snapshots(
cfbd7cf3
FD
447 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
448 )
9ec609ec 449 else:
2e90378a 450 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
cfbd7cf3
FD
451 raise ValueError(
452 'discarded packets have no default clock snapshots for this stream class'
453 )
2e90378a 454
cfbd7cf3 455 ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr)
9ec609ec
SM
456
457 if ptr is None:
694c792b 458 raise bt2._MemoryError('cannot discarded packets message object')
9ec609ec 459
3fb99a22 460 msg = bt2_message._DiscardedPacketsMessage(ptr)
9ec609ec
SM
461
462 if count is not None:
463 msg._count = count
464
465 return msg
This page took 0.072686 seconds and 4 git commands to generate.