From ab90fac4e4c306f47ab0903b33d1b3aacb228c46 Mon Sep 17 00:00:00 2001 From: Javier Date: Fri, 25 Dec 2015 23:33:26 +0100 Subject: destroy agents on disconnection --- capabilityagent.cc | 5 ++++- hostmanagerconn.cc | 1 + notificationconn.cc | 1 + sapmanager.cc | 9 +++++++++ sapmanager.h | 5 +---- sappeer.cc | 23 +++++++++++++++++++---- sappeer.h | 1 + webproxyconn.cc | 1 + 8 files changed, 37 insertions(+), 9 deletions(-) diff --git a/capabilityagent.cc b/capabilityagent.cc index 3831e5d..cdc80b6 100644 --- a/capabilityagent.cc +++ b/capabilityagent.cc @@ -52,9 +52,10 @@ void CapabilityAgent::registerServices(SAPManager *manager) void CapabilityAgent::peerFound(SAPPeer *peer) { - new CapabilityPeer(peer, peer); // We make the capability peer a child of the peer object itself, // so that the peer can find it. + CapabilityPeer *capPeer = new CapabilityPeer(peer, peer); + connect(peer, SIGNAL(disconnected()), capPeer, SLOT(deleteLater())); } void CapabilityAgent::requestConnection(SAPConnectionRequest *request) @@ -62,5 +63,7 @@ void CapabilityAgent::requestConnection(SAPConnectionRequest *request) SAPPeer *peer = request->peer(); CapabilityPeer *capPeer = peer->findChild(); + Q_ASSERT(capPeer); + capPeer->requestConnection(request); } diff --git a/hostmanagerconn.cc b/hostmanagerconn.cc index b8d1d31..87bed09 100644 --- a/hostmanagerconn.cc +++ b/hostmanagerconn.cc @@ -16,6 +16,7 @@ HostManagerConn::HostManagerConn(SAPConnection *conn, QObject *parent) : QObject(parent), _conn(conn), _socket(conn->getSocket(103)) { connect(_conn, SIGNAL(disconnected()), SLOT(deleteLater())); + connect(_conn, SIGNAL(destroyed()), SLOT(deleteLater())); Q_ASSERT(_socket); connect(_socket, SIGNAL(connected()), SLOT(handleConnected())); connect(_socket, SIGNAL(messageReceived()), SLOT(handleMessageReceived())); diff --git a/notificationconn.cc b/notificationconn.cc index 592f3e5..7ac17f8 100644 --- a/notificationconn.cc +++ b/notificationconn.cc @@ -25,6 +25,7 @@ NotificationConn::NotificationConn(SAPConnection *conn, QObject *parent) : QObject(parent), _conn(conn), _socket(conn->getSocket(104)) { connect(_conn, SIGNAL(disconnected()), SLOT(deleteLater())); + connect(_conn, SIGNAL(destroyed()), SLOT(deleteLater())); Q_ASSERT(_socket); connect(_socket, SIGNAL(connected()), SLOT(handleConnected())); connect(_socket, SIGNAL(messageReceived()), SLOT(handleMessageReceived())); diff --git a/sapmanager.cc b/sapmanager.cc index c9d7df6..ab3fcd0 100644 --- a/sapmanager.cc +++ b/sapmanager.cc @@ -97,6 +97,15 @@ QSet SAPManager::allProfiles() + QSet::fromList(_providerProfiles.keys()); } +QSet SAPManager::allAgents() +{ + QSet agents; + foreach (const RegisteredAgent &ragent, _agents) { + agents.insert(ragent.agent); + } + return agents; +} + int SAPManager::findUnusedAgentId() const { if (_agents.size() > 20000) { diff --git a/sapmanager.h b/sapmanager.h index e9f60cc..31b84a0 100644 --- a/sapmanager.h +++ b/sapmanager.h @@ -29,10 +29,7 @@ public: SAPServiceInfo serviceInfo(int agentId) const; QSet allProfiles(); - -signals: - -public slots: + QSet allAgents(); private: int findUnusedAgentId() const; diff --git a/sappeer.cc b/sappeer.cc index 7f570eb..551ca45 100644 --- a/sappeer.cc +++ b/sappeer.cc @@ -18,6 +18,14 @@ SAPPeer::SAPPeer(SAProtocol::Role role, const QString &localName, const QString { } +SAPPeer::~SAPPeer() +{ + qDeleteAll(_conns); + qDeleteAll(_sessions); + _conns.clear(); + _sessions.clear(); +} + SAPConnection * SAPPeer::createServiceConnection(const QString &profile, const QString &requesterProfile, SAPServiceInfo::Role requesterRole) { SAPManager *manager = SAPManager::instance(); @@ -212,15 +220,22 @@ void SAPPeer::handleConnected() { emit connected(); - // Manually call the capability agent in order to trigger initial capability discovery. - CapabilityAgent *caps = CapabilityAgent::instance(); - caps->peerFound(this); + // Call in all the agents + SAPManager *manager = SAPManager::instance(); + foreach (SAPAgent *agent, manager->allAgents()) { + agent->peerFound(this); + } } void SAPPeer::handleDisconnected() { - // TODO Figure out who should actually reconnect + // Clear out all active sessions emit disconnected(); + qDeleteAll(_conns); + qDeleteAll(_sessions); + _conns.clear(); + _sessions.clear(); + // TODO Figure out who should actually reconnect } int SAPPeer::findUnusedSessionId() const diff --git a/sappeer.h b/sappeer.h index ffddea4..1c477c4 100644 --- a/sappeer.h +++ b/sappeer.h @@ -19,6 +19,7 @@ class SAPPeer : public QObject Q_OBJECT public: explicit SAPPeer(SAProtocol::Role role, const QString &localName, const QString &peerName, QObject *parent = 0); + ~SAPPeer(); SAPConnection* createServiceConnection(const QString &profile, const QString &requesterProfile, SAPServiceInfo::Role requesterRole); bool terminateServiceConnection(const QString &profile, const QString &requesterProfile, SAPServiceInfo::Role requesterRole); diff --git a/webproxyconn.cc b/webproxyconn.cc index fa4bc9d..91a9510 100644 --- a/webproxyconn.cc +++ b/webproxyconn.cc @@ -11,6 +11,7 @@ WebProxyConn::WebProxyConn(SAPConnection *conn, QObject *parent) _in(conn->getSocket(501)), _out(conn->getSocket(502)) { connect(_conn, SIGNAL(disconnected()), SLOT(deleteLater())); + connect(_conn, SIGNAL(destroyed()), SLOT(deleteLater())); Q_ASSERT(_in && _out); connect(_in, SIGNAL(messageReceived()), SLOT(handleMessageReceived())); } -- cgit v1.2.3