1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-01 14:42:25 +02:00

Move almost all of the rest of the logic of infosystem to the worker

thread
This commit is contained in:
Jeff Mitchell 2011-07-06 12:49:58 -04:00
parent 5d0c734187
commit 6bb26ecde8
4 changed files with 59 additions and 56 deletions

View File

@ -54,7 +54,6 @@ InfoSystem::InfoSystem(QObject *parent)
: QObject(parent)
, m_infoSystemCacheThreadController( 0 )
, m_infoSystemWorkerThreadController( 0 )
, m_nextRequest( 0 )
{
s_instance = this;
@ -75,10 +74,11 @@ InfoSystem::InfoSystem(QObject *parent)
connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( newNam() ) );
connect( m_cache.data(), SIGNAL( info( uint, QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, QVariantMap ) ),
this, SLOT( infoSlot( uint, QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, QVariantMap ) ), Qt::UniqueConnection );
m_worker.data(), SLOT( infoSlot( uint, QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, QVariantMap ) ), Qt::UniqueConnection );
connect( m_worker.data(), SIGNAL( info( uint, QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, QVariantMap ) ),
this, SLOT( infoSlot( uint, QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, QVariantMap ) ), Qt::UniqueConnection );
connect( m_worker.data(), SIGNAL( info( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, QVariantMap ) ),
this, SIGNAL( info( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, QVariantMap ) ), Qt::UniqueConnection );
connect( m_worker.data(), SIGNAL( finished( QString ) ), this, SIGNAL( finished( QString) ), Qt::UniqueConnection );
}
InfoSystem::~InfoSystem()
@ -122,12 +122,7 @@ void
InfoSystem::getInfo( const QString &caller, const InfoType type, const QVariant& input, QVariantMap customData )
{
qDebug() << Q_FUNC_INFO;
uint requestnum = ++m_nextRequest;
qDebug() << "assigning request with requestId " << requestnum;
m_dataTracker[caller][type] = m_dataTracker[caller][type] + 1;
qDebug() << "current count in dataTracker for type" << type << "is" << m_dataTracker[caller][type];
QMetaObject::invokeMethod( m_worker.data(), "getInfo", Qt::QueuedConnection, Q_ARG( uint, requestnum ), Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input ), Q_ARG( QVariantMap, customData ) );
QMetaObject::invokeMethod( m_worker.data(), "getInfo", Qt::QueuedConnection, Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input ), Q_ARG( QVariantMap, customData ) );
}
@ -135,7 +130,7 @@ void
InfoSystem::getInfo( const QString &caller, const InfoTypeMap &input, QVariantMap customData )
{
Q_FOREACH( InfoType type, input.keys() )
getInfo( caller, type, input[type], customData );
QMetaObject::invokeMethod( m_worker.data(), "getInfo", Qt::QueuedConnection, Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input[ type ] ), Q_ARG( QVariantMap, customData ) );
}
@ -143,7 +138,6 @@ void
InfoSystem::pushInfo( const QString &caller, const InfoType type, const QVariant& input )
{
qDebug() << Q_FUNC_INFO;
QMetaObject::invokeMethod( m_worker.data(), "pushInfo", Qt::QueuedConnection, Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input ) );
}
@ -152,34 +146,7 @@ void
InfoSystem::pushInfo( const QString &caller, const InfoTypeMap &input )
{
Q_FOREACH( InfoType type, input.keys() )
pushInfo( caller, type, input[ type ] );
}
void
InfoSystem::infoSlot( uint requestId, QString target, InfoType type, QVariant input, QVariant output, QVariantMap customData )
{
qDebug() << Q_FUNC_INFO << " with requestId " << requestId;
qDebug() << "current count in dataTracker for target " << target << " is " << m_dataTracker[ target ][ type ];
if ( m_dataTracker[ target ][ type ] == 0 )
{
qDebug() << "Caller was not waiting for that type of data!";
return;
}
emit info( target, type, input, output, customData );
m_dataTracker[ target ][ type ] = m_dataTracker[ target ][ type ] - 1;
qDebug() << "current count in dataTracker for target " << target << " is " << m_dataTracker[ target ][ type ];
Q_FOREACH( InfoType testtype, m_dataTracker[ target ].keys() )
{
if ( m_dataTracker[ target ][ testtype ] != 0)
{
qDebug() << "found outstanding request of type" << testtype;
return;
}
}
qDebug() << "emitting finished with target" << target;
emit finished( target );
QMetaObject::invokeMethod( m_worker.data(), "pushInfo", Qt::QueuedConnection, Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input[ type ] ) );
}
} //namespace InfoSystem

View File

@ -163,24 +163,18 @@ public:
void pushInfo( const QString &caller, const InfoTypeMap &input );
signals:
void info( QString caller, Tomahawk::InfoSystem::InfoType, QVariant input, QVariant output, QVariantMap customData );
void info( QString target, Tomahawk::InfoSystem::InfoType, QVariant input, QVariant output, QVariantMap customData );
void finished( QString target );
public slots:
void infoSlot( uint requestId, const QString target, const Tomahawk::InfoSystem::InfoType type, const QVariant input, const QVariant output, const QVariantMap customData );
void newNam() const;
private:
QHash< QString, QHash< InfoType, int > > m_dataTracker;
QWeakPointer< InfoSystemCache > m_cache;
QWeakPointer< InfoSystemWorker > m_worker;
QThread* m_infoSystemCacheThreadController;
QThread* m_infoSystemWorkerThreadController;
uint m_nextRequest;
static InfoSystem* s_instance;
};

