Separate benchmark tool from rpc-port
[platform/core/appfw/rpc-port.git] / benchmark / server / tidl / main.cc
index 7a084f6..803f4d3 100644 (file)
@@ -29,73 +29,109 @@ constexpr const char SERVER_PROC_NAME[] =
 
 namespace bs = rpc_port::BenchmarkStub::stub;
 
+class MainLoop {
+ public:
+  MainLoop();
+  ~MainLoop();
+
+  void Run();
+  void Quit();
+  void SetTermTimer();
+  void RemoveTermTimer();
+
+ private:
+  GMainLoop* loop_;
+  bs::Benchmark stub_;
+  guint timer_ = 0;
+};
+
 class TestService : public bs::Benchmark::ServiceBase {
  public:
   class Factory : public bs::Benchmark::ServiceBase::Factory {
    public:
+    explicit Factory(MainLoop* loop) : loop_(loop) {}
+
     virtual ~Factory() = default;
 
     std::unique_ptr<bs::Benchmark::ServiceBase>
         CreateService(std::string sender, std::string instance) {
       return std::unique_ptr<bs::Benchmark::ServiceBase>(
-          new TestService(std::move(sender), std::move(instance)));
+          new TestService(std::move(sender), std::move(instance), loop_));
     }
+
+   private:
+    MainLoop* loop_ = nullptr;
   };
 
-  TestService(std::string sender, std::string instance) :
-      bs::Benchmark::ServiceBase(std::move(sender), std::move(instance)) {}
+  TestService(std::string sender, std::string instance, MainLoop* loop)
+      : bs::Benchmark::ServiceBase(std::move(sender), std::move(instance)),
+        loop_(loop) {}
 
   virtual ~TestService() = default;
 
   void OnCreate() override {
+    loop_->RemoveTermTimer();
   }
 
   void OnTerminate() override {
+    loop_->SetTermTimer();
   }
 
-  int Test(std::string data) override {
-    return 0;
-  }
+  int Test(std::string data) override { return 0; }
+
+ private:
+  MainLoop* loop_ = nullptr;
 };
 
-class MainLoop {
- public:
-  MainLoop() {
-    loop_ = g_main_loop_new(nullptr, FALSE);
+MainLoop::MainLoop() { loop_ = g_main_loop_new(nullptr, FALSE); }
+
+MainLoop::~MainLoop() {
+  RemoveTermTimer();
+  g_main_loop_unref(loop_);
+}
+
+void MainLoop::Run() {
+  std::string proc_name = std::string(SERVER_PROC_NAME);
+  if (getuid() >= 5000) proc_name = "u" + proc_name;
+
+  int ret = rpc_port_register_proc_info(proc_name.c_str(), nullptr);
+  if (ret != RPC_PORT_ERROR_NONE) {
+    _E("rpc_port_register_proc_info() failed (%d)", ret);
+    return;
   }
 
-  ~MainLoop() {
-    g_main_loop_unref(loop_);
+  try {
+    stub_.Listen(std::shared_ptr<bs::Benchmark::ServiceBase::Factory>(
+        new TestService::Factory(this)));
+  } catch (const bs::Exception&) {
+    _E("stub listen is failed");
   }
 
-  void Run() {
-    std::string proc_name = std::string(SERVER_PROC_NAME);
-    if (getuid() >= 5000) proc_name = "u" + proc_name;
+  g_main_loop_run(loop_);
+}
 
-    int ret = rpc_port_register_proc_info(proc_name.c_str(), nullptr);
-    if (ret != RPC_PORT_ERROR_NONE) {
-      _E("rpc_port_register_proc_info() failed (%d)", ret);
-      return;
-    }
+void MainLoop::Quit() { g_main_loop_quit(loop_); }
 
-    try {
-      stub_.Listen(std::shared_ptr<bs::Benchmark::ServiceBase::Factory>(
-          new TestService::Factory()));
-    } catch (const bs::Exception&) {
-      _E("stub listen is failed");
-    }
+void MainLoop::SetTermTimer() {
+  if (timer_) return;
 
-    g_main_loop_run(loop_);
-  }
+  timer_ = g_timeout_add(
+      5000,
+      [](gpointer data) -> gboolean {
+        auto* main_loop = static_cast<MainLoop*>(data);
+        main_loop->Quit();
+        main_loop->timer_ = 0;
+        return G_SOURCE_REMOVE;
+      },
+      this);
+}
 
-  void Quit() {
-    g_main_loop_quit(loop_);
+void MainLoop::RemoveTermTimer() {
+  if (timer_) {
+    g_source_remove(timer_);
+    timer_ = 0;
   }
-
- private:
-  GMainLoop* loop_;
-  bs::Benchmark stub_;
-};
+}
 
 }  // namespace