src.ctf.lttng-live: make lttng_live_stream_iterator::stream a bt2::Stream::Shared
[babeltrace.git] / src / plugins / ctf / lttng-live / lttng-live.cpp
index 7e32c379e9a343d5fc6156e1c675c8b687fda0ba..ae7e45b1798c4b9cb288b38f9888fb011986a76f 100644 (file)
@@ -13,6 +13,8 @@
 
 #include "common/assert.h"
 #include "cpp-common/bt2c/fmt.hpp"
+#include "cpp-common/bt2c/glib-up.hpp"
+#include "cpp-common/bt2s/make-unique.hpp"
 #include "cpp-common/vendor/fmt/format.h"
 
 #include "plugins/common/muxing/muxing.h"
@@ -93,10 +95,6 @@ static void lttng_live_destroy_trace(struct lttng_live_trace *trace)
     BT_ASSERT(trace->stream_iterators);
     g_ptr_array_free(trace->stream_iterators, TRUE);
 
-    BT_TRACE_PUT_REF_AND_RESET(trace->trace);
-    BT_TRACE_CLASS_PUT_REF_AND_RESET(trace->trace_class);
-
-    lttng_live_metadata_fini(trace);
     delete trace;
 }
 
@@ -109,8 +107,6 @@ static struct lttng_live_trace *lttng_live_create_trace(struct lttng_live_sessio
     lttng_live_trace *trace = new lttng_live_trace {session->logger};
     trace->session = session;
     trace->id = trace_id;
-    trace->trace_class = NULL;
-    trace->trace = NULL;
     trace->stream_iterators =
         g_ptr_array_new_with_free_func((GDestroyNotify) lttng_live_stream_iterator_destroy);
     BT_ASSERT(trace->stream_iterators);
@@ -1073,7 +1069,7 @@ handle_late_message(struct lttng_live_msg_iter *lttng_live_msg_iter,
         goto end;
     }
 
-    stream_class = bt_stream_borrow_class_const(stream_iter->stream);
+    stream_class = bt_stream_borrow_class_const(stream_iter->stream->libObjPtr());
     clock_class = bt_stream_class_borrow_default_clock_class_const(stream_class);
 
     ts_ns_status = bt_clock_class_cycles_to_ns_from_origin(
@@ -1109,13 +1105,13 @@ handle_late_message(struct lttng_live_msg_iter *lttng_live_msg_iter,
     switch (bt_message_get_type(late_msg)) {
     case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
         adjust_status = adjust_discarded_events_message(
-            lttng_live_msg_iter->self_msg_iter, stream_iter->stream, late_msg, &adjusted_message,
-            stream_iter->last_inactivity_ts.value);
+            lttng_live_msg_iter->self_msg_iter, stream_iter->stream->libObjPtr(), late_msg,
+            &adjusted_message, stream_iter->last_inactivity_ts.value);
         break;
     case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
         adjust_status = adjust_discarded_packets_message(
-            lttng_live_msg_iter->self_msg_iter, stream_iter->stream, late_msg, &adjusted_message,
-            stream_iter->last_inactivity_ts.value);
+            lttng_live_msg_iter->self_msg_iter, stream_iter->stream->libObjPtr(), late_msg,
+            &adjusted_message, stream_iter->last_inactivity_ts.value);
         break;
     default:
         bt_common_abort();
@@ -1718,7 +1714,7 @@ lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
         }
 
         viewer_status = live_viewer_connection_create(
-            lttng_live->params.url->str, false, lttng_live_msg_iter, lttng_live_msg_iter->logger,
+            lttng_live->params.url.c_str(), false, lttng_live_msg_iter, lttng_live_msg_iter->logger,
             &lttng_live_msg_iter->viewer_connection);
         if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
             if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
@@ -1763,7 +1759,7 @@ lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
                     "Unable to connect to the requested live viewer session. "
                     "Keep trying to connect because of {}=\"{}\" component parameter: url=\"{}\"",
                     SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_CONTINUE_STR,
-                    lttng_live->params.url->str);
+                    lttng_live->params.url);
                 break;
             case SESSION_NOT_FOUND_ACTION_FAIL:
                 BT_CPPLOGE_APPEND_CAUSE_SPEC(
@@ -1772,7 +1768,7 @@ lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
                     "Fail the message iterator initialization because of {}=\"{}\" "
                     "component parameter: url =\"{}\"",
                     SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_FAIL_STR,
-                    lttng_live->params.url->str);
+                    lttng_live->params.url);
                 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
                 goto error;
             case SESSION_NOT_FOUND_ACTION_END:
@@ -1781,7 +1777,7 @@ lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
                                 "End gracefully at the first _next() call because of {}=\"{}\""
                                 " component parameter: url=\"{}\"",
                                 SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_END_STR,
-                                lttng_live->params.url->str);
+                                lttng_live->params.url);
                 break;
             default:
                 bt_common_abort();