View File

@ -42,6 +42,8 @@ namespace InfoSystem
{
InfoSystemWorker::InfoSystemWorker()
: QObject()
, m_nextRequest( 0 )
{
qDebug() << Q_FUNC_INFO;
}
@ -88,7 +90,7 @@ InfoSystemWorker::init( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache> cac
connect(
plugin.data(),
SIGNAL( info( uint, QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, QVariantMap ) ),
InfoSystem::instance(),
this,
SLOT( infoSlot( uint, QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, QVariantMap ) ),
Qt::UniqueConnection
);
@ -147,24 +149,29 @@ InfoSystemWorker::determineOrderedMatches( const InfoType type ) const
void
InfoSystemWorker::getInfo( uint requestId, QString caller, InfoType type, QVariant input, QVariantMap customData )
InfoSystemWorker::getInfo( QString caller, InfoType type, QVariant input, QVariantMap customData )
{
qDebug() << Q_FUNC_INFO;
QLinkedList< InfoPluginPtr > providers = determineOrderedMatches(type);
QLinkedList< InfoPluginPtr > providers = determineOrderedMatches( type );
if ( providers.isEmpty() )
{
emit info( requestId, caller, type, QVariant(), QVariant(), customData );
emit info( caller, type, QVariant(), QVariant(), customData );
return;
}
InfoPluginPtr ptr = providers.first();
if ( !ptr )
{
emit info( requestId, caller, type, QVariant(), QVariant(), customData );
emit info( caller, type, QVariant(), QVariant(), customData );
return;
}
QMetaObject::invokeMethod( ptr.data(), "getInfo", Qt::QueuedConnection, Q_ARG( uint, requestId ), Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input ), Q_ARG( QVariantMap, customData ) );
uint requestnum = ++m_nextRequest;
qDebug() << "assigning request with requestId " << requestnum;
m_dataTracker[ caller ][ type ] = m_dataTracker[ caller ][ type ] + 1;
qDebug() << "current count in dataTracker for type" << type << "is" << m_dataTracker[ caller ][ type ];
QMetaObject::invokeMethod( ptr.data(), "getInfo", Qt::QueuedConnection, Q_ARG( uint, requestnum ), Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input ), Q_ARG( QVariantMap, customData ) );
}
@ -181,6 +188,33 @@ InfoSystemWorker::pushInfo( const QString caller, const InfoType type, const QVa
}
void
InfoSystemWorker::infoSlot( uint requestId, QString target, InfoType type, QVariant input, QVariant output, QVariantMap customData )
{
qDebug() << Q_FUNC_INFO << " with requestId " << requestId;
qDebug() << "current count in dataTracker for target " << target << " is " << m_dataTracker[ target ][ type ];
if ( m_dataTracker[ target ][ type ] == 0 )
{
qDebug() << "Caller was not waiting for that type of data!";
return;
}
emit info( target, type, input, output, customData );
m_dataTracker[ target ][ type ] = m_dataTracker[ target ][ type ] - 1;
qDebug() << "current count in dataTracker for target " << target << " is " << m_dataTracker[ target ][ type ];
Q_FOREACH( InfoType testtype, m_dataTracker[ target ].keys() )
{
if ( m_dataTracker[ target ][ testtype ] != 0)
{
qDebug() << "found outstanding request of type" << testtype;
return;
}
}
qDebug() << "emitting finished with target" << target;
emit finished( target );
}
QNetworkAccessManager*
InfoSystemWorker::nam() const
{
@ -235,7 +269,6 @@ InfoSystemWorker::newNam()
//FIXME: Currently leaking nam/proxyfactory above -- how to change in a thread-safe way?
}
} //namespace InfoSystem
} //namespace Tomahawk

View File

@ -48,16 +48,23 @@ public:
QNetworkAccessManager* nam() const;
signals:
void info( uint requestId, QString target, Tomahawk::InfoSystem::InfoType, QVariant input, QVariant output, QVariantMap customData );
void info( QString target, Tomahawk::InfoSystem::InfoType, QVariant input, QVariant output, QVariantMap customData );
void finished( QString target );
void namChanged( QNetworkAccessManager* );
public slots:
void init( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache > cache );
void getInfo( uint requestId, const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input, const QVariantMap customData );
void getInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input, const QVariantMap customData );
void pushInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input );
void infoSlot( uint requestId, const QString target, const Tomahawk::InfoSystem::InfoType type, const QVariant input, const QVariant output, const QVariantMap customData );
void newNam();
private:
QHash< QString, QHash< InfoType, int > > m_dataTracker;
QLinkedList< InfoPluginPtr > determineOrderedMatches( const InfoType type ) const;
// For now, statically instantiate plugins; this is just somewhere to keep them
@ -67,6 +74,8 @@ private:
QMap< InfoType, QLinkedList< InfoPluginPtr > > m_infoPushMap;
QWeakPointer< QNetworkAccessManager> m_nam;
uint m_nextRequest;
};
}