bt2: Adapt test_graph.py and make it pass
[babeltrace.git] / bindings / python / bt2 / bt2 / message.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
4b552f8b 24import bt2.clock_snapshot
71fd6f52 25import collections
81447b5b
PP
26import bt2.packet
27import bt2.stream
28import bt2.event
811644b8 29import copy
81447b5b
PP
30import bt2
31
32
33def _create_from_ptr(ptr):
5602ef81 34 msg_type = native_bt.message_get_type(ptr)
81447b5b
PP
35 cls = None
36
5602ef81
SM
37 if msg_type not in _MESSAGE_TYPE_TO_CLS:
38 raise bt2.Error('unknown message type: {}'.format(msg_type))
81447b5b 39
5602ef81 40 return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr)
81447b5b
PP
41
42
5602ef81
SM
43def _msg_types_from_msg_classes(message_types):
44 if message_types is None:
45 msg_types = None
dc43190b 46 else:
5602ef81
SM
47 for msg_cls in message_types:
48 if msg_cls not in _MESSAGE_TYPE_TO_CLS.values():
49 raise ValueError("'{}' is not a message class".format(msg_cls))
dc43190b 50
5602ef81 51 msg_types = [msg_cls._TYPE for msg_cls in message_types]
dc43190b 52
5602ef81 53 return msg_types
dc43190b
PP
54
55
78288f58 56class _Message(object._SharedObject):
2ae9f48c
SM
57 _get_ref = staticmethod(native_bt.message_get_ref)
58 _put_ref = staticmethod(native_bt.message_put_ref)
81447b5b
PP
59
60
5602ef81 61class _CopyableMessage(_Message):
811644b8
PP
62 def __copy__(self):
63 return self._copy(lambda obj: obj)
64
65 def __deepcopy__(self, memo):
66 cpy = self._copy(copy.deepcopy)
67 memo[id(self)] = cpy
68 return cpy
69
70
2ae9f48c 71class _EventMessage(_CopyableMessage):
88f3724d 72 _TYPE = native_bt.MESSAGE_TYPE_EVENT
811644b8 73
2ae9f48c
SM
74 @property
75 def event(self):
76 event_ptr = native_bt.message_event_borrow_event(self._ptr)
77 assert event_ptr is not None
78 return bt2.event._Event._create_from_ptr_and_get_ref(
79 event_ptr, self._ptr, self._get_ref, self._put_ref)
81447b5b 80
2ae9f48c
SM
81 @property
82 def default_clock_snapshot(self):
83 if self.event.event_class.stream_class.default_clock_class is None:
84 return None
81447b5b 85
2ae9f48c 86 snapshot_ptr = native_bt.message_event_borrow_default_clock_snapshot_const(self._ptr)
81447b5b 87
2ae9f48c
SM
88 return bt2.clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref(
89 snapshot_ptr, self._ptr, self._get_ref, self._put_ref)
81447b5b 90
811644b8
PP
91 @property
92 def clock_class_priority_map(self):
5602ef81 93 cc_prio_map_ptr = native_bt.message_event_get_clock_class_priority_map(self._ptr)
811644b8
PP
94 assert(cc_prio_map_ptr)
95 return bt2.clock_class_priority_map.ClockClassPriorityMap._create_from_ptr(cc_prio_map_ptr)
96
97 def __eq__(self, other):
98 if type(other) is not type(self):
99 return False
100
101 if self.addr == other.addr:
102 return True
103
104 self_props = (
105 self.event,
106 self.clock_class_priority_map,
107 )
108 other_props = (
109 other.event,
110 other.clock_class_priority_map,
111 )
112 return self_props == other_props
113
114 def _copy(self, copy_func):
115 # We can always use references here because those properties are
5602ef81
SM
116 # frozen anyway if they are part of a message. Since the
117 # user cannot modify them after copying the message, it's
811644b8 118 # useless to copy/deep-copy them.
5602ef81 119 return EventMessage(self.event, self.clock_class_priority_map)
811644b8
PP
120
121
2ae9f48c 122class _PacketBeginningMessage(_CopyableMessage):
88f3724d 123 _TYPE = native_bt.MESSAGE_TYPE_PACKET_BEGINNING
81447b5b 124
81447b5b
PP
125 @property
126 def packet(self):
5602ef81 127 packet_ptr = native_bt.message_packet_begin_get_packet(self._ptr)
811644b8 128 assert(packet_ptr)
81447b5b
PP
129 return bt2.packet._Packet._create_from_ptr(packet_ptr)
130
811644b8
PP
131 def __eq__(self, other):
132 if type(other) is not type(self):
133 return False
134
135 if self.addr == other.addr:
136 return True
137
138 return self.packet == other.packet
139
140 def _copy(self, copy_func):
141 # We can always use references here because those properties are
5602ef81
SM
142 # frozen anyway if they are part of a message. Since the
143 # user cannot modify them after copying the message, it's
811644b8 144 # useless to copy/deep-copy them.
5602ef81 145 return PacketBeginningMessage(self.packet)
811644b8
PP
146
147
5f25509b 148class _PacketEndMessage(_CopyableMessage):
88f3724d 149 _TYPE = native_bt.MESSAGE_TYPE_PACKET_END
81447b5b 150
81447b5b
PP
151 @property
152 def packet(self):
5602ef81 153 packet_ptr = native_bt.message_packet_end_get_packet(self._ptr)
811644b8 154 assert(packet_ptr)
81447b5b
PP
155 return bt2.packet._Packet._create_from_ptr(packet_ptr)
156
811644b8
PP
157 def __eq__(self, other):
158 if type(other) is not type(self):
159 return False
160
161 if self.addr == other.addr:
162 return True
163
164 return self.packet == other.packet
165
166 def _copy(self, copy_func):
167 # We can always use references here because those properties are
5602ef81
SM
168 # frozen anyway if they are part of a message. Since the
169 # user cannot modify them after copying the message, it's
811644b8 170 # useless to copy/deep-copy them.
5602ef81 171 return PacketEndMessage(self.packet)
811644b8
PP
172
173
2ae9f48c 174class _StreamBeginningMessage(_CopyableMessage):
88f3724d 175 _TYPE = native_bt.MESSAGE_TYPE_STREAM_BEGINNING
81447b5b 176
81447b5b
PP
177 @property
178 def stream(self):
5602ef81 179 stream_ptr = native_bt.message_stream_begin_get_stream(self._ptr)
811644b8 180 assert(stream_ptr)
81447b5b
PP
181 return bt2.stream._create_from_ptr(stream_ptr)
182
811644b8
PP
183 def __eq__(self, other):
184 if type(other) is not type(self):
185 return False
186
187 if self.addr == other.addr:
188 return True
189
190 return self.stream == other.stream
191
192 def _copy(self, copy_func):
193 # We can always use references here because those properties are
5602ef81
SM
194 # frozen anyway if they are part of a message. Since the
195 # user cannot modify them after copying the message, it's
811644b8 196 # useless to copy/deep-copy them.
5602ef81 197 return StreamBeginningMessage(self.stream)
811644b8
PP
198
199
5f25509b 200class _StreamEndMessage(_CopyableMessage):
88f3724d 201 _TYPE = native_bt.MESSAGE_TYPE_STREAM_END
81447b5b 202
81447b5b 203 @property
811644b8 204 def stream(self):
5602ef81 205 stream_ptr = native_bt.message_stream_end_get_stream(self._ptr)
811644b8
PP
206 assert(stream_ptr)
207 return bt2.stream._create_from_ptr(stream_ptr)
208
209 def __eq__(self, other):
210 if type(other) is not type(self):
211 return False
212
213 if self.addr == other.addr:
214 return True
215
216 return self.stream == other.stream
217
218 def _copy(self, copy_func):
219 # We can always use references here because those properties are
5602ef81
SM
220 # frozen anyway if they are part of a message. Since the
221 # user cannot modify them after copying the message, it's
811644b8 222 # useless to copy/deep-copy them.
5602ef81 223 return StreamEndMessage(self.stream)
811644b8
PP
224
225
4b552f8b
SM
226class _InactivityMessageClockSnapshotsIterator(collections.abc.Iterator):
227 def __init__(self, msg_clock_snapshots):
228 self._msg_clock_snapshots = msg_clock_snapshots
229 self._clock_classes = list(msg_clock_snapshots._msg.clock_class_priority_map)
71fd6f52
PP
230 self._at = 0
231
232 def __next__(self):
233 if self._at == len(self._clock_classes):
234 raise StopIteration
235
236 self._at += 1
237 return self._clock_classes[at]
238
239
4b552f8b 240class _InactivityMessageClockSnapshots(collections.abc.Mapping):
5602ef81
SM
241 def __init__(self, msg):
242 self._msg = msg
71fd6f52
PP
243
244 def __getitem__(self, clock_class):
245 utils._check_type(clock_class, bt2.ClockClass)
4b552f8b 246 clock_snapshot_ptr = native_bt.message_inactivity_get_clock_snapshot(self._msg._ptr,
71fd6f52
PP
247 clock_class._ptr)
248
4b552f8b 249 if clock_snapshot_ptr is None:
71fd6f52
PP
250 return
251
4b552f8b
SM
252 clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr)
253 return clock_snapshot
71fd6f52 254
4b552f8b
SM
255 def add(self, clock_snapshot):
256 utils._check_type(clock_snapshot, bt2.clock_snapshot._ClockSnapshot)
257 ret = native_bt.message_inactivity_set_clock_snapshot(self._msg._ptr,
258 clock_snapshot._ptr)
5602ef81 259 utils._handle_ret(ret, "cannot set inactivity message object's clock value")
71fd6f52
PP
260
261 def __len__(self):
5602ef81 262 return len(self._msg.clock_class_priority_map)
71fd6f52
PP
263
264 def __iter__(self):
4b552f8b 265 return _InactivityMessageClockSnapshotsIterator(self)
71fd6f52
PP
266
267
5602ef81 268class InactivityMessage(_CopyableMessage):
88f3724d 269 _TYPE = native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
81447b5b 270
811644b8
PP
271 def __init__(self, cc_prio_map=None):
272 if cc_prio_map is not None:
273 utils._check_type(cc_prio_map, bt2.clock_class_priority_map.ClockClassPriorityMap)
274 cc_prio_map_ptr = cc_prio_map._ptr
275 else:
276 cc_prio_map_ptr = None
81447b5b 277
5602ef81 278 ptr = native_bt.message_inactivity_create(cc_prio_map_ptr)
81447b5b
PP
279
280 if ptr is None:
5602ef81 281 raise bt2.CreationError('cannot create inactivity message object')
81447b5b
PP
282
283 super().__init__(ptr)
284
285 @property
811644b8 286 def clock_class_priority_map(self):
5602ef81 287 cc_prio_map_ptr = native_bt.message_inactivity_get_clock_class_priority_map(self._ptr)
811644b8
PP
288 assert(cc_prio_map_ptr)
289 return bt2.clock_class_priority_map.ClockClassPriorityMap._create_from_ptr(cc_prio_map_ptr)
290
71fd6f52 291 @property
4b552f8b
SM
292 def clock_snapshots(self):
293 return _InactivityMessageClockSnapshots(self)
811644b8 294
4b552f8b
SM
295 def _get_clock_snapshots(self):
296 clock_snapshots = {}
811644b8 297
4b552f8b
SM
298 for clock_class, clock_snapshot in self.clock_snapshots.items():
299 if clock_snapshot is None:
811644b8
PP
300 continue
301
4b552f8b 302 clock_snapshots[clock_class] = clock_snapshot
811644b8 303
4b552f8b 304 return clock_snapshots
811644b8
PP
305
306 def __eq__(self, other):
307 if type(other) is not type(self):
308 return False
309
310 if self.addr == other.addr:
311 return True
312
313 self_props = (
314 self.clock_class_priority_map,
4b552f8b 315 self._get_clock_snapshots(),
811644b8
PP
316 )
317 other_props = (
318 other.clock_class_priority_map,
4b552f8b 319 other._get_clock_snapshots(),
811644b8
PP
320 )
321 return self_props == other_props
322
323 def __copy__(self):
5602ef81 324 cpy = InactivityMessage(self.clock_class_priority_map)
811644b8 325
4b552f8b
SM
326 for clock_class, clock_snapshot in self.clock_snapshots.items():
327 if clock_snapshot is None:
811644b8
PP
328 continue
329
4b552f8b 330 cpy.clock_snapshots.add(clock_snapshot)
811644b8
PP
331
332 return cpy
333
334 def __deepcopy__(self, memo):
335 cc_prio_map_cpy = copy.deepcopy(self.clock_class_priority_map)
5602ef81 336 cpy = InactivityMessage(cc_prio_map_cpy)
811644b8
PP
337
338 # copy clock values
339 for orig_clock_class in self.clock_class_priority_map:
4b552f8b 340 orig_clock_snapshot = self.clock_snapshot(orig_clock_class)
811644b8 341
4b552f8b 342 if orig_clock_snapshot is None:
811644b8
PP
343 continue
344
345 # find equivalent, copied clock class in CC priority map copy
346 for cpy_clock_class in cc_prio_map_cpy:
347 if cpy_clock_class == orig_clock_class:
348 break
349
350 # create copy of clock value from copied clock class
4b552f8b 351 clock_snapshot_cpy = cpy_clock_class(orig_clock_snapshot.cycles)
811644b8 352
5602ef81 353 # set copied clock value in message copy
4b552f8b 354 cpy.clock_snapshots.add(clock_snapshot_cpy)
811644b8
PP
355
356 memo[id(self)] = cpy
357 return cpy
358
359
5602ef81 360class _DiscardedElementsMessage(_Message):
811644b8
PP
361 def __eq__(self, other):
362 if type(other) is not type(self):
363 return False
364
365 if self.addr == other.addr:
366 return True
367
368 self_props = (
369 self.count,
370 self.stream,
4b552f8b
SM
371 self.beginning_clock_snapshot,
372 self.end_clock_snapshot,
811644b8
PP
373 )
374 other_props = (
375 other.count,
376 other.stream,
4b552f8b
SM
377 other.beginning_clock_snapshot,
378 other.end_clock_snapshot,
811644b8
PP
379 )
380 return self_props == other_props
381
382
5602ef81 383class _DiscardedPacketsMessage(_DiscardedElementsMessage):
88f3724d 384 _TYPE = native_bt.MESSAGE_TYPE_DISCARDED_PACKETS
811644b8
PP
385
386 @property
387 def count(self):
5602ef81 388 count = native_bt.message_discarded_packets_get_count(self._ptr)
811644b8
PP
389 assert(count >= 0)
390 return count
391
392 @property
393 def stream(self):
5602ef81 394 stream_ptr = native_bt.message_discarded_packets_get_stream(self._ptr)
811644b8
PP
395 assert(stream_ptr)
396 return bt2.stream._create_from_ptr(stream_ptr)
397
398 @property
4b552f8b
SM
399 def beginning_clock_snapshot(self):
400 clock_snapshot_ptr = native_bt.message_discarded_packets_get_begin_clock_snapshot(self._ptr)
811644b8 401
4b552f8b 402 if clock_snapshot_ptr is None:
811644b8
PP
403 return
404
4b552f8b
SM
405 clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr)
406 return clock_snapshot
811644b8
PP
407
408 @property
4b552f8b
SM
409 def end_clock_snapshot(self):
410 clock_snapshot_ptr = native_bt.message_discarded_packets_get_end_clock_snapshot(self._ptr)
811644b8 411
4b552f8b 412 if clock_snapshot_ptr is None:
811644b8
PP
413 return
414
4b552f8b
SM
415 clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr)
416 return clock_snapshot
811644b8
PP
417
418
5602ef81 419class _DiscardedEventsMessage(_DiscardedElementsMessage):
88f3724d 420 _TYPE = native_bt.MESSAGE_TYPE_DISCARDED_EVENTS
811644b8
PP
421
422 @property
423 def count(self):
5602ef81 424 count = native_bt.message_discarded_events_get_count(self._ptr)
811644b8
PP
425 assert(count >= 0)
426 return count
427
428 @property
429 def stream(self):
5602ef81 430 stream_ptr = native_bt.message_discarded_events_get_stream(self._ptr)
811644b8
PP
431 assert(stream_ptr)
432 return bt2.stream._create_from_ptr(stream_ptr)
433
434 @property
4b552f8b
SM
435 def beginning_clock_snapshot(self):
436 clock_snapshot_ptr = native_bt.message_discarded_events_get_begin_clock_snapshot(self._ptr)
811644b8 437
4b552f8b 438 if clock_snapshot_ptr is None:
811644b8
PP
439 return
440
4b552f8b
SM
441 clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr)
442 return clock_snapshot
811644b8
PP
443
444 @property
4b552f8b
SM
445 def end_clock_snapshot(self):
446 clock_snapshot_ptr = native_bt.message_discarded_events_get_end_clock_snapshot(self._ptr)
811644b8 447
4b552f8b 448 if clock_snapshot_ptr is None:
811644b8
PP
449 return
450
4b552f8b
SM
451 clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr)
452 return clock_snapshot
81447b5b
PP
453
454
5602ef81 455_MESSAGE_TYPE_TO_CLS = {
2ae9f48c
SM
456 native_bt.MESSAGE_TYPE_EVENT: _EventMessage,
457 native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessage,
5f25509b 458 native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessage,
2ae9f48c 459 native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessage,
5f25509b 460 native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessage,
5602ef81
SM
461 native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: InactivityMessage,
462 native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessage,
463 native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessage,
81447b5b 464}
This page took 0.059132 seconds and 4 git commands to generate.