Commit | Line | Data |
---|---|---|
5fa16feb PP |
1 | /* |
2 | * The MIT License (MIT) | |
3 | * | |
4 | * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
5fa16feb PP |
25 | static bt_graph_listener_func_status port_added_listener( |
26 | const void *component, | |
27 | swig_type_info *component_swig_type, | |
28 | bt_component_class_type component_class_type, | |
29 | const void *port, | |
30 | swig_type_info *port_swig_type, | |
31 | bt_port_type port_type, | |
32 | void *py_callable) | |
33 | { | |
34 | PyObject *py_component_ptr = NULL; | |
35 | PyObject *py_port_ptr = NULL; | |
36 | PyObject *py_res = NULL; | |
37 | bt_graph_listener_func_status status; | |
38 | ||
39 | py_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component), component_swig_type, 0); | |
40 | if (!py_component_ptr) { | |
41 | BT_LOGF_STR("Failed to create component SWIG pointer object."); | |
42 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
43 | goto end; | |
44 | } | |
45 | ||
46 | py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port), port_swig_type, 0); | |
47 | if (!py_port_ptr) { | |
48 | BT_LOGF_STR("Failed to create port SWIG pointer object."); | |
49 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
50 | goto end; | |
51 | } | |
52 | ||
53 | py_res = PyObject_CallFunction(py_callable, "(OiOi)", | |
54 | py_component_ptr, component_class_type, py_port_ptr, port_type); | |
55 | if (!py_res) { | |
c7cc9a2a | 56 | loge_exception_append_cause_clear( |
27ba49f1 | 57 | "Graph's port added listener (Python)", |
5fa16feb | 58 | BT_LOG_OUTPUT_LEVEL); |
5fa16feb PP |
59 | status = __BT_FUNC_STATUS_ERROR; |
60 | goto end; | |
61 | } | |
62 | ||
63 | BT_ASSERT(py_res == Py_None); | |
64 | status = __BT_FUNC_STATUS_OK; | |
65 | ||
66 | end: | |
67 | Py_XDECREF(py_res); | |
68 | Py_XDECREF(py_port_ptr); | |
69 | Py_XDECREF(py_component_ptr); | |
70 | return status; | |
71 | } | |
72 | ||
73 | static | |
74 | bt_graph_listener_func_status | |
75 | source_component_output_port_added_listener(const bt_component_source *component_source, | |
76 | const bt_port_output *port_output, void *py_callable) | |
77 | { | |
78 | return port_added_listener( | |
79 | component_source, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE, | |
80 | port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); | |
81 | } | |
82 | ||
83 | static | |
84 | bt_graph_listener_func_status | |
85 | filter_component_input_port_added_listener(const bt_component_filter *component_filter, | |
86 | const bt_port_input *port_input, void *py_callable) | |
87 | { | |
88 | return port_added_listener( | |
89 | component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, | |
90 | port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable); | |
91 | } | |
92 | ||
93 | static | |
94 | bt_graph_listener_func_status | |
95 | filter_component_output_port_added_listener(const bt_component_filter *component_filter, | |
96 | const bt_port_output *port_output, void *py_callable) | |
97 | { | |
98 | return port_added_listener( | |
99 | component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, | |
100 | port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); | |
101 | } | |
102 | ||
103 | static | |
104 | bt_graph_listener_func_status | |
105 | sink_component_input_port_added_listener(const bt_component_sink *component_sink, | |
106 | const bt_port_input *port_input, void *py_callable) | |
107 | { | |
108 | return port_added_listener( | |
109 | component_sink, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK, | |
110 | port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable); | |
111 | } | |
112 | ||
113 | static | |
114 | PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, | |
115 | PyObject *py_callable) | |
116 | { | |
117 | PyObject *py_listener_ids = NULL; | |
118 | PyObject *py_listener_id = NULL; | |
119 | bt_listener_id listener_id; | |
120 | bt_graph_add_listener_status status; | |
121 | const char * const module_name = | |
122 | "graph_add_port_added_listener() (Python)"; | |
123 | ||
124 | BT_ASSERT(graph); | |
125 | BT_ASSERT(py_callable); | |
126 | ||
127 | /* | |
128 | * Behind the scene, we will be registering 4 different listeners and | |
129 | * return all of their ids. | |
130 | */ | |
131 | py_listener_ids = PyTuple_New(4); | |
132 | if (!py_listener_ids) { | |
133 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
134 | "Failed to allocate one PyTuple."); | |
135 | goto error; | |
136 | } | |
137 | ||
138 | /* source output port */ | |
139 | status = bt_graph_add_source_component_output_port_added_listener( | |
140 | graph, source_component_output_port_added_listener, | |
fecbdb3e | 141 | py_callable, &listener_id); |
5fa16feb PP |
142 | if (status != __BT_FUNC_STATUS_OK) { |
143 | /* | |
144 | * bt_graph_add_source_component_output_port_added_listener has | |
145 | * already logged/appended an error cause. | |
146 | */ | |
147 | goto error; | |
148 | } | |
149 | ||
150 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); | |
151 | if (!py_listener_id) { | |
152 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
153 | "Failed to allocate one PyLong."); | |
154 | goto error; | |
155 | } | |
156 | ||
157 | PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id); | |
158 | py_listener_id = NULL; | |
159 | ||
160 | /* filter input port */ | |
161 | status = bt_graph_add_filter_component_input_port_added_listener( | |
162 | graph, filter_component_input_port_added_listener, | |
fecbdb3e | 163 | py_callable, &listener_id); |
5fa16feb PP |
164 | if (status != __BT_FUNC_STATUS_OK) { |
165 | /* | |
166 | * bt_graph_add_filter_component_input_port_added_listener has | |
167 | * already logged/appended an error cause. | |
168 | */ | |
169 | goto error; | |
170 | } | |
171 | ||
172 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); | |
173 | if (!py_listener_id) { | |
174 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
175 | "Failed to allocate one PyLong."); | |
176 | goto error; | |
177 | } | |
178 | ||
179 | PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id); | |
180 | py_listener_id = NULL; | |
181 | ||
182 | /* filter output port */ | |
183 | status = bt_graph_add_filter_component_output_port_added_listener( | |
184 | graph, filter_component_output_port_added_listener, | |
fecbdb3e | 185 | py_callable, &listener_id); |
5fa16feb PP |
186 | if (status != __BT_FUNC_STATUS_OK) { |
187 | /* | |
188 | * bt_graph_add_filter_component_output_port_added_listener has | |
189 | * already logged/appended an error cause. | |
190 | */ | |
191 | goto error; | |
192 | } | |
193 | ||
194 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); | |
195 | if (!py_listener_id) { | |
196 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
197 | "Failed to allocate one PyLong."); | |
198 | goto error; | |
199 | } | |
200 | ||
201 | PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id); | |
202 | py_listener_id = NULL; | |
203 | ||
204 | /* sink input port */ | |
205 | status = bt_graph_add_sink_component_input_port_added_listener( | |
206 | graph, sink_component_input_port_added_listener, | |
fecbdb3e | 207 | py_callable, &listener_id); |
5fa16feb PP |
208 | if (status != __BT_FUNC_STATUS_OK) { |
209 | /* | |
210 | * bt_graph_add_sink_component_input_port_added_listener has | |
211 | * already logged/appended an error cause. | |
212 | */ | |
213 | goto error; | |
214 | } | |
215 | ||
216 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); | |
217 | if (!py_listener_id) { | |
218 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
219 | "Failed to allocate one PyLong."); | |
220 | goto error; | |
221 | } | |
222 | ||
5fa16feb PP |
223 | PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id); |
224 | py_listener_id = NULL; | |
5fa16feb PP |
225 | goto end; |
226 | ||
227 | error: | |
228 | Py_XDECREF(py_listener_ids); | |
229 | py_listener_ids = Py_None; | |
230 | Py_INCREF(py_listener_ids); | |
231 | ||
232 | end: | |
5fa16feb PP |
233 | Py_XDECREF(py_listener_id); |
234 | return py_listener_ids; | |
235 | } | |
236 | ||
b20382e2 PP |
237 | static |
238 | bt_graph_add_component_status | |
239 | bt_bt2_graph_add_source_component( | |
240 | bt_graph *graph, | |
241 | const bt_component_class_source *component_class, | |
242 | const char *name, const bt_value *params, | |
243 | PyObject *obj, bt_logging_level log_level, | |
244 | const bt_component_source **component) | |
245 | { | |
4175c1d5 | 246 | return bt_graph_add_source_component_with_initialize_method_data(graph, |
b20382e2 PP |
247 | component_class, name, params, obj == Py_None ? NULL : obj, |
248 | log_level, component); | |
249 | } | |
250 | ||
251 | static | |
252 | bt_graph_add_component_status | |
253 | bt_bt2_graph_add_filter_component( | |
254 | bt_graph *graph, | |
255 | const bt_component_class_filter *component_class, | |
256 | const char *name, const bt_value *params, | |
257 | PyObject *obj, bt_logging_level log_level, | |
258 | const bt_component_filter **component) | |
259 | { | |
4175c1d5 | 260 | return bt_graph_add_filter_component_with_initialize_method_data(graph, |
b20382e2 PP |
261 | component_class, name, params, obj == Py_None ? NULL : obj, |
262 | log_level, component); | |
263 | } | |
264 | ||
265 | static | |
266 | bt_graph_add_component_status | |
267 | bt_bt2_graph_add_sink_component( | |
268 | bt_graph *graph, | |
269 | const bt_component_class_sink *component_class, | |
270 | const char *name, const bt_value *params, | |
271 | PyObject *obj, bt_logging_level log_level, | |
272 | const bt_component_sink **component) | |
273 | { | |
4175c1d5 | 274 | return bt_graph_add_sink_component_with_initialize_method_data(graph, |
b20382e2 PP |
275 | component_class, name, params, obj == Py_None ? NULL : obj, |
276 | log_level, component); | |
277 | } |