/***************************************************************************
- * Copyright (C) 2005-07 by the Quassel IRC Team *
+ * Copyright (C) 2005-08 by the Quassel Project *
* devel@quassel-irc.org *
* *
* This program is free software; you can redistribute it and/or modify *
***************************************************************************/
#include "signalproxy.h"
+
#include <QObject>
#include <QIODevice>
#include <QAbstractSocket>
#include <QMetaMethod>
#include <QMetaProperty>
#include <QRegExp>
+#include <QThread>
+
+#include "syncableobject.h"
#include "util.h"
class SignalRelay: public QObject {
int sigCount() const;
private:
+ bool isSyncMethod(int i);
+
SignalProxy* proxy;
QObject* caller;
QMultiHash<int, QByteArray> sigNames;
proxy->dispatchSignal(funcIter.value(), params);
funcIter++;
}
- if(synchronize() && proxy->syncMap(caller).contains(_id)) {
- // qDebug() << "__SYNC__ >>>"
- // << caller->metaObject()->className()
- // << caller->objectName()
- // << proxy->methodName(caller, _id)
- // << params;
- params.prepend(QVariant(_id));
+
+ // dispatch Sync Signal if necessary
+ QByteArray signature(caller->metaObject()->method(_id).signature());
+ if(synchronize() && proxy->syncMap(qobject_cast<SyncableObject *>(caller)).contains(signature)) {
+ //qDebug() << "__SYNC__ >>>"
+ // << caller->metaObject()->className()
+ // << caller->objectName()
+ // << signature
+ // << params;
+ // params.prepend(QVariant(_id));
+ params.prepend(signature);
params.prepend(caller->objectName());
params.prepend(caller->metaObject()->className());
proxy->dispatchSignal((int)SignalProxy::Sync, params);
}
void SignalRelay::setSynchronize(bool sync) {
- QHash<int, int>::const_iterator iter = proxy->syncMap(caller).constBegin();
+ const QMetaObject *meta = caller->metaObject();
if(!_sync && sync) {
- while(iter != proxy->syncMap(caller).constEnd()) {
- QMetaObject::connect(caller, iter.key(), this, QObject::staticMetaObject.methodCount() + iter.key());
- iter++;
+ // enable Sync
+ for(int i = 0; i < meta->methodCount(); i++ ) {
+ if(isSyncMethod(i))
+ QMetaObject::connect(caller, i, this, QObject::staticMetaObject.methodCount() + i);
}
} else if (_sync && !sync) {
- while(iter != proxy->syncMap(caller).constEnd()) {
- QMetaObject::disconnect(caller, iter.key(), this, QObject::staticMetaObject.methodCount() + iter.key());
- iter++;
+ // disable Sync
+ for(int i = 0; i < meta->methodCount(); i++ ) {
+ if(isSyncMethod(i))
+ QMetaObject::disconnect(caller, i, this, QObject::staticMetaObject.methodCount() + i);
}
}
_sync = sync;
}
+bool SignalRelay::isSyncMethod(int i) {
+ QByteArray signature = caller->metaObject()->method(i).signature();
+ if(!proxy->syncMap(qobject_cast<SyncableObject *>(caller)).contains(signature))
+ return false;
+
+ if(proxy->proxyMode() == SignalProxy::Server && !signature.contains("Requested"))
+ return true;
+
+ if(proxy->proxyMode() == SignalProxy::Client && signature.contains("Requested"))
+ return true;
+
+ return false;
+}
+
bool SignalRelay::synchronize() const {
return _sync;
}
// OK we're brutal here... but since it's a private slot we know what we've got connected to it...
QIODevice *ioDev = (QIODevice *)(sender());
removePeer(ioDev);
+ qDebug() << "Client disconnected.";
}
void SignalProxy::objectRenamed(QString oldname, QString newname) {
}
void SignalProxy::objectRenamed(QByteArray classname, QString oldname, QString newname) {
- if(_syncSlave.contains(classname) && _syncSlave[classname].contains(oldname))
+ if(_syncSlave.contains(classname) && _syncSlave[classname].contains(oldname) && oldname != newname)
_syncSlave[classname][newname] = _syncSlave[classname].take(oldname);
}
return _classInfo[obj->metaObject()]->argTypes[methodId];
}
+void SignalProxy::setMinArgCount(QObject *obj, int methodId) {
+ const QMetaObject *meta = obj->metaObject();
+ QString signature(meta->method(methodId).signature());
+ int minCount = meta->method(methodId).parameterTypes().count() - signature.count("=");
+ Q_ASSERT(!_classInfo[meta]->minArgCount.contains(methodId));
+ _classInfo[meta]->minArgCount[methodId] = minCount;
+}
+
+const int &SignalProxy::minArgCount(QObject *obj, int methodId) {
+ Q_ASSERT(_classInfo.contains(obj->metaObject()));
+ if(!_classInfo[obj->metaObject()]->minArgCount.contains(methodId))
+ setMinArgCount(obj, methodId);
+ return _classInfo[obj->metaObject()]->minArgCount[methodId];
+}
+
void SignalProxy::setMethodName(QObject *obj, int methodId) {
const QMetaObject *meta = obj->metaObject();
QByteArray method(::methodName(meta->method(methodId)));
}
-void SignalProxy::setSyncMap(QObject *obj) {
+void SignalProxy::setSyncMap(SyncableObject *obj) {
const QMetaObject *meta = obj->metaObject();
- QHash<int, int> syncMap;
+ QHash<QByteArray, int> syncMap;
QList<int> slotIndexes;
for(int i = 0; i < meta->methodCount(); i++) {
}
if(matchIdx != -1) {
slotIndexes.removeAt(slotIndexes.indexOf(matchIdx));
- syncMap[signalIdx] = matchIdx;
+ syncMap[QByteArray(signal.signature())] = matchIdx;
}
}
_classInfo[meta]->syncMap = syncMap;
}
-const QHash<int,int> &SignalProxy::syncMap(QObject *obj) {
+const QHash<QByteArray,int> &SignalProxy::syncMap(SyncableObject *obj) {
Q_ASSERT(_classInfo.contains(obj->metaObject()));
if(_classInfo[obj->metaObject()]->syncMap.isEmpty())
setSyncMap(obj);
return _classInfo[obj->metaObject()]->syncMap;
}
+void SignalProxy::setUpdatedRemotelyId(QObject *obj) {
+ const QMetaObject *meta = obj->metaObject();
+ Q_ASSERT(_classInfo.contains(meta));
+ _classInfo[meta]->updatedRemotelyId = meta->indexOfSignal("updatedRemotely()");
+}
+
+int SignalProxy::updatedRemotelyId(SyncableObject *obj) {
+ Q_ASSERT(_classInfo.contains(obj->metaObject()));
+ return _classInfo[obj->metaObject()]->updatedRemotelyId;
+}
+
void SignalProxy::createClassInfo(QObject *obj) {
- if(!_classInfo.contains(obj->metaObject()))
- _classInfo[obj->metaObject()] = new ClassInfo();
+ if(_classInfo.contains(obj->metaObject()))
+ return;
+
+ ClassInfo *classInfo = new ClassInfo();
+ _classInfo[obj->metaObject()] = classInfo;
+ setUpdatedRemotelyId(obj);
}
bool SignalProxy::attachSignal(QObject* sender, const char* signal, const QByteArray& sigName) {
return true;
}
-void SignalProxy::synchronize(QObject *obj) {
- if(proxyMode() == Server)
- return synchronizeAsMaster(obj);
- else
- return synchronizeAsSlave(obj);
-}
-
-void SignalProxy::synchronizeAsMaster(QObject *sender) {
- createClassInfo(sender);
+void SignalProxy::synchronize(SyncableObject *obj) {
+ createClassInfo(obj);
+ // attaching all the Signals
SignalRelay* relay;
- if(_relayHash.contains(sender))
- relay = _relayHash[sender];
+ if(_relayHash.contains(obj))
+ relay = _relayHash[obj];
else
- relay = _relayHash[sender] = new SignalRelay(this, sender);
+ relay = _relayHash[obj] = new SignalRelay(this, obj);
relay->setSynchronize(true);
- if(sender->metaObject()->indexOfSignal(QMetaObject::normalizedSignature("renameObject(QString, QString)")) != -1)
- connect(sender, SIGNAL(renameObject(QString, QString)), this, SLOT(objectRenamed(QString, QString)));
-
- QByteArray className(sender->metaObject()->className());
- _syncSlave[className][sender->objectName()] = sender;
+ // attaching as slave to receive sync Calls
+ QByteArray className(obj->metaObject()->className());
+ _syncSlave[className][obj->objectName()] = obj;
- setInitialized(sender);
-}
+ if(proxyMode() == Server) {
+ if(obj->metaObject()->indexOfSignal(QMetaObject::normalizedSignature("renameObject(QString, QString)")) != -1)
+ connect(obj, SIGNAL(renameObject(QString, QString)), this, SLOT(objectRenamed(QString, QString)));
-void SignalProxy::synchronizeAsSlave(QObject *receiver) {
- QByteArray className(receiver->metaObject()->className());
- _syncSlave[className][receiver->objectName()] = receiver;
-
- createClassInfo(receiver);
-
- requestInit(receiver);
+ setInitialized(obj);
+ } else {
+ requestInit(obj);
+ }
}
-void SignalProxy::setInitialized(QObject *obj) {
- QMetaObject::invokeMethod(obj, "setInitialized");
+void SignalProxy::setInitialized(SyncableObject *obj) {
+ obj->setInitialized();
+ emit objectInitialized(obj);
}
-bool SignalProxy::initialized(QObject *obj) {
- bool init;
- if(!QMetaObject::invokeMethod(obj, "initialized", Q_RETURN_ARG(bool, init)))
- init = false;
- return init;
+bool SignalProxy::isInitialized(SyncableObject *obj) const {
+ return obj->isInitialized();
}
-void SignalProxy::requestInit(QObject *obj) {
- if(proxyMode() == Server || initialized(obj))
+void SignalProxy::requestInit(SyncableObject *obj) {
+ if(proxyMode() == Server || isInitialized(obj))
return;
QVariantList params;
}
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());
+ 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);
+ stopSync(static_cast<SyncableObject *>(obj));
}
void SignalProxy::detachSignals(QObject* sender) {
- QMetaObject::invokeMethod(this, "_detachSignals",
- Qt::QueuedConnection,
- Q_ARG(QObject*, sender));
-}
-
-void SignalProxy::detachSlots(QObject* receiver) {
- QMetaObject::invokeMethod(this, "_detachSlots",
- Qt::QueuedConnection,
- Q_ARG(QObject*, receiver));
-}
-
-void SignalProxy::_detachSignals(QObject* sender) {
if(!_relayHash.contains(sender))
return;
_relayHash.take(sender)->deleteLater();
}
-void SignalProxy::_detachSlots(QObject* receiver) {
+void SignalProxy::detachSlots(QObject* receiver) {
SlotHash::iterator slotIter = _attachedSlots.begin();
while(slotIter != _attachedSlots.end()) {
if(slotIter.value().first == receiver) {
}
}
+void SignalProxy::stopSync(SyncableObject* 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<QByteArray, ObjectId>::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;
return;
}
- QByteArray className =params.takeFirst().toByteArray();
+ QByteArray className = params.takeFirst().toByteArray();
QString objectName = params.takeFirst().toString();
- int signalId = params.takeFirst().toInt();
+ QByteArray signal = params.takeFirst().toByteArray();
if(!_syncSlave.contains(className) || !_syncSlave[className].contains(objectName)) {
- qWarning() << "no registered receiver:" << className << objectName << "for Sync call" << signalId << params;
+ qWarning() << QString("no registered receiver for sync call: %s::%s (objectName=\"%s\"). Params are:").arg(QString(className)).arg(QString(signal)).arg(objectName)
+ << params;
return;
}
- QObject *receiver = _syncSlave[className][objectName];
- if(!syncMap(receiver).contains(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;
+ SyncableObject *receiver = _syncSlave[className][objectName];
+ if(!syncMap(receiver).contains(signal)) {
+ qWarning() << QString("no matching slot for sync call: %s::%s (objectName=\"%s\"). Params are:").arg(QString(className)).arg(QString(signal)).arg(objectName)
+ << params;
return;
- }
- int slotId = syncMap(receiver)[signalId];
- if(!invokeSlot(receiver, slotId, params))
+ }
+
+ int slotId = syncMap(receiver)[signal];
+ if(!invokeSlot(receiver, slotId, params)) {
qWarning("SignalProxy::handleSync(): invokeMethod for \"%s\" failed ", methodName(receiver, slotId).constData());
+ return;
+ }
+ invokeSlot(receiver, updatedRemotelyId(receiver));
}
void SignalProxy::handleInitRequest(QIODevice *sender, const QVariantList ¶ms) {
return;
}
- QObject *obj = _syncSlave[className][objectName];
+ SyncableObject *obj = _syncSlave[className][objectName];
QVariantList params_;
params_ << obj->metaObject()->className()
void SignalProxy::handleInitData(QIODevice *sender, const QVariantList ¶ms) {
Q_UNUSED(sender)
if(params.count() != 3) {
- qWarning() << "SignalProxy::handleInitRequest() received initRequest with invalid param Count:"
+ qWarning() << "SignalProxy::handleInitData() received initData with invalid param Count:"
<< params;
return;
}
QVariantMap propertyMap(params[2].toMap());
if(!_syncSlave.contains(className)) {
- qWarning() << "SignalProxy::handleInitRequest() received initRequest for unregistered Class:"
+ qWarning() << "SignalProxy::handleInitData() received initData for unregistered Class:"
<< className;
return;
}
if(!_syncSlave[className].contains(objectName)) {
- qWarning() << "SignalProxy::handleInitRequest() received initRequest for unregistered Object:"
+ qWarning() << "SignalProxy::handleInitData() received initData for unregistered Object:"
<< className << objectName;
return;
}
- QObject *obj = _syncSlave[className][objectName];
+ SyncableObject *obj = _syncSlave[className][objectName];
setInitData(obj, propertyMap);
}
}
bool SignalProxy::invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms) {
- int numParams = argTypes(receiver, methodId).count();
- QGenericArgument args[9];
- for(int i = 0; i < numParams; i++)
- args[i] = QGenericArgument(params[i].typeName(), params[i].constData());
- return QMetaObject::invokeMethod(receiver, methodName(receiver, methodId), args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
+ const QList<int> args = argTypes(receiver, methodId);
+ const int numArgs = params.count() < args.count()
+ ? params.count()
+ : args.count();
+
+ if(minArgCount(receiver, methodId) < params.count()) {
+ qWarning() << "SignalProxy::invokeSlot(): not enough params to invoke" << methodName(receiver, methodId);
+ return false;
+ }
+
+ void *_a[numArgs+1];
+ _a[0] = 0;
+ // check for argument compatibility and build params array
+ for(int i = 0; i < numArgs; i++) {
+ if(args[i] != QMetaType::type(params[i].typeName())) {
+ qWarning() << "SignalProxy::invokeSlot(): incompatible param types to invoke" << methodName(receiver, methodId);
+ return false;
+ }
+ _a[i+1] = const_cast<void *>(params[i].constData());
+ }
+
+
+ Qt::ConnectionType type = QThread::currentThread() == receiver->thread()
+ ? Qt::DirectConnection
+ : Qt::QueuedConnection;
+
+ if (type == Qt::DirectConnection) {
+ return receiver->qt_metacall(QMetaObject::InvokeMetaMethod, methodId, _a) < 0;
+ } else {
+ qWarning() << "Queued Connections are not implemented yet";
+ // not to self: qmetaobject.cpp:990 ff
+ return false;
+ }
+
}
void SignalProxy::dataAvailable() {
return methodname;
}
-QVariantMap SignalProxy::initData(QObject *obj) const {
- QVariantMap properties;
-
- const QMetaObject* meta = obj->metaObject();
-
- // we collect data from properties
- for(int i = 0; i < meta->propertyCount(); i++) {
- QMetaProperty prop = meta->property(i);
- properties[QString(prop.name())] = prop.read(obj);
- }
-
- // ...as well as methods, which have names starting with "init"
- for(int i = 0; i < meta->methodCount(); i++) {
- QMetaMethod method = meta->method(i);
- QString methodname(::methodName(method));
- if(!methodname.startsWith("init") || methodname.startsWith("initSet"))
- continue;
-
- QVariant value = QVariant(QVariant::nameToType(method.typeName()));
- QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value);
- QMetaObject::invokeMethod(obj, methodname.toAscii(), genericvalue);
-
- properties[methodBaseName(method)] = value;
- // qDebug() << ">>> SYNC:" << methodBaseName(method) << value;
- }
-
- // properties["Payload"] = QByteArray(10000000, 'a'); // for testing purposes
- return properties;
+QVariantMap SignalProxy::initData(SyncableObject *obj) const {
+ return obj->toVariantMap();
}
-void SignalProxy::setInitData(QObject *obj, const QVariantMap &properties) {
- if(initialized(obj))
+void SignalProxy::setInitData(SyncableObject *obj, const QVariantMap &properties) {
+ if(isInitialized(obj))
return;
-
- const QMetaObject *meta = obj->metaObject();
-
- QVariantMap::const_iterator iterator = properties.constBegin();
- while(iterator != properties.constEnd()) {
- QString name = iterator.key();
- int propertyIndex = meta->indexOfProperty(name.toAscii());
-
- if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
- setInitValue(obj, name, iterator.value());
- else
- obj->setProperty(name.toAscii(), iterator.value());
- // qDebug() << "<<< SYNC:" << name << iterator.value();
- iterator++;
- }
+ obj->fromVariantMap(properties);
setInitialized(obj);
}
-bool SignalProxy::setInitValue(QObject *obj, const QString &property, const QVariant &value) {
- QString handlername = QString("initSet") + property;
- handlername[7] = handlername[7].toUpper();
- QGenericArgument param(value.typeName(), value.constData());
- return QMetaObject::invokeMethod(obj, handlername.toAscii(), param);
-}
-
void SignalProxy::dumpProxyStats() {
QString mode;
if(proxyMode() == Server)
qDebug() << "number of Classes cached:" << _classInfo.count();
}
-void SignalProxy::dumpSyncMap(QObject *object) {
+void SignalProxy::dumpSyncMap(SyncableObject *object) {
const QMetaObject *meta = object->metaObject();
qDebug() << "SignalProxy: SyncMap for Class" << meta->className();
- QHash<int, int> syncMap_ = syncMap(object);
- QHash<int, int>::const_iterator iter = syncMap_.constBegin();
+ QHash<QByteArray, int> syncMap_ = syncMap(object);
+ QHash<QByteArray, int>::const_iterator iter = syncMap_.constBegin();
while(iter != syncMap_.constEnd()) {
- qDebug() << iter.key() << meta->method(iter.key()).signature() << "-->" << iter.value() << meta->method(iter.value()).signature();
+ qDebug() << iter.key() << "-->" << iter.value() << meta->method(iter.value()).signature();
iter++;
}
+// QHash<int, int> syncMap_ = syncMap(object);
+// QHash<int, int>::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++;
+// }
}