Modify LocalExecutionMode of C++ Generator 01/298601/5
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 11 Sep 2023 08:34:00 +0000 (17:34 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 11 Sep 2023 22:33:45 +0000 (07:33 +0900)
If the client calls the Connect() method twice, the method returns
an error value.
The rpc_port_stub_<INPUT_FILE>_lem_xxx() functions uses g_idle_add() if
the caller thread ID is not equal to the process ID.

Change-Id: I5e91b6df7367293f6e021028ed78c060b2d125f9
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
idlc/gen/version2/cpp_generator_base.cc
idlc/gen/version2/cpp_proxy_body_generator.cc
idlc/gen/version2/cpp_proxy_body_generator_cb.hh
idlc/gen/version2/cpp_proxy_header_generator_cb.hh
idlc/gen/version2/cpp_stub_body_generator.cc
idlc/gen/version2/cpp_stub_body_generator_cb.hh

index 6addc1e1edc50f709f36e49064b679a99ff0f718..61f8b7ab093b4013f9150fdfb604b3be3b9c610e 100644 (file)
@@ -458,7 +458,16 @@ std::string CppGeneratorBase::SmartIndent(const std::string& str) {
 
     if (trimmed_line.find('{') != std::string::npos) {
       control_flow_statement_with_single_line = false;
+      bool else_statement = false;
+      if (trimmed_line.find("} else {") != std::string::npos) {
+        else_statement = true;
+        indentation = indentation.substr(0, indentation.length() - 2);
+      }
+
       code += indentation + trimmed_line + NLine(1);
+      if (else_statement)
+        indentation += Tab(1);
+
       if (trimmed_line.find("namespace ") == std::string::npos &&
           trimmed_line.find('}') == std::string::npos)
         indentation += Tab(1);
index 8b1e29d5988b5906a3cdee692ebfabdeb9269bf5..ec246310db7793b8da7b64b3ee1e78c984867d8b 100644 (file)
@@ -66,10 +66,12 @@ void CppProxyBodyGenerator::GenNamespace(std::ofstream& stream) {
       .Change("<INTERFACES>", GenInterfaces())
       .Transform([&](std::string str) { return SmartIndent(str); })
       .Out(stream);
+  stream << NLine(1);
 }
 
 void CppProxyBodyGenerator::GenLemAnonymousNamespace(std::ofstream& stream) {
   stream << SmartIndent(CB_LEM_ANONYMOUS_NAMESPACE);
+  stream << NLine(1);
 }
 
 void CppProxyBodyGenerator::GenLemAPI(std::ofstream& stream) {
index c3b73e1ec3545359dbfd2147174b875511c29a9f..7e3028db482ed517bfb05564ecfb581e69654eb4 100644 (file)
@@ -141,17 +141,16 @@ R"__cpp_cb(
 }
 
 void <CLS_NAME>::Connect(bool sync) {
+  int ret;
   if (local_execution_.get() != nullptr && local_execution_->LoadSymbols()) {
-    if (local_execution_->Connect(sync))
-      return;
+    ret = local_execution_->Connect(sync);
+  } else {
+    if (sync)
+      ret = rpc_port_proxy_connect_sync(proxy_, target_appid_.c_str(), "<CLS_NAME>");
+    else
+      ret = rpc_port_proxy_connect(proxy_, target_appid_.c_str(), "<CLS_NAME>");
   }
 
-  int ret;
-  if (sync)
-    ret = rpc_port_proxy_connect_sync(proxy_, target_appid_.c_str(), "<CLS_NAME>");
-  else
-    ret = rpc_port_proxy_connect(proxy_, target_appid_.c_str(), "<CLS_NAME>");
-
   if (ret != RPC_PORT_ERROR_NONE) {
     _E("Failed to connect to <CLS_NAME>. error(%d)", ret);
     switch (ret) {
@@ -506,16 +505,31 @@ LocalExecution::~LocalExecution() {
   }
 }
 
-bool LocalExecution::Connect(bool sync) {
+int LocalExecution::Connect(bool sync) {
+  std::lock_guard<std::recursive_mutex> lock(mutex_);
+  if (connecting_) {
+     _E("Already connecting");
+    return RPC_PORT_ERROR_INVALID_PARAMETER;
+  }
+
+  if (connected_) {
+    _E("Already connected");
+    return RPC_PORT_ERROR_INVALID_PARAMETER;
+  }
+
   if (connect_func_) {
-    if (connect_func_(this, GetAppId().c_str(), instance_.c_str(), sync) != RPC_PORT_ERROR_NONE)
-      return false;
+    int ret = connect_func_(this, GetAppId().c_str(), instance_.c_str(), sync);
+    if (ret != RPC_PORT_ERROR_NONE)
+      return ret;
   }
 
-  return true;
+  connecting_ = true;
+  return RPC_PORT_ERROR_NONE;
 }
 
 void LocalExecution::Disconnect() {
+  std::lock_guard<std::recursive_mutex> lock(mutex_);
+  connecting_ = false;
   if (disconnect_func_)
     disconnect_func_(this, GetAppId().c_str(), instance_.c_str());
 }
@@ -544,6 +558,7 @@ int LocalExecution::Send(rpc_port_parcel_h request, rpc_port_parcel_h* result) {
 
 void LocalExecution::OnConnected() {
   std::lock_guard<std::recursive_mutex> lock(mutex_);
+  connecting_ = false;
   connected_ = true;
   if (listener_ != nullptr)
     listener_->OnLocalConnected();
@@ -551,6 +566,7 @@ void LocalExecution::OnConnected() {
 
 void LocalExecution::OnDisconnected() {
   std::lock_guard<std::recursive_mutex> lock(mutex_);
+  connecting_ = false;
   connected_ = false;
   if (listener_ != nullptr)
     listener_->OnLocalDisconnected();
index a6f7886950fe584c71fd9681153eeb2184056462..098431f864b02e8618b82d00d4f018bb3b74d5fc 100644 (file)
@@ -196,7 +196,7 @@ class LocalExecution : public std::enable_shared_from_this<LocalExecution> {
   LocalExecution(std::string port_name, IEvent* listener);
   virtual ~LocalExecution();
 
-  bool Connect(bool sync);
+  int Connect(bool sync);
   void Disconnect();
   int Send(rpc_port_parcel_h request, rpc_port_parcel_h* result);
 
@@ -226,6 +226,7 @@ class LocalExecution : public std::enable_shared_from_this<LocalExecution> {
   IEvent* listener_;
   std::string instance_;
   bool connected_ = false;
+  bool connecting_ = false;
   bool loaded_ = false;
   StubConnectFunc connect_func_ = nullptr;
   StubDisconnectFunc disconnect_func_ = nullptr;
index 255fbb8072cff11ea9d1506275f7afeb5983777a..793d54deaf0bccdbe02121ffd943d14fffdbe559 100644 (file)
@@ -66,6 +66,7 @@ void CppStubBodyGenerator::GenNamespace(std::ofstream& stream) {
       .Change("<INTERFACES>", GenInterfaces())
       .Transform([&](std::string str) { return SmartIndent(str); })
       .Out(stream);
+  stream << NLine(1);
 }
 
 void CppStubBodyGenerator::GenLemAnonymousNamespace(std::ofstream& stream) {
@@ -73,6 +74,7 @@ void CppStubBodyGenerator::GenLemAnonymousNamespace(std::ofstream& stream) {
       .Change("<LEM_CONTEXT>", GenLemContext())
       .Transform([&](std::string code) { return SmartIndent(code); })
       .Out(stream);
+  stream << NLine(1);
 }
 
 std::string CppStubBodyGenerator::GenLemContext() {
index c1e6f593053ba2c462194b4cafb81503ad2b6a65..5cf1a7d923e158fa6ad932fa99059deeb5d911b7 100644 (file)
@@ -413,8 +413,7 @@ void <CLS_NAME>::OnLocalReceived(void* context, rpc_port_parcel_h parcel) {
     return;
   }
 
-  rpc_port_parcel_h p = Clone(parcel);
-  service->Dispatch(nullptr, nullptr, p, service);
+  service->Dispatch(nullptr, nullptr, parcel, service);
 }
 )__cpp_cb";
 
@@ -574,6 +573,15 @@ rpc_port_parcel_h Clone(rpc_port_parcel_h parcel) {
   return handle;
 }
 
+void IdleAddOnce(std::function<void()>* func) {
+  g_idle_add([](gpointer user_data) {
+        auto* cb = static_cast<std::function<void()>*>(user_data);
+        (*cb)();
+        delete cb;
+        return G_SOURCE_REMOVE;
+      }, func);
+}
+
 <LEM_CONTEXT>
 
 }  // namespace
@@ -689,7 +697,17 @@ EXPORT_API int rpc_port_stub_<INPUT_FILE>_lem_<CLS_NAME>_connect(void* context,
     return RPC_PORT_ERROR_IO_ERROR;
   }
 
-  <CLS_NAME>_context_->OnConnected(context, sender, instance, sync);
+  if (gettid() != getpid()) {
+    std::string sender_str(sender);
+    std::string instance_str(instance);
+    auto* func = new std::function<void()>([context, sender_str, instance_str, sync] {
+          <CLS_NAME>_context_->OnConnected(context, sender_str, instance_str, sync);
+        });
+    IdleAddOnce(func);
+  } else {
+    <CLS_NAME>_context_->OnConnected(context, sender, instance, sync);
+  }
+
   return RPC_PORT_ERROR_NONE;
 }
 
@@ -699,7 +717,17 @@ EXPORT_API int rpc_port_stub_<INPUT_FILE>_lem_<CLS_NAME>_disconnect(void* contex
     return RPC_PORT_ERROR_IO_ERROR;
   }
 
-  <CLS_NAME>_context_->OnDisconnected(context, sender, instance);
+  if (gettid() != getpid()) {
+    std::string sender_str(sender);
+    std::string instance_str(instance);
+    auto* func = new std::function<void()>([context, sender_str, instance_str] {
+          <CLS_NAME>_context_->OnDisconnected(context, sender_str, instance_str);
+        });
+    IdleAddOnce(func);
+  } else {
+    <CLS_NAME>_context_->OnDisconnected(context, sender, instance);
+  }
+
   return RPC_PORT_ERROR_NONE;
 }
 
@@ -709,7 +737,16 @@ EXPORT_API int rpc_port_stub_<INPUT_FILE>_lem_<CLS_NAME>_send(void* context, rpc
     return RPC_PORT_ERROR_IO_ERROR;
   }
 
-  <CLS_NAME>_context_->OnReceived(context, parcel);
+  rpc_port_parcel_h cloned_parcel = Clone(parcel);
+  if (getpid() != getpid()) {
+    auto* func = new std::function<void()>([context, cloned_parcel] {
+          <CLS_NAME>_context_->OnReceived(context, cloned_parcel);
+        });
+    IdleAddOnce(func);
+  } else {
+    <CLS_NAME>_context_->OnReceived(context, cloned_parcel);
+  }
+
   return RPC_PORT_ERROR_NONE;
 }
 )__cpp_cb";