Fix: lib: usage of output port message iterator
[babeltrace.git] / lib / graph / component-class-sink-colander.c
index d284ba4be3d91c4f23916b109841957334e32c03..7f3cfeca6329f94604cc4f9127d4bd6f782c7fe6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
+ * Copyright 2017-2018 Philippe Proulx <pproulx@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
 #define BT_LOG_TAG "COLANDER"
 #include <babeltrace/lib-logging-internal.h>
 
-#include <babeltrace/ref.h>
-#include <babeltrace/graph/connection.h>
+#include <babeltrace/assert-internal.h>
+#include <babeltrace/assert-pre-internal.h>
+#include <babeltrace/object-internal.h>
 #include <babeltrace/graph/component-class-sink.h>
-#include <babeltrace/graph/private-component-sink.h>
-#include <babeltrace/graph/private-port.h>
-#include <babeltrace/graph/private-connection.h>
-#include <babeltrace/graph/private-component.h>
+#include <babeltrace/graph/self-component-sink.h>
+#include <babeltrace/graph/self-component-port.h>
+#include <babeltrace/graph/self-component-port-input-message-iterator.h>
+#include <babeltrace/graph/self-component.h>
 #include <babeltrace/graph/component-class-sink-colander-internal.h>
-#include <babeltrace/assert-internal.h>
 #include <glib.h>
 
 static
-struct bt_component_class *colander_comp_cls;
-
-struct colander_data {
-       struct bt_notification **user_notif;
-       enum bt_notification_type *notif_types;
-       struct bt_notification_iterator *notif_iter;
-};
+struct bt_component_class_sink *colander_comp_cls;
 
 static
-enum bt_component_status colander_init(
-               struct bt_private_component *priv_comp,
-               struct bt_value *params, void *init_method_data)
+enum bt_self_component_status colander_init(
+               struct bt_self_component_sink *self_comp,
+               const struct bt_value *params, void *init_method_data)
 {
-       enum bt_component_status status = BT_COMPONENT_STATUS_OK;
-       struct colander_data *colander_data = NULL;
+       enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
+       struct bt_component_class_sink_colander_priv_data *colander_data = NULL;
        struct bt_component_class_sink_colander_data *user_provided_data =
                init_method_data;
-       const enum bt_notification_type *notif_type;
 
        if (!init_method_data) {
                BT_LOGW_STR("Component initialization method data is NULL.");
-               status = BT_COMPONENT_STATUS_INVALID;
+               status = BT_SELF_COMPONENT_STATUS_ERROR;
                goto end;
        }
 
-       colander_data = g_new0(struct colander_data, 1);
+       colander_data = g_new0(
+               struct bt_component_class_sink_colander_priv_data, 1);
        if (!colander_data) {
                BT_LOGE_STR("Failed to allocate colander data.");
-               status = BT_COMPONENT_STATUS_NOMEM;
+               status = BT_SELF_COMPONENT_STATUS_NOMEM;
                goto end;
        }
 
-       colander_data->user_notif = user_provided_data->notification;
-
-       if (user_provided_data->notification_types) {
-               notif_type = user_provided_data->notification_types;
-               unsigned long count;
-
-               while (*notif_type != BT_NOTIFICATION_TYPE_SENTINEL) {
-                       notif_type++;
-               }
-
-               count = notif_type - user_provided_data->notification_types + 1;
-
-               colander_data->notif_types =
-                       g_new0(enum bt_notification_type, count);
-               if (!colander_data->notif_types) {
-                       BT_LOGE_STR("Failed to allocate an array of notification types.");
-                       status = BT_COMPONENT_STATUS_NOMEM;
-                       goto end;
-               }
-
-               memcpy(colander_data->notif_types,
-                       user_provided_data->notification_types,
-                       count * sizeof(enum bt_notification_type));
-       }
-
-       status = bt_private_component_sink_add_input_private_port(
-               priv_comp, "in", NULL, NULL);
-       if (status != BT_COMPONENT_STATUS_OK) {
+       colander_data->msgs = user_provided_data->msgs;
+       colander_data->count_addr = user_provided_data->count_addr;
+       status = bt_self_component_sink_add_input_port(self_comp, "in",
+               NULL, NULL);
+       if (status != BT_SELF_COMPONENT_STATUS_OK) {
                BT_LOGE_STR("Cannot add input port.");
                goto end;
        }
 
-       (void) bt_private_component_set_user_data(priv_comp, colander_data);
+       bt_self_component_set_data(
+               bt_self_component_sink_as_self_component(self_comp),
+               colander_data);
 
 end:
        return status;
 }
 
 static
-void colander_finalize(struct bt_private_component *priv_comp)
+void colander_finalize(struct bt_self_component_sink *self_comp)
 {
-       struct colander_data *colander_data =
-               bt_private_component_get_user_data(priv_comp);
+       struct bt_component_class_sink_colander_priv_data *colander_data =
+               bt_self_component_get_data(
+                       bt_self_component_sink_as_self_component(self_comp));
 
        if (!colander_data) {
                return;
        }
 
-       if (colander_data->notif_iter) {
-               bt_put(colander_data->notif_iter);
-       }
-
-       g_free(colander_data->notif_types);
+       BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter);
        g_free(colander_data);
 }
 
 static
