From 8b2850175bf4a9118d99706d20cf54603e4dc394 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Tue, 14 Feb 2017 12:55:29 -0500 Subject: [PATCH] Add the component port interface MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- include/Makefile.am | 2 + .../component/component-port-internal.h | 56 ++++++ include/babeltrace/component/component-port.h | 73 +++++++ lib/component/Makefile.am | 1 + lib/component/component-port.c | 181 ++++++++++++++++++ 5 files changed, 313 insertions(+) create mode 100644 include/babeltrace/component/component-port-internal.h create mode 100644 include/babeltrace/component/component-port.h create mode 100644 lib/component/component-port.c diff --git a/include/Makefile.am b/include/Makefile.am index 30386af8..9ae5d5be 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -58,6 +58,7 @@ babeltracecomponentinclude_HEADERS = \ babeltrace/component/component-class-filter.h \ babeltrace/component/component-class-sink.h \ babeltrace/component/component-connection.h \ + babeltrace/component/component-port.h \ babeltrace/component/component-graph.h \ babeltrace/component/component-source.h \ babeltrace/component/component-sink.h \ @@ -129,6 +130,7 @@ noinst_HEADERS = \ babeltrace/plugin/plugin-python-disabled-internal.h \ babeltrace/component/component-class-internal.h \ babeltrace/component/component-connection-internal.h \ + babeltrace/component/component-port-internal.h \ babeltrace/component/component-internal.h \ babeltrace/component/component-graph-internal.h \ babeltrace/component/component-filter-internal.h \ diff --git a/include/babeltrace/component/component-port-internal.h b/include/babeltrace/component/component-port-internal.h new file mode 100644 index 00000000..39b19e33 --- /dev/null +++ b/include/babeltrace/component/component-port-internal.h @@ -0,0 +1,56 @@ +#ifndef BABELTRACE_COMPONENT_PORT_INTERNAL_H +#define BABELTRACE_COMPONENT_PORT_INTERNAL_H + +/* + * BabelTrace - Babeltrace Component Port + * + * Copyright 2017 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct bt_port { + struct bt_object base; + enum bt_port_type type; + GString *name; + GPtrArray *connections; + uint64_t max_connection_count; +}; + +BT_HIDDEN +struct bt_port *bt_port_create(struct bt_component *parent_component, + enum bt_port_type type, const char *name); + +BT_HIDDEN +int bt_port_add_connection(struct bt_port *port, + struct bt_connection *connection); + +#ifdef __cplusplus +} +#endif + +#endif /* BABELTRACE_COMPONENT_PORT_INTERNAL_H */ diff --git a/include/babeltrace/component/component-port.h b/include/babeltrace/component/component-port.h new file mode 100644 index 00000000..fe372c4b --- /dev/null +++ b/include/babeltrace/component/component-port.h @@ -0,0 +1,73 @@ +#ifndef BABELTRACE_COMPONENT_PORT_H +#define BABELTRACE_COMPONENT_PORT_H + +/* + * BabelTrace - Babeltrace Component Connection Interface + * + * Copyright 2017 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct bt_port; +struct bt_connection; + +enum bt_port_status { + BT_PORT_STATUS_OK = 0, + BT_PORT_STATUS_ERROR = -1, + BT_PORT_STATUS_INVALID = -2, +}; + +enum bt_port_type { + BT_PORT_TYPE_INPUT = 0, + BT_PORT_TYPE_OUTPUT = 1, + BT_PORT_TYPE_UNKOWN = -1, +}; + +extern const char *BT_DEFAULT_PORT_NAME; + +extern const char *bt_port_get_name(struct bt_port *port); +extern enum bt_port_type bt_port_get_type(struct bt_port *port); + +extern enum bt_port_status bt_port_get_connection_count(struct bt_port *port, + uint64_t *count); +extern struct bt_connection *bt_port_get_connection(struct bt_port *port, + int index); + +extern struct bt_component *bt_port_get_component(struct bt_port *port); + +/* Only valid before a connection is established. */ +extern enum bt_port_status bt_port_get_maximum_connection_count( + struct bt_port *port, uint64_t *count); +extern enum bt_port_status bt_port_set_maximum_connection_count( + struct bt_port *port, uint64_t count); + +#ifdef __cplusplus +} +#endif + +#endif /* BABELTRACE_COMPONENT_PORT_H */ diff --git a/lib/component/Makefile.am b/lib/component/Makefile.am index 08da9f19..901c25fb 100644 --- a/lib/component/Makefile.am +++ b/lib/component/Makefile.am @@ -10,6 +10,7 @@ libcomponent_la_SOURCES = \ component-class.c \ component-graph.c \ component-connection.c \ + component-port.c \ source.c \ sink.c \ filter.c \ diff --git a/lib/component/component-port.c b/lib/component/component-port.c new file mode 100644 index 00000000..b08da58d --- /dev/null +++ b/lib/component/component-port.c @@ -0,0 +1,181 @@ +/* + * component-port.c + * + * Babeltrace Port + * + * Copyright 2017 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include + +static +void bt_port_destroy(struct bt_object *obj) +{ + struct bt_port *port = container_of(obj, struct bt_port, base); + + if (port->name) { + g_string_free(port->name, TRUE); + } + if (port->connections) { + g_ptr_array_free(port->connections, TRUE); + } + g_free(port); +} + +BT_HIDDEN +struct bt_port *bt_port_create(struct bt_component *parent_component, + enum bt_port_type type, const char *name) +{ + struct bt_port *port; + + assert(name); + assert(parent_component); + assert(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT); + + if (*name == '\0') { + port = NULL; + goto end; + } + + port = g_new0(struct bt_port, 1); + if (!port) { + goto end; + } + + bt_object_init(port, bt_port_destroy); + port->name = g_string_new(name); + if (!port->name) { + BT_PUT(port); + goto end; + } + + port->type = type; + port->connections = g_ptr_array_new(); + if (!port->connections) { + BT_PUT(port); + goto end; + } + + port->max_connection_count = 1; + + bt_object_set_parent(port, &parent_component->base); +end: + return port; +} + +const char *bt_port_get_name(struct bt_port *port) +{ + return port ? port->name->str : NULL; +} + +enum bt_port_type bt_port_get_type(struct bt_port *port) +{ + return port ? port->type : BT_PORT_TYPE_UNKOWN; +} + +enum bt_port_status bt_port_get_connection_count(struct bt_port *port, + uint64_t *count) +{ + enum bt_port_status status = BT_PORT_STATUS_OK; + + if (!port || !count) { + status = BT_PORT_STATUS_INVALID; + goto end; + } + + *count = (uint64_t) port->connections->len; +end: + return status; +} + +struct bt_connection *bt_port_get_connection(struct bt_port *port, int index) +{ + struct bt_connection *connection; + + if (!port || index < 0 || index >= port->connections->len) { + connection = NULL; + goto end; + } + + connection = bt_get(g_ptr_array_index(port->connections, index)); +end: + return connection; +} + +struct bt_component *bt_port_get_component(struct bt_port *port) +{ + return (struct bt_component *) bt_object_get_parent(port); +} + +BT_HIDDEN +int bt_port_add_connection(struct bt_port *port, + struct bt_connection *connection) +{ + int ret = 0; + + if (port->connections->len == port->max_connection_count) { + ret = -1; + goto end; + } + + /* + * Don't take a reference on connection as its existence is guaranteed + * by the existence of the graph in which the connection exists. + */ + g_ptr_array_add(port->connections, connection); +end: + return ret; +} + +enum bt_port_status bt_port_get_maximum_connection_count( + struct bt_port *port, uint64_t *count) +{ + enum bt_port_status status = BT_PORT_STATUS_OK; + + if (!port || !count) { + status = BT_PORT_STATUS_INVALID; + goto end; + } + + *count = port->max_connection_count; +end: + return status; +} + +enum bt_port_status bt_port_set_maximum_connection_count( + struct bt_port *port, uint64_t count) +{ + enum bt_port_status status = BT_PORT_STATUS_OK; + + if (!port || count < port->connections->len || count == 0) { + status = BT_PORT_STATUS_INVALID; + goto end; + } + + port->max_connection_count = count; +end: + return status; +} -- 2.34.1