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