From c4ab018ae6924dc8bb7ccb5e51bd73d9f780fa2b Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Mon, 27 May 2013 15:37:41 +0200 Subject: [PATCH] Detect duplicate ControlConnections --- src/libtomahawk/Source.cpp | 24 +++++++++++++-- src/libtomahawk/Source.h | 2 +- src/libtomahawk/network/ControlConnection.cpp | 30 ++++++++++++------- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/libtomahawk/Source.cpp b/src/libtomahawk/Source.cpp index 509068e87..eb4a24710 100644 --- a/src/libtomahawk/Source.cpp +++ b/src/libtomahawk/Source.cpp @@ -30,6 +30,7 @@ #include "database/DatabaseCommand_LoadAllSources.h" #include "database/DatabaseCommand_SourceOffline.h" #include "database/DatabaseCommand_UpdateSearchIndex.h" +#include "database/DatabaseImpl.h" #include "database/Database.h" #include @@ -81,10 +82,29 @@ Source::~Source() } -void +bool Source::setControlConnection( ControlConnection* cc ) { - m_cc = cc; + if ( !m_cc.isNull() && m_cc->isReady() && m_cc->isRunning() ) + { + const QString& nodeid = Database::instance()->impl()->dbid(); + if ( cc->id() < nodeid && m_cc->outbound() ) + { + m_cc = cc; + // This ControlConnection is not needed anymore, get rid of it! + m_cc->deleteLater(); + return true; + } + else + { + return false; + } + } + else + { + m_cc = cc; + return true; + } } diff --git a/src/libtomahawk/Source.h b/src/libtomahawk/Source.h index 389ddf25c..860369615 100644 --- a/src/libtomahawk/Source.h +++ b/src/libtomahawk/Source.h @@ -87,7 +87,7 @@ public: int id() const { return m_id; } ControlConnection* controlConnection() const { return m_cc.data(); } - void setControlConnection( ControlConnection* cc ); + bool setControlConnection( ControlConnection* cc ); const QSet< Tomahawk::peerinfo_ptr > peerInfos() const; diff --git a/src/libtomahawk/network/ControlConnection.cpp b/src/libtomahawk/network/ControlConnection.cpp index 4b7e082b4..880626edc 100644 --- a/src/libtomahawk/network/ControlConnection.cpp +++ b/src/libtomahawk/network/ControlConnection.cpp @@ -100,20 +100,28 @@ ControlConnection::setup() // setup source and remote collection for this peer m_source = SourceList::instance()->get( id(), friendlyName, true ); - m_source->setControlConnection( this ); + if ( m_source->setControlConnection( this ) ) + { + // We are the new ControlConnection for this source - // delay setting up collection/etc until source is synced. - // we need it DB synced so it has an ID + exists in DB. - connect( m_source.data(), SIGNAL( syncedWithDatabase() ), - SLOT( registerSource() ), Qt::QueuedConnection ); + // delay setting up collection/etc until source is synced. + // we need it DB synced so it has an ID + exists in DB. + connect( m_source.data(), SIGNAL( syncedWithDatabase() ), + SLOT( registerSource() ), Qt::QueuedConnection ); - m_source->setOnline(); + m_source->setOnline(); - m_pingtimer = new QTimer; - m_pingtimer->setInterval( 5000 ); - connect( m_pingtimer, SIGNAL( timeout() ), SLOT( onPingTimer() ) ); - m_pingtimer->start(); - m_pingtimer_mark.start(); + m_pingtimer = new QTimer; + m_pingtimer->setInterval( 5000 ); + connect( m_pingtimer, SIGNAL( timeout() ), SLOT( onPingTimer() ) ); + m_pingtimer->start(); + m_pingtimer_mark.start(); + } + else + { + // There is already another ControlConnection in use, we are useless. + deleteLater(); + } }