From 9b7dd0110bfd57da87c3426fcfa408ce1abc013a Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Thu, 23 May 2013 15:37:01 +0200 Subject: [PATCH] Weaken references to PeerInfos --- src/accounts/xmpp/sip/XmppSip.cpp | 11 +++++++++++ src/accounts/xmpp/sip/XmppSip.h | 1 + src/libtomahawk/network/Servent.cpp | 22 ++++++++++++++++++++-- src/libtomahawk/network/Servent.h | 3 ++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/accounts/xmpp/sip/XmppSip.cpp b/src/accounts/xmpp/sip/XmppSip.cpp index 317604821..16dee9d43 100644 --- a/src/accounts/xmpp/sip/XmppSip.cpp +++ b/src/accounts/xmpp/sip/XmppSip.cpp @@ -922,6 +922,11 @@ XmppSipPlugin::onNewIq( const Jreen::IQ& iq ) return; } peerInfo->setSipInfos( sipMessage->sipInfos() ); + // If we stored a reference for this peer in our sip-waiting-queue, remove it. + if ( peersWaitingForSip.contains( iq.from().full() ) ) + { + peersWaitingForSip.remove( iq.from().full() ); + } } } } @@ -964,6 +969,11 @@ XmppSipPlugin::handlePeerStatus( const Jreen::JID& jid, Jreen::Presence::Type pr if ( !peerInfo.isNull() ) { peerInfo->setStatus( PeerInfo::Offline ); + // If we stored a reference for this peer in our sip-waiting-queue, remove it. + if ( peersWaitingForSip.contains( fulljid ) ) + { + peersWaitingForSip.remove( fulljid ); + } } return; @@ -981,6 +991,7 @@ XmppSipPlugin::handlePeerStatus( const Jreen::JID& jid, Jreen::Presence::Type pr peerInfo->setContactId( jid.bare() ); peerInfo->setStatus( PeerInfo::Online ); peerInfo->setFriendlyName( m_jidsNames.value( jid.bare() ) ); + peersWaitingForSip[fulljid] = peerInfo; #ifndef ENABLE_HEADLESS if ( !m_avatarManager->avatar( jid.bare() ).isNull() ) diff --git a/src/accounts/xmpp/sip/XmppSip.h b/src/accounts/xmpp/sip/XmppSip.h index 302d12037..b40456a94 100644 --- a/src/accounts/xmpp/sip/XmppSip.h +++ b/src/accounts/xmpp/sip/XmppSip.h @@ -153,6 +153,7 @@ private: enum IqContext { NoContext, RequestDisco, RequestedDisco, SipMessageSent, RequestedVCard, RequestVersion, RequestedVersion }; AvatarManager* m_avatarManager; Jreen::PubSub::Manager* m_pubSubManager; + QMap peersWaitingForSip; }; #endif diff --git a/src/libtomahawk/network/Servent.cpp b/src/libtomahawk/network/Servent.cpp index bd5267a4e..274428988 100644 --- a/src/libtomahawk/network/Servent.cpp +++ b/src/libtomahawk/network/Servent.cpp @@ -311,11 +311,28 @@ Servent::registerOffer( const QString& key, Connection* conn ) m_offers[key] = QPointer(conn); } -void Servent::registerLazyOffer(const QString &key, const peerinfo_ptr &peerInfo, const QString &nodeid ) +void +Servent::registerLazyOffer(const QString &key, const peerinfo_ptr &peerInfo, const QString &nodeid, const int timeout ) { m_lazyoffers[key] = QPair< peerinfo_ptr, QString >( peerInfo, nodeid ); + QTimer* timer = new QTimer( this ); + timer->setSingleShot( true ); + NewClosure( timer, SIGNAL( timeout() ), this, SLOT( deleteLazyOffer( const QString& ) ), key ); + timer->start(); } +void +Servent::deleteLazyOffer( const QString& key ) +{ + m_lazyoffers.remove( key ); + + // Cleanup. + QTimer* timer = (QTimer*)sender(); + if ( timer ) + { + timer->deleteLater(); + } +} void Servent::registerControlConnection( ControlConnection* conn ) @@ -464,8 +481,9 @@ Servent::registerPeer( const Tomahawk::peerinfo_ptr& peerInfo ) QString key = uuid(); const QString& nodeid = Database::instance()->impl()->dbid(); - registerLazyOffer( key, peerInfo, nodeid ); QList sipInfos = getLocalSipInfos( nodeid, key ); + // The offer should be removed after some time or we will build up a heap of unused PeerInfos + registerLazyOffer( key, peerInfo, nodeid, sipInfos.length() * 1.5 * CONNECT_TIMEOUT ); // SipInfos were single-value before 0.7.999 if ( !peerInfo->versionString().isEmpty() && TomahawkUtils::compareVersionStrings( peerInfo->versionString(), "Tomahawk Player EmptyOS 0.7.99" ) < 0) { diff --git a/src/libtomahawk/network/Servent.h b/src/libtomahawk/network/Servent.h index 9724f46ff..303ecd45f 100644 --- a/src/libtomahawk/network/Servent.h +++ b/src/libtomahawk/network/Servent.h @@ -77,7 +77,7 @@ public: QString createConnectionKey( const QString& name = "", const QString &nodeid = "", const QString &key = "", bool onceOnly = true ); void registerOffer( const QString& key, Connection* conn ); - void registerLazyOffer( const QString& key, const Tomahawk::peerinfo_ptr& peerInfo, const QString &nodeid ); + void registerLazyOffer( const QString& key, const Tomahawk::peerinfo_ptr& peerInfo, const QString &nodeid , const int timeout ); void registerControlConnection( ControlConnection* conn ); void unregisterControlConnection( ControlConnection* conn ); @@ -156,6 +156,7 @@ public slots: private slots: void readyRead(); + void deleteLazyOffer( const QString& key ); Connection* claimOffer( ControlConnection* cc, const QString &nodeid, const QString &key, const QHostAddress peer = QHostAddress::Any );