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