| 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 | * Get the user-defined Python class which created this |
| 219 | * component's class in the first place (borrowed |
| 220 | * reference). |
| 221 | */ |
| 222 | py_cls = lookup_cc_ptr_to_py_cls(component_class); |
| 223 | if (!py_cls) { |
| 224 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, |
| 225 | "Cannot find Python class associated to native component class: " |
| 226 | "comp-cls-addr=%p", component_class); |
| 227 | goto error; |
| 228 | } |
| 229 | |
| 230 | /* Parameters pointer -> SWIG pointer Python object */ |
| 231 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), |
| 232 | SWIGTYPE_p_bt_value, 0); |
| 233 | if (!py_params_ptr) { |
| 234 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, |
| 235 | "Failed to create a SWIG pointer object."); |
| 236 | goto error; |
| 237 | } |
| 238 | |
| 239 | py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v), |
| 240 | self_comp_cls_type_swig_type, 0); |
| 241 | if (!py_comp_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 | /* |
| 248 | * Do the equivalent of this: |
| 249 | * |
| 250 | * py_comp = py_cls._bt_init_from_native(py_comp_ptr, |
| 251 | * py_params_ptr, init_method_data ? init_method_data : Py_None) |
| 252 | * |
| 253 | * _UserComponentType._bt_init_from_native() calls the Python |
| 254 | * component object's __init__() function. |
| 255 | * |
| 256 | * We don't take any reference on `init_method_data` which, if |
| 257 | * not `NULL`, is assumed to be a `PyObject *`: the user's |
| 258 | * __init__() function will eventually take a reference if |
| 259 | * needed. If `init_method_data` is `NULL`, then we pass |
| 260 | * `Py_None` as the initialization's Python object. |
| 261 | */ |
| 262 | py_comp = PyObject_CallMethod(py_cls, |
| 263 | "_bt_init_from_native", "(OOO)", py_comp_ptr, py_params_ptr, |
| 264 | init_method_data ? init_method_data : Py_None); |
| 265 | if (!py_comp) { |
| 266 | BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component, |
| 267 | "Failed to call Python class's _bt_init_from_native() method: " |
| 268 | "py-cls-addr=%p", py_cls); |
| 269 | status = py_exc_to_status_component(self_component); |
| 270 | goto end; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Our user Python component object is now fully created and |
| 275 | * initialized by the user. Since we just created it, this |
| 276 | * native component is its only (persistent) owner. |
| 277 | */ |
| 278 | bt_self_component_set_data(self_component, py_comp); |
| 279 | py_comp = NULL; |
| 280 | goto end; |
| 281 | |
| 282 | error: |
| 283 | status = __BT_FUNC_STATUS_ERROR; |
| 284 | |
| 285 | /* |
| 286 | * Clear any exception: we're returning a bad status anyway. If |
| 287 | * this call originated from Python (creation from a plugin's |
| 288 | * component class, for example), then the user gets an |
| 289 | * appropriate creation error. |
| 290 | */ |
| 291 | PyErr_Clear(); |
| 292 | |
| 293 | end: |
| 294 | Py_XDECREF(py_comp); |
| 295 | Py_XDECREF(py_params_ptr); |
| 296 | Py_XDECREF(py_comp_ptr); |
| 297 | return status; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Method of bt_component_class_source to initialize a bt_self_component_source |
| 302 | * of that class. |
| 303 | */ |
| 304 | |
| 305 | static |
| 306 | bt_component_class_init_method_status component_class_source_init( |
| 307 | bt_self_component_source *self_component_source, |
| 308 | const bt_value *params, void *init_method_data) |
| 309 | { |
| 310 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); |
| 311 | return component_class_init( |
| 312 | self_component, |
| 313 | self_component_source, |
| 314 | SWIGTYPE_p_bt_self_component_source, |
| 315 | params, init_method_data); |
| 316 | } |
| 317 | |
| 318 | static |
| 319 | bt_component_class_init_method_status component_class_filter_init( |
| 320 | bt_self_component_filter *self_component_filter, |
| 321 | const bt_value *params, void *init_method_data) |
| 322 | { |
| 323 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); |
| 324 | return component_class_init( |
| 325 | self_component, |
| 326 | self_component_filter, |
| 327 | SWIGTYPE_p_bt_self_component_filter, |
| 328 | params, init_method_data); |
| 329 | } |
| 330 | |
| 331 | static |
| 332 | bt_component_class_init_method_status component_class_sink_init( |
| 333 | bt_self_component_sink *self_component_sink, |
| 334 | const bt_value *params, void *init_method_data) |
| 335 | { |
| 336 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
| 337 | return component_class_init( |
| 338 | self_component, |
| 339 | self_component_sink, |
| 340 | SWIGTYPE_p_bt_self_component_sink, |
| 341 | params, init_method_data); |
| 342 | } |
| 343 | |
| 344 | static |
| 345 | void component_class_finalize(bt_self_component *self_component) |
| 346 | { |
| 347 | PyObject *py_comp = bt_self_component_get_data(self_component); |
| 348 | BT_ASSERT(py_comp); |
| 349 | |
| 350 | /* Call user's _user_finalize() method */ |
| 351 | PyObject *py_method_result = PyObject_CallMethod(py_comp, |
| 352 | "_user_finalize", NULL); |
| 353 | |
| 354 | if (PyErr_Occurred()) { |
| 355 | bt_logging_level log_level = get_self_component_log_level( |
| 356 | self_component); |
| 357 | |
| 358 | BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component, |
| 359 | "User component's _user_finalize() method raised an exception: ignoring:"); |
| 360 | logw_exception(log_level); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Ignore any exception raised by the _user_finalize() method |
| 365 | * because it won't change anything at this point: the component |
| 366 | * is being destroyed anyway. |
| 367 | */ |
| 368 | PyErr_Clear(); |
| 369 | Py_XDECREF(py_method_result); |
| 370 | Py_DECREF(py_comp); |
| 371 | } |
| 372 | |
| 373 | static |
| 374 | void component_class_source_finalize(bt_self_component_source *self_component_source) |
| 375 | { |
| 376 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); |
| 377 | component_class_finalize(self_component); |
| 378 | } |
| 379 | |
| 380 | static |
| 381 | void component_class_filter_finalize(bt_self_component_filter *self_component_filter) |
| 382 | { |
| 383 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); |
| 384 | component_class_finalize(self_component); |
| 385 | } |
| 386 | |
| 387 | static |
| 388 | void component_class_sink_finalize(bt_self_component_sink *self_component_sink) |
| 389 | { |
| 390 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
| 391 | component_class_finalize(self_component); |
| 392 | } |
| 393 | |
| 394 | static |
| 395 | bt_bool component_class_can_seek_beginning( |
| 396 | bt_self_message_iterator *self_message_iterator) |
| 397 | { |
| 398 | PyObject *py_iter; |
| 399 | PyObject *py_result = NULL; |
| 400 | bt_bool can_seek_beginning = false; |
| 401 | |
| 402 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); |
| 403 | BT_ASSERT(py_iter); |
| 404 | |
| 405 | py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native"); |
| 406 | |
| 407 | BT_ASSERT(!py_result || PyBool_Check(py_result)); |
| 408 | |
| 409 | if (py_result) { |
| 410 | can_seek_beginning = PyObject_IsTrue(py_result); |
| 411 | } else { |
| 412 | /* |
| 413 | * Once can_seek_beginning can report errors, convert the |
| 414 | * exception to a status. For now, log and return false; |
| 415 | */ |
| 416 | loge_exception_message_iterator(self_message_iterator); |
| 417 | PyErr_Clear(); |
| 418 | } |
| 419 | |
| 420 | Py_XDECREF(py_result); |
| 421 | |
| 422 | return can_seek_beginning; |
| 423 | } |
| 424 | |
| 425 | static |
| 426 | bt_component_class_message_iterator_seek_beginning_method_status |
| 427 | component_class_seek_beginning(bt_self_message_iterator *self_message_iterator) |
| 428 | { |
| 429 | PyObject *py_iter; |
| 430 | PyObject *py_result; |
| 431 | bt_component_class_message_iterator_seek_beginning_method_status status; |
| 432 | |
| 433 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); |
| 434 | BT_ASSERT(py_iter); |
| 435 | py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native", |
| 436 | NULL); |
| 437 | BT_ASSERT(!py_result || py_result == Py_None); |
| 438 | status = py_exc_to_status_message_iterator(self_message_iterator); |
| 439 | Py_XDECREF(py_result); |
| 440 | return status; |
| 441 | } |
| 442 | |
| 443 | static |
| 444 | bt_component_class_port_connected_method_status component_class_port_connected( |
| 445 | bt_self_component *self_component, |
| 446 | void *self_component_port, |
| 447 | swig_type_info *self_component_port_swig_type, |
| 448 | bt_port_type self_component_port_type, |
| 449 | const void *other_port, |
| 450 | swig_type_info *other_port_swig_type) |
| 451 | { |
| 452 | bt_component_class_port_connected_method_status status; |
| 453 | PyObject *py_comp = NULL; |
| 454 | PyObject *py_self_port_ptr = NULL; |
| 455 | PyObject *py_other_port_ptr = NULL; |
| 456 | PyObject *py_method_result = NULL; |
| 457 | bt_logging_level log_level = get_self_component_log_level( |
| 458 | self_component); |
| 459 | |
| 460 | py_comp = bt_self_component_get_data(self_component); |
| 461 | BT_ASSERT(py_comp); |
| 462 | py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port), |
| 463 | self_component_port_swig_type, 0); |
| 464 | if (!py_self_port_ptr) { |
| 465 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, |
| 466 | "Failed to create a SWIG pointer object."); |
| 467 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 468 | goto end; |
| 469 | } |
| 470 | |
| 471 | py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port), |
| 472 | other_port_swig_type, 0); |
| 473 | if (!py_other_port_ptr) { |
| 474 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, |
| 475 | "Failed to create a SWIG pointer object."); |
| 476 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 477 | goto end; } |
| 478 | |
| 479 | py_method_result = PyObject_CallMethod(py_comp, |
| 480 | "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr, |
| 481 | self_component_port_type, py_other_port_ptr); |
| 482 | BT_ASSERT(!py_method_result || py_method_result == Py_None); |
| 483 | status = py_exc_to_status_component(self_component); |
| 484 | |
| 485 | end: |
| 486 | Py_XDECREF(py_self_port_ptr); |
| 487 | Py_XDECREF(py_other_port_ptr); |
| 488 | Py_XDECREF(py_method_result); |
| 489 | return status; |
| 490 | } |
| 491 | |
| 492 | static |
| 493 | bt_component_class_port_connected_method_status |
| 494 | component_class_source_output_port_connected( |
| 495 | bt_self_component_source *self_component_source, |
| 496 | bt_self_component_port_output *self_component_port_output, |
| 497 | const bt_port_input *other_port_input) |
| 498 | { |
| 499 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); |
| 500 | |
| 501 | return component_class_port_connected( |
| 502 | self_component, |
| 503 | self_component_port_output, |
| 504 | SWIGTYPE_p_bt_self_component_port_output, |
| 505 | BT_PORT_TYPE_OUTPUT, |
| 506 | other_port_input, |
| 507 | SWIGTYPE_p_bt_port_input); |
| 508 | } |
| 509 | |
| 510 | static |
| 511 | bt_component_class_port_connected_method_status |
| 512 | component_class_filter_input_port_connected( |
| 513 | bt_self_component_filter *self_component_filter, |
| 514 | bt_self_component_port_input *self_component_port_input, |
| 515 | const bt_port_output *other_port_output) |
| 516 | { |
| 517 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); |
| 518 | |
| 519 | return component_class_port_connected( |
| 520 | self_component, |
| 521 | self_component_port_input, |
| 522 | SWIGTYPE_p_bt_self_component_port_input, |
| 523 | BT_PORT_TYPE_INPUT, |
| 524 | other_port_output, |
| 525 | SWIGTYPE_p_bt_port_output); |
| 526 | } |
| 527 | |
| 528 | static |
| 529 | bt_component_class_port_connected_method_status |
| 530 | component_class_filter_output_port_connected( |
| 531 | bt_self_component_filter *self_component_filter, |
| 532 | bt_self_component_port_output *self_component_port_output, |
| 533 | const bt_port_input *other_port_input) |
| 534 | { |
| 535 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); |
| 536 | |
| 537 | return component_class_port_connected( |
| 538 | self_component, |
| 539 | self_component_port_output, |
| 540 | SWIGTYPE_p_bt_self_component_port_output, |
| 541 | BT_PORT_TYPE_OUTPUT, |
| 542 | other_port_input, |
| 543 | SWIGTYPE_p_bt_port_input); |
| 544 | } |
| 545 | |
| 546 | static |
| 547 | bt_component_class_port_connected_method_status |
| 548 | component_class_sink_input_port_connected( |
| 549 | bt_self_component_sink *self_component_sink, |
| 550 | bt_self_component_port_input *self_component_port_input, |
| 551 | const bt_port_output *other_port_output) |
| 552 | { |
| 553 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
| 554 | |
| 555 | return component_class_port_connected( |
| 556 | self_component, |
| 557 | self_component_port_input, |
| 558 | SWIGTYPE_p_bt_self_component_port_input, |
| 559 | BT_PORT_TYPE_INPUT, |
| 560 | other_port_output, |
| 561 | SWIGTYPE_p_bt_port_output); |
| 562 | } |
| 563 | |
| 564 | static |
| 565 | bt_component_class_sink_graph_is_configured_method_status |
| 566 | component_class_sink_graph_is_configured( |
| 567 | bt_self_component_sink *self_component_sink) |
| 568 | { |
| 569 | PyObject *py_comp = NULL; |
| 570 | PyObject *py_method_result = NULL; |
| 571 | bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK; |
| 572 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
| 573 | |
| 574 | py_comp = bt_self_component_get_data(self_component); |
| 575 | py_method_result = PyObject_CallMethod(py_comp, |
| 576 | "_bt_graph_is_configured_from_native", NULL); |
| 577 | BT_ASSERT(!py_method_result || py_method_result == Py_None); |
| 578 | status = py_exc_to_status_component(self_component); |
| 579 | Py_XDECREF(py_method_result); |
| 580 | return status; |
| 581 | } |
| 582 | |
| 583 | static |
| 584 | bt_component_class_query_method_status component_class_query( |
| 585 | const bt_component_class *component_class, |
| 586 | bt_self_component_class *self_component_class, |
| 587 | bt_private_query_executor *priv_query_executor, |
| 588 | const char *object, const bt_value *params, void *method_data, |
| 589 | const bt_value **result) |
| 590 | { |
| 591 | PyObject *py_cls = NULL; |
| 592 | PyObject *py_params_ptr = NULL; |
| 593 | PyObject *py_priv_query_exec_ptr = NULL; |
| 594 | PyObject *py_query_func = NULL; |
| 595 | PyObject *py_object = NULL; |
| 596 | PyObject *py_results_addr = NULL; |
| 597 | bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK; |
| 598 | const bt_query_executor *query_exec = |
| 599 | bt_private_query_executor_as_query_executor_const( |
| 600 | priv_query_executor); |
| 601 | bt_logging_level log_level = |
| 602 | bt_query_executor_get_logging_level(query_exec); |
| 603 | |
| 604 | /* |
| 605 | * If there's any `method_data`, assume this component class is |
| 606 | * getting queried from Python, so that `method_data` is a |
| 607 | * Python object to pass to the user's _user_query() method. |
| 608 | */ |
| 609 | BT_ASSERT(!method_data || |
| 610 | bt_bt2_is_python_component_class(component_class)); |
| 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 | /* |
| 645 | * We don't take any reference on `method_data` which, if not |
| 646 | * `NULL`, is assumed to be a `PyObject *`: the user's |
| 647 | * _user_query() function will eventually take a reference if |
| 648 | * needed. If `method_data` is `NULL`, then we pass `Py_None` as |
| 649 | * the initialization's Python object. |
| 650 | */ |
| 651 | py_results_addr = PyObject_CallMethod(py_cls, |
| 652 | "_bt_query_from_native", "(OOOO)", py_priv_query_exec_ptr, |
| 653 | py_object, py_params_ptr, |
| 654 | method_data ? method_data : Py_None); |
| 655 | if (!py_results_addr) { |
| 656 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG, |
| 657 | "Failed to call Python class's _bt_query_from_native() method: " |
| 658 | "py-cls-addr=%p", py_cls); |
| 659 | status = py_exc_to_status_component_class(self_component_class, |
| 660 | log_level); |
| 661 | goto end; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * The returned object, on success, is an integer object |
| 666 | * (PyLong) containing the address of a BT value object (new |
| 667 | * reference). |
| 668 | */ |
| 669 | *result = PyLong_AsVoidPtr(py_results_addr); |
| 670 | BT_ASSERT(!PyErr_Occurred()); |
| 671 | BT_ASSERT(*result); |
| 672 | goto end; |
| 673 | |
| 674 | error: |
| 675 | PyErr_Clear(); |
| 676 | status = __BT_FUNC_STATUS_ERROR; |
| 677 | |
| 678 | end: |
| 679 | Py_XDECREF(py_params_ptr); |
| 680 | Py_XDECREF(py_priv_query_exec_ptr); |
| 681 | Py_XDECREF(py_query_func); |
| 682 | Py_XDECREF(py_object); |
| 683 | Py_XDECREF(py_results_addr); |
| 684 | return status; |
| 685 | } |
| 686 | |
| 687 | static |
| 688 | bt_component_class_query_method_status component_class_source_query( |
| 689 | bt_self_component_class_source *self_component_class_source, |
| 690 | bt_private_query_executor *priv_query_executor, |
| 691 | const char *object, const bt_value *params, void *method_data, |
| 692 | const bt_value **result) |
| 693 | { |
| 694 | const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); |
| 695 | const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); |
| 696 | bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source); |
| 697 | |
| 698 | return component_class_query(component_class, self_component_class, |
| 699 | priv_query_executor, object, params, method_data, result); |
| 700 | } |
| 701 | |
| 702 | static |
| 703 | bt_component_class_query_method_status component_class_filter_query( |
| 704 | bt_self_component_class_filter *self_component_class_filter, |
| 705 | bt_private_query_executor *priv_query_executor, |
| 706 | const char *object, const bt_value *params, void *method_data, |
| 707 | const bt_value **result) |
| 708 | { |
| 709 | const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); |
| 710 | const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); |
| 711 | bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter); |
| 712 | |
| 713 | return component_class_query(component_class, self_component_class, |
| 714 | priv_query_executor, object, params, method_data, result); |
| 715 | } |
| 716 | |
| 717 | static |
| 718 | bt_component_class_query_method_status component_class_sink_query( |
| 719 | bt_self_component_class_sink *self_component_class_sink, |
| 720 | bt_private_query_executor *priv_query_executor, |
| 721 | const char *object, const bt_value *params, void *method_data, |
| 722 | const bt_value **result) |
| 723 | { |
| 724 | const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); |
| 725 | const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); |
| 726 | bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink); |
| 727 | |
| 728 | return component_class_query(component_class, self_component_class, |
| 729 | priv_query_executor, object, params, method_data, result); |
| 730 | } |
| 731 | |
| 732 | static |
| 733 | bt_component_class_message_iterator_init_method_status |
| 734 | component_class_message_iterator_init( |
| 735 | bt_self_message_iterator *self_message_iterator, |
| 736 | bt_self_component *self_component, |
| 737 | bt_self_component_port_output *self_component_port_output) |
| 738 | { |
| 739 | bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK; |
| 740 | PyObject *py_comp_cls = NULL; |
| 741 | PyObject *py_iter_cls = NULL; |
| 742 | PyObject *py_iter_ptr = NULL; |
| 743 | PyObject *py_component_port_output_ptr = NULL; |
| 744 | PyObject *py_init_method_result = NULL; |
| 745 | PyObject *py_iter = NULL; |
| 746 | PyObject *py_comp; |
| 747 | bt_logging_level log_level = get_self_component_log_level( |
| 748 | self_component); |
| 749 | |
| 750 | py_comp = bt_self_component_get_data(self_component); |
| 751 | |
| 752 | /* Find user's Python message iterator class */ |
| 753 | py_comp_cls = PyObject_GetAttrString(py_comp, "__class__"); |
| 754 | if (!py_comp_cls) { |
| 755 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, |
| 756 | "Cannot get Python object's `__class__` attribute."); |
| 757 | goto python_error; |
| 758 | } |
| 759 | |
| 760 | py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls"); |
| 761 | if (!py_iter_cls) { |
| 762 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, |
| 763 | "Cannot get Python class's `_iter_cls` attribute."); |
| 764 | goto python_error; |
| 765 | } |
| 766 | |
| 767 | py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator), |
| 768 | SWIGTYPE_p_bt_self_message_iterator, 0); |
| 769 | if (!py_iter_ptr) { |
| 770 | const char *err = "Failed to create a SWIG pointer object."; |
| 771 | |
| 772 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, |
| 773 | "%s", err); |
| 774 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( |
| 775 | self_message_iterator, err); |
| 776 | goto error; |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Create object with borrowed native message iterator |
| 781 | * reference: |
| 782 | * |
| 783 | * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr) |
| 784 | */ |
| 785 | py_iter = PyObject_CallMethod(py_iter_cls, "__new__", |
| 786 | "(OO)", py_iter_cls, py_iter_ptr); |
| 787 | if (!py_iter) { |
| 788 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, |
| 789 | "Failed to call Python class's __new__() method: " |
| 790 | "py-cls-addr=%p", py_iter_cls); |
| 791 | goto python_error; |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * Initialize object: |
| 796 | * |
| 797 | * py_iter.__init__(self_output_port) |
| 798 | * |
| 799 | * through the _init_for_native helper static method. |
| 800 | * |
| 801 | * At this point, py_iter._ptr is set, so this initialization |
| 802 | * function has access to self._component (which gives it the |
| 803 | * user Python component object from which the iterator was |
| 804 | * created). |
| 805 | */ |
| 806 | py_component_port_output_ptr = SWIG_NewPointerObj( |
| 807 | SWIG_as_voidptr(self_component_port_output), |
| 808 | SWIGTYPE_p_bt_self_component_port_output, 0); |
| 809 | if (!py_component_port_output_ptr) { |
| 810 | const char *err = "Failed to create a SWIG pointer object."; |
| 811 | |
| 812 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, |
| 813 | "%s", err); |
| 814 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( |
| 815 | self_message_iterator, err); |
| 816 | goto error; |
| 817 | } |
| 818 | |
| 819 | py_init_method_result = PyObject_CallMethod(py_iter, |
| 820 | "_bt_init_from_native", "O", py_component_port_output_ptr); |
| 821 | if (!py_init_method_result) { |
| 822 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, |
| 823 | "User's __init__() method failed:"); |
| 824 | goto python_error; |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * Since the Python code can never instantiate a user-defined |
| 829 | * message iterator class, the native message iterator |
| 830 | * object does NOT belong to a user Python message iterator |
| 831 | * object (borrowed reference). However this Python object is |
| 832 | * owned by this native message iterator object. |
| 833 | * |
| 834 | * In the Python world, the lifetime of the native message |
| 835 | * iterator is managed by a _GenericMessageIterator |
| 836 | * instance: |
| 837 | * |
| 838 | * _GenericMessageIterator instance: |
| 839 | * owns a native bt_message_iterator object (iter) |
| 840 | * owns a _UserMessageIterator instance (py_iter) |
| 841 | * self._ptr is a borrowed reference to the |
| 842 | * native bt_private_connection_private_message_iterator |
| 843 | * object (iter) |
| 844 | */ |
| 845 | bt_self_message_iterator_set_data(self_message_iterator, py_iter); |
| 846 | py_iter = NULL; |
| 847 | goto end; |
| 848 | |
| 849 | python_error: |
| 850 | /* Handling of errors that cause a Python exception to be set. */ |
| 851 | status = py_exc_to_status_message_iterator(self_message_iterator); |
| 852 | BT_ASSERT(status != __BT_FUNC_STATUS_OK); |
| 853 | goto end; |
| 854 | |
| 855 | error: |
| 856 | /* Handling of errors that don't cause a Python exception to be set. */ |
| 857 | status = __BT_FUNC_STATUS_ERROR; |
| 858 | |
| 859 | end: |
| 860 | BT_ASSERT(!PyErr_Occurred()); |
| 861 | |
| 862 | Py_XDECREF(py_comp_cls); |
| 863 | Py_XDECREF(py_iter_cls); |
| 864 | Py_XDECREF(py_iter_ptr); |
| 865 | Py_XDECREF(py_component_port_output_ptr); |
| 866 | Py_XDECREF(py_init_method_result); |
| 867 | Py_XDECREF(py_iter); |
| 868 | return status; |
| 869 | } |
| 870 | |
| 871 | static |
| 872 | bt_component_class_message_iterator_init_method_status |
| 873 | component_class_source_message_iterator_init( |
| 874 | bt_self_message_iterator *self_message_iterator, |
| 875 | bt_self_component_source *self_component_source, |
| 876 | bt_self_component_port_output *self_component_port_output) |
| 877 | { |
| 878 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); |
| 879 | |
| 880 | return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); |
| 881 | } |
| 882 | |
| 883 | static |
| 884 | bt_component_class_message_iterator_init_method_status |
| 885 | component_class_filter_message_iterator_init( |
| 886 | bt_self_message_iterator *self_message_iterator, |
| 887 | bt_self_component_filter *self_component_filter, |
| 888 | bt_self_component_port_output *self_component_port_output) |
| 889 | { |
| 890 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); |
| 891 | |
| 892 | return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); |
| 893 | } |
| 894 | |
| 895 | static |
| 896 | void component_class_message_iterator_finalize( |
| 897 | bt_self_message_iterator *message_iterator) |
| 898 | { |
| 899 | PyObject *py_message_iter = bt_self_message_iterator_get_data( |
| 900 | message_iterator); |
| 901 | PyObject *py_method_result = NULL; |
| 902 | |
| 903 | BT_ASSERT(py_message_iter); |
| 904 | |
| 905 | /* Call user's _user_finalize() method */ |
| 906 | py_method_result = PyObject_CallMethod(py_message_iter, |
| 907 | "_user_finalize", NULL); |
| 908 | |
| 909 | if (PyErr_Occurred()) { |
| 910 | bt_self_component *self_comp = |
| 911 | bt_self_message_iterator_borrow_component( |
| 912 | message_iterator); |
| 913 | bt_logging_level log_level = get_self_component_log_level( |
| 914 | self_comp); |
| 915 | |
| 916 | BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp, |
| 917 | "User's _user_finalize() method raised an exception: ignoring:"); |
| 918 | logw_exception(get_self_message_iterator_log_level( |
| 919 | message_iterator)); |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Ignore any exception raised by the _user_finalize() method |
| 924 | * because it won't change anything at this point: the component |
| 925 | * is being destroyed anyway. |
| 926 | */ |
| 927 | PyErr_Clear(); |
| 928 | Py_XDECREF(py_method_result); |
| 929 | Py_DECREF(py_message_iter); |
| 930 | } |
| 931 | |
| 932 | /* Valid for both sources and filters. */ |
| 933 | |
| 934 | static |
| 935 | bt_component_class_message_iterator_next_method_status |
| 936 | component_class_message_iterator_next( |
| 937 | bt_self_message_iterator *message_iterator, |
| 938 | bt_message_array_const msgs, uint64_t capacity, |
| 939 | uint64_t *count) |
| 940 | { |
| 941 | bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK; |
| 942 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); |
| 943 | PyObject *py_method_result = NULL; |
| 944 | |
| 945 | BT_ASSERT(py_message_iter); |
| 946 | py_method_result = PyObject_CallMethod(py_message_iter, |
| 947 | "_bt_next_from_native", NULL); |
| 948 | if (!py_method_result) { |
| 949 | status = py_exc_to_status_message_iterator(message_iterator); |
| 950 | BT_ASSERT(status != __BT_FUNC_STATUS_OK); |
| 951 | goto end; |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | * The returned object, on success, is an integer object |
| 956 | * (PyLong) containing the address of a native message |
| 957 | * object (which is now ours). |
| 958 | */ |
| 959 | msgs[0] = PyLong_AsVoidPtr(py_method_result); |
| 960 | *count = 1; |
| 961 | |
| 962 | /* Clear potential overflow error; should never happen */ |
| 963 | BT_ASSERT(!PyErr_Occurred()); |
| 964 | goto end; |
| 965 | |
| 966 | end: |
| 967 | Py_XDECREF(py_method_result); |
| 968 | return status; |
| 969 | } |
| 970 | |
| 971 | static |
| 972 | bt_component_class_sink_consume_method_status |
| 973 | component_class_sink_consume(bt_self_component_sink *self_component_sink) |
| 974 | { |
| 975 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
| 976 | PyObject *py_comp = bt_self_component_get_data(self_component); |
| 977 | PyObject *py_method_result = NULL; |
| 978 | bt_component_class_sink_consume_method_status status; |
| 979 | |
| 980 | BT_ASSERT(py_comp); |
| 981 | py_method_result = PyObject_CallMethod(py_comp, |
| 982 | "_user_consume", NULL); |
| 983 | status = py_exc_to_status_component(self_component); |
| 984 | BT_ASSERT(py_method_result || status != __BT_FUNC_STATUS_OK); |
| 985 | Py_XDECREF(py_method_result); |
| 986 | return status; |
| 987 | } |
| 988 | |
| 989 | static |
| 990 | int component_class_set_help_and_desc( |
| 991 | bt_component_class *component_class, |
| 992 | const char *description, const char *help) |
| 993 | { |
| 994 | int ret; |
| 995 | |
| 996 | if (description) { |
| 997 | ret = bt_component_class_set_description(component_class, description); |
| 998 | if (ret) { |
| 999 | BT_LOGE("Cannot set component class's description: " |
| 1000 | "comp-cls-addr=%p", component_class); |
| 1001 | goto end; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | if (help) { |
| 1006 | ret = bt_component_class_set_help(component_class, help); |
| 1007 | if (ret) { |
| 1008 | BT_LOGE("Cannot set component class's help text: " |
| 1009 | "comp-cls-addr=%p", component_class); |
| 1010 | goto end; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | ret = 0; |
| 1015 | |
| 1016 | end: |
| 1017 | return ret; |
| 1018 | } |
| 1019 | |
| 1020 | static |
| 1021 | bt_component_class_source *bt_bt2_component_class_source_create( |
| 1022 | PyObject *py_cls, const char *name, const char *description, |
| 1023 | const char *help) |
| 1024 | { |
| 1025 | bt_component_class_source *component_class_source; |
| 1026 | bt_component_class *component_class; |
| 1027 | int ret; |
| 1028 | |
| 1029 | BT_ASSERT(py_cls); |
| 1030 | component_class_source = bt_component_class_source_create(name, |
| 1031 | component_class_message_iterator_next); |
| 1032 | if (!component_class_source) { |
| 1033 | BT_LOGE_STR("Cannot create source component class."); |
| 1034 | goto end; |
| 1035 | } |
| 1036 | |
| 1037 | component_class = bt_component_class_source_as_component_class(component_class_source); |
| 1038 | |
| 1039 | if (component_class_set_help_and_desc(component_class, description, help)) { |
| 1040 | goto end; |
| 1041 | } |
| 1042 | |
| 1043 | ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init); |
| 1044 | BT_ASSERT(ret == 0); |
| 1045 | ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize); |
| 1046 | BT_ASSERT(ret == 0); |
| 1047 | ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source, |
| 1048 | component_class_can_seek_beginning); |
| 1049 | BT_ASSERT(ret == 0); |
| 1050 | ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source, |
| 1051 | component_class_seek_beginning); |
| 1052 | BT_ASSERT(ret == 0); |
| 1053 | ret = bt_component_class_source_set_output_port_connected_method(component_class_source, |
| 1054 | component_class_source_output_port_connected); |
| 1055 | BT_ASSERT(ret == 0); |
| 1056 | ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query); |
| 1057 | BT_ASSERT(ret == 0); |
| 1058 | ret = bt_component_class_source_set_message_iterator_init_method( |
| 1059 | component_class_source, component_class_source_message_iterator_init); |
| 1060 | BT_ASSERT(ret == 0); |
| 1061 | ret = bt_component_class_source_set_message_iterator_finalize_method( |
| 1062 | component_class_source, component_class_message_iterator_finalize); |
| 1063 | BT_ASSERT(ret == 0); |
| 1064 | register_cc_ptr_to_py_cls(component_class, py_cls); |
| 1065 | |
| 1066 | end: |
| 1067 | return component_class_source; |
| 1068 | } |
| 1069 | |
| 1070 | static |
| 1071 | bt_component_class_filter *bt_bt2_component_class_filter_create( |
| 1072 | PyObject *py_cls, const char *name, const char *description, |
| 1073 | const char *help) |
| 1074 | { |
| 1075 | bt_component_class *component_class; |
| 1076 | bt_component_class_filter *component_class_filter; |
| 1077 | int ret; |
| 1078 | |
| 1079 | BT_ASSERT(py_cls); |
| 1080 | component_class_filter = bt_component_class_filter_create(name, |
| 1081 | component_class_message_iterator_next); |
| 1082 | if (!component_class_filter) { |
| 1083 | BT_LOGE_STR("Cannot create filter component class."); |
| 1084 | goto end; |
| 1085 | } |
| 1086 | |
| 1087 | component_class = bt_component_class_filter_as_component_class(component_class_filter); |
| 1088 | |
| 1089 | if (component_class_set_help_and_desc(component_class, description, help)) { |
| 1090 | goto end; |
| 1091 | } |
| 1092 | |
| 1093 | ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init); |
| 1094 | BT_ASSERT(ret == 0); |
| 1095 | ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize); |
| 1096 | BT_ASSERT(ret == 0); |
| 1097 | ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter, |
| 1098 | component_class_can_seek_beginning); |
| 1099 | BT_ASSERT(ret == 0); |
| 1100 | ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter, |
| 1101 | component_class_seek_beginning); |
| 1102 | BT_ASSERT(ret == 0); |
| 1103 | ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter, |
| 1104 | component_class_filter_input_port_connected); |
| 1105 | BT_ASSERT(ret == 0); |
| 1106 | ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter, |
| 1107 | component_class_filter_output_port_connected); |
| 1108 | BT_ASSERT(ret == 0); |
| 1109 | ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query); |
| 1110 | BT_ASSERT(ret == 0); |
| 1111 | ret = bt_component_class_filter_set_message_iterator_init_method( |
| 1112 | component_class_filter, component_class_filter_message_iterator_init); |
| 1113 | BT_ASSERT(ret == 0); |
| 1114 | ret = bt_component_class_filter_set_message_iterator_finalize_method( |
| 1115 | component_class_filter, component_class_message_iterator_finalize); |
| 1116 | BT_ASSERT(ret == 0); |
| 1117 | register_cc_ptr_to_py_cls(component_class, py_cls); |
| 1118 | |
| 1119 | end: |
| 1120 | return component_class_filter; |
| 1121 | } |
| 1122 | |
| 1123 | static |
| 1124 | bt_component_class_sink *bt_bt2_component_class_sink_create( |
| 1125 | PyObject *py_cls, const char *name, const char *description, |
| 1126 | const char *help) |
| 1127 | { |
| 1128 | bt_component_class_sink *component_class_sink; |
| 1129 | bt_component_class *component_class; |
| 1130 | int ret; |
| 1131 | |
| 1132 | BT_ASSERT(py_cls); |
| 1133 | component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume); |
| 1134 | |
| 1135 | if (!component_class_sink) { |
| 1136 | BT_LOGE_STR("Cannot create sink component class."); |
| 1137 | goto end; |
| 1138 | } |
| 1139 | |
| 1140 | component_class = bt_component_class_sink_as_component_class(component_class_sink); |
| 1141 | |
| 1142 | if (component_class_set_help_and_desc(component_class, description, help)) { |
| 1143 | goto end; |
| 1144 | } |
| 1145 | |
| 1146 | ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init); |
| 1147 | BT_ASSERT(ret == 0); |
| 1148 | ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize); |
| 1149 | BT_ASSERT(ret == 0); |
| 1150 | ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink, |
| 1151 | component_class_sink_input_port_connected); |
| 1152 | BT_ASSERT(ret == 0); |
| 1153 | ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink, |
| 1154 | component_class_sink_graph_is_configured); |
| 1155 | BT_ASSERT(ret == 0); |
| 1156 | ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query); |
| 1157 | BT_ASSERT(ret == 0); |
| 1158 | register_cc_ptr_to_py_cls(component_class, py_cls); |
| 1159 | |
| 1160 | end: |
| 1161 | return component_class_sink; |
| 1162 | } |