void ControlConnection::handleMsg( msg_ptr msg ) { if ( msg->is( Msg::PING ) ) { // qDebug() << "Received Connection PING, nice." << m_pingtimer_mark.elapsed(); m_pingtimer_mark.restart(); return; } // if small and not compresed, print it out for debug if ( msg->length() < 1024 && !msg->is( Msg::COMPRESSED ) ) { qDebug() << id() << "got msg:" << QString::fromLatin1( msg->payload() ); } // All control connection msgs are JSON if ( !msg->is( Msg::JSON ) ) { Q_ASSERT( msg->is( Msg::JSON ) ); markAsFailed(); return; } QVariantMap m = msg->json().toMap(); if ( !m.isEmpty() ) { if ( m.value( "conntype" ).toString() == "request-offer" ) { QString theirkey = m["key"].toString(); QString ourkey = m["offer"].toString(); QString theirdbid = m["controlid"].toString(); servent()->reverseOfferRequest( this, theirdbid, ourkey, theirkey ); } else if ( m.value( "method" ).toString() == "dbsync-offer" ) { m_dbconnkey = m.value( "key" ).toString() ; setupDbSyncConnection(); } else if ( m.value( "method" ) == "protovercheckfail" ) { qDebug() << "*** Remote peer protocol version mismatch, connection closed"; shutdown( true ); return; } else { tDebug() << id() << "Unhandled msg:" << QString::fromLatin1( msg->payload() ); } return; } tDebug() << id() << "Invalid msg:" << QString::fromLatin1( msg->payload() ); }
void MsgProcessor::append( msg_ptr msg ) { if( QThread::currentThread() != thread() ) { qDebug() << "reinvoking msgprocessor::append in correct thread, ie not" << QThread::currentThread(); QMetaObject::invokeMethod( this, "append", Qt::QueuedConnection, Q_ARG(msg_ptr, msg) ); return; } m_msgs.append( msg ); m_msg_ready.insert( msg.data(), false ); m_totmsgsize += msg->payload().length(); if( m_mode & NOTHING ) { //qDebug() << "MsgProcessor::NOTHING"; handleProcessedMsg( msg ); return; } QFuture<msg_ptr> fut = QtConcurrent::run(&MsgProcessor::process, msg, m_mode, m_threshold); QFutureWatcher<msg_ptr> * watcher = new QFutureWatcher<msg_ptr>; connect( watcher, SIGNAL( finished() ), this, SLOT( processed() ), Qt::QueuedConnection ); watcher->setFuture( fut ); }
/// This method is run by QtConcurrent: msg_ptr MsgProcessor::process( msg_ptr msg, quint32 mode, quint32 threshold ) { // uncompress if needed if( (mode & UNCOMPRESS_ALL) && msg->is( Msg::COMPRESSED ) ) { qDebug() << "MsgProcessor::UNCOMPRESSING"; msg->m_payload = qUncompress( msg->payload() ); msg->m_length = msg->m_payload.length(); msg->m_flags ^= Msg::COMPRESSED; } // parse json payload into qvariant if needed if( (mode & PARSE_JSON) && msg->is( Msg::JSON ) && msg->m_json_parsed == false ) { qDebug() << "MsgProcessor::PARSING JSON"; bool ok; QJson::Parser parser; msg->m_json = parser.parse( msg->payload(), &ok ); msg->m_json_parsed = true; } // compress if needed if( (mode & COMPRESS_IF_LARGE) && !msg->is( Msg::COMPRESSED ) && msg->length() > threshold ) { qDebug() << "MsgProcessor::COMPRESSING"; msg->m_payload = qCompress( msg->payload(), 9 ); msg->m_length = msg->m_payload.length(); msg->m_flags |= Msg::COMPRESSED; } return msg; }
void StreamConnection::handleMsg( msg_ptr msg ) { Q_ASSERT( msg->is( Msg::RAW ) ); if ( msg->payload().startsWith( "block" ) ) { int block = QString( msg->payload() ).mid( 5 ).toInt(); m_readdev->seek( block * BufferIODevice::blockSize() ); qDebug() << "Seeked to block:" << block; QByteArray sm; sm.append( QString( "doneblock%1" ).arg( block ) ); sendMsg( Msg::factory( sm, Msg::RAW | Msg::FRAGMENT ) ); QTimer::singleShot( 0, this, SLOT( sendSome() ) ); } else if ( msg->payload().startsWith( "doneblock" ) ) { int block = QString( msg->payload() ).mid( 9 ).toInt(); ( (BufferIODevice*)m_iodev.data() )->seeked( block ); m_curBlock = block; qDebug() << "Next block is now:" << block; } else if ( msg->payload().startsWith( "data" ) ) { m_badded += msg->payload().length() - 4; ( (BufferIODevice*)m_iodev.data() )->addData( m_curBlock++, msg->payload().mid( 4 ) ); } //qDebug() << Q_FUNC_INFO << "flags" << (int) msg->flags() // << "payload len" << msg->payload().length() // << "written to device so far: " << m_badded; if ( m_iodev && ( (BufferIODevice*)m_iodev.data() )->nextEmptyBlock() < 0 ) { m_allok = true; // tell our iodev there is no more data to read, no args meaning a success: ( (BufferIODevice*)m_iodev.data() )->inputComplete(); shutdown(); } }
void DBSyncConnection::handleMsg( msg_ptr msg ) { Q_ASSERT( !msg->is( Msg::COMPRESSED ) ); if ( m_state == FETCHING ) changeState( PARSING ); // "everything is synced" indicated by non-json msg containing "ok": if ( !msg->is( Msg::JSON ) && msg->is( Msg::DBOP ) && msg->payload() == "ok" ) { changeState( SYNCED ); // calc the collection stats, to updates the "X tracks" in the sidebar etc // this is done automatically if you run a dbcmd to add files. DatabaseCommand_CollectionStats* cmd = new DatabaseCommand_CollectionStats( m_source ); connect( cmd, SIGNAL( done( const QVariantMap & ) ), m_source.data(), SLOT( setStats( const QVariantMap& ) ), Qt::QueuedConnection ); Database::instance()->enqueue( QSharedPointer<DatabaseCommand>(cmd) ); return; }