lib: Make graph listeners return an error status
[babeltrace.git] / bindings / python / bt2 / bt2 / graph.py
CommitLineData
811644b8
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
24import bt2.connection
25import bt2.component
26import functools
27import bt2.port
28import bt2
29
30
31class GraphListenerType:
32 PORT_ADDED = 0
33 PORT_REMOVED = 1
34 PORTS_CONNECTED = 2
35 PORTS_DISCONNECTED = 3
36
37
38def _graph_port_added_listener_from_native(user_listener, port_ptr):
39 try:
40 port = bt2.port._create_from_ptr(port_ptr)
41 port._get()
42 user_listener(port)
43 except:
44 pass
45
46
47def _graph_port_removed_listener_from_native(user_listener, port_ptr):
48 try:
49 port = bt2.port._create_from_ptr(port_ptr)
50 port._get()
51 user_listener(port)
52 except:
53 pass
54
55
56def _graph_ports_connected_listener_from_native(user_listener,
57 upstream_port_ptr,
58 downstream_port_ptr):
59 try:
60 upstream_port = bt2.port._create_from_ptr(upstream_port_ptr)
61 upstream_port._get()
62 downstream_port = bt2.port._create_from_ptr(downstream_port_ptr)
63 downstream_port._get()
64 user_listener(upstream_port, downstream_port)
65 except:
66 pass
67
68
69def _graph_ports_disconnected_listener_from_native(user_listener,
70 upstream_comp_ptr,
71 downstream_comp_ptr,
72 upstream_port_ptr,
73 downstream_port_ptr):
74 try:
75 upstream_comp = bt2.component._create_generic_component_from_ptr(upstream_comp_ptr)
76 upstream_comp._get()
77 downstream_comp = bt2.component._create_generic_component_from_ptr(downstream_comp_ptr)
78 downstream_comp._get()
79 upstream_port = bt2.port._create_from_ptr(upstream_port_ptr)
80 upstream_port._get()
81 downstream_port = bt2.port._create_from_ptr(downstream_port_ptr)
82 downstream_port._get()
83 user_listener(upstream_comp, downstream_comp, upstream_port,
84 downstream_port)
85 except:
86 pass
87
88
78288f58 89class Graph(object._SharedObject):
2f16a6a2
PP
90 _get_ref = staticmethod(native_bt.graph_get_ref)
91 _put_ref = staticmethod(native_bt.graph_put_ref)
601c0026 92
811644b8
PP
93 def __init__(self):
94 ptr = native_bt.graph_create()
95
96 if ptr is None:
97 raise bt2.CreationError('cannot create graph object')
98
99 super().__init__(ptr)
100
101 def _handle_status(self, status, gen_error_msg):
102 if status == native_bt.GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
103 raise bt2.PortConnectionRefused
104 elif status == native_bt.GRAPH_STATUS_CANCELED:
105 raise bt2.GraphCanceled
106 elif status == native_bt.GRAPH_STATUS_END:
107 raise bt2.Stop
108 elif status == native_bt.GRAPH_STATUS_AGAIN:
109 raise bt2.TryAgain
811644b8
PP
110 elif status < 0:
111 raise bt2.Error(gen_error_msg)
112
894a8df5
SM
113 def add_component(self, component_class, name, params=None):
114 if issubclass(component_class, bt2.component._UserSourceComponent):
811644b8 115 cc_ptr = component_class._cc_ptr
894a8df5
SM
116 add_fn = native_bt.graph_add_source_component
117 cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
118 elif issubclass(component_class, bt2.component._UserFilterComponent):
119 cc_ptr = component_class._cc_ptr
120 add_fn = native_bt.graph_add_filter_component
121 cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
122 elif issubclass(component_class, bt2.component._UserSinkComponent):
123 cc_ptr = component_class._cc_ptr
124 add_fn = native_bt.graph_add_sink_component
125 cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK
811644b8 126 else:
894a8df5 127 raise TypeError("'{}' is not a component class".format(
601c0026 128 component_class.__class__.__name__))
811644b8
PP
129
130 utils._check_str(name)
131 params = bt2.create_value(params)
132
601c0026 133 params_ptr = params._ptr if params is not None else None
811644b8 134
894a8df5
SM
135 status, comp_ptr = add_fn(self._ptr, cc_ptr, name, params_ptr)
136 self._handle_status(status, 'cannot add component to graph')
137 assert comp_ptr
138 return bt2.component._create_component_from_ptr(comp_ptr, cc_type)
811644b8
PP
139
140 def connect_ports(self, upstream_port, downstream_port):
141 utils._check_type(upstream_port, bt2.port._OutputPort)
142 utils._check_type(downstream_port, bt2.port._InputPort)
143 status, conn_ptr = native_bt.graph_connect_ports(self._ptr,
144 upstream_port._ptr,
145 downstream_port._ptr)
146 self._handle_status(status, 'cannot connect component ports within graph')
147 assert(conn_ptr)
148 return bt2.connection._Connection._create_from_ptr(conn_ptr)
149
150 def add_listener(self, listener_type, listener):
151 if not hasattr(listener, '__call__'):
152 raise TypeError("'listener' parameter is not callable")
153
154 if listener_type == GraphListenerType.PORT_ADDED:
155 fn = native_bt.py3_graph_add_port_added_listener
156 listener_from_native = functools.partial(_graph_port_added_listener_from_native,
157 listener)
158 elif listener_type == GraphListenerType.PORT_REMOVED:
159 fn = native_bt.py3_graph_add_port_removed_listener
160 listener_from_native = functools.partial(_graph_port_removed_listener_from_native,
161 listener)
162 elif listener_type == GraphListenerType.PORTS_CONNECTED:
163 fn = native_bt.py3_graph_add_ports_connected_listener
164 listener_from_native = functools.partial(_graph_ports_connected_listener_from_native,
165 listener)
166 elif listener_type == GraphListenerType.PORTS_DISCONNECTED:
167 fn = native_bt.py3_graph_add_ports_disconnected_listener
168 listener_from_native = functools.partial(_graph_ports_disconnected_listener_from_native,
169 listener)
170 else:
171 raise TypeError
172
173 listener_id = fn(self._ptr, listener_from_native)
174 utils._handle_ret(listener_id, 'cannot add listener to graph object')
175 return bt2._ListenerHandle(listener_id, self)
176
177 def run(self):
178 status = native_bt.graph_run(self._ptr)
179
180 if status == native_bt.GRAPH_STATUS_END:
181 return
182
183 self._handle_status(status, 'graph object stopped running because of an unexpected error')
184
185 def cancel(self):
186 status = native_bt.graph_cancel(self._ptr)
187 self._handle_status(status, 'cannot cancel graph object')
188
189 @property
190 def is_canceled(self):
191 is_canceled = native_bt.graph_is_canceled(self._ptr)
192 assert(is_canceled >= 0)
193 return is_canceled > 0
194
2ae9f48c
SM
195 def create_output_port_message_iterator(self, output_port):
196 utils._check_type(output_port, bt2.port._OutputPort)
197 msg_iter_ptr = native_bt.port_output_message_iterator_create(self._ptr, output_port._ptr)
198
199 if msg_iter_ptr is None:
200 raise bt2.CreationError('cannot create output port message iterator')
201
202 return bt2.message_iterator._OutputPortMessageIterator(msg_iter_ptr)
203
811644b8
PP
204 def __eq__(self, other):
205 if type(other) is not type(self):
206 return False
207
208 return self.addr == other.addr
This page took 0.037301 seconds and 4 git commands to generate.