lib: notification iterator: transfer a batch of notifications
[babeltrace.git] / plugins / utils / dummy / dummy.c
index 66b29968a6f5ecbf0418e14975a4b425e93a4a9e..926a6dd56728ef9fc54449c4f448568402ed6097 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/plugin/plugin-dev.h>
-#include <babeltrace/component/component.h>
-#include <babeltrace/component/component-sink.h>
-#include <babeltrace/component/notification/iterator.h>
-#include <babeltrace/component/notification/notification.h>
-#include <assert.h>
-
-enum bt_component_status dummy_consume(struct bt_component *component)
+#include <babeltrace/babeltrace.h>
+#include <babeltrace/babeltrace-internal.h>
+#include <plugins-common.h>
+#include <babeltrace/assert-internal.h>
+#include "dummy.h"
+
+void destroy_private_dummy_data(struct dummy *dummy)
+{
+       bt_put(dummy->notif_iter);
+       g_free(dummy);
+}
+
+void dummy_finalize(struct bt_private_component *component)
+{
+       struct dummy *dummy;
+
+       BT_ASSERT(component);
+       dummy = bt_private_component_get_user_data(component);
+       BT_ASSERT(dummy);
+       destroy_private_dummy_data(dummy);
+}
+
+enum bt_component_status dummy_init(struct bt_private_component *component,
+               struct bt_value *params, UNUSED_VAR void *init_method_data)
 {
        enum bt_component_status ret;
-       struct bt_notification *notif = NULL;
-       struct bt_notification_iterator *it = NULL;
-       unsigned int it_count;
-       size_t i;
-       bool got_one = false;
-
-       ret = bt_component_sink_get_input_count(component, &it_count);
-       assert(ret == 0);
-
-       for (i = 0; i < it_count; i++) {
-               enum bt_notification_iterator_status it_ret;
-
-               ret = bt_component_sink_get_input_iterator(component, i, &it);
-               assert(ret == 0);
-               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(it);
-                       continue;
-               default:
-                       break;
-               }
+       struct dummy *dummy = g_new0(struct dummy, 1);
 
-               notif = bt_notification_iterator_get_notification(it);
-               if (!notif) {
-                       ret = BT_COMPONENT_STATUS_ERROR;
-                       goto end;
-               }
+       if (!dummy) {
+               ret = BT_COMPONENT_STATUS_NOMEM;
+               goto end;
+       }
+
+       ret = bt_private_component_sink_add_input_private_port(component,
+               "in", NULL, NULL);
+       if (ret != BT_COMPONENT_STATUS_OK) {
+               goto end;
+       }
+
+       ret = bt_private_component_set_user_data(component, dummy);
+       if (ret != BT_COMPONENT_STATUS_OK) {
+               goto error;
+       }
+
+       goto end;
+
+error:
+       destroy_private_dummy_data(dummy);
+
+end:
+       return ret;
+}
 
-               /*
-                * Dummy! I'm doing nothing with this notification,
-                * NOTHING.
-                */
-               got_one = true;
-               BT_PUT(it);
-               BT_PUT(notif);
+void dummy_port_connected(
+               struct bt_private_component *component,
+               struct bt_private_port *self_port,
+               struct bt_port *other_port)
+{
+       struct dummy *dummy;
+       struct bt_notification_iterator *iterator;
+       struct bt_private_connection *connection;
+       enum bt_connection_status conn_status;
+
+       dummy = bt_private_component_get_user_data(component);
+       BT_ASSERT(dummy);
+       connection = bt_private_port_get_private_connection(self_port);
+       BT_ASSERT(connection);
+       conn_status = bt_private_connection_create_notification_iterator(
+               connection, &iterator);
+       if (conn_status != BT_CONNECTION_STATUS_OK) {
+               dummy->error = true;
+               goto end;
        }
 
-       if (!got_one) {
+       BT_MOVE(dummy->notif_iter, iterator);
+
+end:
+       bt_put(connection);
+}
+
+enum bt_component_status dummy_consume(struct bt_private_component *component)
+{
+       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
+       bt_notification_array notifs;
+       uint64_t count;
+       struct dummy *dummy;
+       enum bt_notification_iterator_status it_ret;
+       uint64_t i;
+
+       dummy = bt_private_component_get_user_data(component);
+       BT_ASSERT(dummy);
+
+       if (unlikely(dummy->error)) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+
+       if (unlikely(!dummy->notif_iter)) {
+               ret = BT_COMPONENT_STATUS_END;
+               goto end;
+       }
+
+       /* Consume one notification  */
+       it_ret = bt_private_connection_notification_iterator_next(
+               dummy->notif_iter, &notifs, &count);
+       switch (it_ret) {
+       case BT_NOTIFICATION_ITERATOR_STATUS_OK:
+               ret = BT_COMPONENT_STATUS_OK;
+
+               for (i = 0; i < count; i++) {
+                       bt_put(notifs[i]);
+               }
+
+               break;
+       case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
+               ret = BT_COMPONENT_STATUS_AGAIN;
+               goto end;
+       case BT_NOTIFICATION_ITERATOR_STATUS_END:
                ret = BT_COMPONENT_STATUS_END;
+               goto end;
+       case BT_NOTIFICATION_ITERATOR_STATUS_ERROR:
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       default:
+               break;
        }
 
 end:
-       bt_put(it);
-       bt_put(notif);
        return ret;
 }
This page took 0.03481 seconds and 4 git commands to generate.