-void colander_port_connected(struct bt_private_component *priv_comp,
-               struct bt_private_port *self_priv_port,
-               struct bt_port *other_port)
+enum bt_self_component_status colander_graph_is_configured(
+       bt_self_component_sink *self_comp)
 {
-       enum bt_connection_status conn_status;
-       struct bt_private_connection *priv_conn =
-               bt_private_port_get_private_connection(self_priv_port);
-       struct colander_data *colander_data =
-               bt_private_component_get_user_data(priv_comp);
+       enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
+       struct bt_component_class_sink_colander_priv_data *colander_data =
+               bt_self_component_get_data(
+                       bt_self_component_sink_as_self_component(self_comp));
+
+       struct bt_self_component_port_input *self_port =
+               bt_self_component_sink_borrow_input_port_by_name(self_comp, "in");
+       BT_ASSERT(self_port);
 
-       BT_ASSERT(priv_conn);
        BT_ASSERT(colander_data);
-       BT_PUT(colander_data->notif_iter);
-       conn_status = bt_private_connection_create_notification_iterator(
-               priv_conn, colander_data->notif_types,
-               &colander_data->notif_iter);
-       if (conn_status) {
-               BT_LOGE("Cannot create notification iterator from connection: "
-                       "comp-addr=%p, conn-addr=%p", priv_comp, priv_conn);
+       BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter);
+       colander_data->msg_iter =
+               bt_self_component_port_input_message_iterator_create(
+                       self_port);
+       if (!colander_data->msg_iter) {
+               BT_LIB_LOGE("Cannot create message iterator on "
+                       "self component input port: %![port-]+p",
+                       self_port);
+               status = BT_SELF_COMPONENT_STATUS_NOMEM;
                goto end;
        }
 
 end:
-       bt_put(priv_conn);
+       return status;
 }
 
 static
-enum bt_component_status colander_consume(
-               struct bt_private_component *priv_comp)
+enum bt_self_component_status colander_consume(
+               struct bt_self_component_sink *self_comp)
 {
-       enum bt_component_status status = BT_COMPONENT_STATUS_OK;
-       enum bt_notification_iterator_status notif_iter_status;
-       struct bt_notification *notif = NULL;
-       struct colander_data *colander_data =
-               bt_private_component_get_user_data(priv_comp);
+       enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
+       enum bt_message_iterator_status msg_iter_status;
+       struct bt_component_class_sink_colander_priv_data *colander_data =
+               bt_self_component_get_data(
+                       bt_self_component_sink_as_self_component(self_comp));
+       bt_message_array_const msgs;
 
        BT_ASSERT(colander_data);
 
-       if (!colander_data->notif_iter) {
-               BT_LOGW("Trying to consume without an upstream notification iterator: "
-                       "comp-addr=%p", priv_comp);
+       if (!colander_data->msg_iter) {
+               BT_LIB_LOGW("Trying to consume without an "
+                       "upstream message iterator: %![comp-]+c",
+                       self_comp);
                goto end;
        }
 
-       notif_iter_status = bt_notification_iterator_next(
-               colander_data->notif_iter);
-       switch (notif_iter_status) {
-       case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED:
-               status = BT_COMPONENT_STATUS_OK;
+       msg_iter_status =
+               bt_self_component_port_input_message_iterator_next(
+                       colander_data->msg_iter, &msgs,
+                       colander_data->count_addr);
+       switch (msg_iter_status) {
+       case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
+               status = BT_SELF_COMPONENT_STATUS_AGAIN;
                goto end;
-       case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
-               status = BT_COMPONENT_STATUS_AGAIN;
+       case BT_MESSAGE_ITERATOR_STATUS_END:
+               status = BT_SELF_COMPONENT_STATUS_END;
                goto end;
-       case BT_NOTIFICATION_ITERATOR_STATUS_END:
-               status = BT_COMPONENT_STATUS_END;
-               goto end;
-       case BT_NOTIFICATION_ITERATOR_STATUS_OK:
+       case BT_MESSAGE_ITERATOR_STATUS_OK:
+               /* Move messages to user (count already set) */
+               memcpy(colander_data->msgs, msgs,
+                       sizeof(*msgs) * *colander_data->count_addr);
                break;
        default:
-               status = BT_COMPONENT_STATUS_ERROR;
+               status = BT_SELF_COMPONENT_STATUS_ERROR;
                goto end;
        }
 
-       notif = bt_notification_iterator_get_notification(
-               colander_data->notif_iter);
-       BT_ASSERT(notif);
-
 end:
-       /* Move notification to user's pointer, even if NULL. */
-       *colander_data->user_notif = notif;
        return status;
 }
 
-struct bt_component_class *bt_component_class_sink_colander_get(void)
+struct bt_component_class_sink *bt_component_class_sink_colander_get(void)
 {
        if (colander_comp_cls) {
                goto end;
@@ -210,19 +181,19 @@ struct bt_component_class *bt_component_class_sink_colander_get(void)
                goto end;
        }
 
-       (void) bt_component_class_set_init_method(colander_comp_cls,
-               colander_init);
-       (void) bt_component_class_set_finalize_method(colander_comp_cls,
-               colander_finalize);
-       (void) bt_component_class_set_port_connected_method(colander_comp_cls,
-               colander_port_connected);
-       (void) bt_component_class_freeze(colander_comp_cls);
+       (void) bt_component_class_sink_set_init_method(
+               colander_comp_cls, colander_init);
+       (void) bt_component_class_sink_set_finalize_method(
+               colander_comp_cls, colander_finalize);
+       (void) bt_component_class_sink_set_graph_is_configured_method(
+               colander_comp_cls, colander_graph_is_configured);
 
 end:
-       return bt_get(colander_comp_cls);
+       bt_object_get_ref(colander_comp_cls);
+       return (void *) colander_comp_cls;
 }
 
 __attribute__((destructor)) static
 void put_colander(void) {
-       BT_PUT(colander_comp_cls);
+       BT_OBJECT_PUT_REF_AND_RESET(colander_comp_cls);
 }
This page took 0.030454 seconds and 4 git commands to generate.