Add the component port interface
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 14 Feb 2017 17:55:29 +0000 (12:55 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sun, 28 May 2017 16:57:38 +0000 (12:57 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
include/Makefile.am
include/babeltrace/component/component-port-internal.h [new file with mode: 0644]
include/babeltrace/component/component-port.h [new file with mode: 0644]
lib/component/Makefile.am
lib/component/component-port.c [new file with mode: 0644]

index 30386af88ea93b96c6c12526f19a841ca24347e8..9ae5d5beae914672d1efae202ca696ded1cf5736 100644 (file)
@@ -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 (file)
index 0000000..39b19e3
--- /dev/null
@@ -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 <jeremie.galarneau@efficios.com>
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/component/component-port.h>
+
+#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 (file)
index 0000000..fe372c4
--- /dev/null
@@ -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 <jeremie.galarneau@efficios.com>
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <stdint.h>
+
+#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 */
index 08da9f1914b9d921eabb25ff4e4ee21b8be768a7..901c25fb39cedf17c52388977926ac942952e29e 100644 (file)
@@ -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 (file)
index 0000000..b08da58
--- /dev/null
@@ -0,0 +1,181 @@
+/*
+ * component-port.c
+ *
+ * Babeltrace Port
+ *
+ * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/component/component-internal.h>
+#include <babeltrace/component/component-port-internal.h>
+#include <babeltrace/object-internal.h>
+#include <babeltrace/compiler.h>
+
+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;
+}
This page took 0.028827 seconds and 4 git commands to generate.