~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/network/ControlConnection.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
 
4
 *   Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
 
5
 *
 
6
 *   Tomahawk is free software: you can redistribute it and/or modify
 
7
 *   it under the terms of the GNU General Public License as published by
 
8
 *   the Free Software Foundation, either version 3 of the License, or
 
9
 *   (at your option) any later version.
 
10
 *
 
11
 *   Tomahawk is distributed in the hope that it will be useful,
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
14
 *   GNU General Public License for more details.
 
15
 *
 
16
 *   You should have received a copy of the GNU General Public License
 
17
 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "ControlConnection.h"
 
21
 
 
22
#include "StreamConnection.h"
 
23
#include "database/Database.h"
 
24
#include "database/DatabaseCommand_CollectionStats.h"
 
25
#include "DbSyncConnection.h"
 
26
#include "SourceList.h"
 
27
#include "network/DbSyncConnection.h"
 
28
#include "network/Servent.h"
 
29
#include "sip/SipHandler.h"
 
30
#include "utils/Logger.h"
 
31
 
 
32
#define TCP_TIMEOUT 600
 
33
 
 
34
using namespace Tomahawk;
 
35
 
 
36
 
 
37
ControlConnection::ControlConnection( Servent* parent, const QHostAddress &ha )
 
38
    : Connection( parent )
 
39
    , m_dbsyncconn( 0 )
 
40
    , m_registered( false )
 
41
    , m_pingtimer( 0 )
 
42
{
 
43
    qDebug() << "CTOR controlconnection";
 
44
    setId("ControlConnection()");
 
45
 
 
46
    // auto delete when connection closes:
 
47
    connect( this, SIGNAL( finished() ), SLOT( deleteLater() ) );
 
48
 
 
49
    this->setMsgProcessorModeIn( MsgProcessor::UNCOMPRESS_ALL | MsgProcessor::PARSE_JSON );
 
50
    this->setMsgProcessorModeOut( MsgProcessor::COMPRESS_IF_LARGE );
 
51
 
 
52
    m_peerIpAddress = ha;
 
53
}
 
54
 
 
55
 
 
56
ControlConnection::ControlConnection( Servent* parent, const QString &ha )
 
57
    : Connection( parent )
 
58
    , m_dbsyncconn( 0 )
 
59
    , m_registered( false )
 
60
    , m_pingtimer( 0 )
 
61
{
 
62
    qDebug() << "CTOR controlconnection";
 
63
    setId("ControlConnection()");
 
64
 
 
65
    // auto delete when connection closes:
 
66
    connect( this, SIGNAL( finished() ), SLOT( deleteLater() ) );
 
67
 
 
68
    this->setMsgProcessorModeIn( MsgProcessor::UNCOMPRESS_ALL | MsgProcessor::PARSE_JSON );
 
69
    this->setMsgProcessorModeOut( MsgProcessor::COMPRESS_IF_LARGE );
 
70
 
 
71
    if ( !ha.isEmpty() )
 
72
    {
 
73
        QHostAddress qha( ha );
 
74
        if ( !qha.isNull() )
 
75
            m_peerIpAddress = qha;
 
76
        else
 
77
        {
 
78
            QHostInfo qhi = QHostInfo::fromName( ha );
 
79
            if ( !qhi.addresses().isEmpty() )
 
80
                m_peerIpAddress = qhi.addresses().first();
 
81
        }
 
82
    }
 
83
}
 
84
 
 
85
 
 
86
ControlConnection::~ControlConnection()
 
87
{
 
88
    qDebug() << "DTOR controlconnection";
 
89
 
 
90
    if ( !m_source.isNull() )
 
91
        m_source->setOffline();
 
92
 
 
93
    delete m_pingtimer;
 
94
    m_servent->unregisterControlConnection( this );
 
95
    if ( m_dbsyncconn )
 
96
        m_dbsyncconn->deleteLater();
 
97
}
 
98
 
 
99
 
 
100
source_ptr
 
101
ControlConnection::source() const
 
102
{
 
103
    return m_source;
 
104
}
 
105
 
 
106
 
 
107
Connection*
 
108
ControlConnection::clone()
 
