Add disconnect API in C++ generator 51/263751/15
authorChanggyu Choi <changyu.choi@samsung.com>
Fri, 10 Sep 2021 07:14:48 +0000 (16:14 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Mon, 13 Sep 2021 07:58:40 +0000 (16:58 +0900)
Change-Id: Ie873e02416e3516293048dfa97eb98d23ba1572b
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
idlc/gen/cpp_proxy_body_gen.cc
idlc/gen/cpp_proxy_body_gen_cb.h
idlc/gen/cpp_proxy_header_gen_cb.h
idlc/gen/cpp_stub_body_gen.cc
idlc/gen/cpp_stub_body_gen_cb.h
idlc/gen/cpp_stub_header_gen_cb.h

index 7df3e8b94a3dd9db2ef4467bef030d8e9857ccb7..0ec6fd47fdd39ad62eb9bbaca75f56a15761b8e5 100644 (file)
@@ -171,6 +171,8 @@ void CppProxyBodyGen::GenInvocation(std::ofstream& stream,
     return;
   }
 
+  stream << Tab(1) << "int ret;" << NLine(1);
+
   auto parcel_read = [&]() -> std::string {
         std::string code;
         for (auto& i : decl.GetParameters().GetParams()) {
@@ -188,7 +190,7 @@ void CppProxyBodyGen::GenInvocation(std::ofstream& stream,
         if (decl.GetType().ToString() != "void") {
           code += AddIndent(TAB_SIZE * 2,
               ConvertTypeToDeserializer(decl.GetType(), "ret",
-                "parcel_received"));
+                "parcel_received", false));
         }
 
         return code;
index 96ba70890cc5c21e2a64ac3e92d3c07f3a2aae06..c9708bd4e799eef325d2b88714d457728626735a 100644 (file)
@@ -119,6 +119,14 @@ void ##::Connect(bool sync) {
   }
 }
 
+void ##::Disconnect() {
+  int ret = rpc_port_disconnect(port_);
+  if (ret != RPC_PORT_ERROR_NONE) {
+    _E("Failed to disconnect ##");
+    throw InvalidIDException();
+  }
+}
+
 void ##::DisposeCallback(const std::string& tag) {
   for (auto& i : delegate_list_) {
     if (i->GetTag() == tag) {
index aea484f1fbc946511d8aa99abda339b6df0b9cda..f1d198ac20c477b8ca4f80b3a3651f737ddf28ff 100644 (file)
@@ -77,6 +77,14 @@ R"__cpp_cb(
   /// <remark> If you want to use this method, you must add privileges.</remark>
   void Connect(bool sync = false);
 
+  /// <summary>
+  /// Disconnects from the service app.
+  /// </summary>
+  /// <exception cref="InvalidIDException">
+  /// Thrown when the stub port is invalid.
+  /// </exception>
+  void Disconnect();
+
   /// <summary>
   /// Disposes delegate objects in this interface
   /// </summary>
index e460964fc87ff45581993899b32b81f7c63ecfac..3e50889f35e4d357b6df5f79c03fa0b25dd1d00f 100644 (file)
@@ -97,6 +97,13 @@ void CppStubBodyGen::GenServiceBase(std::ofstream& stream,
       return iface.GetID();
     });
   stream << NLine(1);
+  GenTemplate(CB_SERVICE_DEFAULT_METHOD, stream,
+    [&]()->std::string {
+      return iface.GetID();
+    }, [&]()->std::string {
+      return iface.GetID();
+    });
+  stream << NLine(1);
 }
 
 void CppStubBodyGen::GenConstructor(std::ofstream& stream,
@@ -153,13 +160,9 @@ void CppStubBodyGen::GenReceivedEvent(std::ofstream& stream,
   if (options_->IsThreadEnabled()) {
     stream << CB_RUN_PENDING_JOB_BACK << NLine(1);
 
-    GenTemplate(CB_ON_RECEIVED_CB, stream,
-      [&]()->std::string {
-        return iface.GetID();
-      },
-      [&]()->std::string {
-        return iface.GetID();
-      });
+    std::string code = ReplaceAll(CB_ON_RECEIVED_CB, "[NAME]",
+        iface.GetID());
+    stream << code;
   } else {
     stream << CB_ON_RECEIVED_CB_BACK << NLine(1);
   }
index deea256ee14b5f1f20eca4e6059446acad5e1225..ee8959f8e82988dc85dcc806aafb50d16961d70b 100644 (file)
@@ -55,9 +55,18 @@ void ##::Listen(std::shared_ptr<##::ServiceBase::Factory> service_factory) {
   }
 }
 
