| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_H |
| 8 | #define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_H |
| 9 | |
| 10 | static bt_graph_listener_func_status port_added_listener( |
| 11 | const void *component, |
| 12 | swig_type_info *component_swig_type, |
| 13 | bt_component_class_type component_class_type, |
| 14 | const void *port, |
| 15 | swig_type_info *port_swig_type, |
| 16 | bt_port_type port_type, |
| 17 | void *py_callable) |
| 18 | { |
| 19 | PyObject *py_component_ptr = NULL; |
| 20 | PyObject *py_port_ptr = NULL; |
| 21 | PyObject *py_res = NULL; |
| 22 | bt_graph_listener_func_status status; |
| 23 | |
| 24 | py_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component), component_swig_type, 0); |
| 25 | if (!py_component_ptr) { |
| 26 | BT_LOGF_STR("Failed to create component SWIG pointer object."); |
| 27 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 28 | goto end; |
| 29 | } |
| 30 | |
| 31 | py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port), port_swig_type, 0); |
| 32 | if (!py_port_ptr) { |
| 33 | BT_LOGF_STR("Failed to create port SWIG pointer object."); |
| 34 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 35 | goto end; |
| 36 | } |
| 37 | |
| 38 | py_res = PyObject_CallFunction(py_callable, "(OiOi)", |
| 39 | py_component_ptr, component_class_type, py_port_ptr, port_type); |
| 40 | if (!py_res) { |
| 41 | loge_exception_append_cause_clear( |
| 42 | "Graph's port added listener (Python)", |
| 43 | BT_LOG_OUTPUT_LEVEL); |
| 44 | status = __BT_FUNC_STATUS_ERROR; |
| 45 | goto end; |
| 46 | } |
| 47 | |
| 48 | BT_ASSERT(py_res == Py_None); |
| 49 | status = __BT_FUNC_STATUS_OK; |
| 50 | |
| 51 | end: |
| 52 | Py_XDECREF(py_res); |
| 53 | Py_XDECREF(py_port_ptr); |
| 54 | Py_XDECREF(py_component_ptr); |
| 55 | return status; |
| 56 | } |
| 57 | |
| 58 | static |
| 59 | bt_graph_listener_func_status |
| 60 | source_component_output_port_added_listener(const bt_component_source *component_source, |
| 61 | const bt_port_output *port_output, void *py_callable) |
| 62 | { |
| 63 | return port_added_listener( |
| 64 | component_source, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE, |
| 65 | port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); |
| 66 | } |
| 67 | |
| 68 | static |
| 69 | bt_graph_listener_func_status |
| 70 | filter_component_input_port_added_listener(const bt_component_filter *component_filter, |
| 71 | const bt_port_input *port_input, void *py_callable) |
| 72 | { |
| 73 | return port_added_listener( |
| 74 | component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, |
| 75 | port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable); |
| 76 | } |
| 77 | |
| 78 | static |
| 79 | bt_graph_listener_func_status |
| 80 | filter_component_output_port_added_listener(const bt_component_filter *component_filter, |
| 81 | const bt_port_output *port_output, void *py_callable) |
| 82 | { |
| 83 | return port_added_listener( |
| 84 | component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, |
| 85 | port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); |
| 86 | } |
| 87 | |
| 88 | static |
| 89 | bt_graph_listener_func_status |
| 90 | sink_component_input_port_added_listener(const bt_component_sink *component_sink, |
| 91 | const bt_port_input *port_input, void *py_callable) |
| 92 | { |
| 93 | return port_added_listener( |
| 94 | component_sink, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK, |
| 95 | port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable); |
| 96 | } |
| 97 | |
| 98 | static |
| 99 | PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, |
| 100 | PyObject *py_callable) |
| 101 | { |
| 102 | PyObject *py_listener_ids = NULL; |
| 103 | PyObject *py_listener_id = NULL; |
| 104 | bt_listener_id listener_id; |
| 105 | bt_graph_add_listener_status status; |
| 106 | const char * const module_name = |
| 107 | "graph_add_port_added_listener() (Python)"; |
| 108 | |
| 109 | BT_ASSERT(graph); |
| 110 | BT_ASSERT(py_callable); |
| 111 | |
| 112 | /* |
| 113 | * Behind the scene, we will be registering 4 different listeners and |
| 114 | * return all of their ids. |
| 115 | */ |
| 116 | py_listener_ids = PyTuple_New(4); |
| 117 | if (!py_listener_ids) { |
| 118 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 119 | "Failed to allocate one PyTuple."); |
| 120 | goto error; |
| 121 | } |
| 122 | |
| 123 | /* source output port */ |
| 124 | status = bt_graph_add_source_component_output_port_added_listener( |
| 125 | graph, source_component_output_port_added_listener, |
| 126 | py_callable, &listener_id); |
| 127 | if (status != __BT_FUNC_STATUS_OK) { |
| 128 | /* |
| 129 | * bt_graph_add_source_component_output_port_added_listener has |
| 130 | * already logged/appended an error cause. |
| 131 | */ |
| 132 | goto error; |
| 133 | } |
| 134 | |
| 135 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
| 136 | if (!py_listener_id) { |
| 137 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 138 | "Failed to allocate one PyLong."); |
| 139 | goto error; |
| 140 | } |
| 141 | |
| 142 | PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id); |
| 143 | py_listener_id = NULL; |
| 144 | |
| 145 | /* filter input port */ |
| 146 | status = bt_graph_add_filter_component_input_port_added_listener( |
| 147 | graph, filter_component_input_port_added_listener, |
| 148 | py_callable, &listener_id); |
| 149 | if (status != __BT_FUNC_STATUS_OK) { |
| 150 | /* |
| 151 | * bt_graph_add_filter_component_input_port_added_listener has |
| 152 | * already logged/appended an error cause. |
| 153 | */ |
| 154 | goto error; |
| 155 | } |
| 156 | |
| 157 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
| 158 | if (!py_listener_id) { |
| 159 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 160 | "Failed to allocate one PyLong."); |
| 161 | goto error; |
| 162 | } |
| 163 | |
| 164 | PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id); |
| 165 | py_listener_id = NULL; |
| 166 | |
| 167 | /* filter output port */ |
| 168 | status = bt_graph_add_filter_component_output_port_added_listener( |
| 169 | graph, filter_component_output_port_added_listener, |
| 170 | py_callable, &listener_id); |
| 171 | if (status != __BT_FUNC_STATUS_OK) { |
| 172 | /* |
| 173 | * bt_graph_add_filter_component_output_port_added_listener has |
| 174 | * already logged/appended an error cause. |
| 175 | */ |
| 176 | goto error; |
| 177 | } |
| 178 | |
| 179 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
| 180 | if (!py_listener_id) { |
| 181 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 182 | "Failed to allocate one PyLong."); |
| 183 | goto error; |
| 184 | } |
| 185 | |
| 186 | PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id); |
| 187 | py_listener_id = NULL; |
| 188 | |
| 189 | /* sink input port */ |
| 190 | status = bt_graph_add_sink_component_input_port_added_listener( |
| 191 | graph, sink_component_input_port_added_listener, |
| 192 | py_callable, &listener_id); |
| 193 | if (status != __BT_FUNC_STATUS_OK) { |
| 194 | /* |
| 195 | * bt_graph_add_sink_component_input_port_added_listener has |
| 196 | * already logged/appended an error cause. |
| 197 | */ |
| 198 | goto error; |
| 199 | } |
| 200 | |
| 201 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
| 202 | if (!py_listener_id) { |
| 203 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 204 | "Failed to allocate one PyLong."); |
| 205 | goto error; |
| 206 | } |
| 207 | |
| 208 | PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id); |
| 209 | py_listener_id = NULL; |
| 210 | goto end; |
| 211 | |
| 212 | error: |
| 213 | Py_XDECREF(py_listener_ids); |
| 214 | py_listener_ids = Py_None; |
| 215 | Py_INCREF(py_listener_ids); |
| 216 | |
| 217 | end: |
| 218 | Py_XDECREF(py_listener_id); |
| 219 | return py_listener_ids; |
| 220 | } |
| 221 | |
| 222 | static |
| 223 | bt_graph_add_component_status |
| 224 | bt_bt2_graph_add_source_component( |
| 225 | bt_graph *graph, |
| 226 | const bt_component_class_source *component_class, |
| 227 | const char *name, const bt_value *params, |
| 228 | PyObject *obj, bt_logging_level log_level, |
| 229 | const bt_component_source **component) |
| 230 | { |
| 231 | return bt_graph_add_source_component_with_initialize_method_data(graph, |
| 232 | component_class, name, params, obj == Py_None ? NULL : obj, |
| 233 | log_level, component); |
| 234 | } |
| 235 | |
| 236 | static |
| 237 | bt_graph_add_component_status |
| 238 | bt_bt2_graph_add_filter_component( |
| 239 | bt_graph *graph, |
| 240 | const bt_component_class_filter *component_class, |
| 241 | const char *name, const bt_value *params, |
| 242 | PyObject *obj, bt_logging_level log_level, |
| 243 | const bt_component_filter **component) |
| 244 | { |
| 245 | return bt_graph_add_filter_component_with_initialize_method_data(graph, |
| 246 | component_class, name, params, obj == Py_None ? NULL : obj, |
| 247 | log_level, component); |
| 248 | } |
| 249 | |
| 250 | static |
| 251 | bt_graph_add_component_status |
| 252 | bt_bt2_graph_add_sink_component( |
| 253 | bt_graph *graph, |
| 254 | const bt_component_class_sink *component_class, |
| 255 | const char *name, const bt_value *params, |
| 256 | PyObject *obj, bt_logging_level log_level, |
| 257 | const bt_component_sink **component) |
| 258 | { |
| 259 | return bt_graph_add_sink_component_with_initialize_method_data(graph, |
| 260 | component_class, name, params, obj == Py_None ? NULL : obj, |
| 261 | log_level, component); |
| 262 | } |
| 263 | |
| 264 | #endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_H */ |