2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/types.h>
21 #include <rpc-port-internal.h>
23 #include "BenchmarkStub.h"
24 #include "log-private.hh"
27 constexpr const char SERVER_PROC_NAME[] =
28 "d::org.tizen.appfw.rpc_port.benchmark";
30 namespace bs = rpc_port::BenchmarkStub::stub;
40 void RemoveTermTimer();
48 class TestService : public bs::Benchmark::ServiceBase {
50 class Factory : public bs::Benchmark::ServiceBase::Factory {
52 explicit Factory(MainLoop* loop) : loop_(loop) {}
54 virtual ~Factory() = default;
56 std::unique_ptr<bs::Benchmark::ServiceBase>
57 CreateService(std::string sender, std::string instance) {
58 return std::unique_ptr<bs::Benchmark::ServiceBase>(
59 new TestService(std::move(sender), std::move(instance), loop_));
63 MainLoop* loop_ = nullptr;
66 TestService(std::string sender, std::string instance, MainLoop* loop)
67 : bs::Benchmark::ServiceBase(std::move(sender), std::move(instance)),
70 virtual ~TestService() = default;
72 void OnCreate() override {
73 loop_->RemoveTermTimer();
76 void OnTerminate() override {
77 loop_->SetTermTimer();
80 int Test(std::string data) override { return 0; }
83 MainLoop* loop_ = nullptr;
86 MainLoop::MainLoop() { loop_ = g_main_loop_new(nullptr, FALSE); }
88 MainLoop::~MainLoop() {
90 g_main_loop_unref(loop_);
93 void MainLoop::Run() {
94 std::string proc_name = std::string(SERVER_PROC_NAME);
95 if (getuid() >= 5000) proc_name = "u" + proc_name;
97 int ret = rpc_port_register_proc_info(proc_name.c_str(), nullptr);
98 if (ret != RPC_PORT_ERROR_NONE) {
99 _E("rpc_port_register_proc_info() failed (%d)", ret);
104 stub_.Listen(std::shared_ptr<bs::Benchmark::ServiceBase::Factory>(
105 new TestService::Factory(this)));
106 } catch (const bs::Exception&) {
107 _E("stub listen is failed");
110 g_main_loop_run(loop_);
113 void MainLoop::Quit() { g_main_loop_quit(loop_); }
115 void MainLoop::SetTermTimer() {
118 timer_ = g_timeout_add(
120 [](gpointer data) -> gboolean {
121 auto* main_loop = static_cast<MainLoop*>(data);
123 main_loop->timer_ = 0;
124 return G_SOURCE_REMOVE;
129 void MainLoop::RemoveTermTimer() {
131 g_source_remove(timer_);
138 int main(int argc, char** argv) {