109
{
 
110
    ControlConnection* clone = new ControlConnection( servent(), m_peerIpAddress.toString() );
 
111
    clone->setOnceOnly( onceOnly() );
 
112
    clone->setName( name() );
 
113
    return clone;
 
114
}
 
115
 
 
116
 
 
117
void
 
118
ControlConnection::setup()
 
119
{
 
120
    qDebug() << Q_FUNC_INFO << id() << name();
 
121
 
 
122
    if ( !m_source.isNull() )
 
123
    {
 
124
        qDebug() << "This source seems to be online already.";
 
125
        Q_ASSERT( false );
 
126
        return;
 
127
    }
 
128
 
 
129
    QString friendlyName = name();
 
130
 
 
131
    tDebug() << "Detected name:" << name() << friendlyName << m_sock->peerAddress();
 
132
 
 
133
    // setup source and remote collection for this peer
 
134
    m_source = SourceList::instance()->get( id(), friendlyName, true );
 
135
    m_source->setControlConnection( this );
 
136
 
 
137
    // delay setting up collection/etc until source is synced.
 
138
    // we need it DB synced so it has an ID + exists in DB.
 
139
    connect( m_source.data(), SIGNAL( syncedWithDatabase() ),
 
140
                                SLOT( registerSource() ), Qt::QueuedConnection );
 
141
 
 
142
    m_source->setOnline();
 
143
 
 
144
    m_pingtimer = new QTimer;
 
145
    m_pingtimer->setInterval( 5000 );
 
146
    connect( m_pingtimer, SIGNAL( timeout() ), SLOT( onPingTimer() ) );
 
147
    m_pingtimer->start();
 
148
    m_pingtimer_mark.start();
 
149
}
 
150
 
 
151
 
 
152
// source was synced to DB, set it up properly:
 
153
void
 
154
ControlConnection::registerSource()
 
155
{
 
156
    qDebug() << Q_FUNC_INFO << m_source->id();
 
157
    Source* source = (Source*) sender();
 
158
    Q_UNUSED( source )
 
159
    Q_ASSERT( source == m_source.data() );
 
160
 
 
161
#ifndef ENABLE_HEADLESS
 
162
//    qDebug() << Q_FUNC_INFO << "Setting avatar ... " << name() << !SipHandler::instance()->avatar( name() ).isNull();
 
163
    if ( !SipHandler::instance()->avatar( name() ).isNull() )
 
164
    {
 
165
        source->setAvatar( SipHandler::instance()->avatar( name() ) );
 
166
    }
 
167
#endif
 
168
 
 
169
    m_registered = true;
 
170
    m_servent->registerControlConnection( this );
 
171
    setupDbSyncConnection();
 
172
}
 
173
 
 
174
 
 
175
void
 
176
ControlConnection::setupDbSyncConnection( bool ondemand )
 
177
{
 
178
    qDebug() << Q_FUNC_INFO << ondemand << m_source->id() << m_dbconnkey << m_dbsyncconn << m_registered;
 
179
 
 
180
    if ( m_dbsyncconn || !m_registered )
 
181
        return;
 
182
 
 
183
    Q_ASSERT( m_source->id() > 0 );
 
184
 
 
185
    if ( !m_dbconnkey.isEmpty() )
 
186
    {
 
187
        qDebug() << "Connecting to DBSync offer from peer...";
 
188
        m_dbsyncconn = new DBSyncConnection( m_servent, m_source );
 
189
 
 
190
        m_servent->createParallelConnection( this, m_dbsyncconn, m_dbconnkey );
 
191
        m_dbconnkey.clear();
 
192
    }
 
193
    else if ( !outbound() || ondemand ) // only one end makes the offer
 
194
    {
 
195
        qDebug() << "Offering a DBSync key to peer...";
 
196
        m_dbsyncconn = new DBSyncConnection( m_servent, m_source );
 
197
 
 
198
        QString key = uuid();
 
199
        m_servent->registerOffer( key, m_dbsyncconn );
 
200
        QVariantMap m;
 
201
        m.insert( "method", "dbsync-offer" );
 
202
        m.insert( "key", key );
 
203
        sendMsg( m );
 
204
    }
 
205
 
 
206
    if ( m_dbsyncconn )
 
207
    {
 
208
        connect( m_dbsyncconn, SIGNAL( finished() ),
 
209
                 m_dbsyncconn,   SLOT( deleteLater() ) );
 
210
 
 
211
        connect( m_dbsyncconn, SIGNAL( destroyed( QObject* ) ),
 
212
                                 SLOT( dbSyncConnFinished( QObject* ) ), Qt::DirectConnection );
 
213
    }
 
214
}
 
