Commit | Line | Data |
---|---|---|
6fa6bfa1 SM |
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 | ||
fb25b9e3 | 25 | %include <babeltrace2/graph/component-class.h> |
e4b56bb9 SM |
26 | %include <babeltrace2/graph/component-class-const.h> |
27 | %include <babeltrace2/graph/component-class-source-const.h> | |
28 | %include <babeltrace2/graph/component-class-source.h> | |
29 | %include <babeltrace2/graph/component-class-filter-const.h> | |
30 | %include <babeltrace2/graph/component-class-filter.h> | |
31 | %include <babeltrace2/graph/component-class-sink-const.h> | |
32 | %include <babeltrace2/graph/component-class-sink.h> | |
33 | %include <babeltrace2/graph/self-component-class-source.h> | |
34 | %include <babeltrace2/graph/self-component-class-filter.h> | |
35 | %include <babeltrace2/graph/self-component-class-sink.h> | |
6fa6bfa1 SM |
36 | |
37 | %{ | |
38 | /* | |
39 | * This hash table associates a BT component class object address to a | |
40 | * user-defined Python class (PyObject *). The keys and values are NOT | |
41 | * owned by this hash table. The Python class objects are owned by the | |
42 | * Python module, which should not be unloaded until it is not possible | |
43 | * to create a user Python component anyway. | |
44 | * | |
45 | * This hash table is written to when a user-defined Python component | |
fb25b9e3 | 46 | * class is created by one of the bt_bt2_component_class_*_create() |
6fa6bfa1 SM |
47 | * functions. |
48 | * | |
49 | * This function is read from when a user calls bt_component_create() | |
50 | * with a component class pointer created by one of the functions above. | |
51 | * In this case, the original Python class needs to be found to | |
52 | * instantiate it and associate the created Python component object with | |
53 | * a BT component object instance. | |
54 | */ | |
55 | ||
56 | static GHashTable *bt_cc_ptr_to_py_cls; | |
57 | ||
efb177a7 SM |
58 | static |
59 | void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc, | |
6fa6bfa1 SM |
60 | PyObject *py_cls) |
61 | { | |
62 | if (!bt_cc_ptr_to_py_cls) { | |
63 | /* | |
64 | * Lazy-initializing this GHashTable because GLib | |
65 | * might not be initialized yet and it needs to be | |
66 | * before we call g_hash_table_new() | |
67 | */ | |
68 | BT_LOGD_STR("Creating native component class to Python component class hash table."); | |
69 | bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal); | |
70 | BT_ASSERT(bt_cc_ptr_to_py_cls); | |
71 | } | |
72 | ||
73 | g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc, | |
74 | (gpointer) py_cls); | |
75 | } | |
76 | ||
efb177a7 SM |
77 | static |
78 | PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc) | |
6fa6bfa1 SM |
79 | { |
80 | if (!bt_cc_ptr_to_py_cls) { | |
81 | BT_LOGW("Cannot look up Python component class because hash table is NULL: " | |
82 | "comp-cls-addr=%p", bt_cc); | |
83 | return NULL; | |
84 | } | |
85 | ||
86 | return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls, | |
87 | (gconstpointer) bt_cc); | |
88 | } | |
89 | ||
90 | ||
91 | /* | |
92 | * Useful Python objects. | |
93 | */ | |
94 | ||
95 | static PyObject *py_mod_bt2 = NULL; | |
96 | static PyObject *py_mod_bt2_exc_error_type = NULL; | |
97 | static PyObject *py_mod_bt2_exc_try_again_type = NULL; | |
98 | static PyObject *py_mod_bt2_exc_stop_type = NULL; | |
fa4c33e3 | 99 | static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL; |
fb25b9e3 PP |
100 | static PyObject *py_mod_bt2_exc_invalid_object_type = NULL; |
101 | static PyObject *py_mod_bt2_exc_invalid_params_type = NULL; | |
102 | static PyObject *py_mod_bt2_exc_unsupported_type = NULL; | |
6fa6bfa1 | 103 | |
efb177a7 | 104 | static |
fb25b9e3 | 105 | void bt_bt2_cc_init_from_bt2(void) |
6fa6bfa1 SM |
106 | { |
107 | /* | |
108 | * This is called once the bt2 package is loaded. | |
109 | * | |
110 | * Those modules and functions are needed while the package is | |
111 | * used. Loading them here is safe because we know the bt2 | |
112 | * package is imported, and we know that the user cannot use the | |
113 | * code here without importing bt2 first. | |
114 | */ | |
115 | py_mod_bt2 = PyImport_ImportModule("bt2"); | |
116 | BT_ASSERT(py_mod_bt2); | |
117 | py_mod_bt2_exc_error_type = | |
118 | PyObject_GetAttrString(py_mod_bt2, "Error"); | |
119 | BT_ASSERT(py_mod_bt2_exc_error_type); | |
120 | py_mod_bt2_exc_try_again_type = | |
121 | PyObject_GetAttrString(py_mod_bt2, "TryAgain"); | |
122 | BT_ASSERT(py_mod_bt2_exc_try_again_type); | |
123 | py_mod_bt2_exc_stop_type = | |
124 | PyObject_GetAttrString(py_mod_bt2, "Stop"); | |
125 | BT_ASSERT(py_mod_bt2_exc_stop_type); | |
fb25b9e3 PP |
126 | py_mod_bt2_exc_invalid_object_type = |
127 | PyObject_GetAttrString(py_mod_bt2, "InvalidObject"); | |
128 | BT_ASSERT(py_mod_bt2_exc_invalid_object_type); | |
129 | py_mod_bt2_exc_invalid_params_type = | |
130 | PyObject_GetAttrString(py_mod_bt2, "InvalidParams"); | |
131 | BT_ASSERT(py_mod_bt2_exc_invalid_params_type); | |
132 | py_mod_bt2_exc_unsupported_type = | |
133 | PyObject_GetAttrString(py_mod_bt2, "Unsupported"); | |
134 | BT_ASSERT(py_mod_bt2_exc_unsupported_type); | |
6fa6bfa1 SM |
135 | } |
136 | ||
efb177a7 | 137 | static |
fb25b9e3 | 138 | void bt_bt2_cc_exit_handler(void) |
6fa6bfa1 SM |
139 | { |
140 | /* | |
141 | * This is an exit handler (set by the bt2 package). | |
142 | * | |
143 | * We only give back the references that we took in | |
fb25b9e3 | 144 | * bt_bt2_cc_init_from_bt2() here. The global variables continue |
6fa6bfa1 SM |
145 | * to exist for the code of this file, but they are now borrowed |
146 | * references. If this code is executed, it means that somehow | |
147 | * the modules are still loaded, so it should be safe to use | |
148 | * them even without a strong reference. | |
149 | * | |
150 | * We cannot do this in the library's destructor because it | |
151 | * gets executed once Python is already finalized. | |
152 | */ | |
153 | Py_XDECREF(py_mod_bt2); | |
154 | Py_XDECREF(py_mod_bt2_exc_error_type); | |
155 | Py_XDECREF(py_mod_bt2_exc_try_again_type); | |
156 | Py_XDECREF(py_mod_bt2_exc_stop_type); | |
fa4c33e3 | 157 | Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type); |
fb25b9e3 PP |
158 | Py_XDECREF(py_mod_bt2_exc_invalid_object_type); |
159 | Py_XDECREF(py_mod_bt2_exc_invalid_params_type); | |
6fa6bfa1 SM |
160 | } |
161 | ||
162 | ||
163 | /* Library destructor */ | |
164 | ||
165 | __attribute__((destructor)) | |
efb177a7 | 166 | static |
fb25b9e3 | 167 | void native_comp_class_dtor(void) { |
6fa6bfa1 SM |
168 | /* Destroy component class association hash table */ |
169 | if (bt_cc_ptr_to_py_cls) { | |
170 | BT_LOGD_STR("Destroying native component class to Python component class hash table."); | |
171 | g_hash_table_destroy(bt_cc_ptr_to_py_cls); | |
172 | } | |
173 | } | |
174 | ||
fb25b9e3 PP |
175 | static inline |
176 | void log_exception(int log_level) | |
6fa6bfa1 | 177 | { |
2d7aa9b6 | 178 | GString *gstr; |
6fa6bfa1 SM |
179 | |
180 | BT_ASSERT(PyErr_Occurred() != NULL); | |
2d7aa9b6 PP |
181 | gstr = bt_py_common_format_exception(BT_LOG_OUTPUT_LEVEL); |
182 | if (!gstr) { | |
183 | /* bt_py_common_format_exception() logs errors */ | |
6fa6bfa1 SM |
184 | goto end; |
185 | } | |
186 | ||
fb25b9e3 | 187 | BT_LOG_WRITE(log_level, BT_LOG_TAG, "%s", gstr->str); |
6fa6bfa1 SM |
188 | |
189 | end: | |
2d7aa9b6 PP |
190 | if (gstr) { |
191 | g_string_free(gstr, TRUE); | |
6fa6bfa1 | 192 | } |
6fa6bfa1 SM |
193 | } |
194 | ||
fb25b9e3 PP |
195 | static inline |
196 | void loge_exception(void) | |
6fa6bfa1 | 197 | { |
fb25b9e3 | 198 | log_exception(BT_LOG_ERROR); |
6fa6bfa1 SM |
199 | } |
200 | ||
fb25b9e3 PP |
201 | static inline |
202 | void logw_exception(void) | |
6fa6bfa1 | 203 | { |
fb25b9e3 | 204 | log_exception(BT_LOG_WARN); |
6fa6bfa1 SM |
205 | } |
206 | ||
fb25b9e3 PP |
207 | static inline |
208 | int py_exc_to_status(void) | |
6fa6bfa1 | 209 | { |
fb25b9e3 | 210 | int status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
211 | PyObject *exc = PyErr_Occurred(); |
212 | ||
213 | if (!exc) { | |
214 | goto end; | |
215 | } | |
216 | ||
217 | if (PyErr_GivenExceptionMatches(exc, | |
fb25b9e3 PP |
218 | py_mod_bt2_exc_try_again_type)) { |
219 | status = __BT_FUNC_STATUS_AGAIN; | |
6fa6bfa1 | 220 | } else if (PyErr_GivenExceptionMatches(exc, |
fb25b9e3 PP |
221 | py_mod_bt2_exc_stop_type)) { |
222 | status = __BT_FUNC_STATUS_END; | |
6fa6bfa1 | 223 | } else if (PyErr_GivenExceptionMatches(exc, |
fb25b9e3 PP |
224 | py_mod_bt2_exc_invalid_object_type)) { |
225 | status = __BT_FUNC_STATUS_INVALID_OBJECT; | |
226 | } else if (PyErr_GivenExceptionMatches(exc, | |
227 | py_mod_bt2_exc_invalid_params_type)) { | |
228 | status = __BT_FUNC_STATUS_INVALID_PARAMS; | |
229 | } else if (PyErr_GivenExceptionMatches(exc, | |
230 | py_mod_bt2_exc_unsupported_type)) { | |
231 | status = __BT_FUNC_STATUS_UNSUPPORTED; | |
6fa6bfa1 | 232 | } else { |
fb25b9e3 PP |
233 | /* Unknown exception: convert to general error */ |
234 | logw_exception(); | |
235 | status = __BT_FUNC_STATUS_ERROR; | |
6fa6bfa1 SM |
236 | } |
237 | ||
238 | end: | |
239 | PyErr_Clear(); | |
240 | return status; | |
241 | } | |
242 | ||
fb25b9e3 PP |
243 | /* Component class proxy methods (delegate to the attached Python object) */ |
244 | ||
efb177a7 | 245 | static |
fb25b9e3 | 246 | bt_component_class_init_method_status component_class_init( |
6fa6bfa1 SM |
247 | bt_self_component *self_component, |
248 | void *self_component_v, | |
249 | swig_type_info *self_comp_cls_type_swig_type, | |
250 | const bt_value *params, | |
251 | void *init_method_data) | |
252 | { | |
253 | const bt_component *component = bt_self_component_as_component(self_component); | |
254 | const bt_component_class *component_class = bt_component_borrow_class_const(component); | |
fb25b9e3 | 255 | bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
256 | PyObject *py_cls = NULL; |
257 | PyObject *py_comp = NULL; | |
258 | PyObject *py_params_ptr = NULL; | |
259 | PyObject *py_comp_ptr = NULL; | |
260 | ||
261 | (void) init_method_data; | |
262 | ||
263 | BT_ASSERT(self_component); | |
264 | BT_ASSERT(self_component_v); | |
265 | BT_ASSERT(self_comp_cls_type_swig_type); | |
266 | ||
267 | /* | |
268 | * Get the user-defined Python class which created this | |
269 | * component's class in the first place (borrowed | |
270 | * reference). | |
271 | */ | |
272 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
273 | if (!py_cls) { | |
274 | BT_LOGE("Cannot find Python class associated to native component class: " | |
275 | "comp-cls-addr=%p", component_class); | |
276 | goto error; | |
277 | } | |
278 | ||
279 | /* Parameters pointer -> SWIG pointer Python object */ | |
280 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
281 | SWIGTYPE_p_bt_value, 0); | |
282 | if (!py_params_ptr) { | |
283 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
284 | goto error; | |
285 | } | |
286 | ||
287 | py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v), | |
288 | self_comp_cls_type_swig_type, 0); | |
289 | if (!py_comp_ptr) { | |
290 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
291 | goto error; | |
292 | } | |
293 | ||
294 | /* | |
295 | * Do the equivalent of this: | |
296 | * | |
deec48a6 | 297 | * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr) |
6fa6bfa1 | 298 | * |
deec48a6 | 299 | * _UserComponentType._bt_init_from_native() calls the Python |
6fa6bfa1 SM |
300 | * component object's __init__() function. |
301 | */ | |
302 | py_comp = PyObject_CallMethod(py_cls, | |
deec48a6 | 303 | "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr); |
6fa6bfa1 | 304 | if (!py_comp) { |
deec48a6 | 305 | BT_LOGW("Failed to call Python class's _bt_init_from_native() method: " |
6fa6bfa1 | 306 | "py-cls-addr=%p", py_cls); |
fb25b9e3 | 307 | logw_exception(); |
6fa6bfa1 SM |
308 | goto error; |
309 | } | |
310 | ||
311 | /* | |
312 | * Our user Python component object is now fully created and | |
313 | * initialized by the user. Since we just created it, this | |
314 | * native component is its only (persistent) owner. | |
315 | */ | |
316 | bt_self_component_set_data(self_component, py_comp); | |
317 | py_comp = NULL; | |
318 | goto end; | |
319 | ||
320 | error: | |
fb25b9e3 | 321 | status = __BT_FUNC_STATUS_ERROR; |
6fa6bfa1 SM |
322 | |
323 | /* | |
324 | * Clear any exception: we're returning a bad status anyway. If | |
325 | * this call originated from Python (creation from a plugin's | |
326 | * component class, for example), then the user gets an | |
327 | * appropriate creation error. | |
328 | */ | |
329 | PyErr_Clear(); | |
330 | ||
331 | end: | |
332 | Py_XDECREF(py_comp); | |
333 | Py_XDECREF(py_params_ptr); | |
334 | Py_XDECREF(py_comp_ptr); | |
335 | return status; | |
336 | } | |
337 | ||
338 | /* | |
339 | * Method of bt_component_class_source to initialize a bt_self_component_source | |
340 | * of that class. | |
341 | */ | |
342 | ||
efb177a7 | 343 | static |
fb25b9e3 | 344 | bt_component_class_init_method_status component_class_source_init( |
efb177a7 | 345 | bt_self_component_source *self_component_source, |
6fa6bfa1 SM |
346 | const bt_value *params, void *init_method_data) |
347 | { | |
348 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
fb25b9e3 | 349 | return component_class_init( |
6fa6bfa1 SM |
350 | self_component, |
351 | self_component_source, | |
352 | SWIGTYPE_p_bt_self_component_source, | |
353 | params, init_method_data); | |
354 | } | |
355 | ||
efb177a7 | 356 | static |
fb25b9e3 | 357 | bt_component_class_init_method_status component_class_filter_init( |
efb177a7 | 358 | bt_self_component_filter *self_component_filter, |
6fa6bfa1 SM |
359 | const bt_value *params, void *init_method_data) |
360 | { | |
361 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
fb25b9e3 | 362 | return component_class_init( |
6fa6bfa1 SM |
363 | self_component, |
364 | self_component_filter, | |
365 | SWIGTYPE_p_bt_self_component_filter, | |
366 | params, init_method_data); | |
367 | } | |
368 | ||
efb177a7 | 369 | static |
fb25b9e3 | 370 | bt_component_class_init_method_status component_class_sink_init( |
efb177a7 | 371 | bt_self_component_sink *self_component_sink, |
6fa6bfa1 SM |
372 | const bt_value *params, void *init_method_data) |
373 | { | |
374 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
fb25b9e3 | 375 | return component_class_init( |
6fa6bfa1 SM |
376 | self_component, |
377 | self_component_sink, | |
378 | SWIGTYPE_p_bt_self_component_sink, | |
379 | params, init_method_data); | |
380 | } | |
381 | ||
efb177a7 | 382 | static |
fb25b9e3 | 383 | void component_class_finalize(bt_self_component *self_component) |
6fa6bfa1 SM |
384 | { |
385 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
386 | BT_ASSERT(py_comp); | |
387 | ||
388 | /* Call user's _finalize() method */ | |
389 | PyObject *py_method_result = PyObject_CallMethod(py_comp, | |
390 | "_finalize", NULL); | |
391 | ||
392 | if (PyErr_Occurred()) { | |
fb25b9e3 PP |
393 | BT_LOGW("User component's _finalize() method raised an exception: ignoring:"); |
394 | logw_exception(); | |
6fa6bfa1 SM |
395 | } |
396 | ||
397 | /* | |
398 | * Ignore any exception raised by the _finalize() method because | |
399 | * it won't change anything at this point: the component is | |
400 | * being destroyed anyway. | |
401 | */ | |
402 | PyErr_Clear(); | |
403 | Py_XDECREF(py_method_result); | |
404 | Py_DECREF(py_comp); | |
405 | } | |
406 | ||
efb177a7 | 407 | static |
fb25b9e3 | 408 | void component_class_source_finalize(bt_self_component_source *self_component_source) |
6fa6bfa1 SM |
409 | { |
410 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
fb25b9e3 | 411 | component_class_finalize(self_component); |
6fa6bfa1 SM |
412 | } |
413 | ||
efb177a7 | 414 | static |
fb25b9e3 | 415 | void component_class_filter_finalize(bt_self_component_filter *self_component_filter) |
6fa6bfa1 SM |
416 | { |
417 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
fb25b9e3 | 418 | component_class_finalize(self_component); |
6fa6bfa1 SM |
419 | } |
420 | ||
efb177a7 | 421 | static |
fb25b9e3 | 422 | void component_class_sink_finalize(bt_self_component_sink *self_component_sink) |
6fa6bfa1 SM |
423 | { |
424 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
fb25b9e3 | 425 | component_class_finalize(self_component); |
6fa6bfa1 SM |
426 | } |
427 | ||
39ddfa44 | 428 | static |
fb25b9e3 | 429 | bt_bool component_class_can_seek_beginning( |
39ddfa44 SM |
430 | bt_self_message_iterator *self_message_iterator) |
431 | { | |
432 | PyObject *py_iter; | |
433 | PyObject *py_result = NULL; | |
434 | bt_bool can_seek_beginning = false; | |
435 | ||
436 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); | |
437 | BT_ASSERT(py_iter); | |
438 | ||
deec48a6 | 439 | py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native"); |
39ddfa44 SM |
440 | |
441 | BT_ASSERT(!py_result || PyBool_Check(py_result)); | |
442 | ||
443 | if (py_result) { | |
444 | can_seek_beginning = PyObject_IsTrue(py_result); | |
445 | } else { | |
446 | /* | |
447 | * Once can_seek_beginning can report errors, convert the | |
448 | * exception to a status. For now, log and return false; | |
449 | */ | |
fb25b9e3 | 450 | loge_exception(); |
39ddfa44 SM |
451 | PyErr_Clear(); |
452 | } | |
453 | ||
454 | Py_XDECREF(py_result); | |
455 | ||
456 | return can_seek_beginning; | |
457 | } | |
458 | ||
459 | static | |
fb25b9e3 PP |
460 | bt_component_class_message_iterator_seek_beginning_method_status |
461 | component_class_seek_beginning(bt_self_message_iterator *self_message_iterator) | |
39ddfa44 SM |
462 | { |
463 | PyObject *py_iter; | |
464 | PyObject *py_result; | |
fb25b9e3 | 465 | bt_component_class_message_iterator_seek_beginning_method_status status; |
39ddfa44 SM |
466 | |
467 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); | |
468 | BT_ASSERT(py_iter); | |
deec48a6 | 469 | py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native", |
fb25b9e3 | 470 | NULL); |
39ddfa44 | 471 | BT_ASSERT(!py_result || py_result == Py_None); |
fb25b9e3 | 472 | status = py_exc_to_status(); |
39ddfa44 | 473 | Py_XDECREF(py_result); |
39ddfa44 SM |
474 | return status; |
475 | } | |
476 | ||
efb177a7 | 477 | static |
fb25b9e3 | 478 | bt_component_class_port_connected_method_status component_class_port_connected( |
6fa6bfa1 SM |
479 | bt_self_component *self_component, |
480 | void *self_component_port, | |
481 | swig_type_info *self_component_port_swig_type, | |
482 | bt_port_type self_component_port_type, | |
483 | const void *other_port, | |
484 | swig_type_info *other_port_swig_type) | |
485 | { | |
fb25b9e3 | 486 | bt_component_class_port_connected_method_status status; |
6fa6bfa1 SM |
487 | PyObject *py_comp = NULL; |
488 | PyObject *py_self_port_ptr = NULL; | |
489 | PyObject *py_other_port_ptr = NULL; | |
490 | PyObject *py_method_result = NULL; | |
491 | ||
492 | py_comp = bt_self_component_get_data(self_component); | |
493 | BT_ASSERT(py_comp); | |
6fa6bfa1 SM |
494 | py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port), |
495 | self_component_port_swig_type, 0); | |
496 | if (!py_self_port_ptr) { | |
497 | BT_LOGF_STR("Failed to create a SWIG pointer object."); | |
fb25b9e3 | 498 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
6fa6bfa1 SM |
499 | goto end; |
500 | } | |
501 | ||
502 | py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port), | |
503 | other_port_swig_type, 0); | |
504 | if (!py_other_port_ptr) { | |
505 | BT_LOGF_STR("Failed to create a SWIG pointer object."); | |
fb25b9e3 | 506 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
6fa6bfa1 SM |
507 | goto end; } |
508 | ||
509 | py_method_result = PyObject_CallMethod(py_comp, | |
deec48a6 | 510 | "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr, |
6fa6bfa1 | 511 | self_component_port_type, py_other_port_ptr); |
6fa6bfa1 | 512 | BT_ASSERT(!py_method_result || py_method_result == Py_None); |
fb25b9e3 | 513 | status = py_exc_to_status(); |
6fa6bfa1 SM |
514 | |
515 | end: | |
516 | Py_XDECREF(py_self_port_ptr); | |
517 | Py_XDECREF(py_other_port_ptr); | |
518 | Py_XDECREF(py_method_result); | |
6fa6bfa1 SM |
519 | return status; |
520 | } | |
521 | ||
efb177a7 | 522 | static |
fb25b9e3 PP |
523 | bt_component_class_port_connected_method_status |
524 | component_class_source_output_port_connected( | |
6fa6bfa1 SM |
525 | bt_self_component_source *self_component_source, |
526 | bt_self_component_port_output *self_component_port_output, | |
527 | const bt_port_input *other_port_input) | |
528 | { | |
529 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
530 | ||
fb25b9e3 | 531 | return component_class_port_connected( |
6fa6bfa1 SM |
532 | self_component, |
533 | self_component_port_output, | |
534 | SWIGTYPE_p_bt_self_component_port_output, | |
535 | BT_PORT_TYPE_OUTPUT, | |
536 | other_port_input, | |
537 | SWIGTYPE_p_bt_port_input); | |
538 | } | |
539 | ||
efb177a7 | 540 | static |
fb25b9e3 PP |
541 | bt_component_class_port_connected_method_status |
542 | component_class_filter_input_port_connected( | |
6fa6bfa1 SM |
543 | bt_self_component_filter *self_component_filter, |
544 | bt_self_component_port_input *self_component_port_input, | |
545 | const bt_port_output *other_port_output) | |
546 | { | |
547 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
548 | ||
fb25b9e3 | 549 | return component_class_port_connected( |
6fa6bfa1 SM |
550 | self_component, |
551 | self_component_port_input, | |
552 | SWIGTYPE_p_bt_self_component_port_input, | |
553 | BT_PORT_TYPE_INPUT, | |
554 | other_port_output, | |
555 | SWIGTYPE_p_bt_port_output); | |
556 | } | |
557 | ||
efb177a7 | 558 | static |
fb25b9e3 PP |
559 | bt_component_class_port_connected_method_status |
560 | component_class_filter_output_port_connected( | |
6fa6bfa1 SM |
561 | bt_self_component_filter *self_component_filter, |
562 | bt_self_component_port_output *self_component_port_output, | |
563 | const bt_port_input *other_port_input) | |
564 | { | |
565 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
566 | ||
fb25b9e3 | 567 | return component_class_port_connected( |
6fa6bfa1 SM |
568 | self_component, |
569 | self_component_port_output, | |
570 | SWIGTYPE_p_bt_self_component_port_output, | |
571 | BT_PORT_TYPE_OUTPUT, | |
572 | other_port_input, | |
573 | SWIGTYPE_p_bt_port_input); | |
574 | } | |
575 | ||
efb177a7 | 576 | static |
fb25b9e3 PP |
577 | bt_component_class_port_connected_method_status |
578 | component_class_sink_input_port_connected( | |
6fa6bfa1 SM |
579 | bt_self_component_sink *self_component_sink, |
580 | bt_self_component_port_input *self_component_port_input, | |
581 | const bt_port_output *other_port_output) | |
582 | { | |
583 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
584 | ||
fb25b9e3 | 585 | return component_class_port_connected( |
6fa6bfa1 SM |
586 | self_component, |
587 | self_component_port_input, | |
588 | SWIGTYPE_p_bt_self_component_port_input, | |
589 | BT_PORT_TYPE_INPUT, | |
590 | other_port_output, | |
591 | SWIGTYPE_p_bt_port_output); | |
592 | } | |
593 | ||
efb177a7 | 594 | static |
fb25b9e3 PP |
595 | bt_component_class_sink_graph_is_configured_method_status |
596 | component_class_sink_graph_is_configured( | |
efb177a7 | 597 | bt_self_component_sink *self_component_sink) |
6fa6bfa1 SM |
598 | { |
599 | PyObject *py_comp = NULL; | |
600 | PyObject *py_method_result = NULL; | |
fb25b9e3 | 601 | bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
602 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
603 | ||
604 | py_comp = bt_self_component_get_data(self_component); | |
605 | py_method_result = PyObject_CallMethod(py_comp, | |
deec48a6 | 606 | "_bt_graph_is_configured_from_native", NULL); |
6fa6bfa1 | 607 | BT_ASSERT(!py_method_result || py_method_result == Py_None); |
fb25b9e3 | 608 | status = py_exc_to_status(); |
6fa6bfa1 | 609 | Py_XDECREF(py_method_result); |
6fa6bfa1 SM |
610 | return status; |
611 | } | |
612 | ||
efb177a7 | 613 | static |
fb25b9e3 | 614 | bt_component_class_query_method_status component_class_query( |
6fa6bfa1 SM |
615 | const bt_component_class *component_class, |
616 | const bt_query_executor *query_executor, | |
617 | const char *object, const bt_value *params, | |
83da519a | 618 | bt_logging_level log_level, |
6fa6bfa1 SM |
619 | const bt_value **result) |
620 | { | |
621 | PyObject *py_cls = NULL; | |
622 | PyObject *py_params_ptr = NULL; | |
623 | PyObject *py_query_exec_ptr = NULL; | |
624 | PyObject *py_query_func = NULL; | |
625 | PyObject *py_object = NULL; | |
626 | PyObject *py_results_addr = NULL; | |
fb25b9e3 | 627 | bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
628 | |
629 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
630 | if (!py_cls) { | |
631 | BT_LOGE("Cannot find Python class associated to native component class: " | |
632 | "comp-cls-addr=%p", component_class); | |
633 | goto error; | |
634 | } | |
635 | ||
636 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
637 | SWIGTYPE_p_bt_value, 0); | |
638 | if (!py_params_ptr) { | |
639 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
640 | goto error; | |
641 | } | |
642 | ||
643 | py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor), | |
644 | SWIGTYPE_p_bt_query_executor, 0); | |
645 | if (!py_query_exec_ptr) { | |
646 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
647 | goto error; | |
648 | } | |
649 | ||
650 | py_object = SWIG_FromCharPtr(object); | |
651 | if (!py_object) { | |
652 | BT_LOGE_STR("Failed to create a Python string."); | |
653 | goto error; | |
654 | } | |
655 | ||
656 | py_results_addr = PyObject_CallMethod(py_cls, | |
deec48a6 | 657 | "_bt_query_from_native", "(OOOi)", py_query_exec_ptr, |
83da519a | 658 | py_object, py_params_ptr, (int) log_level); |
6fa6bfa1 | 659 | if (!py_results_addr) { |
deec48a6 | 660 | BT_LOGW("Failed to call Python class's _bt_query_from_native() method: " |
6fa6bfa1 | 661 | "py-cls-addr=%p", py_cls); |
fb25b9e3 | 662 | status = py_exc_to_status(); |
6fa6bfa1 SM |
663 | goto end; |
664 | } | |
665 | ||
666 | /* | |
667 | * The returned object, on success, is an integer object | |
668 | * (PyLong) containing the address of a BT value object (new | |
669 | * reference). | |
670 | */ | |
c240a5d8 | 671 | *result = PyLong_AsVoidPtr(py_results_addr); |
6fa6bfa1 SM |
672 | BT_ASSERT(!PyErr_Occurred()); |
673 | BT_ASSERT(*result); | |
674 | goto end; | |
675 | ||
676 | error: | |
677 | PyErr_Clear(); | |
fb25b9e3 | 678 | status = __BT_FUNC_STATUS_ERROR; |
6fa6bfa1 SM |
679 | |
680 | end: | |
681 | Py_XDECREF(py_params_ptr); | |
682 | Py_XDECREF(py_query_exec_ptr); | |
683 | Py_XDECREF(py_query_func); | |
684 | Py_XDECREF(py_object); | |
685 | Py_XDECREF(py_results_addr); | |
686 | return status; | |
687 | } | |
688 | ||
efb177a7 | 689 | static |
fb25b9e3 | 690 | bt_component_class_query_method_status component_class_source_query( |
6fa6bfa1 SM |
691 | bt_self_component_class_source *self_component_class_source, |
692 | const bt_query_executor *query_executor, | |
693 | const char *object, const bt_value *params, | |
83da519a | 694 | bt_logging_level log_level, |
6fa6bfa1 SM |
695 | const bt_value **result) |
696 | { | |
697 | const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); | |
698 | const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); | |
fb25b9e3 PP |
699 | |
700 | return component_class_query(component_class, query_executor, object, params, log_level, result); | |
6fa6bfa1 SM |
701 | } |
702 | ||
efb177a7 | 703 | static |
fb25b9e3 | 704 | bt_component_class_query_method_status component_class_filter_query( |
6fa6bfa1 SM |
705 | bt_self_component_class_filter *self_component_class_filter, |
706 | const bt_query_executor *query_executor, | |
707 | const char *object, const bt_value *params, | |
83da519a | 708 | bt_logging_level log_level, |
6fa6bfa1 SM |
709 | const bt_value **result) |
710 | { | |
711 | const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); | |
712 | const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); | |
fb25b9e3 PP |
713 | |
714 | return component_class_query(component_class, query_executor, object, params, log_level, result); | |
6fa6bfa1 SM |
715 | } |
716 | ||
efb177a7 | 717 | static |
fb25b9e3 | 718 | bt_component_class_query_method_status component_class_sink_query( |
6fa6bfa1 SM |
719 | bt_self_component_class_sink *self_component_class_sink, |
720 | const bt_query_executor *query_executor, | |
721 | const char *object, const bt_value *params, | |
83da519a | 722 | bt_logging_level log_level, |
6fa6bfa1 SM |
723 | const bt_value **result) |
724 | { | |
725 | const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); | |
726 | const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); | |
fb25b9e3 PP |
727 | |
728 | return component_class_query(component_class, query_executor, object, params, log_level, result); | |
6fa6bfa1 SM |
729 | } |
730 | ||
efb177a7 | 731 | static |
fb25b9e3 PP |
732 | bt_component_class_message_iterator_init_method_status |
733 | component_class_message_iterator_init( | |
6fa6bfa1 SM |
734 | bt_self_message_iterator *self_message_iterator, |
735 | bt_self_component *self_component, | |
736 | bt_self_component_port_output *self_component_port_output) | |
737 | { | |
fb25b9e3 | 738 | bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
739 | PyObject *py_comp_cls = NULL; |
740 | PyObject *py_iter_cls = NULL; | |
741 | PyObject *py_iter_ptr = NULL; | |
a4dcfa96 | 742 | PyObject *py_component_port_output_ptr = NULL; |
6fa6bfa1 SM |
743 | PyObject *py_init_method_result = NULL; |
744 | PyObject *py_iter = NULL; | |
745 | PyObject *py_comp; | |
746 | ||
747 | py_comp = bt_self_component_get_data(self_component); | |
748 | ||
fa4c33e3 | 749 | /* Find user's Python message iterator class */ |
6fa6bfa1 SM |
750 | py_comp_cls = PyObject_GetAttrString(py_comp, "__class__"); |
751 | if (!py_comp_cls) { | |
752 | BT_LOGE_STR("Cannot get Python object's `__class__` attribute."); | |
753 | goto error; | |
754 | } | |
755 | ||
756 | py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls"); | |
757 | if (!py_iter_cls) { | |
758 | BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute."); | |
759 | goto error; | |
760 | } | |
761 | ||
762 | py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator), | |
763 | SWIGTYPE_p_bt_self_message_iterator, 0); | |
764 | if (!py_iter_ptr) { | |
765 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
766 | goto error; | |
767 | } | |
768 | ||
769 | /* | |
fa4c33e3 | 770 | * Create object with borrowed native message iterator |
6fa6bfa1 SM |
771 | * reference: |
772 | * | |
773 | * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr) | |
774 | */ | |
775 | py_iter = PyObject_CallMethod(py_iter_cls, "__new__", | |
776 | "(OO)", py_iter_cls, py_iter_ptr); | |
777 | if (!py_iter) { | |
778 | BT_LOGE("Failed to call Python class's __new__() method: " | |
779 | "py-cls-addr=%p", py_iter_cls); | |
fb25b9e3 | 780 | loge_exception(); |
6fa6bfa1 SM |
781 | goto error; |
782 | } | |
783 | ||
784 | /* | |
785 | * Initialize object: | |
786 | * | |
a4dcfa96 SM |
787 | * py_iter.__init__(self_output_port) |
788 | * | |
789 | * through the _init_for_native helper static method. | |
6fa6bfa1 SM |
790 | * |
791 | * At this point, py_iter._ptr is set, so this initialization | |
792 | * function has access to self._component (which gives it the | |
793 | * user Python component object from which the iterator was | |
794 | * created). | |
795 | */ | |
fb25b9e3 PP |
796 | py_component_port_output_ptr = SWIG_NewPointerObj( |
797 | SWIG_as_voidptr(self_component_port_output), | |
a4dcfa96 SM |
798 | SWIGTYPE_p_bt_self_component_port_output, 0); |
799 | if (!py_component_port_output_ptr) { | |
800 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
801 | goto error; | |
802 | } | |
803 | ||
fb25b9e3 | 804 | py_init_method_result = PyObject_CallMethod(py_iter, |
deec48a6 | 805 | "_bt_init_from_native", "O", py_component_port_output_ptr); |
6fa6bfa1 | 806 | if (!py_init_method_result) { |
fb25b9e3 PP |
807 | BT_LOGW_STR("User's __init__() method failed:"); |
808 | logw_exception(); | |
6fa6bfa1 SM |
809 | goto error; |
810 | } | |
811 | ||
812 | /* | |
813 | * Since the Python code can never instantiate a user-defined | |
fa4c33e3 SM |
814 | * message iterator class, the native message iterator |
815 | * object does NOT belong to a user Python message iterator | |
6fa6bfa1 | 816 | * object (borrowed reference). However this Python object is |
fa4c33e3 | 817 | * owned by this native message iterator object. |
6fa6bfa1 | 818 | * |
fa4c33e3 SM |
819 | * In the Python world, the lifetime of the native message |
820 | * iterator is managed by a _GenericMessageIterator | |
6fa6bfa1 SM |
821 | * instance: |
822 | * | |
fa4c33e3 SM |
823 | * _GenericMessageIterator instance: |
824 | * owns a native bt_message_iterator object (iter) | |
6fa6bfa1 SM |
825 | * owns a _UserMessageIterator instance (py_iter) |
826 | * self._ptr is a borrowed reference to the | |
fa4c33e3 | 827 | * native bt_private_connection_private_message_iterator |
6fa6bfa1 SM |
828 | * object (iter) |
829 | */ | |
830 | bt_self_message_iterator_set_data(self_message_iterator, py_iter); | |
831 | py_iter = NULL; | |
832 | goto end; | |
833 | ||
834 | error: | |
fb25b9e3 PP |
835 | status = py_exc_to_status(); |
836 | if (status == __BT_FUNC_STATUS_OK) { | |
6fa6bfa1 SM |
837 | /* |
838 | * Looks like there wasn't any exception from the Python | |
839 | * side, but we're still in an error state here. | |
840 | */ | |
fb25b9e3 | 841 | status = __BT_FUNC_STATUS_ERROR; |
6fa6bfa1 SM |
842 | } |
843 | ||
844 | /* | |
845 | * Clear any exception: we're returning a bad status anyway. If | |
846 | * this call originated from Python, then the user gets an | |
847 | * appropriate creation error. | |
848 | */ | |
849 | PyErr_Clear(); | |
850 | ||
851 | end: | |
852 | Py_XDECREF(py_comp_cls); | |
853 | Py_XDECREF(py_iter_cls); | |
854 | Py_XDECREF(py_iter_ptr); | |
a4dcfa96 | 855 | Py_XDECREF(py_component_port_output_ptr); |
6fa6bfa1 SM |
856 | Py_XDECREF(py_init_method_result); |
857 | Py_XDECREF(py_iter); | |
858 | return status; | |
859 | } | |
860 | ||
efb177a7 | 861 | static |
fb25b9e3 PP |
862 | bt_component_class_message_iterator_init_method_status |
863 | component_class_source_message_iterator_init( | |
6fa6bfa1 SM |
864 | bt_self_message_iterator *self_message_iterator, |
865 | bt_self_component_source *self_component_source, | |
866 | bt_self_component_port_output *self_component_port_output) | |
867 | { | |
868 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
fb25b9e3 PP |
869 | |
870 | return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); | |
6fa6bfa1 SM |
871 | } |
872 | ||
efb177a7 | 873 | static |
fb25b9e3 PP |
874 | bt_component_class_message_iterator_init_method_status |
875 | component_class_filter_message_iterator_init( | |
6fa6bfa1 SM |
876 | bt_self_message_iterator *self_message_iterator, |
877 | bt_self_component_filter *self_component_filter, | |
878 | bt_self_component_port_output *self_component_port_output) | |
879 | { | |
880 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
fb25b9e3 PP |
881 | |
882 | return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); | |
6fa6bfa1 SM |
883 | } |
884 | ||
efb177a7 | 885 | static |
fb25b9e3 | 886 | void component_class_message_iterator_finalize( |
6fa6bfa1 SM |
887 | bt_self_message_iterator *message_iterator) |
888 | { | |
889 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); | |
890 | PyObject *py_method_result = NULL; | |
891 | ||
892 | BT_ASSERT(py_message_iter); | |
893 | ||
894 | /* Call user's _finalize() method */ | |
895 | py_method_result = PyObject_CallMethod(py_message_iter, | |
896 | "_finalize", NULL); | |
897 | ||
898 | if (PyErr_Occurred()) { | |
fb25b9e3 PP |
899 | BT_LOGW("User's _finalize() method raised an exception: ignoring:"); |
900 | logw_exception(); | |
6fa6bfa1 SM |
901 | } |
902 | ||
903 | /* | |
904 | * Ignore any exception raised by the _finalize() method because | |
905 | * it won't change anything at this point: the component is | |
906 | * being destroyed anyway. | |
907 | */ | |
908 | PyErr_Clear(); | |
909 | Py_XDECREF(py_method_result); | |
910 | Py_DECREF(py_message_iter); | |
911 | } | |
912 | ||
913 | /* Valid for both sources and filters. */ | |
914 | ||
efb177a7 | 915 | static |
fb25b9e3 PP |
916 | bt_component_class_message_iterator_next_method_status |
917 | component_class_message_iterator_next( | |
efb177a7 SM |
918 | bt_self_message_iterator *message_iterator, |
919 | bt_message_array_const msgs, uint64_t capacity, | |
920 | uint64_t *count) | |
6fa6bfa1 | 921 | { |
fb25b9e3 | 922 | bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
923 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); |
924 | PyObject *py_method_result = NULL; | |
925 | ||
926 | BT_ASSERT(py_message_iter); | |
927 | py_method_result = PyObject_CallMethod(py_message_iter, | |
deec48a6 | 928 | "_bt_next_from_native", NULL); |
6fa6bfa1 | 929 | if (!py_method_result) { |
fb25b9e3 PP |
930 | status = py_exc_to_status(); |
931 | BT_ASSERT(status != __BT_FUNC_STATUS_OK); | |
6fa6bfa1 SM |
932 | goto end; |
933 | } | |
934 | ||
935 | /* | |
936 | * The returned object, on success, is an integer object | |
fa4c33e3 | 937 | * (PyLong) containing the address of a native message |
6fa6bfa1 SM |
938 | * object (which is now ours). |
939 | */ | |
c240a5d8 | 940 | msgs[0] = PyLong_AsVoidPtr(py_method_result); |
6fa6bfa1 SM |
941 | *count = 1; |
942 | ||
943 | /* Clear potential overflow error; should never happen */ | |
944 | BT_ASSERT(!PyErr_Occurred()); | |
945 | goto end; | |
946 | ||
947 | end: | |
948 | Py_XDECREF(py_method_result); | |
949 | return status; | |
950 | } | |
951 | ||
efb177a7 | 952 | static |
fb25b9e3 PP |
953 | bt_component_class_sink_consume_method_status |
954 | component_class_sink_consume(bt_self_component_sink *self_component_sink) | |
6fa6bfa1 SM |
955 | { |
956 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
957 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
958 | PyObject *py_method_result = NULL; | |
fb25b9e3 | 959 | bt_component_class_sink_consume_method_status status; |
6fa6bfa1 SM |
960 | |
961 | BT_ASSERT(py_comp); | |
962 | py_method_result = PyObject_CallMethod(py_comp, | |
963 | "_consume", NULL); | |
fb25b9e3 PP |
964 | status = py_exc_to_status(); |
965 | if (!py_method_result && status == __BT_FUNC_STATUS_OK) { | |
6fa6bfa1 SM |
966 | /* Pretty sure this should never happen, but just in case */ |
967 | BT_LOGE("User's _consume() method failed without raising an exception: " | |
968 | "status=%d", status); | |
fb25b9e3 | 969 | status = __BT_FUNC_STATUS_ERROR; |
6fa6bfa1 SM |
970 | } |
971 | ||
972 | Py_XDECREF(py_method_result); | |
973 | return status; | |
974 | } | |
975 | ||
976 | static | |
fb25b9e3 | 977 | int component_class_set_help_and_desc( |
6fa6bfa1 SM |
978 | bt_component_class *component_class, |
979 | const char *description, const char *help) | |
980 | { | |
981 | int ret; | |
982 | ||
983 | if (description) { | |
984 | ret = bt_component_class_set_description(component_class, description); | |
985 | if (ret) { | |
986 | BT_LOGE("Cannot set component class's description: " | |
987 | "comp-cls-addr=%p", component_class); | |
988 | goto end; | |
989 | } | |
990 | } | |
991 | ||
992 | if (help) { | |
993 | ret = bt_component_class_set_help(component_class, help); | |
994 | if (ret) { | |
995 | BT_LOGE("Cannot set component class's help text: " | |
996 | "comp-cls-addr=%p", component_class); | |
997 | goto end; | |
998 | } | |
999 | } | |
1000 | ||
1001 | ret = 0; | |
1002 | ||
1003 | end: | |
1004 | return ret; | |
1005 | } | |
1006 | ||
1007 | static | |
fb25b9e3 | 1008 | bt_component_class_source *bt_bt2_component_class_source_create( |
6fa6bfa1 SM |
1009 | PyObject *py_cls, const char *name, const char *description, |
1010 | const char *help) | |
1011 | { | |
1012 | bt_component_class_source *component_class_source; | |
1013 | bt_component_class *component_class; | |
1014 | int ret; | |
1015 | ||
1016 | BT_ASSERT(py_cls); | |
6fa6bfa1 | 1017 | component_class_source = bt_component_class_source_create(name, |
fb25b9e3 | 1018 | component_class_message_iterator_next); |
6fa6bfa1 SM |
1019 | if (!component_class_source) { |
1020 | BT_LOGE_STR("Cannot create source component class."); | |
1021 | goto end; | |
1022 | } | |
1023 | ||
1024 | component_class = bt_component_class_source_as_component_class(component_class_source); | |
1025 | ||
fb25b9e3 | 1026 | if (component_class_set_help_and_desc(component_class, description, help)) { |
6fa6bfa1 SM |
1027 | goto end; |
1028 | } | |
1029 | ||
fb25b9e3 | 1030 | ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init); |
6fa6bfa1 | 1031 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1032 | ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize); |
39ddfa44 SM |
1033 | BT_ASSERT(ret == 0); |
1034 | ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source, | |
fb25b9e3 | 1035 | component_class_can_seek_beginning); |
39ddfa44 SM |
1036 | BT_ASSERT(ret == 0); |
1037 | ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source, | |
fb25b9e3 | 1038 | component_class_seek_beginning); |
6fa6bfa1 | 1039 | BT_ASSERT(ret == 0); |
6fa6bfa1 | 1040 | ret = bt_component_class_source_set_output_port_connected_method(component_class_source, |
fb25b9e3 | 1041 | component_class_source_output_port_connected); |
6fa6bfa1 | 1042 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1043 | ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query); |
6fa6bfa1 SM |
1044 | BT_ASSERT(ret == 0); |
1045 | ret = bt_component_class_source_set_message_iterator_init_method( | |
fb25b9e3 | 1046 | component_class_source, component_class_source_message_iterator_init); |
6fa6bfa1 SM |
1047 | BT_ASSERT(ret == 0); |
1048 | ret = bt_component_class_source_set_message_iterator_finalize_method( | |
fb25b9e3 | 1049 | component_class_source, component_class_message_iterator_finalize); |
6fa6bfa1 | 1050 | BT_ASSERT(ret == 0); |
6fa6bfa1 SM |
1051 | register_cc_ptr_to_py_cls(component_class, py_cls); |
1052 | ||
1053 | end: | |
1054 | return component_class_source; | |
1055 | } | |
1056 | ||
1057 | static | |
fb25b9e3 | 1058 | bt_component_class_filter *bt_bt2_component_class_filter_create( |
6fa6bfa1 SM |
1059 | PyObject *py_cls, const char *name, const char *description, |
1060 | const char *help) | |
1061 | { | |
1062 | bt_component_class *component_class; | |
1063 | bt_component_class_filter *component_class_filter; | |
1064 | int ret; | |
1065 | ||
1066 | BT_ASSERT(py_cls); | |
6fa6bfa1 | 1067 | component_class_filter = bt_component_class_filter_create(name, |
fb25b9e3 | 1068 | component_class_message_iterator_next); |
6fa6bfa1 SM |
1069 | if (!component_class_filter) { |
1070 | BT_LOGE_STR("Cannot create filter component class."); | |
1071 | goto end; | |
1072 | } | |
1073 | ||
1074 | component_class = bt_component_class_filter_as_component_class(component_class_filter); | |
1075 | ||
fb25b9e3 | 1076 | if (component_class_set_help_and_desc(component_class, description, help)) { |
6fa6bfa1 SM |
1077 | goto end; |
1078 | } | |
1079 | ||
fb25b9e3 | 1080 | ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init); |
6fa6bfa1 | 1081 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1082 | ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize); |
6fa6bfa1 | 1083 | BT_ASSERT(ret == 0); |
39ddfa44 | 1084 | ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter, |
fb25b9e3 | 1085 | component_class_can_seek_beginning); |
39ddfa44 SM |
1086 | BT_ASSERT(ret == 0); |
1087 | ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter, | |
fb25b9e3 | 1088 | component_class_seek_beginning); |
39ddfa44 | 1089 | BT_ASSERT(ret == 0); |
6fa6bfa1 | 1090 | ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter, |
fb25b9e3 | 1091 | component_class_filter_input_port_connected); |
6fa6bfa1 SM |
1092 | BT_ASSERT(ret == 0); |
1093 | ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter, | |
fb25b9e3 | 1094 | component_class_filter_output_port_connected); |
6fa6bfa1 | 1095 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1096 | ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query); |
6fa6bfa1 SM |
1097 | BT_ASSERT(ret == 0); |
1098 | ret = bt_component_class_filter_set_message_iterator_init_method( | |
fb25b9e3 | 1099 | component_class_filter, component_class_filter_message_iterator_init); |
6fa6bfa1 SM |
1100 | BT_ASSERT(ret == 0); |
1101 | ret = bt_component_class_filter_set_message_iterator_finalize_method( | |
fb25b9e3 | 1102 | component_class_filter, component_class_message_iterator_finalize); |
6fa6bfa1 | 1103 | BT_ASSERT(ret == 0); |
6fa6bfa1 SM |
1104 | register_cc_ptr_to_py_cls(component_class, py_cls); |
1105 | ||
1106 | end: | |
1107 | return component_class_filter; | |
1108 | } | |
1109 | ||
1110 | static | |
fb25b9e3 | 1111 | bt_component_class_sink *bt_bt2_component_class_sink_create( |
6fa6bfa1 SM |
1112 | PyObject *py_cls, const char *name, const char *description, |
1113 | const char *help) | |
1114 | { | |
1115 | bt_component_class_sink *component_class_sink; | |
1116 | bt_component_class *component_class; | |
1117 | int ret; | |
1118 | ||
1119 | BT_ASSERT(py_cls); | |
fb25b9e3 | 1120 | component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume); |
6fa6bfa1 SM |
1121 | |
1122 | if (!component_class_sink) { | |
1123 | BT_LOGE_STR("Cannot create sink component class."); | |
1124 | goto end; | |
1125 | } | |
1126 | ||
1127 | component_class = bt_component_class_sink_as_component_class(component_class_sink); | |
1128 | ||
fb25b9e3 | 1129 | if (component_class_set_help_and_desc(component_class, description, help)) { |
6fa6bfa1 SM |
1130 | goto end; |
1131 | } | |
1132 | ||
fb25b9e3 | 1133 | ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init); |
6fa6bfa1 | 1134 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1135 | ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize); |
6fa6bfa1 | 1136 | BT_ASSERT(ret == 0); |
6fa6bfa1 | 1137 | ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink, |
fb25b9e3 | 1138 | component_class_sink_input_port_connected); |
6fa6bfa1 SM |
1139 | BT_ASSERT(ret == 0); |
1140 | ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink, | |
fb25b9e3 | 1141 | component_class_sink_graph_is_configured); |
6fa6bfa1 | 1142 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1143 | ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query); |
6fa6bfa1 | 1144 | BT_ASSERT(ret == 0); |
6fa6bfa1 SM |
1145 | register_cc_ptr_to_py_cls(component_class, py_cls); |
1146 | ||
1147 | end: | |
1148 | return component_class_sink; | |
1149 | } | |
1150 | %} | |
1151 | ||
fb25b9e3 | 1152 | struct bt_component_class_source *bt_bt2_component_class_source_create( |
6fa6bfa1 SM |
1153 | PyObject *py_cls, const char *name, const char *description, |
1154 | const char *help); | |
fb25b9e3 | 1155 | struct bt_component_class_filter *bt_bt2_component_class_filter_create( |
6fa6bfa1 SM |
1156 | PyObject *py_cls, const char *name, const char *description, |
1157 | const char *help); | |
fb25b9e3 | 1158 | struct bt_component_class_sink *bt_bt2_component_class_sink_create( |
6fa6bfa1 SM |
1159 | PyObject *py_cls, const char *name, const char *description, |
1160 | const char *help); | |
fb25b9e3 PP |
1161 | void bt_bt2_cc_init_from_bt2(void); |
1162 | void bt_bt2_cc_exit_handler(void); |