-void ##::OnConnectedCB(const char* sender, const char* instance, void *data) {
+void ##::OnConnectedCB(const char* sender, const char* instance, voiddata) {
   ##* stub = static_cast<##*>(data);
   auto s = stub->service_factory_->CreateService(sender, instance);
+
+  rpc_port_h port;
+  int ret = rpc_port_stub_get_port(stub->stub_, RPC_PORT_PORT_CALLBACK, instance.c_str(), &port);
+  if (ret != RPC_PORT_ERROR_NONE) {
+    _E("Failed to get the port(%d)", ret);
+    return;
+  }
+
+  s->SetPort(port);
   s->OnCreate();
   stub->services_.emplace_back(std::move(s));
 }
@@ -73,7 +82,6 @@ void ##::OnDisconnectedCB(const char* sender, const char* instance, void *data)
     }
   }
 }
-
 )__cpp_cb";
 
 const char CB_DEFAULT_THREAD_METHODS[] =
@@ -126,7 +134,7 @@ int [NAME]::OnReceivedCB(const char* sender, const char* instance, rpc_port_h po
   }
 
   if (b.get() == nullptr) {
-    _E("Failed to find $$ context(%s)", instance);
+    _E("Failed to find [NAME] context(%s)", instance);
     return -1;
   }
 
@@ -201,11 +209,14 @@ R"__cpp_cb(    default:
 }
 )__cpp_cb";
 
+/**
+ * [NAME] The name of the interface.
+ */
 const char CB_ON_RECEIVED_CB[] =
 R"__cpp_cb(
-int $$::OnReceivedCB(const char* sender, const char* instance, rpc_port_h port, void *data)
+int [NAME]::OnReceivedCB(const char* sender, const char* instance, rpc_port_h port, void *data)
 {
-  auto* cxt = static_cast<$$*>(data);
+  auto* cxt = static_cast<[NAME]*>(data);
   std::shared_ptr<ServiceBase> b;
   rpc_port_parcel_h parcel;
   rpc_port_h callback_port;
@@ -218,7 +229,7 @@ int $$::OnReceivedCB(const char* sender, const char* instance, rpc_port_h port,
   }
 
   if (b.get() == nullptr) {
-    _E("Failed to find $$ context(%s)", instance);
+    _E("Failed to find [NAME] context(%s)", instance);
     return -1;
   }
 
@@ -242,6 +253,23 @@ int $$::OnReceivedCB(const char* sender, const char* instance, rpc_port_h port,
 const char CB_CTOR_SERVICE_BASE[] =
 R"__cpp_cb($$::ServiceBase::ServiceBase(std::string sender, std::string instance)
     : sender_(std::move(sender)), instance_(std::move(instance)),
-      active_object_(new ActiveObject()) {})__cpp_cb";
+      active_object_(new ActiveObject()) {}
+)__cpp_cb";
+
+const char CB_SERVICE_DEFAULT_METHOD[] =
+R"__cpp_cb(void $$::ServiceBase::SetPort(rpc_port_h port) {
+  port_ = port;
+}
+
+void $$::ServiceBase::Disconnect() {
+  int ret = rpc_port_disconnect(port_);
+  if (ret == RPC_PORT_ERROR_NONE) {
+    _E("Failed to disconnect the port(%d)", ret);
+    return;
+  }
+
+  port_ = nullptr;
+}
+)__cpp_cb";
 
 #endif  // IDLC_CPP_GEN_CPP_STUB_BODY_GEN_CB_H_
index b4db8a5503de28238e704dc59f76646722b9935c..fde4fdae687ce63720878839d082ded4c5087aa9 100644 (file)
@@ -66,6 +66,20 @@ R"__cpp_cb(
       return instance_;
     }
 
+    /// <summary>
+    /// Sets the client app port
+    /// </summary>
+    /// <param name="port">The port of the client</param>
+    void SetPort(rpc_port_h port);
+
+    /// <summary>
+    /// Disconnects from the client app
+    /// </summary>
+    /// <exception cref="InvalidIOException">
+    /// Thrown when internal I/O error happen.
+    /// </exception>
+    void Disconnect();
+
     /// <summary>
     /// This method will be called when the client is connected
     /// </summary>
@@ -107,6 +121,7 @@ R"__cpp_cb(
    private:
     std::string sender_;
     std::string instance_;
+    rpc_port_h port_;
     std::unique_ptr<ActiveObject> active_object_;
   };)__cpp_cb";
 
@@ -133,7 +148,6 @@ R"__cpp_cb(  ##();
 
 )__cpp_cb";
 
-
 const char CB_JOB[] = R"__cls_job(class Job {
  public:
   class IEvent {