fixes after rebase on eepp-jgalar/fixes-mar-7
[babeltrace.git] / plugins / writer / writer.c
index 8e4703768f9473a6e6f5da5bde9910f94ec55477..5dbd5d596e54a8aa122a6f895f7d49168d6cd427 100644 (file)
 
 #include <babeltrace/ctf-ir/packet.h>
 #include <babeltrace/plugin/plugin-dev.h>
-#include <babeltrace/component/component.h>
-#include <babeltrace/component/component-sink.h>
-#include <babeltrace/component/notification/notification.h>
-#include <babeltrace/component/notification/iterator.h>
-#include <babeltrace/component/notification/event.h>
-#include <babeltrace/component/notification/packet.h>
+#include <babeltrace/graph/component.h>
+#include <babeltrace/graph/private-component.h>
+#include <babeltrace/graph/component-sink.h>
+#include <babeltrace/graph/private-component-sink.h>
+#include <babeltrace/graph/private-port.h>
+#include <babeltrace/graph/private-connection.h>
+#include <babeltrace/graph/notification.h>
+#include <babeltrace/graph/notification-iterator.h>
+#include <babeltrace/graph/notification-event.h>
+#include <babeltrace/graph/notification-packet.h>
 #include <plugins-common.h>
 #include <stdio.h>
 #include <stdbool.h>
 #include <glib.h>
 #include "writer.h"
+#include <assert.h>
 
 static
 void destroy_writer_component_data(struct writer_component *writer_component)
 {
+       bt_put(writer_component->input_iterator);
        g_hash_table_destroy(writer_component->stream_map);
        g_hash_table_destroy(writer_component->stream_class_map);
        g_hash_table_destroy(writer_component->trace_map);
@@ -51,10 +57,10 @@ void destroy_writer_component_data(struct writer_component *writer_component)
 }
 
 static
-void destroy_writer_component(struct bt_component *component)
+void finalize_writer_component(struct bt_private_component *component)
 {
        struct writer_component *writer_component = (struct writer_component *)
-               bt_component_get_private_data(component);
+               bt_private_component_get_user_data(component);
 
        destroy_writer_component_data(writer_component);
        g_free(writer_component);
@@ -63,22 +69,19 @@ void destroy_writer_component(struct bt_component *component)
 static
 void unref_stream_class(struct bt_ctf_stream_class *writer_stream_class)
 {
-       BT_PUT(writer_stream_class);
-       g_free(writer_stream_class);
+       bt_put(writer_stream_class);
 }
 
 static
 void unref_stream(struct bt_ctf_stream_class *writer_stream)
 {
-       BT_PUT(writer_stream);
-       g_free(writer_stream);
+       bt_put(writer_stream);
 }
 
 static
 void unref_trace(struct bt_ctf_writer *writer)
 {
-       BT_PUT(writer);
-       g_free(writer);
+       bt_put(writer);
 }
 
 static
@@ -94,6 +97,7 @@ struct writer_component *create_writer_component(void)
        writer_component->err = stderr;
        writer_component->trace_id = 0;
        writer_component->trace_name_base = g_string_new("trace");
+       writer_component->processed_first_event = false;
        if (!writer_component->trace_name_base) {
                g_free(writer_component);
                writer_component = NULL;
@@ -181,17 +185,57 @@ end:
 }
 
 static
-enum bt_component_status run(struct bt_component *component)
+enum bt_component_status writer_component_accept_port_connection(
+               struct bt_private_component *component,
+               struct bt_private_port *self_port,
+               struct bt_port *other_port)
+{
+       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
+       struct bt_private_connection *connection;
+       struct writer_component *writer;
+
+       writer = bt_private_component_get_user_data(component);
+       assert(writer);
+       assert(!writer->input_iterator);
+       connection = bt_private_port_get_private_connection(self_port);
+       assert(connection);
+        writer->input_iterator =
+               bt_private_connection_create_notification_iterator(connection);
+
+       if (!writer->input_iterator) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+       }
+       bt_put(connection);
+       return ret;
+}
+
+static
+enum bt_component_status run(struct bt_private_component *component)
 {
        enum bt_component_status ret;
        struct bt_notification *notification = NULL;
        struct bt_notification_iterator *it;
        struct writer_component *writer_component =
-               bt_component_get_private_data(component);
-
-       ret = bt_component_sink_get_input_iterator(component, 0, &it);
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               goto end;
+               bt_private_component_get_user_data(component);
+
+       it = writer_component->input_iterator;
+       assert(it);
+
+       if (likely(writer_component->processed_first_event)) {
+               enum bt_notification_iterator_status it_ret;
+
+               it_ret = bt_notification_iterator_next(it);
+               switch (it_ret) {
+                       case BT_NOTIFICATION_ITERATOR_STATUS_ERROR:
+                               ret = BT_COMPONENT_STATUS_ERROR;
+                               goto end;
+                       case BT_NOTIFICATION_ITERATOR_STATUS_END:
+                               ret = BT_COMPONENT_STATUS_END;
+                               BT_PUT(writer_component->input_iterator);
+                               goto end;
+                       default:
+                               break;
+               }
        }
 
        notification = bt_notification_iterator_get_notification(it);
@@ -200,21 +244,16 @@ enum bt_component_status run(struct bt_component *component)
                goto end;
        }
 
-       ret = bt_notification_iterator_next(it);
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               goto end;
-       }
-
        ret = handle_notification(writer_component, notification);
+       writer_component->processed_first_event = true;
 end:
-       bt_put(it);
        bt_put(notification);
        return ret;
 }
 
 static
 enum bt_component_status writer_component_init(
-       struct bt_component *component, struct bt_value *params,
+       struct bt_private_component *component, struct bt_value *params,
        UNUSED_VAR void *init_method_data)
 {
        enum bt_component_status ret;
@@ -241,6 +280,7 @@ enum bt_component_status writer_component_init(
                ret = BT_COMPONENT_STATUS_INVALID;
                goto error;
        }
+       bt_put(value);
 
        writer_component->base_path = g_string_new(path);
        if (!writer_component) {
@@ -248,7 +288,7 @@ enum bt_component_status writer_component_init(
                goto error;
        }
 
-       ret = bt_component_set_private_data(component, writer_component);
+       ret = bt_private_component_set_user_data(component, writer_component);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto error;
        }
@@ -268,5 +308,7 @@ BT_PLUGIN_AUTHOR("Jérémie Galarneau");
 BT_PLUGIN_LICENSE("MIT");
 BT_PLUGIN_SINK_COMPONENT_CLASS(writer, run);
 BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(writer, writer_component_init);
-BT_PLUGIN_SINK_COMPONENT_CLASS_DESTROY_METHOD(writer, destroy_writer_component);
+BT_PLUGIN_SINK_COMPONENT_CLASS_ACCEPT_PORT_CONNECTION_METHOD(writer,
+               writer_component_accept_port_connection);
+BT_PLUGIN_SINK_COMPONENT_CLASS_FINALIZE_METHOD(writer, finalize_writer_component);
 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(writer, "Formats CTF-IR to CTF.");
This page took 0.025419 seconds and 4 git commands to generate.