Subscribe to notifications when creating a notif. iterator
[babeltrace.git] / plugins / writer / writer.c
index 48c3152a7987d53c0e848169a44db9b9d4fa54c9..3bca09b61f79f466756c8a50c0ea109f791b58b4 100644 (file)
@@ -69,7 +69,7 @@ void finalize_writer_component(struct bt_private_component *component)
 static
 void unref_stream_class(struct bt_ctf_stream_class *writer_stream_class)
 {
-       return;
+       bt_put(writer_stream_class);
 }
 
 static
@@ -81,7 +81,7 @@ void unref_stream(struct bt_ctf_stream_class *writer_stream)
 static
 void unref_trace(struct bt_ctf_writer *writer)
 {
-       return;
+       bt_put(writer);
 }
 
 static
@@ -97,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;
@@ -184,12 +185,11 @@ end:
 }
 
 static
-enum bt_component_status writer_component_accept_port_connection(
+void writer_component_port_connected(
                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;
 
@@ -198,21 +198,21 @@ enum bt_component_status writer_component_accept_port_connection(
        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);
+       writer->input_iterator =
+               bt_private_connection_create_notification_iterator(connection,
+                       NULL);
 
        if (!writer->input_iterator) {
-               ret = BT_COMPONENT_STATUS_ERROR;
+               writer->error = true;
        }
+
        bt_put(connection);
-       return ret;
 }
 
 static
 enum bt_component_status run(struct bt_private_component *component)
 {
        enum bt_component_status ret;
-       enum bt_notification_iterator_status it_status;
        struct bt_notification *notification = NULL;
        struct bt_notification_iterator *it;
        struct writer_component *writer_component =
@@ -221,21 +221,37 @@ enum bt_component_status run(struct bt_private_component *component)
        it = writer_component->input_iterator;
        assert(it);
 
-       notification = bt_notification_iterator_get_notification(it);
-       if (!notification) {
+       if (unlikely(writer_component->error)) {
                ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
 
-       it_status = bt_notification_iterator_next(it);
-       if (it_status != BT_COMPONENT_STATUS_OK) {
+       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);
+       if (!notification) {
                ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
 
        ret = handle_notification(writer_component, notification);
+       writer_component->processed_first_event = true;
 end:
-       bt_put(it);
        bt_put(notification);
        return ret;
 }
@@ -250,12 +266,22 @@ enum bt_component_status writer_component_init(
        struct writer_component *writer_component = create_writer_component();
        struct bt_value *value = NULL;
        const char *path;
+       void *priv_port;
 
        if (!writer_component) {
                ret = BT_COMPONENT_STATUS_NOMEM;
                goto end;
        }
 
+       priv_port = bt_private_component_sink_add_input_private_port(component,
+               "in", NULL);
+       if (!priv_port) {
+               ret = BT_COMPONENT_STATUS_NOMEM;
+               goto end;
+       }
+
+       bt_put(priv_port);
+
        value = bt_value_map_get(params, "path");
        if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
                fprintf(writer_component->err,
@@ -297,7 +323,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_ACCEPT_PORT_CONNECTION_METHOD(writer,
-               writer_component_accept_port_connection);
+BT_PLUGIN_SINK_COMPONENT_CLASS_PORT_CONNECTED_METHOD(writer,
+               writer_component_port_connected);
 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.027359 seconds and 4 git commands to generate.