test_error.py: remove dangling print()
[babeltrace.git] / src / 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
81447b5b
PP
25import bt2.packet
26import bt2.stream
27import bt2.event
28import bt2
29
30
31def _create_from_ptr(ptr):
5602ef81 32 msg_type = native_bt.message_get_type(ptr)
5602ef81 33 return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr)
81447b5b
PP
34
35
78288f58 36class _Message(object._SharedObject):
2ae9f48c
SM
37 _get_ref = staticmethod(native_bt.message_get_ref)
38 _put_ref = staticmethod(native_bt.message_put_ref)
81447b5b 39
2e90378a
PP
40 @staticmethod
41 def _check_has_default_clock_class(clock_class):
42 if clock_class is None:
1153eccb 43 raise ValueError(
cfbd7cf3
FD
44 'cannot get default clock snapshot: stream class has no default clock class'
45 )
2e90378a 46
81447b5b 47
9ec609ec
SM
48class _MessageWithDefaultClockSnapshot:
49 def _get_default_clock_snapshot(self, borrow_clock_snapshot_ptr):
9ec609ec 50 snapshot_ptr = borrow_clock_snapshot_ptr(self._ptr)
811644b8 51
9ec609ec 52 return bt2.clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref(
cfbd7cf3
FD
53 snapshot_ptr, self._ptr, self._get_ref, self._put_ref
54 )
811644b8 55
9ec609ec
SM
56
57class _EventMessage(_Message, _MessageWithDefaultClockSnapshot):
cfbd7cf3
FD
58 _borrow_default_clock_snapshot_ptr = staticmethod(
59 native_bt.message_event_borrow_default_clock_snapshot_const
60 )
9ec609ec 61
9ec609ec
SM
62 @property
63 def default_clock_snapshot(self):
26fc5aed 64 self._check_has_default_clock_class(self.event.stream.cls.default_clock_class)
9ec609ec 65 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
811644b8 66
2ae9f48c
SM
67 @property
68 def event(self):
69 event_ptr = native_bt.message_event_borrow_event(self._ptr)
70 assert event_ptr is not None
71 return bt2.event._Event._create_from_ptr_and_get_ref(
cfbd7cf3
FD
72 event_ptr, self._ptr, self._get_ref, self._put_ref
73 )
81447b5b 74
81447b5b 75
9ec609ec 76class _PacketMessage(_Message, _MessageWithDefaultClockSnapshot):
81447b5b 77 @property
9ec609ec 78 def default_clock_snapshot(self):
e8ac1aae 79 self._check_has_default_clock_class(self.packet.stream.cls.default_clock_class)
9ec609ec 80 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
81447b5b 81
81447b5b
PP
82 @property
83 def packet(self):
9ec609ec
SM
84 packet_ptr = self._borrow_packet_ptr(self._ptr)
85 assert packet_ptr is not None
86 return bt2.packet._Packet._create_from_ptr_and_get_ref(packet_ptr)
811644b8 87
811644b8 88
9ec609ec
SM
89class _PacketBeginningMessage(_PacketMessage):
90 _borrow_packet_ptr = staticmethod(native_bt.message_packet_beginning_borrow_packet)
cfbd7cf3
FD
91 _borrow_default_clock_snapshot_ptr = staticmethod(
92 native_bt.message_packet_beginning_borrow_default_clock_snapshot_const
93 )
811644b8 94
811644b8 95
9ec609ec
SM
96class _PacketEndMessage(_PacketMessage):
97 _borrow_packet_ptr = staticmethod(native_bt.message_packet_end_borrow_packet)
cfbd7cf3
FD
98 _borrow_default_clock_snapshot_ptr = staticmethod(
99 native_bt.message_packet_end_borrow_default_clock_snapshot_const
100 )
811644b8 101
81447b5b 102
188edac1 103class _StreamMessage(_Message, _MessageWithDefaultClockSnapshot):
81447b5b
PP
104 @property
105 def stream(self):
9ec609ec
SM
106 stream_ptr = self._borrow_stream_ptr(self._ptr)
107 assert stream_ptr
108 return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr)
811644b8 109
81447b5b 110 @property
9ec609ec 111 def default_clock_snapshot(self):
188edac1
SM
112 self._check_has_default_clock_class(self.stream.cls.default_clock_class)
113
9ec609ec 114 status, snapshot_ptr = self._borrow_default_clock_snapshot_ptr(self._ptr)
81447b5b 115
188edac1 116 if status == native_bt.MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN:
c6af194f 117 return bt2.clock_snapshot._UnknownClockSnapshot()
81447b5b 118
188edac1 119 return bt2.clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref(
cfbd7cf3
FD
120 snapshot_ptr, self._ptr, self._get_ref, self._put_ref
121 )
811644b8 122
188edac1
SM
123 def _default_clock_snapshot(self, raw_value):
124 utils._check_uint64(raw_value)
125 self._set_default_clock_snapshot(self._ptr, raw_value)
811644b8 126
188edac1 127 _default_clock_snapshot = property(fset=_default_clock_snapshot)
811644b8 128
811644b8 129
188edac1
SM
130class _StreamBeginningMessage(_StreamMessage):
131 _borrow_stream_ptr = staticmethod(native_bt.message_stream_beginning_borrow_stream)
cfbd7cf3
FD
132 _borrow_default_clock_snapshot_ptr = staticmethod(
133 native_bt.message_stream_beginning_borrow_default_clock_snapshot_const
134 )
135 _set_default_clock_snapshot = staticmethod(
136 native_bt.message_stream_beginning_set_default_clock_snapshot
137 )
811644b8 138
188edac1
SM
139
140class _StreamEndMessage(_StreamMessage):
141 _borrow_stream_ptr = staticmethod(native_bt.message_stream_end_borrow_stream)
cfbd7cf3
FD
142 _borrow_default_clock_snapshot_ptr = staticmethod(
143 native_bt.message_stream_end_borrow_default_clock_snapshot_const
144 )
145 _set_default_clock_snapshot = staticmethod(
146 native_bt.message_stream_end_set_default_clock_snapshot
147 )
811644b8
PP
148
149
9ec609ec 150class _MessageIteratorInactivityMessage(_Message, _MessageWithDefaultClockSnapshot):
cfbd7cf3
FD
151 _borrow_default_clock_snapshot_ptr = staticmethod(
152 native_bt.message_message_iterator_inactivity_borrow_default_clock_snapshot_const
153 )
811644b8
PP
154
155 @property
9ec609ec 156 def default_clock_snapshot(self):
2e90378a
PP
157 # This kind of message always has a default clock class: no
158 # need to call self._check_has_default_clock_class() here.
9ec609ec
SM
159 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
160
811644b8 161
9ec609ec 162class _DiscardedMessage(_Message, _MessageWithDefaultClockSnapshot):
811644b8
PP
163 @property
164 def stream(self):
9ec609ec
SM
165 stream_ptr = self._borrow_stream_ptr(self._ptr)
166 assert stream_ptr
167 return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr)
811644b8 168
811644b8
PP
169 @property
170 def count(self):
9ec609ec
SM
171 avail, count = self._get_count(self._ptr)
172 if avail is native_bt.PROPERTY_AVAILABILITY_AVAILABLE:
173 return count
811644b8 174
9ec609ec
SM
175 def _set_count(self, count):
176 utils._check_uint64(count)
177 self._set_count(self._ptr, count)
178
179 _count = property(fset=_set_count)
811644b8 180
2e90378a
PP
181 def _check_has_default_clock_snapshots(self):
182 if not self._has_default_clock_snapshots:
1153eccb 183 raise ValueError(
cfbd7cf3
FD
184 'cannot get default clock snapshot: such a message has no clock snapshots for this stream class'
185 )
2e90378a 186
811644b8 187 @property
9ec609ec 188 def beginning_default_clock_snapshot(self):
2e90378a 189 self._check_has_default_clock_snapshots()
cfbd7cf3
FD
190 return self._get_default_clock_snapshot(
191 self._borrow_beginning_clock_snapshot_ptr
192 )
811644b8 193
9ec609ec
SM
194 @property
195 def end_default_clock_snapshot(self):
2e90378a 196 self._check_has_default_clock_snapshots()
9ec609ec 197 return self._get_default_clock_snapshot(self._borrow_end_clock_snapshot_ptr)
811644b8 198
811644b8 199
9ec609ec 200class _DiscardedEventsMessage(_DiscardedMessage):
cfbd7cf3
FD
201 _borrow_stream_ptr = staticmethod(
202 native_bt.message_discarded_events_borrow_stream_const
203 )
9ec609ec
SM
204 _get_count = staticmethod(native_bt.message_discarded_events_get_count)
205 _set_count = staticmethod(native_bt.message_discarded_events_set_count)
cfbd7cf3
FD
206 _borrow_beginning_clock_snapshot_ptr = staticmethod(
207 native_bt.message_discarded_events_borrow_beginning_default_clock_snapshot_const
208 )
209 _borrow_end_clock_snapshot_ptr = staticmethod(
210 native_bt.message_discarded_events_borrow_end_default_clock_snapshot_const
211 )
811644b8 212
2e90378a
PP
213 @property
214 def _has_default_clock_snapshots(self):
e8ac1aae 215 return self.stream.cls.discarded_events_have_default_clock_snapshots
2e90378a 216
811644b8 217
9ec609ec 218class _DiscardedPacketsMessage(_DiscardedMessage):
cfbd7cf3
FD
219 _borrow_stream_ptr = staticmethod(
220 native_bt.message_discarded_packets_borrow_stream_const
221 )
9ec609ec
SM
222 _get_count = staticmethod(native_bt.message_discarded_packets_get_count)
223 _set_count = staticmethod(native_bt.message_discarded_packets_set_count)
cfbd7cf3
FD
224 _borrow_beginning_clock_snapshot_ptr = staticmethod(
225 native_bt.message_discarded_packets_borrow_beginning_default_clock_snapshot_const
226 )
227 _borrow_end_clock_snapshot_ptr = staticmethod(
228 native_bt.message_discarded_packets_borrow_end_default_clock_snapshot_const
229 )
81447b5b 230
2e90378a
PP
231 @property
232 def _has_default_clock_snapshots(self):
e8ac1aae 233 return self.stream.cls.discarded_packets_have_default_clock_snapshots
2e90378a 234
81447b5b 235
5602ef81 236_MESSAGE_TYPE_TO_CLS = {
2ae9f48c 237 native_bt.MESSAGE_TYPE_EVENT: _EventMessage,
9ec609ec 238 native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: _MessageIteratorInactivityMessage,
2ae9f48c 239 native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessage,
5f25509b 240 native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessage,
9ec609ec
SM
241 native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessage,
242 native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessage,
5602ef81 243 native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessage,
9ec609ec 244 native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessage,
81447b5b 245}
This page took 0.054803 seconds and 4 git commands to generate.