[connman] Share a single connection to D-Bus system bus. 82/10982/2
authorOssama Othman <ossama.othman@intel.com>
Sun, 13 Oct 2013 06:29:39 +0000 (23:29 -0700)
committerOssama Othman <ossama.othman@intel.com>
Wed, 16 Oct 2013 21:52:38 +0000 (14:52 -0700)
Previously each GDBusProxy to a Connman object had its own
system bus connection.  That was wasteful and unnecessary.
All Connman D-Bus proxies now share a single connection.  This
reduces resource utilization and improves performance in some
cases, e.g. the settings daemon WiFi connect/disconnect
implementation.

Change-Id: Idec33eeb90f8403170adc4f9eec5984bb98b5ce6
Signed-off-by: Ossama Othman <ossama.othman@intel.com>
20 files changed:
plugins/connman/Makefile.am
plugins/connman/bluetooth.cpp
plugins/connman/bluetooth.hpp
plugins/connman/clock.cpp
plugins/connman/clock.hpp
plugins/connman/connman.cpp
plugins/connman/connman.hpp
plugins/connman/connman_manager.cpp
plugins/connman/connman_manager.hpp
plugins/connman/dbus_connection.cpp [new file with mode: 0644]
plugins/connman/dbus_connection.hpp [new file with mode: 0644]
plugins/connman/ethernet.cpp
plugins/connman/ethernet.hpp
plugins/connman/registration.cpp
plugins/connman/service.cpp
plugins/connman/service.hpp
plugins/connman/technology.cpp
plugins/connman/technology.hpp
plugins/connman/wifi.cpp
plugins/connman/wifi.hpp

index 0b8b08f..e87ac62 100644 (file)
@@ -33,6 +33,7 @@ IVI_SETTINGS_PLUGIN_CPPFLAGS = \
 IVI_SETTINGS_PLUGIN_LIBRARY = $(top_builddir)/lib/libsettings.la
 
 connman_la_SOURCES =           \
+       dbus_connection.cpp     \
        connman.cpp             \
        connman_manager.cpp     \
        service.cpp             \
@@ -61,6 +62,7 @@ connman_la_LDFLAGS  = -no-undefined   \
 noinst_HEADERS =               \
        config.h                \
        connman_api.hpp         \
+       dbus_connection.hpp     \
        connman.hpp             \
        connman_manager.hpp     \
        service.hpp             \
index 3509f86..5ad2fbd 100644 (file)
@@ -36,9 +36,10 @@ namespace
 
 // ----------------------------------------------------------------------
 