@@ -1973,28 +1969,10 @@ end:
     }
 }
 
-static void lttng_live_component_destroy_data(struct lttng_live_component *lttng_live)
-{
-    if (!lttng_live) {
-        return;
-    }
-
-    if (lttng_live->params.url) {
-        g_string_free(lttng_live->params.url, TRUE);
-    }
-
-    delete lttng_live;
-}
-
 void lttng_live_component_finalize(bt_self_component_source *component)
 {
-    lttng_live_component *data = (lttng_live_component *) bt_self_component_get_data(
-        bt_self_component_source_as_self_component(component));
-
-    if (!data) {
-        return;
-    }
-    lttng_live_component_destroy_data(data);
+    lttng_live_component::UP {static_cast<lttng_live_component *>(
+        bt_self_component_get_data(bt_self_component_source_as_self_component(component)))};
 }
 
 static enum session_not_found_action
@@ -2033,42 +2011,32 @@ static struct bt_param_validation_map_value_entry_descr params_descr[] = {
 
 static bt_component_class_initialize_method_status
 lttng_live_component_create(const bt_value *params, bt_self_component_source *self_comp,
-                            struct lttng_live_component **component)
+                            lttng_live_component::UP& component)
 {
-    struct lttng_live_component *lttng_live = NULL;
     const bt_value *inputs_value;
     const bt_value *url_value;
     const bt_value *value;
-    const char *url;
     enum bt_param_validation_status validation_status;
     gchar *validation_error = NULL;
-    bt_component_class_initialize_method_status status;
     bt2c::Logger logger {bt2::SelfSourceComponent {self_comp}, "PLUGIN/SRC.CTF.LTTNG-LIVE/COMP"};
 
     validation_status = bt_param_validation_validate(params, params_descr, &validation_error);
     if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
-        status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
-        goto error;
+        return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
     } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
+        bt2c::GCharUP errorFreer {validation_error};
         BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "{}", validation_error);
-        status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
-        goto error;
+        return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
     }
 
-    lttng_live = new lttng_live_component {std::move(logger)};
+    auto lttng_live = bt2s::make_unique<lttng_live_component>(std::move(logger));
     lttng_live->self_comp = bt_self_component_source_as_self_component(self_comp);
     lttng_live->max_query_size = MAX_QUERY_SIZE;
     lttng_live->has_msg_iter = false;
 
     inputs_value = bt_value_map_borrow_entry_value_const(params, INPUTS_PARAM);
     url_value = bt_value_array_borrow_element_by_index_const(inputs_value, 0);
-    url = bt_value_string_get(url_value);
-
-    lttng_live->params.url = g_string_new(url);
-    if (!lttng_live->params.url) {
-        status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
-        goto error;
-    }
+    lttng_live->params.url = bt_value_string_get(url_value);
 
     value = bt_value_map_borrow_entry_value_const(params, SESS_NOT_FOUND_ACTION_PARAM);
     if (value) {
@@ -2080,17 +2048,8 @@ lttng_live_component_create(const bt_value *params, bt_self_component_source *se
         lttng_live->params.sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE;
     }
 
-    status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
-    goto end;
-
-error:
-    lttng_live_component_destroy_data(lttng_live);
-    lttng_live = NULL;
-end:
-    g_free(validation_error);
-
-    *component = lttng_live;
-    return status;
+    component = std::move(lttng_live);
+    return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
 }
 
 bt_component_class_initialize_method_status
@@ -2098,31 +2057,26 @@ lttng_live_component_init(bt_self_component_source *self_comp_src,
                           bt_self_component_source_configuration *, const bt_value *params, void *)
 {
     try {
-        struct lttng_live_component *lttng_live;
+        lttng_live_component::UP lttng_live;
         bt_component_class_initialize_method_status ret;
         bt_self_component *self_comp = bt_self_component_source_as_self_component(self_comp_src);
         bt_self_component_add_port_status add_port_status;
 
-        ret = lttng_live_component_create(params, self_comp_src, &lttng_live);
+        ret = lttng_live_component_create(params, self_comp_src, lttng_live);
         if (ret != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
-            goto error;
+            return ret;
         }
 
         add_port_status =
             bt_self_component_source_add_output_port(self_comp_src, "out", NULL, NULL);
         if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
             ret = (bt_component_class_initialize_method_status) add_port_status;
-            goto end;
+            return ret;
         }
 
-        bt_self_component_set_data(self_comp, lttng_live);
-        goto end;
+        bt_self_component_set_data(self_comp, lttng_live.release());
 
-error:
-        lttng_live_component_destroy_data(lttng_live);
-        lttng_live = NULL;
-end:
-        return ret;
+        return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
     } catch (const std::bad_alloc&) {
         return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
     } catch (const bt2::Error&) {
This page took 0.025487 seconds and 4 git commands to generate.