Remove GDBus dependency from rpc-port library 02/264202/3
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 14 Sep 2021 10:14:36 +0000 (19:14 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 14 Sep 2021 10:19:53 +0000 (19:19 +0900)
The rpc-port was used the GDBusConnection to notify the presence of debug port.
After this patch is applied, the rpc-port library doesn't use the GDBusConnection.
The thread of the debug-port tries to connect to the debug port of the tool
automatically if it's not connected.

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

index ab660a3..8e182fe 100644 (file)
@@ -30,10 +30,6 @@ namespace internal {
 
 namespace {
 const char PATH_RPC_PORT_UTIL_SOCK[] = "/run/aul/daemons/.rpc-port-util-sock";
-const char RPC_PORT_SIGNAL_PATH[] = "/Org/Tizen/RPC/Port/Signal";
-const char RPC_PORT_SIGNAL_INTERFACE[] = "org.tizen.rpc.port.signal";
-const char RPC_PORT_SIGNAL_DEBUG[] = "Debug";
-const char RPC_PORT_SIGNAL_NEW[] = "New";
 }  // namespace
 
 std::atomic<DebugPort*> DebugPort::inst_;
@@ -64,7 +60,6 @@ DebugPort* DebugPort::GetInst() {
 
 void DebugPort::Dispose() {
   Unwatch();
-  Unsubscribe();
   JoinThread();
   disposed_ = true;
 }
@@ -108,9 +103,6 @@ std::shared_ptr<DebugPort::Session> DebugPort::FindSession(int port) {
 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 -1;
-
   auto session = FindSession(port);
   if (session.get() == nullptr) {
     _E("Failed to find session. port(%d)", port);
@@ -134,9 +126,7 @@ int DebugPort::Send(int port, bool is_read, uint32_t seq,
 }
 
 void DebugPort::Init() {
-  Subscribe();
-  EmitSignal(RPC_PORT_SIGNAL_NEW);
-  is_running_ = false;
+  CreateThread();
   disposed_ = false;
 }
 
@@ -184,15 +174,15 @@ int DebugPort::Watch(int fd) {
 }
 
 void DebugPort::Unwatch() {
-  if (watch_tag_) {
-    g_source_remove(watch_tag_);
-    watch_tag_ = 0;
-  }
-
   if (io_) {
     g_io_channel_unref(io_);
     io_ = nullptr;
   }
+
+  if (watch_tag_) {
+    g_source_remove(watch_tag_);
+    watch_tag_ = 0;
+  }
 }
 
 gboolean DebugPort::OnDebugPortDisconnectedCb(GIOChannel* io,
@@ -208,122 +198,6 @@ gboolean DebugPort::OnDebugPortDisconnectedCb(GIOChannel* io,
   return G_SOURCE_REMOVE;
 }
 
-GDBusConnection* DebugPort::GetConnection() {
-  if (conn_)
-    return conn_;
-
-  GError* error = nullptr;
-  conn_ = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error);
-  if (conn_ == nullptr) {
-    _E("g_bus_get_sync() is failed. error(%s)",
-        error ? error->message : "unknown");
-    g_clear_error(&error);
-    return nullptr;
-  }
-
-  return conn_;
-}
-
-void DebugPort::Subscribe() {
-  GDBusConnection* conn = GetConnection();
-  if (conn == nullptr)
-    return;
-
-  subs_tag_ = g_dbus_connection_signal_subscribe(conn,
-      nullptr,
-      RPC_PORT_SIGNAL_INTERFACE,
-      RPC_PORT_SIGNAL_DEBUG,
-      RPC_PORT_SIGNAL_PATH,
-      nullptr,
-      G_DBUS_SIGNAL_FLAGS_NONE,
-      OnGDBusSignalCb,
-      this,
-      nullptr);
-  if (subs_tag_ == 0) {
-    _E("g_dbus_connection_signal_subscribe() is failed");
-    return;
-  }
-
-  _D("tag(%u)", subs_tag_);
-}
-
-void DebugPort::Unsubscribe() {
-  if (subs_tag_)
-    g_dbus_connection_signal_unsubscribe(conn_, subs_tag_);
-
-  if (conn_)
-    g_object_unref(conn_);
-}
-
-void DebugPort::EmitSignal(std::string signal) {
-  GDBusConnection* conn = GetConnection();
-  if (conn == nullptr)
-    return;
-
-  GError* error = nullptr;
-  gboolean ret = g_dbus_connection_emit_signal(conn,
-      nullptr,
-      RPC_PORT_SIGNAL_PATH,
-      RPC_PORT_SIGNAL_INTERFACE,
-      signal.c_str(),
-      nullptr,
-      &error);
-  if (ret != TRUE) {
-    _E("g_dbus_connection_emit_signal() is failed. error(%s)",
-        error ? error->message : "unknown");
-    g_clear_error(&error);
-    return;
-  }
-
-  ret = g_dbus_connection_flush_sync(conn, nullptr, &error);
-  if (ret != TRUE) {
-    _E("g_dbus_connection_flush_sync() is failed. error(%s)",
-        error ? error->message : "unknown");
-    g_clear_error(&error);
-    return;
-  }
-
-  _W("EmitSignal(%s)", signal.c_str());
-}
-
-void DebugPort::OnGDBusSignalCb(GDBusConnection *connection,
-    const gchar* sender_name,
-    const gchar* object_path,
-    const gchar* interface_name,
-    const gchar* signal_name,
-    GVariant* parameters,
-    gpointer user_data) {
-  _W("signal_name(%s)", signal_name);
-  std::string signal(signal_name);
-  if (signal != RPC_PORT_SIGNAL_DEBUG)
-    return;
-
-  gchar* port_name = nullptr;
-  gint pid = -1;
-  g_variant_get(parameters, "(&si)", &port_name, &pid);
-  _W("port_name(%s), pid(%d)", port_name, pid);
-
-  if (pid != 0 && pid != getpid()) {
-    _W("Invalid pid(%d)", pid);
-    return;
-  }
-
-  auto* debug_port = static_cast<DebugPort*>(user_data);
-  int fd = debug_port->Connect();
-  if (fd < 0)
-    return;
-
-  std::lock_guard<std::recursive_mutex> lock(debug_port->GetMutex());
-  debug_port->port_.reset(new Port(fd, signal));
-  int ret = debug_port->Watch(fd);
-  if (ret < 0)
-    return;
-
-  debug_port->CreateThread();
-  debug_port->SetConnectionStatus(true);
-  _W("Connected");
-}
-
 void DebugPort::SetConnectionStatus(bool status) {
   std::lock_guard<std::recursive_mutex> lock(GetMutex());
   connected_ = status;
@@ -344,8 +218,22 @@ void DebugPort::CreateThread() {
           break;
         }
 
-        if (!IsConnected())
-          continue;
+        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");
+          }
+        }
 
         int ret = port_->Write(reinterpret_cast<void*>(&len), sizeof(len));
         if (ret < 0) {
index ac63f41..06ccdd4 100644 (file)
@@ -99,10 +99,6 @@ class DebugPort {
   int Connect();
   int Watch(int fd);
   void Unwatch();
-  GDBusConnection* GetConnection();
-  void Subscribe();
-  void Unsubscribe();
-  void EmitSignal(std::string signal);
   void SetConnectionStatus(bool status);
   void CreateThread();
   void JoinThread();
@@ -111,19 +107,10 @@ class DebugPort {
 
   static gboolean OnDebugPortDisconnectedCb(GIOChannel* io,
       GIOCondition cond, gpointer data);
-  static void OnGDBusSignalCb(GDBusConnection *connection,
-      const gchar* sender_name,
-      const gchar* object_path,
-      const gchar* interface_name,
-      const gchar* signal_name,
-      GVariant* parameters,
-      gpointer user_data);
 
  private:
   bool disposed_ = true;
   bool connected_ = false;
-  GDBusConnection* conn_ = nullptr;
-  guint subs_tag_ = 0;
   std::unique_ptr<Port> port_;
   GIOChannel* io_ = nullptr;
   guint watch_tag_ = 0;
index 202d9da..9e8388a 100644 (file)
 
 namespace rpc_port {
 namespace util {
+namespace {
 
-static const char PATH_RPC_PORT_UTIL_SOCK[] =
+constexpr const char PATH_RPC_PORT_UTIL_SOCK[] =
     "/run/aul/daemons/.rpc-port-util-sock";
-static const char RPC_PORT_SIGNAL_PATH[] = "/Org/Tizen/RPC/Port/Signal";
-static const char RPC_PORT_SIGNAL_INTERFACE[] = "org.tizen.rpc.port.signal";
-static const char RPC_PORT_SIGNAL_DEBUG[] = "Debug";
-static const char RPC_PORT_SIGNAL_NEW[] = "New";
+
+}  // namespace
 
 class DisconnectedEvent {
  public:
@@ -66,8 +65,6 @@ class DisconnectedEvent {
 DebugPort::DebugPort(std::string port_name, int pid)
   : port_name_(std::move(port_name)),
     pid_(pid) {
-  Subscribe();
-
   int fd = CreateSocket();
   if (fd < 0) {
     _E("Failed to create socket");
@@ -83,86 +80,6 @@ DebugPort::~DebugPort() {
 
   if (fd_ > 0)
     close(fd_);
-
-  Unsubscribe();
-}
-
-void DebugPort::EmitSignal() {
-  GDBusConnection* conn = GetConnection();
-  if (conn == nullptr)
-    return;
-
-  GError* error = nullptr;
-  gboolean ret = g_dbus_connection_emit_signal(conn,
-      nullptr,
-      RPC_PORT_SIGNAL_PATH,
-      RPC_PORT_SIGNAL_INTERFACE,
-      RPC_PORT_SIGNAL_DEBUG,
-      g_variant_new("(si)", port_name_.c_str(), pid_),
-      &error);
-  if (ret != TRUE) {
-    _E("g_dbus_connection_emit_signal() is failed. error(%s)",
-        error ? error->message : "Unknown");
-    g_clear_error(&error);
-    return;
-  }
-
-  ret = g_dbus_connection_flush_sync(conn, nullptr, &error);
-  if (ret != TRUE) {
-    _E("g_dbus_connection_flush_sync() is failed. error(%s)",
-        error ? error->message : "Unknown");
-    g_clear_error(&error);
-    return;
-  }
-
-  _W("EmitSignal");
-}
-
-GDBusConnection* DebugPort::GetConnection() {
-  if (conn_)
-    return conn_;
-
-  GError* error = nullptr;
-  conn_ = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error);
-  if (conn_ == nullptr) {
-    _E("g_bus_get_sync() is failed. error(%s)",
-        error ? error->message : "Unknown");
-    g_clear_error(&error);
-    return nullptr;
-  }
-
-  return conn_;
-}
-
-void DebugPort::Subscribe() {
-  GDBusConnection* conn = GetConnection();
-  if (conn == nullptr)
-    return;
-
-  subs_tag_ = g_dbus_connection_signal_subscribe(conn,
-      nullptr,
-      RPC_PORT_SIGNAL_INTERFACE,
-      RPC_PORT_SIGNAL_NEW,
-      RPC_PORT_SIGNAL_PATH,
-      nullptr,
-      G_DBUS_SIGNAL_FLAGS_NONE,
-      OnGDBusSignalCb,
-      this,
-      nullptr);
-  if (subs_tag_ == 0) {
-    _E("g_dbus_connection_signal_subscribe() is failed");
-    return;
-  }
-
-  _W("tag(%u)", subs_tag_);
-}
-
-void DebugPort::Unsubscribe() {
-  if (subs_tag_)
-    g_dbus_connection_signal_unsubscribe(conn_, subs_tag_);
-
-  if (conn_)
-    g_object_unref(conn_);
 }
 
 int DebugPort::CreateSocket() {
@@ -267,22 +184,6 @@ void DebugPort::OnDisconnected(int pid, int fd) {
       }, event);
 }
 
-void DebugPort::OnGDBusSignalCb(GDBusConnection *connection,
-    const gchar* sender_name,
-    const gchar* object_path,
-    const gchar* interface_name,
-    const gchar* signal_name,
-    GVariant* parameters,
-    gpointer user_data) {
-  _E("signal_name(%s)", signal_name);
-  std::string signal(signal_name);
-  if (signal != RPC_PORT_SIGNAL_NEW)
-    return;
-
-  auto* debug_port = static_cast<DebugPort*>(user_data);
-  debug_port->EmitSignal();
-}
-
 int DebugPort::Watch(int fd) {
   GIOChannel* io = g_io_channel_unix_new(fd);
   if (io == nullptr) {
index 5c85bb4..836f6bc 100644 (file)
@@ -38,12 +38,7 @@ class DebugPort : public Logger::IEvent {
   DebugPort(std::string port_name, int pid);
   virtual ~DebugPort();
 
-  void EmitSignal();
-
  private:
-  GDBusConnection* GetConnection();
-  void Subscribe();
-  void Unsubscribe();
   int CreateSocket();
   int Watch(int fd);
   void Unwatch();
@@ -54,13 +49,6 @@ class DebugPort : public Logger::IEvent {
   void OnDataReceived(int pid, rpc_port_parcel_h parcel) override;
   void OnDisconnected(int pid, int fd) override;
 
-  static void OnGDBusSignalCb(GDBusConnection *connection,
-      const gchar* sender_name,
-      const gchar* object_path,
-      const gchar* interface_name,
-      const gchar* signal_name,
-      GVariant* parameters,
-      gpointer user_data);
   static gboolean OnDebugPortConnectedCb(GIOChannel* io,
       GIOCondition cond,
       gpointer data);
@@ -72,8 +60,6 @@ class DebugPort : public Logger::IEvent {
  private:
   std::string port_name_;
   int pid_;
-  GDBusConnection* conn_ = nullptr;
-  guint subs_tag_ = 0;
   int fd_ = -1;
   GIOChannel* io_ = nullptr;
   guint watch_tag_ = 0;
index d038565..68559fe 100644 (file)
@@ -57,10 +57,7 @@ int main(int argc, char** argv) {
   }
 
   DebugPort debug_port(options->GetPortName(), options->GetPid());
-  debug_port.EmitSignal();
-
   MainLoop loop;
   loop.Run();
-
   return 0;
 }