X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fsignalproxy.cpp;h=7aab0252543258799c9b5a1549d9836e03fcfa29;hp=b7ac96818a46a65bfd4e653a460b00551ce572e1;hb=dcc90a564e00e206f05db224f1654c8907f149f4;hpb=a7f5d6a23f7214b11f6db85346a67fd7d02767da diff --git a/src/common/signalproxy.cpp b/src/common/signalproxy.cpp index b7ac9681..7aab0252 100644 --- a/src/common/signalproxy.cpp +++ b/src/common/signalproxy.cpp @@ -5,7 +5,7 @@ * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * + * (at your option) version 3. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * @@ -16,16 +16,6 @@ * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - *************************************************************************** - * SignalProxy has been inspired by QxtRPCPeer, part of libqxt, * - * the Qt eXTension Library . We would like to * - * thank Arvid "aep" Picciani and Adam "ahigerd" Higerd for providing * - * QxtRPCPeer, valuable input and the genius idea to (ab)use Qt's * - * Meta Object System for transmitting signals over the network. * - * * - * To make contribution back into libqxt possible, redistribution and * - * modification of this file is additionally allowed under the terms of * - * the Common Public License, version 1.0, as published by IBM. * ***************************************************************************/ #include "signalproxy.h" @@ -39,6 +29,7 @@ #include #include #include +#include #include #include "util.h" @@ -72,7 +63,7 @@ SignalRelay::SignalRelay(SignalProxy* parent, QObject* source) caller(source), _sync(false) { - QObject::connect(source, SIGNAL(destroyed()), parent, SLOT(detachSender())); + QObject::connect(source, SIGNAL(destroyed()), parent, SLOT(detachSender()), Qt::DirectConnection); } int SignalRelay::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { @@ -256,7 +247,8 @@ void SignalProxy::objectRenamed(QString oldname, QString newname) { } void SignalProxy::objectRenamed(QByteArray classname, QString oldname, QString newname) { - if(_syncSlave.contains(classname) && _syncSlave[classname].contains(oldname)) + QMutexLocker locker(&slaveMutex); + if(_syncSlave.contains(classname) && _syncSlave[classname].contains(oldname) && oldname != newname) _syncSlave[classname][newname] = _syncSlave[classname].take(oldname); } @@ -337,7 +329,6 @@ const QByteArray &SignalProxy::methodName(QObject *obj, int methodId) { void SignalProxy::setSyncMap(QObject *obj) { const QMetaObject *meta = obj->metaObject(); - QHash syncMap; QList slotIndexes; @@ -420,7 +411,7 @@ bool SignalProxy::attachSlot(const QByteArray& sigName, QObject* recv, const cha _attachedSlots.insert(funcName, qMakePair(recv, methodId)); QObject::disconnect(recv, SIGNAL(destroyed()), this, SLOT(detachSender())); - QObject::connect(recv, SIGNAL(destroyed()), this, SLOT(detachSender())); + QObject::connect(recv, SIGNAL(destroyed()), this, SLOT(detachSender()), Qt::DirectConnection); return true; } @@ -443,7 +434,7 @@ void SignalProxy::synchronizeAsMaster(QObject *sender) { relay->setSynchronize(true); if(sender->metaObject()->indexOfSignal(QMetaObject::normalizedSignature("renameObject(QString, QString)")) != -1) - connect(sender, SIGNAL(renameObject(QString, QString)), this, SLOT(objectRenamed(QString, QString))); + connect(sender, SIGNAL(renameObject(QString, QString)), this, SLOT(objectRenamed(QString, QString)), Qt::DirectConnection); QByteArray className(sender->metaObject()->className()); _syncSlave[className][sender->objectName()] = sender; @@ -482,30 +473,37 @@ void SignalProxy::requestInit(QObject *obj) { } void SignalProxy::detachSender() { - // this is a slot so we can bypass the QueuedConnection - // and if someone is forcing direct connection in a multithreaded environment he's just nuts... - _detachSignals(sender()); - _detachSlots(sender()); + // this is a private slot. the reason for that is, that we enfoce Qt::DirectConnection. + // the result is, that we can be sure, that a registered receiver is removed immediately when it's destroyed. + // since we're guarding the the internal datastructures with mutexes this means, + // that the object destruction is deffered until we're finished delivering our sync/init/whatever request. + // all in all: thread safety! *sigh* + detachObject(sender()); } // detachObject/Signals/Slots() can be called as a result of an incoming call // this might destroy our the iterator used for delivery // thus we wrap the actual disconnection by using QueuedConnections void SignalProxy::detachObject(QObject* obj) { - detachSignals(obj); - detachSlots(obj); + QMutexLocker locker(&slaveMutex); + _detachSignals(obj); + _detachSlots(obj); + _stopSync(obj); } void SignalProxy::detachSignals(QObject* sender) { - QMetaObject::invokeMethod(this, "_detachSignals", - Qt::QueuedConnection, - Q_ARG(QObject*, sender)); + QMutexLocker locker(&slaveMutex); + _detachSignals(sender); } void SignalProxy::detachSlots(QObject* receiver) { - QMetaObject::invokeMethod(this, "_detachSlots", - Qt::QueuedConnection, - Q_ARG(QObject*, receiver)); + QMutexLocker locker(&slaveMutex); + _detachSlots(receiver); +} + +void SignalProxy::stopSync(QObject* obj) { + QMutexLocker locker(&slaveMutex); + _stopSync(obj); } void SignalProxy::_detachSignals(QObject* sender) { @@ -524,6 +522,22 @@ void SignalProxy::_detachSlots(QObject* receiver) { } } +void SignalProxy::_stopSync(QObject* obj) { + if(_relayHash.contains(obj)) + _relayHash[obj]->setSynchronize(false); + + // we can't use a className here, since it might be effed up, if we receive the call as a result of a decon + // gladly the objectName() is still valid. So we have only to iterate over the classes not each instance! *sigh* + QHash::iterator classIter = _syncSlave.begin(); + while(classIter != _syncSlave.end()) { + if(classIter->contains(obj->objectName())) { + classIter->remove(obj->objectName()); + break; + } + classIter++; + } +} + void SignalProxy::dispatchSignal(QIODevice *receiver, const QVariant &identifier, const QVariantList ¶ms) { QVariantList packedFunc; packedFunc << identifier; @@ -547,6 +561,11 @@ void SignalProxy::receivePeerSignal(QIODevice *sender, const QVariant &packedFun if(call.type() != QVariant::Int) return handleSignal(call.toByteArray(), params); + // well yes we are locking code here and not only data. + // otherwise each handler would have to lock the data anyway. this leaves us on the safe side + // unable to forget a lock + QMutexLocker locker(&slaveMutex); + switch(call.toInt()) { case Sync: return handleSync(params); @@ -566,7 +585,7 @@ void SignalProxy::handleSync(QVariantList params) { return; } - QByteArray className =params.takeFirst().toByteArray(); + QByteArray className = params.takeFirst().toByteArray(); QString objectName = params.takeFirst().toString(); int signalId = params.takeFirst().toInt(); @@ -577,8 +596,18 @@ void SignalProxy::handleSync(QVariantList params) { QObject *receiver = _syncSlave[className][objectName]; if(!syncMap(receiver).contains(signalId)) { - qWarning() << "received Sync Call with invalid SignalId" << className << objectName << signalId; + const QMetaObject *meta = receiver->metaObject(); + QString signalName; + if(signalId < meta->methodCount()) + signalName = QString(meta->method(signalId).signature()); + else + signalName = QString::number(signalId); + + qWarning() << "received Sync Call for Object" << receiver + << "- no matching Slot for Signal:" << signalName; + return; } + int slotId = syncMap(receiver)[signalId]; if(!invokeSlot(receiver, slotId, params)) qWarning("SignalProxy::handleSync(): invokeMethod for \"%s\" failed ", methodName(receiver, slotId).constData()); @@ -593,7 +622,7 @@ void SignalProxy::handleInitRequest(QIODevice *sender, const QVariantList ¶m QByteArray className(params[0].toByteArray()); QString objectName(params[1].toString()); - + if(!_syncSlave.contains(className)) { qWarning() << "SignalProxy::handleInitRequest() received initRequest for unregistered Class:" << className; @@ -605,7 +634,7 @@ void SignalProxy::handleInitRequest(QIODevice *sender, const QVariantList ¶m << className << objectName; return; } - + QObject *obj = _syncSlave[className][objectName]; QVariantList params_; @@ -821,4 +850,14 @@ void SignalProxy::dumpProxyStats() { qDebug() << "number of Classes cached:" << _classInfo.count(); } +void SignalProxy::dumpSyncMap(QObject *object) { + const QMetaObject *meta = object->metaObject(); + qDebug() << "SignalProxy: SyncMap for Class" << meta->className(); + QHash syncMap_ = syncMap(object); + QHash::const_iterator iter = syncMap_.constBegin(); + while(iter != syncMap_.constEnd()) { + qDebug() << iter.key() << meta->method(iter.key()).signature() << "-->" << iter.value() << meta->method(iter.value()).signature(); + iter++; + } +}