215
 
 
216
 
 
217
void
 
218
ControlConnection::dbSyncConnFinished( QObject* c )
 
219
{
 
220
    qDebug() << Q_FUNC_INFO << "DBSync connection closed (for now)";
 
221
    if ( (DBSyncConnection*)c == m_dbsyncconn )
 
222
    {
 
223
        //qDebug() << "Setting m_dbsyncconn to NULL";
 
224
        m_dbsyncconn = NULL;
 
225
    }
 
226
    else
 
227
        qDebug() << "Old DbSyncConn destroyed?!";
 
228
}
 
229
 
 
230
 
 
231
DBSyncConnection*
 
232
ControlConnection::dbSyncConnection()
 
233
{
 
234
    qDebug() << Q_FUNC_INFO << m_source->id();
 
235
    if ( !m_dbsyncconn )
 
236
    {
 
237
        setupDbSyncConnection( true );
 
238
//        Q_ASSERT( m_dbsyncconn );
 
239
    }
 
240
 
 
241
    return m_dbsyncconn;
 
242
}
 
243
 
 
244
 
 
245
void
 
246
ControlConnection::handleMsg( msg_ptr msg )
 
247
{
 
248
    if ( msg->is( Msg::PING ) )
 
249
    {
 
250
        // qDebug() << "Received Connection PING, nice." << m_pingtimer_mark.elapsed();
 
251
        m_pingtimer_mark.restart();
 
252
        return;
 
253
    }
 
254
 
 
255
    // if small and not compresed, print it out for debug
 
256
    if ( msg->length() < 1024 && !msg->is( Msg::COMPRESSED ) )
 
257
    {
 
258
        qDebug() << id() << "got msg:" << QString::fromAscii( msg->payload() );
 
259
    }
 
260
 
 
261
    // All control connection msgs are JSON
 
262
    if ( !msg->is( Msg::JSON ) )
 
263
    {
 
264
        Q_ASSERT( msg->is( Msg::JSON ) );
 
265
        markAsFailed();
 
266
        return;
 
267
    }
 
268
 
 
269
    QVariantMap m = msg->json().toMap();
 
270
    if ( !m.isEmpty() )
 
271
    {
 
272
        if ( m.value( "conntype" ).toString() == "request-offer" )
 
273
        {
 
274
            QString theirkey = m["key"].toString();
 
275
            QString ourkey   = m["offer"].toString();
 
276
            QString theirdbid = m["controlid"].toString();
 
277
            servent()->reverseOfferRequest( this, theirdbid, ourkey, theirkey );
 
278
        }
 
279
        else if ( m.value( "method" ).toString() == "dbsync-offer" )
 
280
        {
 
281
            m_dbconnkey = m.value( "key" ).toString() ;
 
282
            setupDbSyncConnection();
 
283
        }
 
284
        else if ( m.value( "method" ) == "protovercheckfail" )
 
285
        {
 
286
            qDebug() << "*** Remote peer protocol version mismatch, connection closed";
 
287
            shutdown( true );
 
288
            return;
 
289
        }
 
290
        else
 
291
        {
 
292
            tDebug() << id() << "Unhandled msg:" << QString::fromAscii( msg->payload() );
 
293
        }
 
294
 
 
295
        return;
 
296
    }
 
297
 
 
298
    tDebug() << id() << "Invalid msg:" << QString::fromAscii( msg->payload() );
 
299
}
 
300
 
 
301
 
 
302
 
 
303
void
 
304
ControlConnection::onPingTimer()
 
305
{
 
306
    if ( m_pingtimer_mark.elapsed() >= TCP_TIMEOUT * 1000 )
 
307
    {
 
308
        qDebug() << "Timeout reached! Shutting down connection to" << m_source->friendlyName();
 
309
        shutdown( true );
 
310
    }
 
311
 
 
312
    sendMsg( Msg::factory( QByteArray(), Msg::PING ) );
 
313
}