Commit | Line | Data |
---|---|---|
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 | ||
23 | from bt2 import native_bt, object, utils | |
f192fc47 | 24 | import bt2.clock_snapshot |
8ae92ae2 | 25 | import collections |
81447b5b PP |
26 | import bt2.packet |
27 | import bt2.stream | |
28 | import bt2.event | |
f6a5e476 | 29 | import copy |
81447b5b PP |
30 | import bt2 |
31 | ||
32 | ||
33 | def _create_from_ptr(ptr): | |
fa4c33e3 | 34 | msg_type = native_bt.message_get_type(ptr) |
81447b5b PP |
35 | cls = None |
36 | ||
fa4c33e3 SM |
37 | if msg_type not in _MESSAGE_TYPE_TO_CLS: |
38 | raise bt2.Error('unknown message type: {}'.format(msg_type)) | |
81447b5b | 39 | |
fa4c33e3 | 40 | return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr) |
81447b5b PP |
41 | |
42 | ||
fa4c33e3 SM |
43 | def _msg_types_from_msg_classes(message_types): |
44 | if message_types is None: | |
45 | msg_types = None | |
26c5273a | 46 | else: |
fa4c33e3 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)) | |
26c5273a | 50 | |
fa4c33e3 | 51 | msg_types = [msg_cls._TYPE for msg_cls in message_types] |
26c5273a | 52 | |
fa4c33e3 | 53 | return msg_types |
26c5273a PP |
54 | |
55 | ||
c3044a97 | 56 | class _Message(object._SharedObject): |
27d97a3f SM |
57 | _get_ref = staticmethod(native_bt.message_get_ref) |
58 | _put_ref = staticmethod(native_bt.message_put_ref) | |
81447b5b PP |
59 | |
60 | ||
fa4c33e3 | 61 | class _CopyableMessage(_Message): |
f6a5e476 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 | ||
27d97a3f | 71 | class _EventMessage(_CopyableMessage): |
5f66202c | 72 | _TYPE = native_bt.MESSAGE_TYPE_EVENT |
f6a5e476 | 73 | |
27d97a3f 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 | |
27d97a3f 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 | |
27d97a3f | 86 | snapshot_ptr = native_bt.message_event_borrow_default_clock_snapshot_const(self._ptr) |
81447b5b | 87 | |
27d97a3f 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 | |
f6a5e476 PP |
91 | @property |
92 | def clock_class_priority_map(self): | |
fa4c33e3 | 93 | cc_prio_map_ptr = native_bt.message_event_get_clock_class_priority_map(self._ptr) |
f6a5e476 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 | |
fa4c33e3 SM |
116 | # frozen anyway if they are part of a message. Since the |
117 | # user cannot modify them after copying the message, it's | |
f6a5e476 | 118 | # useless to copy/deep-copy them. |
fa4c33e3 | 119 | return EventMessage(self.event, self.clock_class_priority_map) |
f6a5e476 PP |
120 | |
121 | ||
27d97a3f | 122 | class _PacketBeginningMessage(_CopyableMessage): |
5f66202c | 123 | _TYPE = native_bt.MESSAGE_TYPE_PACKET_BEGINNING |
81447b5b | 124 | |
81447b5b PP |
125 | @property |
126 | def packet(self): | |
fa4c33e3 | 127 | packet_ptr = native_bt.message_packet_begin_get_packet(self._ptr) |
f6a5e476 | 128 | assert(packet_ptr) |
81447b5b PP |
129 | return bt2.packet._Packet._create_from_ptr(packet_ptr) |
130 | ||
f6a5e476 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 | |
fa4c33e3 SM |
142 | # frozen anyway if they are part of a message. Since the |
143 | # user cannot modify them after copying the message, it's | |
f6a5e476 | 144 | # useless to copy/deep-copy them. |
fa4c33e3 | 145 | return PacketBeginningMessage(self.packet) |
f6a5e476 PP |
146 | |
147 | ||
871a292a | 148 | class _PacketEndMessage(_CopyableMessage): |
5f66202c | 149 | _TYPE = native_bt.MESSAGE_TYPE_PACKET_END |
81447b5b | 150 | |
81447b5b PP |
151 | @property |
152 | def packet(self): | |
fa4c33e3 | 153 | packet_ptr = native_bt.message_packet_end_get_packet(self._ptr) |
f6a5e476 | 154 | assert(packet_ptr) |
81447b5b PP |
155 | return bt2.packet._Packet._create_from_ptr(packet_ptr) |
156 | ||
f6a5e476 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 | |
fa4c33e3 SM |
168 | # frozen anyway if they are part of a message. Since the |
169 | # user cannot modify them after copying the message, it's | |
f6a5e476 | 170 | # useless to copy/deep-copy them. |
fa4c33e3 | 171 | return PacketEndMessage(self.packet) |
f6a5e476 PP |
172 | |
173 | ||
27d97a3f | 174 | class _StreamBeginningMessage(_CopyableMessage): |
5f66202c | 175 | _TYPE = native_bt.MESSAGE_TYPE_STREAM_BEGINNING |
81447b5b | 176 | |
81447b5b PP |
177 | @property |
178 | def stream(self): | |
fa4c33e3 | 179 | stream_ptr = native_bt.message_stream_begin_get_stream(self._ptr) |
f6a5e476 | 180 | assert(stream_ptr) |
81447b5b PP |
181 | return bt2.stream._create_from_ptr(stream_ptr) |
182 | ||
f6a5e476 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 | |
fa4c33e3 SM |
194 | # frozen anyway if they are part of a message. Since the |
195 | # user cannot modify them after copying the message, it's | |
f6a5e476 | 196 | # useless to copy/deep-copy them. |
fa4c33e3 | 197 | return StreamBeginningMessage(self.stream) |
f6a5e476 PP |
198 | |
199 | ||
871a292a | 200 | class _StreamEndMessage(_CopyableMessage): |
5f66202c | 201 | _TYPE = native_bt.MESSAGE_TYPE_STREAM_END |
81447b5b | 202 | |
81447b5b | 203 | @property |
f6a5e476 | 204 | def stream(self): |
fa4c33e3 | 205 | stream_ptr = native_bt.message_stream_end_get_stream(self._ptr) |
f6a5e476 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 | |
fa4c33e3 SM |
220 | # frozen anyway if they are part of a message. Since the |
221 | # user cannot modify them after copying the message, it's | |
f6a5e476 | 222 | # useless to copy/deep-copy them. |
fa4c33e3 | 223 | return StreamEndMessage(self.stream) |
f6a5e476 PP |
224 | |
225 | ||
f192fc47 SM |
226 | class _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) | |
8ae92ae2 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 | ||
f192fc47 | 240 | class _InactivityMessageClockSnapshots(collections.abc.Mapping): |
fa4c33e3 SM |
241 | def __init__(self, msg): |
242 | self._msg = msg | |
8ae92ae2 PP |
243 | |
244 | def __getitem__(self, clock_class): | |
245 | utils._check_type(clock_class, bt2.ClockClass) | |
f192fc47 | 246 | clock_snapshot_ptr = native_bt.message_inactivity_get_clock_snapshot(self._msg._ptr, |
8ae92ae2 PP |
247 | clock_class._ptr) |
248 | ||
f192fc47 | 249 | if clock_snapshot_ptr is None: |
8ae92ae2 PP |
250 | return |
251 | ||
f192fc47 SM |
252 | clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr) |
253 | return clock_snapshot | |
8ae92ae2 | 254 | |
f192fc47 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) | |
fa4c33e3 | 259 | utils._handle_ret(ret, "cannot set inactivity message object's clock value") |
8ae92ae2 PP |
260 | |
261 | def __len__(self): | |
fa4c33e3 | 262 | return len(self._msg.clock_class_priority_map) |
8ae92ae2 PP |
263 | |
264 | def __iter__(self): | |
f192fc47 | 265 | return _InactivityMessageClockSnapshotsIterator(self) |
8ae92ae2 PP |
266 | |
267 | ||
fa4c33e3 | 268 | class InactivityMessage(_CopyableMessage): |
5f66202c | 269 | _TYPE = native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY |
81447b5b | 270 | |
f6a5e476 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 | |
fa4c33e3 | 278 | ptr = native_bt.message_inactivity_create(cc_prio_map_ptr) |
81447b5b PP |
279 | |
280 | if ptr is None: | |
fa4c33e3 | 281 | raise bt2.CreationError('cannot create inactivity message object') |
81447b5b PP |
282 | |
283 | super().__init__(ptr) | |
284 | ||
285 | @property | |
f6a5e476 | 286 | def clock_class_priority_map(self): |
fa4c33e3 | 287 | cc_prio_map_ptr = native_bt.message_inactivity_get_clock_class_priority_map(self._ptr) |
f6a5e476 PP |
288 | assert(cc_prio_map_ptr) |
289 | return bt2.clock_class_priority_map.ClockClassPriorityMap._create_from_ptr(cc_prio_map_ptr) | |
290 | ||
8ae92ae2 | 291 | @property |
f192fc47 SM |
292 | def clock_snapshots(self): |
293 | return _InactivityMessageClockSnapshots(self) | |
f6a5e476 | 294 | |
f192fc47 SM |
295 | def _get_clock_snapshots(self): |
296 | clock_snapshots = {} | |
f6a5e476 | 297 | |
f192fc47 SM |
298 | for clock_class, clock_snapshot in self.clock_snapshots.items(): |
299 | if clock_snapshot is None: | |
f6a5e476 PP |
300 | continue |
301 | ||
f192fc47 | 302 | clock_snapshots[clock_class] = clock_snapshot |
f6a5e476 | 303 | |
f192fc47 | 304 | return clock_snapshots |
f6a5e476 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, | |
f192fc47 | 315 | self._get_clock_snapshots(), |
f6a5e476 PP |
316 | ) |
317 | other_props = ( | |
318 | other.clock_class_priority_map, | |
f192fc47 | 319 | other._get_clock_snapshots(), |
f6a5e476 PP |
320 | ) |
321 | return self_props == other_props | |
322 | ||
323 | def __copy__(self): | |
fa4c33e3 | 324 | cpy = InactivityMessage(self.clock_class_priority_map) |
f6a5e476 | 325 | |
f192fc47 SM |
326 | for clock_class, clock_snapshot in self.clock_snapshots.items(): |
327 | if clock_snapshot is None: | |
f6a5e476 PP |
328 | continue |
329 | ||
f192fc47 | 330 | cpy.clock_snapshots.add(clock_snapshot) |
f6a5e476 PP |
331 | |
332 | return cpy | |
333 | ||
334 | def __deepcopy__(self, memo): | |
335 | cc_prio_map_cpy = copy.deepcopy(self.clock_class_priority_map) | |
fa4c33e3 | 336 | cpy = InactivityMessage(cc_prio_map_cpy) |
f6a5e476 PP |
337 | |
338 | # copy clock values | |
339 | for orig_clock_class in self.clock_class_priority_map: | |
f192fc47 | 340 | orig_clock_snapshot = self.clock_snapshot(orig_clock_class) |
f6a5e476 | 341 | |
f192fc47 | 342 | if orig_clock_snapshot is None: |
f6a5e476 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 | |
f192fc47 | 351 | clock_snapshot_cpy = cpy_clock_class(orig_clock_snapshot.cycles) |
f6a5e476 | 352 | |
fa4c33e3 | 353 | # set copied clock value in message copy |
f192fc47 | 354 | cpy.clock_snapshots.add(clock_snapshot_cpy) |
f6a5e476 PP |
355 | |
356 | memo[id(self)] = cpy | |
357 | return cpy | |
358 | ||
359 | ||
fa4c33e3 | 360 | class _DiscardedElementsMessage(_Message): |
f6a5e476 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, | |
f192fc47 SM |
371 | self.beginning_clock_snapshot, |
372 | self.end_clock_snapshot, | |
f6a5e476 PP |
373 | ) |
374 | other_props = ( | |
375 | other.count, | |
376 | other.stream, | |
f192fc47 SM |
377 | other.beginning_clock_snapshot, |
378 | other.end_clock_snapshot, | |
f6a5e476 PP |
379 | ) |
380 | return self_props == other_props | |
381 | ||
382 | ||
fa4c33e3 | 383 | class _DiscardedPacketsMessage(_DiscardedElementsMessage): |
5f66202c | 384 | _TYPE = native_bt.MESSAGE_TYPE_DISCARDED_PACKETS |
f6a5e476 PP |
385 | |
386 | @property | |
387 | def count(self): | |
fa4c33e3 | 388 | count = native_bt.message_discarded_packets_get_count(self._ptr) |
f6a5e476 PP |
389 | assert(count >= 0) |
390 | return count | |
391 | ||
392 | @property | |
393 | def stream(self): | |
fa4c33e3 | 394 | stream_ptr = native_bt.message_discarded_packets_get_stream(self._ptr) |
f6a5e476 PP |
395 | assert(stream_ptr) |
396 | return bt2.stream._create_from_ptr(stream_ptr) | |
397 | ||
398 | @property | |
f192fc47 SM |
399 | def beginning_clock_snapshot(self): |
400 | clock_snapshot_ptr = native_bt.message_discarded_packets_get_begin_clock_snapshot(self._ptr) | |
f6a5e476 | 401 | |
f192fc47 | 402 | if clock_snapshot_ptr is None: |
f6a5e476 PP |
403 | return |
404 | ||
f192fc47 SM |
405 | clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr) |
406 | return clock_snapshot | |
f6a5e476 PP |
407 | |
408 | @property | |
f192fc47 SM |
409 | def end_clock_snapshot(self): |
410 | clock_snapshot_ptr = native_bt.message_discarded_packets_get_end_clock_snapshot(self._ptr) | |
f6a5e476 | 411 | |
f192fc47 | 412 | if clock_snapshot_ptr is None: |
f6a5e476 PP |
413 | return |
414 | ||
f192fc47 SM |
415 | clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr) |
416 | return clock_snapshot | |
f6a5e476 PP |
417 | |
418 | ||
fa4c33e3 | 419 | class _DiscardedEventsMessage(_DiscardedElementsMessage): |
5f66202c | 420 | _TYPE = native_bt.MESSAGE_TYPE_DISCARDED_EVENTS |
f6a5e476 PP |
421 | |
422 | @property | |
423 | def count(self): | |
fa4c33e3 | 424 | count = native_bt.message_discarded_events_get_count(self._ptr) |
f6a5e476 PP |
425 | assert(count >= 0) |
426 | return count | |
427 | ||
428 | @property | |
429 | def stream(self): | |
fa4c33e3 | 430 | stream_ptr = native_bt.message_discarded_events_get_stream(self._ptr) |
f6a5e476 PP |
431 | assert(stream_ptr) |
432 | return bt2.stream._create_from_ptr(stream_ptr) | |
433 | ||
434 | @property | |
f192fc47 SM |
435 | def beginning_clock_snapshot(self): |
436 | clock_snapshot_ptr = native_bt.message_discarded_events_get_begin_clock_snapshot(self._ptr) | |
f6a5e476 | 437 | |
f192fc47 | 438 | if clock_snapshot_ptr is None: |
f6a5e476 PP |
439 | return |
440 | ||
f192fc47 SM |
441 | clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr) |
442 | return clock_snapshot | |
f6a5e476 PP |
443 | |
444 | @property | |
f192fc47 SM |
445 | def end_clock_snapshot(self): |
446 | clock_snapshot_ptr = native_bt.message_discarded_events_get_end_clock_snapshot(self._ptr) | |
f6a5e476 | 447 | |
f192fc47 | 448 | if clock_snapshot_ptr is None: |
f6a5e476 PP |
449 | return |
450 | ||
f192fc47 SM |
451 | clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr) |
452 | return clock_snapshot | |
81447b5b PP |
453 | |
454 | ||
fa4c33e3 | 455 | _MESSAGE_TYPE_TO_CLS = { |
27d97a3f SM |
456 | native_bt.MESSAGE_TYPE_EVENT: _EventMessage, |
457 | native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessage, | |
871a292a | 458 | native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessage, |
27d97a3f | 459 | native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessage, |
871a292a | 460 | native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessage, |
fa4c33e3 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 | } |