bt2: check that port is connected when creating message iterator
[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
415d43a1
SM
221 if not input_port.is_connected:
222 raise ValueError('input port is not connected')
223
75882e97
FD
224 (
225 status,
226 msg_iter_ptr,
9a2c8b8e 227 ) = native_bt.bt2_message_iterator_create_from_message_iterator(
ca02df0a
PP
228 self._bt_ptr, input_port._ptr
229 )
e803df70
SM
230 utils._handle_func_status(status, 'cannot create message iterator object')
231 assert msg_iter_ptr is not None
ca02df0a
PP
232
233 return _UserComponentInputPortMessageIterator(msg_iter_ptr)
234
36d9460d 235 def _create_event_message(self, event_class, parent, default_clock_snapshot=None):
3fb99a22 236 utils._check_type(event_class, bt2_event_class._EventClass)
26fc5aed
PP
237
238 if event_class.stream_class.supports_packets:
3fb99a22 239 utils._check_type(parent, bt2_packet._Packet)
26fc5aed 240 else:
3fb99a22 241 utils._check_type(parent, bt2_stream._Stream)
2ae9f48c
SM
242
243 if default_clock_snapshot is not None:
c6af194f 244 if event_class.stream_class.default_clock_class is None:
cfbd7cf3
FD
245 raise ValueError(
246 'event messages in this stream must not have a default clock snapshot'
247 )
c6af194f 248
2ae9f48c 249 utils._check_uint64(default_clock_snapshot)
26fc5aed
PP
250
251 if event_class.stream_class.supports_packets:
252 ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot(
cfbd7cf3
FD
253 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
254 )
26fc5aed
PP
255 else:
256 ptr = native_bt.message_event_create_with_default_clock_snapshot(
cfbd7cf3
FD
257 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
258 )
2ae9f48c 259 else:
c6af194f 260 if event_class.stream_class.default_clock_class is not None:
cfbd7cf3
FD
261 raise ValueError(
262 'event messages in this stream must have a default clock snapshot'
263 )
c6af194f 264
26fc5aed
PP
265 if event_class.stream_class.supports_packets:
266 ptr = native_bt.message_event_create_with_packet(
cfbd7cf3
FD
267 self._bt_ptr, event_class._ptr, parent._ptr
268 )
26fc5aed
PP
269 else:
270 ptr = native_bt.message_event_create(
cfbd7cf3
FD
271 self._bt_ptr, event_class._ptr, parent._ptr
272 )
2ae9f48c
SM
273
274 if ptr is None:
694c792b 275 raise bt2._MemoryError('cannot create event message object')
2ae9f48c 276
3fb99a22 277 return bt2_message._EventMessage(ptr)
2ae9f48c 278
9ec609ec 279 def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot):
3fb99a22 280 utils._check_type(clock_class, bt2_clock_class._ClockClass)
9ec609ec 281 ptr = native_bt.message_message_iterator_inactivity_create(
cfbd7cf3
FD
282 self._bt_ptr, clock_class._ptr, clock_snapshot
283 )
9ec609ec
SM
284
285 if ptr is None:
694c792b 286 raise bt2._MemoryError('cannot create inactivity message object')
9ec609ec 287
3fb99a22 288 return bt2_message._MessageIteratorInactivityMessage(ptr)
9ec609ec 289
188edac1 290 def _create_stream_beginning_message(self, stream, default_clock_snapshot=None):
3fb99a22 291 utils._check_type(stream, bt2_stream._Stream)
2ae9f48c 292
85906b6b 293 ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr)
2ae9f48c 294 if ptr is None:
694c792b 295 raise bt2._MemoryError('cannot create stream beginning message object')
2ae9f48c 296
3fb99a22 297 msg = bt2_message._StreamBeginningMessage(ptr)
9ec609ec 298
188edac1
SM
299 if default_clock_snapshot is not None:
300 msg._default_clock_snapshot = default_clock_snapshot
9ec609ec 301
9ec609ec
SM
302 return msg
303
188edac1 304 def _create_stream_end_message(self, stream, default_clock_snapshot=None):
3fb99a22 305 utils._check_type(stream, bt2_stream._Stream)
5f25509b 306
85906b6b 307 ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr)
5f25509b 308 if ptr is None:
694c792b 309 raise bt2._MemoryError('cannot create stream end message object')
5f25509b 310
3fb99a22 311 msg = bt2_message._StreamEndMessage(ptr)
188edac1
SM
312
313 if default_clock_snapshot is not None:
314 msg._default_clock_snapshot = default_clock_snapshot
315
316 return msg
5f25509b 317
2ae9f48c 318 def _create_packet_beginning_message(self, packet, default_clock_snapshot=None):
3fb99a22 319 utils._check_type(packet, bt2_packet._Packet)
2ae9f48c 320
e8ac1aae 321 if packet.stream.cls.packets_have_beginning_default_clock_snapshot:
2ae9f48c 322 if default_clock_snapshot is None:
cfbd7cf3
FD
323 raise ValueError(
324 "packet beginning messages in this stream must have a default clock snapshot"
325 )
2ae9f48c
SM
326
327 utils._check_uint64(default_clock_snapshot)
328 ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot(
cfbd7cf3
FD
329 self._bt_ptr, packet._ptr, default_clock_snapshot
330 )
2ae9f48c
SM
331 else:
332 if default_clock_snapshot is not None:
cfbd7cf3
FD
333 raise ValueError(
334 "packet beginning messages in this stream must not have a default clock snapshot"
335 )
2ae9f48c 336
85906b6b 337 ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr)
2ae9f48c
SM
338
339 if ptr is None:
694c792b 340 raise bt2._MemoryError('cannot create packet beginning message object')
2ae9f48c 341
3fb99a22 342 return bt2_message._PacketBeginningMessage(ptr)
5f25509b
SM
343
344 def _create_packet_end_message(self, packet, default_clock_snapshot=None):
3fb99a22 345 utils._check_type(packet, bt2_packet._Packet)
5f25509b 346
e8ac1aae 347 if packet.stream.cls.packets_have_end_default_clock_snapshot:
9ec609ec 348 if default_clock_snapshot is None:
cfbd7cf3
FD
349 raise ValueError(
350 "packet end messages in this stream must have a default clock snapshot"
351 )
9ec609ec 352
5f25509b
SM
353 utils._check_uint64(default_clock_snapshot)
354 ptr = native_bt.message_packet_end_create_with_default_clock_snapshot(
cfbd7cf3
FD
355 self._bt_ptr, packet._ptr, default_clock_snapshot
356 )
5f25509b 357 else:
9ec609ec 358 if default_clock_snapshot is not None:
cfbd7cf3
FD
359 raise ValueError(
360 "packet end messages in this stream must not have a default clock snapshot"
361 )
9ec609ec 362
85906b6b 363 ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr)
5f25509b
SM
364
365 if ptr is None:
694c792b 366 raise bt2._MemoryError('cannot create packet end message object')
5f25509b 367
3fb99a22 368 return bt2_message._PacketEndMessage(ptr)
9ec609ec 369
cfbd7cf3
FD
370 def _create_discarded_events_message(
371 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
372 ):
3fb99a22 373 utils._check_type(stream, bt2_stream._Stream)
9ec609ec 374
e8ac1aae 375 if not stream.cls.supports_discarded_events:
2e90378a
PP
376 raise ValueError('stream class does not support discarded events')
377
e8ac1aae 378 if stream.cls.discarded_events_have_default_clock_snapshots:
2e90378a 379 if beg_clock_snapshot is None or end_clock_snapshot is None:
cfbd7cf3
FD
380 raise ValueError(
381 'discarded events have default clock snapshots for this stream class'
382 )
2e90378a 383
9ec609ec
SM
384 utils._check_uint64(beg_clock_snapshot)
385 utils._check_uint64(end_clock_snapshot)
5d9ef4cb
SM
386
387 if beg_clock_snapshot > end_clock_snapshot:
388 raise ValueError(
389 'beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})'.format(
390 beg_clock_snapshot, end_clock_snapshot
391 )
392 )
393
9ec609ec 394 ptr = native_bt.message_discarded_events_create_with_default_clock_snapshots(
cfbd7cf3
FD
395 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
396 )
9ec609ec 397 else:
2e90378a 398 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
cfbd7cf3
FD
399 raise ValueError(
400 'discarded events have no default clock snapshots for this stream class'
401 )
2e90378a 402
cfbd7cf3 403 ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr)
9ec609ec
SM
404
405 if ptr is None:
694c792b 406 raise bt2._MemoryError('cannot discarded events message object')
9ec609ec 407
3fb99a22 408 msg = bt2_message._DiscardedEventsMessage(ptr)
9ec609ec
SM
409
410 if count is not None:
411 msg._count = count
412
413 return msg
414
cfbd7cf3
FD
415 def _create_discarded_packets_message(
416 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
417 ):
3fb99a22 418 utils._check_type(stream, bt2_stream._Stream)
9ec609ec 419
e8ac1aae 420 if not stream.cls.supports_discarded_packets:
2e90378a
PP
421 raise ValueError('stream class does not support discarded packets')
422
e8ac1aae 423 if stream.cls.discarded_packets_have_default_clock_snapshots:
2e90378a 424 if beg_clock_snapshot is None or end_clock_snapshot is None:
cfbd7cf3
FD
425 raise ValueError(
426 'discarded packets have default clock snapshots for this stream class'
427 )
2e90378a 428
9ec609ec
SM
429 utils._check_uint64(beg_clock_snapshot)
430 utils._check_uint64(end_clock_snapshot)
5d9ef4cb
SM
431
432 if beg_clock_snapshot > end_clock_snapshot:
433 raise ValueError(
434 'beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})'.format(
435 beg_clock_snapshot, end_clock_snapshot
436 )
437 )
438
9ec609ec 439 ptr = native_bt.message_discarded_packets_create_with_default_clock_snapshots(
cfbd7cf3
FD
440 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
441 )
9ec609ec 442 else:
2e90378a 443 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
cfbd7cf3
FD
444 raise ValueError(
445 'discarded packets have no default clock snapshots for this stream class'
446 )
2e90378a 447
cfbd7cf3 448 ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr)
9ec609ec
SM
449
450 if ptr is None:
694c792b 451 raise bt2._MemoryError('cannot discarded packets message object')
9ec609ec 452
3fb99a22 453 msg = bt2_message._DiscardedPacketsMessage(ptr)
9ec609ec
SM
454
455 if count is not None:
456 msg._count = count
457
458 return msg
This page took 0.089068 seconds and 4 git commands to generate.