From f0c7a3e2dab5cbf518dcb82a23eec32ddeacee36 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Mon, 13 Aug 2012 22:12:55 +0200 Subject: [PATCH] D-Bus server: simplified server class Instad of having three of the helper classes as members, only store pointers to them in Server. The advantage is that including Server.h becomes simpler for .cpp files which do not need to use these classes and that they can be constructed/destructed more explicitly. This is particularly important because they get access to the Server instance which is still in the process of initializing itself. --- src/dbus/server/auto-sync-manager.cpp | 1 + src/dbus/server/connman-client.cpp | 1 + src/dbus/server/network-manager-client.cpp | 1 + src/dbus/server/server.cpp | 49 ++++++++++++++++------ src/dbus/server/server.h | 21 ++++++---- src/dbus/server/session.cpp | 1 + 6 files changed, 55 insertions(+), 19 deletions(-) diff --git a/src/dbus/server/auto-sync-manager.cpp b/src/dbus/server/auto-sync-manager.cpp index b409c1e6..2487e192 100644 --- a/src/dbus/server/auto-sync-manager.cpp +++ b/src/dbus/server/auto-sync-manager.cpp @@ -21,6 +21,7 @@ #include "session.h" #include "server.h" #include "dbus-callbacks.h" +#include "presence-status.h" #include #include diff --git a/src/dbus/server/connman-client.cpp b/src/dbus/server/connman-client.cpp index 3481d79c..674feae6 100644 --- a/src/dbus/server/connman-client.cpp +++ b/src/dbus/server/connman-client.cpp @@ -19,6 +19,7 @@ #include "connman-client.h" #include "server.h" +#include "presence-status.h" SE_BEGIN_CXX diff --git a/src/dbus/server/network-manager-client.cpp b/src/dbus/server/network-manager-client.cpp index 91ebd7be..6c462d50 100644 --- a/src/dbus/server/network-manager-client.cpp +++ b/src/dbus/server/network-manager-client.cpp @@ -20,6 +20,7 @@ #include "network-manager-client.h" #include "server.h" +#include "presence-status.h" SE_BEGIN_CXX diff --git a/src/dbus/server/server.cpp b/src/dbus/server/server.cpp index be751c2c..ddd10c1d 100644 --- a/src/dbus/server/server.cpp +++ b/src/dbus/server/server.cpp @@ -33,6 +33,9 @@ #include "restart.h" #include "client.h" #include "auto-sync-manager.h" +#include "connman-client.h" +#include "network-manager-client.h" +#include "presence-status.h" #include @@ -207,7 +210,7 @@ void Server::checkPresence(const std::string &server, std::string &status, std::vector &transports) { - return m_presence.checkPresence(server, status, transports); + return getPresenceStatus().checkPresence(server, status, transports); } void Server::getSessions(std::vector &sessions) @@ -246,9 +249,6 @@ Server::Server(GMainLoop *loop, configChanged(*this, "ConfigChanged"), infoRequest(*this, "InfoRequest"), logOutput(*this, "LogOutput"), - m_presence(*this), - m_connman(*this), - m_networkManager(*this), m_autoTerm(m_loop, m_shutdownRequested, duration), m_parentLogger(LoggerBase::instance()) { @@ -280,26 +280,40 @@ Server::Server(GMainLoop *loop, add(infoRequest); add(logOutput); + // log entering and leaving idle state + m_idleSignal.connect(boost::bind(logIdle, _1)); + + // connect ConfigChanged signal to source for that information + m_configChangedSignal.connect(boost::bind(boost::ref(configChanged))); +} + +void Server::activate() +{ + // Activate our D-Bus object *before* interacting with D-Bus + // any further. Otherwise GIO D-Bus will start processing + // messages for us while we start up and reject them because + // out object isn't visible to it yet. + GDBusCXX::DBusObjectHelper::activate(); + LoggerBase::pushLogger(this); setLevel(LoggerBase::DEBUG); + m_presence.reset(new PresenceStatus(*this)); + // Assume that Bluetooth is available. Neither ConnMan nor Network // manager can tell us about that. The "Bluetooth" ConnMan technology // is about IP connection via Bluetooth - not what we need. getPresenceStatus().updatePresenceStatus(true, PresenceStatus::BT_TRANSPORT); - if (!m_connman.isAvailable() && - !m_networkManager.isAvailable()) { + m_connman.reset(new ConnmanClient(*this)); + m_networkManager.reset(new NetworkManagerClient(*this)); + + if ((!m_connman || !m_connman->isAvailable()) && + (!m_networkManager || !m_networkManager->isAvailable())) { // assume that we are online if no network manager was found at all getPresenceStatus().updatePresenceStatus(true, PresenceStatus::HTTP_TRANSPORT); } - // log entering and leaving idle state - m_idleSignal.connect(boost::bind(logIdle, _1)); - - // connect ConfigChanged signal to source for that information - m_configChangedSignal.connect(boost::bind(boost::ref(configChanged))); - // create auto sync manager, now that server is ready m_autoSync = AutoSyncManager::createAutoSyncManager(*this); } @@ -314,6 +328,9 @@ Server::~Server() m_infoReqMap.clear(); m_timeouts.clear(); m_delayDeletion.clear(); + m_connman.reset(); + m_networkManager.reset(); + m_presence.reset(); LoggerBase::popLogger(); } @@ -760,6 +777,14 @@ void Server::removeInfoReq(const std::string &id) m_infoReqMap.erase(id); } +PresenceStatus &Server::getPresenceStatus() +{ + if (!m_presence) { + SE_THROW("internal error: Server::getPresenceStatus() called while server has no instance"); + } + return *m_presence; +} + void Server::getDeviceList(SyncConfig::DeviceList &devices) { //wait bluez or other device managers diff --git a/src/dbus/server/server.h b/src/dbus/server/server.h index 3022414e..4a6a0208 100644 --- a/src/dbus/server/server.h +++ b/src/dbus/server/server.h @@ -26,13 +26,13 @@ #include #include +#include + #include "exceptions.h" #include "auto-term.h" -#include "connman-client.h" -#include "network-manager-client.h" -#include "presence-status.h" #include "timeout.h" #include "dbus-callbacks.h" +#include "read-operations.h" #include SE_BEGIN_CXX @@ -46,6 +46,12 @@ class Restart; class Client; class GLibNotify; class AutoSyncManager; +class PresenceStatus; +class ConnmanClient; +class NetworkManagerClient; + +// TODO: avoid polluting namespace +using namespace std; /** * Implements the main org.syncevolution.Server interface. @@ -345,9 +351,9 @@ class Server : public GDBusCXX::DBusObjectHelper, /** remove InfoReq from hash map */ void removeInfoReq(const std::string &infoReqId); - PresenceStatus m_presence; - ConnmanClient m_connman; - NetworkManagerClient m_networkManager; + boost::scoped_ptr m_presence; + boost::scoped_ptr m_connman; + boost::scoped_ptr m_networkManager; /** Manager to automatic sync */ boost::shared_ptr m_autoSync; @@ -381,6 +387,7 @@ public: boost::shared_ptr &restart, const GDBusCXX::DBusConnectionPtr &conn, int duration); + void activate(); ~Server(); /** access to the GMainLoop reference used by this Server instance */ @@ -558,7 +565,7 @@ public: /** poll_nm callback for connman, used for presence detection*/ void connmanCallback(const std::map > >& props, const string &error); - PresenceStatus& getPresenceStatus() {return m_presence;} + PresenceStatus& getPresenceStatus(); void clearPeerTempls() { m_matchedTempls.clear(); } void addPeerTempl(const string &templName, const boost::shared_ptr peerTempl); diff --git a/src/dbus/server/session.cpp b/src/dbus/server/session.cpp index eae0f4f0..a3ec0b6c 100644 --- a/src/dbus/server/session.cpp +++ b/src/dbus/server/session.cpp @@ -25,6 +25,7 @@ #include "info-req.h" #include "session-common.h" #include "dbus-callbacks.h" +#include "presence-status.h" #include #include