-ivi::settings::bluetooth::bluetooth(connman_manager & manager,
+ivi::settings::bluetooth::bluetooth(GDBusConnection * connection,
+                                    connman_manager & manager,
                                     event_callback const & e)
-  : technology_(technology_name, manager, e)
+  : technology_(technology_name, connection, manager, e)
 {
 }
 
index fad039f..c947f99 100644 (file)
@@ -55,7 +55,9 @@ namespace ivi
     public:
 
       /// Constructor.
-      bluetooth(connman_manager & manager, event_callback const & e);
+      bluetooth(GDBusConnection * connection,
+                connman_manager & manager,
+                event_callback const & e);
 
       /// Destructor.
       virtual ~bluetooth();
index 8d739cd..f0ff628 100644 (file)
@@ -35,8 +35,9 @@
 #include <boost/lexical_cast.hpp>
 
 
-ivi::settings::clock::clock(event_callback const & e)
-  : connman_("net.connman.Clock", "/", e)
+ivi::settings::clock::clock(GDBusConnection * connection,
+                            event_callback const & e)
+  : connman_("net.connman.Clock", "/", connection, e)
 {
 }
 
index f6d6c18..636c6b9 100644 (file)
@@ -58,7 +58,7 @@ namespace ivi
     public:
 
       /// Constructor.
-      clock(event_callback const & e);
+      clock(GDBusConnection * connection, event_callback const & e);
 
       /// Destructor.
       virtual ~clock();
index d0a827e..707111b 100644 (file)
@@ -37,6 +37,7 @@
 
 ivi::settings::connman::connman(char const * interface,
                                 char const * path,
+                                GDBusConnection * connection,
                                 event_callback const & e)
   : proxy_(nullptr)
   , event_callback_(e)
@@ -47,14 +48,14 @@ ivi::settings::connman::connman(char const * interface,
   GError * error = nullptr;
 
   proxy_ =
-    g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
-                                   G_DBUS_PROXY_FLAGS_NONE,
-                                   nullptr, // GDBusInterfaceInfo
-                                   name,
-                                   path,
-                                   interface,
-                                   nullptr, // GCancellable
-                                   &error);
+    g_dbus_proxy_new_sync (connection,
+                           G_DBUS_PROXY_FLAGS_NONE,
+                           nullptr, // GDBusInterfaceInfo
+                           name,
+                           path,
+                           interface,
+                           nullptr, // GCancellable
+                           &error);
 
   unique_ptr<GError> safe_error(error);
 
@@ -70,7 +71,7 @@ ivi::settings::connman::connman(char const * interface,
   // Listen for changes to properties.
   subscription_id_ =
       g_dbus_connection_signal_subscribe(
-        g_dbus_proxy_get_connection(G_DBUS_PROXY(proxy_)),
+        connection,
         nullptr,
         interface,
         "PropertyChanged",
index bdde0f5..de4a7b4 100644 (file)
@@ -52,11 +52,15 @@ namespace ivi
       /**
        * Constructor.
        *
-       * @param[in] interface Connman D-Bus interface.
-       * @param[in] path      Connman D-Bus object path.
+       * @param[in] interface  Connman D-Bus interface.
+       * @param[in] path       Connman D-Bus object path.
+       * @param[in] connection Underlying D-Bus connection.
+       * @param[in] e          Callback through which events will be
+       *                       sent to clients.
        */
       connman(char const * interface,
               char const * path,
+              GDBusConnection * connection,
               event_callback const & e);
 
       /// Destructor.
@@ -82,6 +86,13 @@ namespace ivi
       /// Get pointer to underlying GDBusProxy.
       GDBusProxy * proxy() const { return proxy_; }
 
+      /// Get pointer to underlying GDBusConnection.
+      GDBusConnection *
+      connection() const
+      {
+        return g_dbus_proxy_get_connection(G_DBUS_PROXY(proxy_));
+      }
+
       /// Convenience function to get D-Bus interface name.
       char const *
       interface_name() const
index cda4ee7..f1c13a1 100644 (file)
@@ -25,6 +25,7 @@
  */
 
 #include "connman_manager.hpp"
+#include "dbus_connection.hpp"
 
 #include <settingsd/dbus_signal_callback.hpp>
 #include <settingsd/glib_traits.hpp>
 
 
 ivi::settings::connman_manager::connman_manager(
+  GDBusConnection * connection,
   event_callback const & e)
   : connman_("net.connman.Manager",     // Interface
              "/",                       // Object path
+             connection,
              e)
   , event_callback_(e)
   , subscription_id_(
       g_dbus_connection_signal_subscribe(
-        g_dbus_proxy_get_connection(G_DBUS_PROXY(connman_.proxy())),
+        connection,
         nullptr,
         connman_.interface_name(),
         "ServicesChanged",
@@ -64,9 +67,8 @@ ivi::settings::connman_manager::connman_manager(
 
 ivi::settings::connman_manager::~connman_manager()
 {
-  g_dbus_connection_signal_unsubscribe(
-    g_dbus_proxy_get_connection(G_DBUS_PROXY(connman_.proxy())),
-    subscription_id_);
+  g_dbus_connection_signal_unsubscribe(connman_.connection(),
+                                       subscription_id_);
 }
 
 GVariant *
index 1361df5..27b5f64 100644 (file)
@@ -53,7 +53,8 @@ namespace ivi
     public:
 
       /// Constructor.
-      connman_manager(event_callback const & e);
+      connman_manager(GDBusConnection * connection,
+                      event_callback const & e);
 
       /// Destructor.
       ~connman_manager();
diff --git a/plugins/connman/dbus_connection.cpp b/plugins/connman/dbus_connection.cpp
new file mode 100644 (file)
index 0000000..bb8b0a9
--- /dev/null
@@ -0,0 +1,68 @@
+/**
+ * @file dbus_connection.cpp
+ *
+ * @brief Settings daemon D-Bus connection adapter.
+ *
+ * @author Michael Leibowitz @<michael.leibowitz@@intel.com@>
+ * @author Ossama Othman @<ossama.othman@@intel.com@>
+ *
+ * @copyright @par
+ * Copyright 2013 Intel Corporation All Rights Reserved.
+ * @par
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ * @par
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * @par
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ */
+
+#include "dbus_connection.hpp"
+
+#include <settingsd/glib_traits.hpp>
+#include <settingsd/unique_ptr.hpp>
+
+#include <stdexcept>
+
+
+ivi::settings::dbus_connection::dbus_connection()
+  : connection_(nullptr)
+{
+  GError * error = nullptr;
+
+  connection_ = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error);
+
+  if (connection_ == nullptr) {
+    unique_ptr<GError> safe_error(error);
+    throw std::runtime_error(error->message);
+  }
+}
+
+ivi::settings::dbus_connection::~dbus_connection()
+{
+  GError * error = nullptr;
+
+  if (connection_ != nullptr
+      && !g_dbus_connection_close_sync(connection_,
+                                       nullptr,
+                                       &error)) {
+    g_critical("D-Bus connection close failed: %s.\n",
+               error->message);
+    g_error_free(error);
+  }
+}
+
+
+// Local Variables:
+// mode:c++
+// c-basic-offset:2
+// indent-tabs-mode: nil
+// End:
diff --git a/plugins/connman/dbus_connection.hpp b/plugins/connman/dbus_connection.hpp
new file mode 100644 (file)
index 0000000..62a25ec
--- /dev/null
@@ -0,0 +1,82 @@
+/**
+ * @file dbus_connection.hpp
+ *
+ * @brief D-Bus connecton adapter header.
+ *
+ * @author Ossama Othman @<ossama.othman@@intel.com@>
+ *
+ * @copyright @par
+ * Copyright 2013 Intel Corporation All Rights Reserved.
+ * @par
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * @par
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * @par
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * @note This is an internal header.
+ */
+
+#ifndef IVI_SETTINGS_DBUS_CONNECTION_HPP
+#define IVI_SETTINGS_DBUS_CONNECTION_HPP
+
+#include <gio/gio.h>
+
+
+namespace ivi
+{
+  namespace settings
+  {
+    class dbus_connection
+    {
+    public:
+
+      /// Constructor.
+      dbus_connection();
+
+      /// Destructor.
+      ~dbus_connection();
+
+      /// Get pointer to underlying D-Bus connection
+      GDBusConnection *
+      connection()
+      {
+        return connection_;
+      }
+
+    private:
+
+      /**
+       * @name Prevent copying
+       */
+      //@{
+      dbus_connection(dbus_connection const &) = delete;
+      dbus_connection & operator=(dbus_connection const &) = delete;
+      //@}
+
+    private:
+
+      /// Underlying connection to D-Bus system bus.
+      GDBusConnection * connection_;
+
+    };
+  }
+}
+
+#endif  /* IVI_SETTINGS_DBUS_CONNECTION_HPP */
+
+
+// Local Variables:
+// mode:c++
+// c-basic-offset:2
+// indent-tabs-mode: nil
+// End:
index 6719974..c740a48 100644 (file)
@@ -36,9 +36,10 @@ namespace
 
 // ----------------------------------------------------------------------
 
-ivi::settings::ethernet::ethernet(connman_manager & manager,
+ivi::settings::ethernet::ethernet(GDBusConnection * connection,
+                                  connman_manager & manager,
                                   event_callback const & e)
-  : technology_(technology_name, manager, e)
+  : technology_(technology_name, connection, manager, e)
 {
 }
 
index e9b3b77..a777e1b 100644 (file)
@@ -55,7 +55,9 @@ namespace ivi
     public:
 
       /// Constructor.
-      ethernet(connman_manager & manager, event_callback const & e);
+      ethernet(GDBusConnection * connection,
+               connman_manager & manager,
+               event_callback const & e);
 
       /// Destructor.
       virtual ~ethernet();
index adccd49..2117697 100644 (file)
@@ -25,6 +25,7 @@
  */
 
 #include "connman_api.hpp"
+#include "dbus_connection.hpp"
 #include "connman_manager.hpp"
 #include "bluetooth.hpp"
 #include "clock.hpp"
@@ -49,21 +50,26 @@ extern "C" IVI_SETTINGS_CONNMAN_API bool
 register_settings(ivi::settings::registrar & r,
                   ivi::settings::event_callback const & e)
 {
+  // Only one connection to the D-Bus system bus is needed.  All
+  // connman based settings plugins will share it.
+  static ivi::settings::dbus_connection con;
+  GDBusConnection * const connection = con.connection();
+
   // Only one instance of connman_manager is needed since the
   // corresponding connman Manager is really a global object.
-  static ivi::settings::connman_manager manager(e);
+  static ivi::settings::connman_manager manager(connection, e);
 
   std::unique_ptr<ivi::settings::plugin> bt(
-    new ivi::settings::bluetooth(manager, e));
+    new ivi::settings::bluetooth(connection, manager, e));
 
   std::unique_ptr<ivi::settings::plugin> eth(
-    new ivi::settings::ethernet(manager, e));
+    new ivi::settings::ethernet(connection, manager, e));
 
   std::unique_ptr<ivi::settings::plugin> wifi(
-    new ivi::settings::wifi(manager, e));
+    new ivi::settings::wifi(connection, manager, e));
 
   std::unique_ptr<ivi::settings::plugin> clk(
-    new ivi::settings::clock(e));
+    new ivi::settings::clock(connection, e));
 
   return
     r.register_setting(std::move(bt))
index 1f7bf82..757e1d7 100644 (file)
 
 
 ivi::settings::service::service(std::string service_path,
+                                GDBusConnection * connection,
                                 event_callback const & e)
   : connman_("net.connman.Service",     // Interface
              service_path.c_str(),      // Object path
+             connection,
              e)
 {
 }
index fc1f289..3f75e2a 100644 (file)
@@ -54,10 +54,12 @@ namespace ivi
        *
        * @param[in] service_path The D-Bus object path for connman
        *                         service.
+       * @param[in] connection   Underlying D-Bus connection.
        * @param[in] e            Callback through which events will be
        *                         sent to clients.
        */
       service(std::string service_path,
+              GDBusConnection * connection,
               event_callback const & e);
 
       /// Connect to the service.
index 92a25c0..2deaf5a 100644 (file)
 
 
 ivi::settings::technology::technology(std::string tech,
+                                      GDBusConnection * connection,
                                       connman_manager & manager,
                                       event_callback const & e)
   : connman_("net.connman.Technology",     // Interface
              ("/net/connman/technology/"
               + tech).c_str(),             // Object path
+             connection,
              e)
   , manager_(manager)
   , technology_(tech)
@@ -215,7 +217,9 @@ ivi::settings::technology::connect(JsonReader * reader,
 
   /// @todo Refactor malformed JSON request handling code.
   if (service_path != nullptr) {
-    service s(service_path, event_callback_);
+    service s(service_path,
+              connman_.connection(),
+              event_callback_);
     s.connect(response);
   } else {
     response.send_error(
@@ -234,7 +238,9 @@ ivi::settings::technology::disconnect(JsonReader * reader,
   json_reader_end_member(reader);
 
   if (service_path != nullptr) {
-    service s(service_path, event_callback_);
+    service s(service_path,
+              connman_.connection(),
+              event_callback_);
     s.disconnect(response);
   } else {
     response.send_error(
index a7bf190..8c74b29 100644 (file)
@@ -56,11 +56,15 @@ namespace ivi
       /**
        * Constructor.
        *
-       * @param[in] tech The connman technology, e.g. "bluetooth".
-       * @param[in] e    Callback through which events will be sent to
-       *                 clients.
+       * @param[in] tech       The connman technology,
+       *                       e.g. "bluetooth".
+       * @param[in] connection Underlying D-Bus connection.
+       * @param[in] manager    Connman manager proxy.
+       * @param[in] e          Callback through which events will be
+       *                       sent to clients.
        */
       technology(std::string tech,
+                 GDBusConnection * connection,
                  connman_manager & manager,
                  event_callback const & e);
 
index 31deafc..3738d71 100644 (file)
@@ -36,9 +36,10 @@ namespace
 
 // ----------------------------------------------------------------------
 
-ivi::settings::wifi::wifi(connman_manager & manager,
+ivi::settings::wifi::wifi(GDBusConnection * connection,
+                          connman_manager & manager,
                           event_callback const & e)
-  : technology_(technology_name, manager, e)
+  : technology_(technology_name, connection, manager, e)
 {
 }
 
index 6d75c93..2ab51ec 100644 (file)
@@ -57,7 +57,9 @@ namespace ivi
     public:
 
       /// Constructor.
-      wifi(connman_manager & manager, event_callback const & e);
+      wifi(GDBusConnection * connection,
+           connman_manager & manager,
+           event_callback const & e);
 
       /// Destructor.
       virtual ~wifi();