Separate benchmark tool from rpc-port
[platform/core/appfw/rpc-port.git] / benchmark / server / dbus / main.cc
index fc2390e..c1be8f7 100644 (file)
@@ -33,7 +33,15 @@ constexpr const char INTROSPECTION_XML[] = R"__dbus(
     <method name='Test'>
       <arg type='s' name='data' direction='in'/>
       <arg type='i' name='ret' direction='out'/>
-      </method>
+    </method>
+    <method name='Start'>
+      <arg type='i' name='caller_pid' direction='in'/>
+      <arg type='i' name='ret' direction='out'/>
+    </method>
+    <method name='Stop'>
+      <arg type='i' name='caller_pid' direction='in'/>
+      <arg type='i' name='ret' direction='out'/>
+    </method>
   </interface>
 </node>
 )__dbus";
@@ -71,6 +79,24 @@ class MainLoop {
 
           GVariant* param = g_variant_new("(i)", ret);
           g_dbus_method_invocation_return_value(invocation, param);
+        } else if (std::string_view(method_name) == "Stop") {
+          gint caller_pid = -1;
+          g_variant_get(parameters, "(i)", &caller_pid);
+          _D("Stop request received from caller(%d)", caller_pid);
+          auto* main_loop = static_cast<MainLoop*>(user_data);
+          int ret = main_loop->SetTermTimer();
+
+          GVariant* param = g_variant_new("(i)", ret);
+          g_dbus_method_invocation_return_value(invocation, param);
+        } else if (std::string_view(method_name) == "Start") {
+          gint caller_pid = -1;
+          g_variant_get(parameters, "(i)", &caller_pid);
+          _D("Start request received from caller(%d)", caller_pid);
+          auto* main_loop = static_cast<MainLoop*>(user_data);
+          int ret = main_loop->UnsetTermTimer();
+
+          GVariant* param = g_variant_new("(i)", ret);
+          g_dbus_method_invocation_return_value(invocation, param);
         }
       },
       nullptr,
@@ -84,7 +110,8 @@ class MainLoop {
         &interface_vtable,
         this, nullptr, &error);
     if (reg_id == 0) {
-      _E("g_dbus_connection_register_object error(%s)", error ? error->message : "");
+      _E("g_dbus_connection_register_object error(%s)",
+          error ? error->message : "");
       g_error_free(error);
     }
   }
@@ -129,9 +156,33 @@ class MainLoop {
     g_main_loop_quit(loop_);
   }
 
+  int SetTermTimer() {
+    if (timer_) return 0;
+
+    timer_ = g_timeout_add(
+        5000, [](gpointer user_data) {
+          auto* main_loop = static_cast<MainLoop*>(user_data);
+          main_loop->Quit();
+          main_loop->timer_ = 0;
+          return G_SOURCE_REMOVE;
+        }, this);
+
+    return 0;
+  }
+
+  int UnsetTermTimer() {
+    if (timer_) {
+      g_source_remove(timer_);
+      timer_ = 0;
+    }
+
+    return 0;
+  }
+
  private:
   GMainLoop* loop_ = nullptr;
   GDBusNodeInfo* introspection_data_ = nullptr;
+  guint timer_ = 0;
 };
 
 }  // namespace