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