Fix Debug Port 09/265009/3
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 7 Oct 2021 01:53:38 +0000 (10:53 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 7 Oct 2021 03:37:18 +0000 (12:37 +0900)
After this patch is applied, the thread of the DebugPort is created when
the socket is connected to the tool(server). The rpc-port-util tool
sends the event at start time.

Requires:
 - https://review.tizen.org/gerrit/#/c/platform/core/appfw/amd/+/265008/

Change-Id: Ib5b58099cc541f12db6329324b9e71fa15435c01
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/debug-port-internal.cc
src/debug-port-internal.hh
utils/debug-port.cc
utils/main.cc

index 2f3219e..3d01bcd 100644 (file)
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+#include "debug-port-internal.hh"
+
+#include <bundle_internal.h>
 #include <parcel.hh>
 #include <sys/socket.h>
 #include <sys/types.h>
@@ -22,7 +25,6 @@
 
 #include <utility>
 
-#include "debug-port-internal.hh"
 #include "log-private.hh"
 
 #undef RPC_DTOR
@@ -32,7 +34,11 @@ namespace rpc_port {
 namespace internal {
 
 namespace {
-const char PATH_RPC_PORT_UTIL_SOCK[] = "/run/aul/daemons/.rpc-port-util-sock";
+
+constexpr const char PATH_RPC_PORT_UTIL_SOCK[] =
+    "/run/aul/daemons/.rpc-port-util-sock";
+constexpr const char ENDPOINT_RPC_PORT_DEBUG[] = "org.tizen.rpcport.debug";
+constexpr const char KEY_PORT_NAME[] = "__K_PORT_NAME__";
 
 RPC_DTOR void DebugPortDtor() {
   DebugPort::GetInst()->Dispose();
@@ -70,6 +76,11 @@ void DebugPort::Dispose() {
   if (disposed_)
     return;
 
+  if (conn_) {
+    aul_app_com_leave(conn_);
+    conn_ = nullptr;
+  }
+
   Unwatch();
   JoinThread();
   disposed_ = true;
@@ -108,14 +119,28 @@ std::shared_ptr<DebugPort::Session> DebugPort::FindSession(int port) {
       return s;
   }
 
-  return {};
+  return nullptr;
+}
+
+std::shared_ptr<DebugPort::Session> DebugPort::FindSession(
+    const std::string& port_name) {
+  std::lock_guard<std::recursive_mutex> lock(GetMutex());
+  for (auto& s : sessions_) {
+    if (s->GetPortName() == port_name)
+      return s;
+  }
+
+  return nullptr;
 }
 
 int DebugPort::Send(int port, bool is_read, uint32_t seq,
     const void* buf, unsigned int size) {
   std::lock_guard<std::recursive_mutex> lock(GetMutex());
+  if (!IsConnected())
+    return 0;
+
   auto session = FindSession(port);
-  if (session.get() == nullptr) {
+  if (session == nullptr) {
     _E("Failed to find session. port(%d)", port);
     return -1;
   }
@@ -137,7 +162,25 @@ int DebugPort::Send(int port, bool is_read, uint32_t seq,
 }
 
 void DebugPort::Init() {
-  CreateThread();
+  aul_app_com_create_async(ENDPOINT_RPC_PORT_DEBUG, nullptr, AppComCb, this,
+      &conn_);
+  if (conn_ == nullptr)
+    return;
+
+  do {
+    int fd = Connect();
+    if (fd < 0)
+      break;
+
+    port_.reset(new Port(fd, "Debug"));
+    if (Watch(fd) < 0)
+      break;
+
+    SetConnectionStatus(true);
+    _W("Connected");
+    CreateThread();
+  } while (0);
+
   disposed_ = false;
 }
 
@@ -232,22 +275,8 @@ void DebugPort::CreateThread() {
           break;
         }
 
-        if (!IsConnected()) {
-          int fd = Connect();
-          if (fd < 0)
-            continue;
-
-          {
-            std::lock_guard<std::recursive_mutex> lock(GetMutex());
-            port_.reset(new Port(fd, "Debug"));
-            int ret = Watch(fd);
-            if (ret < 0)
-              continue;
-
-            SetConnectionStatus(true);
-            _W("Connected");
-          }
-        }
+        if (!IsConnected())
+          continue;
 
         int ret = port_->Write(reinterpret_cast<void*>(&len), sizeof(len));
         if (ret < 0) {
@@ -278,5 +307,33 @@ void DebugPort::JoinThread() {
   }
 }
 
+int DebugPort::AppComCb(const char* endpoint, aul_app_com_result_e result,
+    bundle* envelope, void* user_data) {
+  const char* val = bundle_get_val(envelope, KEY_PORT_NAME);
+  if (val == nullptr)
+    return -1;
+
+  auto* handle = static_cast<DebugPort*>(user_data);
+  std::string port_name(val);
+  if (port_name.empty() || handle->FindSession(port_name) != nullptr) {
+    auto* handle = static_cast<DebugPort*>(user_data);
+    int fd = handle->Connect();
+    if (fd < 0)
+      return -1;
+
+    std::lock_guard<std::recursive_mutex> lock(handle->GetMutex());
+    handle->port_.reset(new Port(fd, "Debug"));
+    int ret = handle->Watch(fd);
+    if (ret < 0)
+      return -1;
+
+    handle->SetConnectionStatus(true);
+    _W("Connected");
+    handle->CreateThread();
+  }
+
+  return 0;
+}
+
 }  // namespace internal
 }  // namespace rpc_port
index 06ccdd4..1e8718d 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef DEBUG_PORT_INTERNAL_HH_
 #define DEBUG_PORT_INTERNAL_HH_
 
+#include <aul_app_com.h>
 #include <gio/gio.h>
 #include <glib.h>
 #include <parcel.hh>
@@ -104,9 +105,12 @@ class DebugPort {
   void JoinThread();
 
   std::shared_ptr<DebugPort::Session> FindSession(int port);
+  std::shared_ptr<DebugPort::Session> FindSession(const std::string& port_name);
 
   static gboolean OnDebugPortDisconnectedCb(GIOChannel* io,
       GIOCondition cond, gpointer data);
+  static int AppComCb(const char* endpoint, aul_app_com_result_e result,
+      bundle* envelope, void* user_data);
 
  private:
   bool disposed_ = true;
@@ -119,6 +123,7 @@ class DebugPort {
   std::atomic<bool> is_running_;
   SharedQueue<std::shared_ptr<tizen_base::Parcel>> queue_;
   mutable std::recursive_mutex rec_mutex_;
+  aul_app_com_connection_h conn_ = nullptr;
 };
 
 }  // namespace internal
index 9e8388a..b1c5fc7 100644 (file)
@@ -131,7 +131,7 @@ void DebugPort::OnDataReceived(int pid, rpc_port_parcel_h parcel) {
   if (port_name == nullptr)
     return;
 
-  if (port_name != port_name_)
+  if (!port_name_.empty() && port_name != port_name_)
     return;
 
   char* destination = nullptr;
index 68559fe..cdec65a 100644 (file)
  * limitations under the License.
  */
 
+#include <aul_app_com.h>
 #include <glib.h>
 
+#include <bundle_cpp.h>
+
 #include "debug-port.hh"
 #include "log-private.hh"
 #include "options.hh"
@@ -25,6 +28,9 @@ using namespace rpc_port::util;
 
 namespace {
 
+constexpr const char ENDPOINT_RPC_PORT_DEBUG[] = "org.tizen.rpcport.debug";
+constexpr const char KEY_PORT_NAME[] = "__K_PORT_NAME__";
+
 class MainLoop {
  public:
   MainLoop() {
@@ -47,6 +53,11 @@ class MainLoop {
   GMainLoop* loop_;
 };
 
+void SendEvent(const std::string& port_name) {
+  tizen_base::Bundle envelope { { KEY_PORT_NAME, port_name } };
+  aul_app_com_send(ENDPOINT_RPC_PORT_DEBUG, envelope.GetHandle());
+}
+
 }  // namespace
 
 int main(int argc, char** argv) {
@@ -57,6 +68,7 @@ int main(int argc, char** argv) {
   }
 
   DebugPort debug_port(options->GetPortName(), options->GetPid());
+  SendEvent(options->GetPortName());
   MainLoop loop;
   loop.Run();
   return 0;