Fix: tests: live: listen on python < 3.5 needs backlog parameter
[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
811644b8 113
c5f330cd
SM
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.
5602ef81 121class _UserMessageIterator(_MessageIterator):
81447b5b 122 def __new__(cls, ptr):
811644b8 123 # User iterator objects are always created by the native side,
81447b5b
PP
124 # that is, never instantiated directly by Python code.
125 #
811644b8
PP
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
85906b6b 129 # self._bt_ptr argument being set.
81447b5b 130 #
85906b6b 131 # self._bt_ptr is NOT owned by this object here, so there's nothing
81447b5b
PP
132 # to do in __del__().
133 self = super().__new__(cls)
85906b6b 134 self._bt_ptr = ptr
81447b5b
PP
135 return self
136
85906b6b 137 def _bt_init_from_native(self, self_output_port_ptr):
3fb99a22 138 self_output_port = bt2_port._create_self_from_ptr_and_get_ref(
cfbd7cf3
FD
139 self_output_port_ptr, native_bt.PORT_TYPE_OUTPUT
140 )
c5f330cd
SM
141 self.__init__(self_output_port)
142
143 def __init__(self, output_port):
81447b5b
PP
144 pass
145
146 @property
811644b8 147 def _component(self):
85906b6b 148 return native_bt.bt2_get_user_component_from_user_msg_iter(self._bt_ptr)
81447b5b 149
14503fb1
SM
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
81447b5b
PP
158 @property
159 def addr(self):
85906b6b 160 return int(self._bt_ptr)
81447b5b 161
9b4f9b42
PP
162 @property
163 def _is_interrupted(self):
164 return bool(native_bt.self_message_iterator_is_interrupted(self._bt_ptr))
165
6a91742b 166 def _user_finalize(self):
81447b5b
PP
167 pass
168
811644b8
PP
169 def __next__(self):
170 raise bt2.Stop
171
85906b6b 172 def _bt_next_from_native(self):
811644b8
PP
173 # this can raise anything: it's catched by the native part
174 try:
5602ef81 175 msg = next(self)
811644b8
PP
176 except StopIteration:
177 raise bt2.Stop
4c4935bf 178 except Exception:
811644b8
PP
179 raise
180
f0a42b33 181 utils._check_type(msg, bt2_message._MessageConst)
81447b5b 182
d79a8353
SM
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)
2ae9f48c 188
85906b6b 189 def _bt_can_seek_beginning_from_native(self):
f00b8d40
SM
190 # Here, we mimic the behavior of the C API:
191 #
14cfc8ce 192 # - If the iterator has a _user_can_seek_beginning method,
6a91742b 193 # read it and use that result.
5a096c63 194 # - Otherwise, the presence or absence of a `_user_seek_beginning`
f00b8d40 195 # method indicates whether the iterator can seek beginning.
6a91742b 196 if hasattr(self, '_user_can_seek_beginning'):
14cfc8ce 197 can_seek_beginning = self._user_can_seek_beginning()
f00b8d40
SM
198 utils._check_bool(can_seek_beginning)
199 return can_seek_beginning
200 else:
6a91742b 201 return hasattr(self, '_user_seek_beginning')
f00b8d40 202
85906b6b 203 def _bt_seek_beginning_from_native(self):
6a91742b 204 self._user_seek_beginning()
f00b8d40 205
c182d7dd
SM
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
ca02df0a 227 def _create_input_port_message_iterator(self, input_port):
3fb99a22 228 utils._check_type(input_port, bt2_port._UserComponentInputPort)
ca02df0a 229
e803df70 230 status, msg_iter_ptr = native_bt.bt2_self_component_port_input_message_iterator_create_from_message_iterator(
ca02df0a
PP
231 self._bt_ptr, input_port._ptr
232 )
e803df70
SM
233 utils._handle_func_status(status, 'cannot create message iterator object')
234 assert msg_iter_ptr is not None
ca02df0a
PP
235
236 return _UserComponentInputPortMessageIterator(msg_iter_ptr)
237
36d9460d 238 def _create_event_message(self, event_class, parent, default_clock_snapshot=None):
3fb99a22 239 utils._check_type(event_class, bt2_event_class._EventClass)
26fc5aed
PP
240
241 if event_class.stream_class.supports_packets:
3fb99a22 242 utils._check_type(parent, bt2_packet._Packet)
26fc5aed 243 else:
3fb99a22 244 utils._check_type(parent, bt2_stream._Stream)
2ae9f48c
SM
245
246 if default_clock_snapshot is not None:
c6af194f 247 if event_class.stream_class.default_clock_class is None:
cfbd7cf3
FD
248 raise ValueError(
249 'event messages in this stream must not have a default clock snapshot'
250 )
c6af194f 251
2ae9f48c 252 utils._check_uint64(default_clock_snapshot)
26fc5aed
PP
253
254 if event_class.stream_class.supports_packets:
255 ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot(
cfbd7cf3
FD
256 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
257 )
26fc5aed
PP
258 else:
259 ptr = native_bt.message_event_create_with_default_clock_snapshot(
cfbd7cf3
FD
260 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
261 )
2ae9f48c 262 else:
c6af194f 263 if event_class.stream_class.default_clock_class is not None:
cfbd7cf3
FD
264 raise ValueError(
265 'event messages in this stream must have a default clock snapshot'
266 )
c6af194f 267
26fc5aed
PP
268 if event_class.stream_class.supports_packets:
269 ptr = native_bt.message_event_create_with_packet(
cfbd7cf3
FD
270 self._bt_ptr, event_class._ptr, parent._ptr
271 )
26fc5aed
PP
272 else:
273 ptr = native_bt.message_event_create(
cfbd7cf3
FD
274 self._bt_ptr, event_class._ptr, parent._ptr
275 )
2ae9f48c
SM
276
277 if ptr is None:
694c792b 278 raise bt2._MemoryError('cannot create event message object')
2ae9f48c 279
3fb99a22 280 return bt2_message._EventMessage(ptr)
2ae9f48c 281
9ec609ec 282 def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot):
3fb99a22 283 utils._check_type(clock_class, bt2_clock_class._ClockClass)
9ec609ec 284 ptr = native_bt.message_message_iterator_inactivity_create(
cfbd7cf3
FD
285 self._bt_ptr, clock_class._ptr, clock_snapshot
286 )
9ec609ec
SM
287
288 if ptr is None:
694c792b 289 raise bt2._MemoryError('cannot create inactivity message object')
9ec609ec 290
3fb99a22 291 return bt2_message._MessageIteratorInactivityMessage(ptr)
9ec609ec 292
188edac1 293 def _create_stream_beginning_message(self, stream, default_clock_snapshot=None):
3fb99a22 294 utils._check_type(stream, bt2_stream._Stream)
2ae9f48c 295
85906b6b 296 ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr)
2ae9f48c 297 if ptr is None:
694c792b 298 raise bt2._MemoryError('cannot create stream beginning message object')
2ae9f48c 299
3fb99a22 300 msg = bt2_message._StreamBeginningMessage(ptr)
9ec609ec 301
188edac1
SM
302 if default_clock_snapshot is not None:
303 msg._default_clock_snapshot = default_clock_snapshot
9ec609ec 304
9ec609ec
SM
305 return msg
306
188edac1 307 def _create_stream_end_message(self, stream, default_clock_snapshot=None):
3fb99a22 308 utils._check_type(stream, bt2_stream._Stream)
5f25509b 309
85906b6b 310 ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr)
5f25509b 311 if ptr is None:
694c792b 312 raise bt2._MemoryError('cannot create stream end message object')
5f25509b 313
3fb99a22 314 msg = bt2_message._StreamEndMessage(ptr)
188edac1
SM
315
316 if default_clock_snapshot is not None:
317 msg._default_clock_snapshot = default_clock_snapshot
318
319 return msg
5f25509b 320
2ae9f48c 321 def _create_packet_beginning_message(self, packet, default_clock_snapshot=None):
3fb99a22 322 utils._check_type(packet, bt2_packet._Packet)
2ae9f48c 323
e8ac1aae 324 if packet.stream.cls.packets_have_beginning_default_clock_snapshot:
2ae9f48c 325 if default_clock_snapshot is None:
cfbd7cf3
FD
326 raise ValueError(
327 "packet beginning messages in this stream must have a default clock snapshot"
328 )
2ae9f48c
SM
329
330 utils._check_uint64(default_clock_snapshot)
331 ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot(
cfbd7cf3
FD
332 self._bt_ptr, packet._ptr, default_clock_snapshot
333 )
2ae9f48c
SM
334 else:
335 if default_clock_snapshot is not None:
cfbd7cf3
FD
336 raise ValueError(
337 "packet beginning messages in this stream must not have a default clock snapshot"
338 )
2ae9f48c 339
85906b6b 340 ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr)
2ae9f48c
SM
341
342 if ptr is None:
694c792b 343 raise bt2._MemoryError('cannot create packet beginning message object')
2ae9f48c 344
3fb99a22 345 return bt2_message._PacketBeginningMessage(ptr)
5f25509b
SM
346
347 def _create_packet_end_message(self, packet, default_clock_snapshot=None):
3fb99a22 348 utils._check_type(packet, bt2_packet._Packet)
5f25509b 349
e8ac1aae 350 if packet.stream.cls.packets_have_end_default_clock_snapshot:
9ec609ec 351 if default_clock_snapshot is None:
cfbd7cf3
FD
352 raise ValueError(
353 "packet end messages in this stream must have a default clock snapshot"
354 )
9ec609ec 355
5f25509b
SM
356 utils._check_uint64(default_clock_snapshot)
357 ptr = native_bt.message_packet_end_create_with_default_clock_snapshot(
cfbd7cf3
FD
358 self._bt_ptr, packet._ptr, default_clock_snapshot
359 )
5f25509b 360 else:
9ec609ec 361 if default_clock_snapshot is not None:
cfbd7cf3
FD
362 raise ValueError(
363 "packet end messages in this stream must not have a default clock snapshot"
364 )
9ec609ec 365
85906b6b 366 ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr)
5f25509b
SM
367
368 if ptr is None:
694c792b 369 raise bt2._MemoryError('cannot create packet end message object')
5f25509b 370
3fb99a22 371 return bt2_message._PacketEndMessage(ptr)
9ec609ec 372
cfbd7cf3
FD
373 def _create_discarded_events_message(
374 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
375 ):
3fb99a22 376 utils._check_type(stream, bt2_stream._Stream)
9ec609ec 377
e8ac1aae 378 if not stream.cls.supports_discarded_events:
2e90378a
PP
379 raise ValueError('stream class does not support discarded events')
380
e8ac1aae 381 if stream.cls.discarded_events_have_default_clock_snapshots:
2e90378a 382 if beg_clock_snapshot is None or end_clock_snapshot is None:
cfbd7cf3
FD
383 raise ValueError(
384 'discarded events have default clock snapshots for this stream class'
385 )
2e90378a 386
9ec609ec
SM
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(
cfbd7cf3
FD
390 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
391 )
9ec609ec 392 else:
2e90378a 393 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
cfbd7cf3
FD
394 raise ValueError(
395 'discarded events have no default clock snapshots for this stream class'
396 )
2e90378a 397
cfbd7cf3 398 ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr)
9ec609ec
SM
399
400 if ptr is None:
694c792b 401 raise bt2._MemoryError('cannot discarded events message object')
9ec609ec 402
3fb99a22 403 msg = bt2_message._DiscardedEventsMessage(ptr)
9ec609ec
SM
404
405 if count is not None:
406 msg._count = count
407
408 return msg
409
cfbd7cf3
FD
410 def _create_discarded_packets_message(
411 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
412 ):
3fb99a22 413 utils._check_type(stream, bt2_stream._Stream)
9ec609ec 414
e8ac1aae 415 if not stream.cls.supports_discarded_packets:
2e90378a
PP
416 raise ValueError('stream class does not support discarded packets')
417
e8ac1aae 418 if stream.cls.discarded_packets_have_default_clock_snapshots:
2e90378a 419 if beg_clock_snapshot is None or end_clock_snapshot is None:
cfbd7cf3
FD
420 raise ValueError(
421 'discarded packets have default clock snapshots for this stream class'
422 )
2e90378a 423
9ec609ec
SM
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(
cfbd7cf3
FD
427 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
428 )
9ec609ec 429 else:
2e90378a 430 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
cfbd7cf3
FD
431 raise ValueError(
432 'discarded packets have no default clock snapshots for this stream class'
433 )
2e90378a 434
cfbd7cf3 435 ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr)
9ec609ec
SM
436
437 if ptr is None:
694c792b 438 raise bt2._MemoryError('cannot discarded packets message object')
9ec609ec 439
3fb99a22 440 msg = bt2_message._DiscardedPacketsMessage(ptr)
9ec609ec
SM
441
442 if count is not None:
443 msg._count = count
444
445 return msg
This page took 0.072896 seconds and 4 git commands to generate.