void attachSignal(int methodId, const QByteArray &func);
void setSynchronize(bool);
- bool synchronize() const;
- int sigCount() const;
+ inline bool synchronize() const { return _sync; }
+ inline int sigCount() const { return sigNames.count(); }
private:
bool isSyncMethod(int i);
_id = QObject::qt_metacall(_c, _id, _a);
if(_id < 0)
return _id;
+
+ SignalProxy::ExtendedMetaObject *eMeta = proxy->extendedMetaObject(caller);
+ Q_ASSERT(eMeta);
if(_c == QMetaObject::InvokeMetaMethod) {
if(sigNames.contains(_id) || synchronize()) {
- const QList<int> &argTypes = proxy->argTypes(caller, _id);
+ const QList<int> &argTypes = eMeta->argTypes(_id);
QVariantList params;
int n = argTypes.size();
for(int i=0; i<n; i++) {
// dispatch Sync Signal if necessary
QByteArray signature(caller->metaObject()->method(_id).signature());
SyncableObject *syncObject = qobject_cast<SyncableObject *>(caller);
- if(synchronize() && proxy->syncMap(syncObject).contains(signature)) {
+ if(synchronize() && eMeta->syncMap().contains(signature)) {
//qDebug() << "__SYNC__ >>>"
// << caller->metaObject()->className()
// << caller->objectName()
return false;
QByteArray signature = syncObject->syncMetaObject()->method(i).signature();
- if(!proxy->syncMap(syncObject).contains(signature))
+ SignalProxy::ExtendedMetaObject *eMeta = proxy->extendedMetaObject(caller);
+ Q_ASSERT(eMeta);
+ if(!eMeta->syncMap().contains(signature))
return false;
if(proxy->proxyMode() == SignalProxy::Server && !signature.contains("Requested"))
return false;
}
-bool SignalRelay::synchronize() const {
- return _sync;
-}
-
-int SignalRelay::sigCount() const {
- // only for debuging purpose
- return sigNames.count();
-}
-
void SignalRelay::attachSignal(int methodId, const QByteArray &func) {
// we ride without safetybelts here... all checking for valid method etc pp has to be done by the caller
// all connected methodIds are offset by the standard methodCount of QObject
}
}
-void SignalProxy::setArgTypes(QObject* obj, int methodId) {
- const QMetaObject *meta = metaObject(obj);
- QList<QByteArray> p = meta->method(methodId).parameterTypes();
- QList<int> argTypes;
- int ct = p.count();
- for(int i=0; i<ct; i++)
- argTypes.append(QMetaType::type(p.value(i)));
-
- Q_ASSERT(!_classInfo[meta]->argTypes.contains(methodId));
- _classInfo[meta]->argTypes[methodId] = argTypes;
-}
-
-const QList<int> &SignalProxy::argTypes(QObject *obj, int methodId) {
- const QMetaObject *meta = metaObject(obj);
- Q_ASSERT(_classInfo.contains(meta));
- if(!_classInfo[meta]->argTypes.contains(methodId))
- setArgTypes(obj, methodId);
- return _classInfo[meta]->argTypes[methodId];
-}
-
-void SignalProxy::setReturnType(QObject *obj, int methodId) {
- const QMetaObject *meta = metaObject(obj);
- int returnType = QMetaType::type(meta->method(methodId).typeName());
-
- Q_ASSERT(!_classInfo[meta]->returnType.contains(methodId));
- _classInfo[meta]->returnType[methodId] = returnType;
-}
-
-const int &SignalProxy::returnType(QObject *obj, int methodId) {
- const QMetaObject *meta = metaObject(obj);
- Q_ASSERT(_classInfo.contains(meta));
- if(!_classInfo[meta]->returnType.contains(methodId))
- setReturnType(obj, methodId);
- return _classInfo[meta]->returnType[methodId];
-}
-
-void SignalProxy::setMinArgCount(QObject *obj, int methodId) {
- const QMetaObject *meta = metaObject(obj);
- 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) {
- const QMetaObject *meta = metaObject(obj);
- Q_ASSERT(_classInfo.contains(meta));
- if(!_classInfo[meta]->minArgCount.contains(methodId))
- setMinArgCount(obj, methodId);
- return _classInfo[meta]->minArgCount[methodId];
-}
-
-void SignalProxy::setMethodName(QObject *obj, int methodId) {
- const QMetaObject *meta = metaObject(obj);
- QByteArray method(::methodName(meta->method(methodId)));
- Q_ASSERT(!_classInfo[meta]->methodNames.contains(methodId));
- _classInfo[meta]->methodNames[methodId] = method;
-}
-
-const QByteArray &SignalProxy::methodName(QObject *obj, int methodId) {
- const QMetaObject *meta = metaObject(obj);
- Q_ASSERT(_classInfo.contains(meta));
- if(!_classInfo[meta]->methodNames.contains(methodId))
- setMethodName(obj, methodId);
- return _classInfo[meta]->methodNames[methodId];
-}
-
-
-void SignalProxy::setSyncMap(SyncableObject *obj) {
- const QMetaObject *meta = obj->syncMetaObject();
- QHash<QByteArray, int> syncMap;
-
- QList<int> slotIndexes;
- for(int i = 0; i < meta->methodCount(); i++) {
- if(meta->method(i).methodType() == QMetaMethod::Slot)
- slotIndexes << i;
- }
-
- // we're faking sync pairs for sync replies
- QByteArray slotSignature;
- foreach(int slotIdx, slotIndexes) {
- slotSignature = QByteArray(meta->method(slotIdx).signature());
- if(!slotSignature.startsWith("receive"))
- continue;
- syncMap[slotSignature] = slotIdx;
- }
-
- QMetaMethod signal, slot;
- int matchIdx;
- for(int signalIdx = 0; signalIdx < meta->methodCount(); signalIdx++) {
- signal = meta->method(signalIdx);
- if(signal.methodType() != QMetaMethod::Signal)
- continue;
-
- matchIdx = -1;
- foreach(int slotIdx, slotIndexes) {
- slot = meta->method(slotIdx);
- if(methodsMatch(signal, slot)) {
- matchIdx = slotIdx;
- break;
- }
- }
- if(matchIdx != -1) {
- slotIndexes.removeAt(slotIndexes.indexOf(matchIdx));
- syncMap[QByteArray(signal.signature())] = matchIdx;
- }
- }
-
- Q_ASSERT(_classInfo[meta]->syncMap.isEmpty());
- _classInfo[meta]->syncMap = syncMap;
-}
-
-const QHash<QByteArray,int> &SignalProxy::syncMap(SyncableObject *obj) {
- const QMetaObject *meta = obj->syncMetaObject();
- Q_ASSERT(_classInfo.contains(meta));
- if(_classInfo[meta]->syncMap.isEmpty())
- setSyncMap(obj);
- return _classInfo[meta]->syncMap;
-}
-
-void SignalProxy::setReceiveMap(SyncableObject *obj) {
- const QMetaObject *meta = obj->syncMetaObject();
- Q_ASSERT(_classInfo.contains(meta));
-
- QHash<int, int> receiveMap;
-
- QMetaMethod requestSlot;
- QByteArray returnTypeName;
- QByteArray signature;
- QByteArray methodName;
- QByteArray params;
- int paramsPos;
- int receiverId;
- const int methodCount = meta->methodCount();
- for(int i = 0; i < methodCount; i++) {
- requestSlot = meta->method(i);
- if(requestSlot.methodType() != QMetaMethod::Slot)
- continue;
-
- returnTypeName = requestSlot.typeName();
- if(QMetaType::Void == (QMetaType::Type)returnType(obj, i))
- continue;
-
- signature = QByteArray(requestSlot.signature());
- if(!signature.startsWith("request"))
- continue;
-
- paramsPos = signature.indexOf('(');
- if(paramsPos == -1)
- continue;
-
- methodName = signature.left(paramsPos);
- params = signature.mid(paramsPos);
-
- methodName = methodName.replace("request", "receive");
- params = params.left(params.count() - 1) + ", " + returnTypeName + ")";
-
- signature = QMetaObject::normalizedSignature(methodName + params);
- receiverId = meta->indexOfSlot(signature);
-
- if(receiverId == -1) {
- signature = QMetaObject::normalizedSignature(methodName + "(" + returnTypeName + ")");
- receiverId = meta->indexOfSlot(signature);
- }
-
- if(receiverId != -1)
- receiveMap[i] = receiverId;
- }
- _classInfo[meta]->receiveMap = receiveMap;
-}
-
-const QHash<int, int> &SignalProxy::receiveMap(SyncableObject *obj) {
- const QMetaObject *meta = obj->syncMetaObject();
- Q_ASSERT(_classInfo.contains(meta));
- if(_classInfo[meta]->receiveMap.isEmpty())
- setReceiveMap(obj);
- return _classInfo[meta]->receiveMap;
-}
-
-void SignalProxy::setUpdatedRemotelyId(SyncableObject *obj) {
- const QMetaObject *meta = obj->syncMetaObject();
- Q_ASSERT(_classInfo.contains(meta));
- _classInfo[meta]->updatedRemotelyId = meta->indexOfSignal("updatedRemotely()");
-}
-
-int SignalProxy::updatedRemotelyId(SyncableObject *obj) {
- Q_ASSERT(_classInfo.contains(obj->syncMetaObject()));
- return _classInfo[obj->syncMetaObject()]->updatedRemotelyId;
-}
-
-const QMetaObject *SignalProxy::metaObject(QObject *obj) {
- if(SyncableObject *syncObject = qobject_cast<SyncableObject *>(obj))
+const QMetaObject *SignalProxy::metaObject(const QObject *obj) {
+ if(const SyncableObject *syncObject = qobject_cast<const SyncableObject *>(obj))
return syncObject->syncMetaObject();
else
return obj->metaObject();
}
-void SignalProxy::createClassInfo(QObject *obj) {
- const QMetaObject *meta = metaObject(obj);
- if(_classInfo.contains(meta))
- return;
+SignalProxy::ExtendedMetaObject *SignalProxy::extendedMetaObject(const QObject *obj) const {
+ if(_extendedMetaObjects.contains(metaObject(obj)))
+ return _extendedMetaObjects[metaObject(obj)];
+ else
+ return 0;
+}
- ClassInfo *classInfo = new ClassInfo();
- _classInfo[meta] = classInfo;
+void SignalProxy::createExtendedMetaObject(const QObject *obj) {
+ const QMetaObject *meta = metaObject(obj);
+ if(!_extendedMetaObjects.contains(meta)) {
+ _extendedMetaObjects[meta] = new ExtendedMetaObject(meta);
+ }
}
bool SignalProxy::attachSignal(QObject* sender, const char* signal, const QByteArray& sigName) {
return false;
}
- createClassInfo(sender);
+ createExtendedMetaObject(sender);
SignalRelay* relay;
if(_relayHash.contains(sender))
return false;
}
- createClassInfo(recv);
+ createExtendedMetaObject(recv);
QByteArray funcName = QMetaObject::normalizedSignature(sigName.constData());
_attachedSlots.insert(funcName, qMakePair(recv, methodId));
}
void SignalProxy::synchronize(SyncableObject *obj) {
- createClassInfo(obj);
- setUpdatedRemotelyId(obj);
+ createExtendedMetaObject(obj);
// attaching all the Signals
SignalRelay* relay;
}
}
-void SignalProxy::requestInit(SyncableObject *obj) {
- if(proxyMode() == Server || obj->isInitialized())
- return;
-
- QVariantList params;
- params << obj->syncMetaObject()->className()
- << obj->objectName();
- dispatchSignal(InitRequest, params);
-}
-
void SignalProxy::detachSender() {
detachObject(sender());
}
}
SyncableObject *receiver = _syncSlave[className][objectName];
- if(!syncMap(receiver).contains(signal)) {
+ ExtendedMetaObject *eMeta = extendedMetaObject(receiver);
+ if(!eMeta->syncMap().contains(signal)) {
qWarning() << QString("no matching slot for sync call: %1::%2 (objectName=\"%3\"). Params are:").arg(QString(className)).arg(QString(signal)).arg(objectName)
<< params;
return;
}
- int slotId = syncMap(receiver)[signal];
+ int slotId = eMeta->syncMap()[signal];
- QVariant returnValue((QVariant::Type)returnType(receiver, slotId));
+ QVariant returnValue((QVariant::Type)eMeta->returnType(slotId));
if(!invokeSlot(receiver, slotId, params, returnValue)) {
- qWarning("SignalProxy::handleSync(): invokeMethod for \"%s\" failed ", methodName(receiver, slotId).constData());
+ qWarning("SignalProxy::handleSync(): invokeMethod for \"%s\" failed ", eMeta->methodName(slotId).constData());
return;
}
- if(returnValue.type() != QVariant::Invalid && receiveMap(receiver).contains(slotId)) {
- int receiverId = receiveMap(receiver)[slotId];
+ if(returnValue.type() != QVariant::Invalid && eMeta->receiveMap().contains(slotId)) {
+ int receiverId = eMeta->receiveMap()[slotId];
QVariantList returnParams;
returnParams << className
<< objectName
<< QByteArray(receiver->metaObject()->method(receiverId).signature());
- if(argTypes(receiver, receiverId).count() > 1)
+ if(eMeta->argTypes(receiverId).count() > 1)
returnParams << params;
returnParams << returnValue;
sender->dispatchSignal(Sync, returnParams);
}
// send emit update signal
- invokeSlot(receiver, updatedRemotelyId(receiver));
+ invokeSlot(receiver, eMeta->updatedRemotelyId());
}
void SignalProxy::handleInitRequest(AbstractPeer *sender, const QVariantList ¶ms) {
while(slot != _attachedSlots.constEnd() && slot.key() == funcName) {
receiver = (*slot).first;
methodId = (*slot).second;
- if(!invokeSlot(receiver, methodId, params))
- qWarning("SignalProxy::handleSignal(): invokeMethod for \"%s\" failed ", methodName(receiver, methodId).constData());
+ if(!invokeSlot(receiver, methodId, params)) {
+ ExtendedMetaObject *eMeta = extendedMetaObject(receiver);
+ qWarning("SignalProxy::handleSignal(): invokeMethod for \"%s\" failed ", eMeta->methodName(methodId).constData());
+ }
slot++;
}
}
bool SignalProxy::invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue) {
- const QList<int> args = argTypes(receiver, methodId);
+ ExtendedMetaObject *eMeta = extendedMetaObject(receiver);
+ const QList<int> args = eMeta->argTypes(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);
+ if(eMeta->minArgCount(methodId) > params.count()) {
+ qWarning() << "SignalProxy::invokeSlot(): not enough params to invoke" << eMeta->methodName(methodId);
return false;
}
return false;
}
if(args[i] != QMetaType::type(params[i].typeName())) {
- qWarning() << "SignalProxy::invokeSlot(): incompatible param types to invoke" << methodName(receiver, methodId);
+ qWarning() << "SignalProxy::invokeSlot(): incompatible param types to invoke" << eMeta->methodName(methodId);
return false;
}
_a[i+1] = const_cast<void *>(params[i].constData());
return true;
}
-bool SignalProxy::methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const {
- // if we don't even have the same basename it's a sure NO
- QString baseName = methodBaseName(signal);
- if(baseName != methodBaseName(slot))
- return false;
-
- // are the signatures compatible?
- if(!QObject::staticMetaObject.checkConnectArgs(signal.signature(), slot.signature()))
- return false;
-
- // we take an educated guess if the signals and slots match
- QString signalsuffix = ::methodName(signal);
- QString slotprefix = ::methodName(slot);
- if(!baseName.isEmpty()) {
- signalsuffix = signalsuffix.mid(baseName.count()).toLower();
- slotprefix = slotprefix.left(slotprefix.count() - baseName.count()).toLower();
- }
-
- uint sizediff = qAbs(slotprefix.size() - signalsuffix.size());
- int ratio = editingDistance(slotprefix, signalsuffix) - sizediff;
-// if(ratio < 2) {
-// qDebug() << Q_FUNC_INFO;
-// qDebug() << methodBaseName(signal) << methodBaseName(slot);
-// qDebug() << signalsuffix << slotprefix << sizediff << ratio;
-// }
- return (ratio < 2);
-}
-
-QString SignalProxy::methodBaseName(const QMetaMethod &method) {
- QString methodname = QString(method.signature()).section("(", 0, 0);
-
- // determine where we have to chop:
- int upperCharPos;
- if(method.methodType() == QMetaMethod::Slot) {
- // we take evertyhing from the first uppercase char if it's slot
- upperCharPos = methodname.indexOf(QRegExp("[A-Z]"));
- if(upperCharPos == -1)
- return QString();
- methodname = methodname.mid(upperCharPos);
- } else {
- // and if it's a signal we discard everything from the last uppercase char
- upperCharPos = methodname.lastIndexOf(QRegExp("[A-Z]"));
- if(upperCharPos == -1)
- return QString();
- methodname = methodname.left(upperCharPos);
- }
-
- methodname[0] = methodname[0].toUpper();
+void SignalProxy::requestInit(SyncableObject *obj) {
+ if(proxyMode() == Server || obj->isInitialized())
+ return;
- return methodname;
+ QVariantList params;
+ params << obj->syncMetaObject()->className()
+ << obj->objectName();
+ dispatchSignal(InitRequest, params);
}
QVariantMap SignalProxy::initData(SyncableObject *obj) const {
obj->fromVariantMap(properties);
obj->setInitialized();
emit objectInitialized(obj);
- invokeSlot(obj, updatedRemotelyId(obj));
+ invokeSlot(obj, extendedMetaObject(obj)->updatedRemotelyId());
}
void SignalProxy::sendHeartBeat() {
}
}
-
void SignalProxy::dumpProxyStats() {
QString mode;
if(proxyMode() == Server)
qDebug() << " number of Signals:" << sigCount;
qDebug() << " attached Slots:" << _attachedSlots.count();
qDebug() << " number of synced Slaves:" << slaveCount;
- qDebug() << "number of Classes cached:" << _classInfo.count();
+ qDebug() << "number of Classes cached:" << _extendedMetaObjects.count();
}
void SignalProxy::dumpSyncMap(SyncableObject *object) {
const QMetaObject *meta = object->metaObject();
+ ExtendedMetaObject *eMeta = extendedMetaObject(object);
qDebug() << "SignalProxy: SyncMap for Class" << meta->className();
- QHash<QByteArray, int> syncMap_ = syncMap(object);
+ QHash<QByteArray, int> syncMap_ = eMeta->syncMap();
QHash<QByteArray, int>::const_iterator iter = syncMap_.constBegin();
while(iter != syncMap_.constEnd()) {
qDebug() << qPrintable(QString("%1 --> %2 %3").arg(QString(iter.key()), 40).arg(iter.value()).arg(QString(meta->method(iter.value()).signature())));
if(wasSecure != _secure)
emit secureStateChanged(_secure);
}
+
+
+// ==================================================
+// ExtendedMetaObject
+// ==================================================
+SignalProxy::ExtendedMetaObject::ExtendedMetaObject(const QMetaObject *meta)
+ : _meta(meta),
+ _updatedRemotelyId(-1)
+{
+}
+
+const QList<int> &SignalProxy::ExtendedMetaObject::argTypes(int methodId) {
+ if(!_argTypes.contains(methodId)) {
+ QList<QByteArray> paramTypes = _meta->method(methodId).parameterTypes();
+ QList<int> argTypes;
+ for(int i = 0; i < paramTypes.count(); i++) {
+ argTypes.append(QMetaType::type(paramTypes[i]));
+ }
+ _argTypes[methodId] = argTypes;
+ }
+ return _argTypes[methodId];
+}
+
+const int &SignalProxy::ExtendedMetaObject::returnType(int methodId) {
+ if(!_returnType.contains(methodId)) {
+ _returnType[methodId] = QMetaType::type(_meta->method(methodId).typeName());
+ }
+ return _returnType[methodId];
+}
+
+const int &SignalProxy::ExtendedMetaObject::minArgCount(int methodId) {
+ if(!_minArgCount.contains(methodId)) {
+ QString signature(_meta->method(methodId).signature());
+ _minArgCount[methodId] = _meta->method(methodId).parameterTypes().count() - signature.count("=");
+ }
+ return _minArgCount[methodId];
+}
+
+const QByteArray &SignalProxy::ExtendedMetaObject::methodName(int methodId) {
+ if(!_methodNames.contains(methodId)) {
+ _methodNames[methodId] = methodName(_meta->method(methodId));
+ }
+ return _methodNames[methodId];
+}
+
+const QHash<QByteArray, int> &SignalProxy::ExtendedMetaObject::syncMap() {
+ if(_syncMap.isEmpty()) {
+ QHash<QByteArray, int> syncMap;
+
+ QList<int> slotIndexes;
+ for(int i = 0; i < _meta->methodCount(); i++) {
+ if(_meta->method(i).methodType() == QMetaMethod::Slot)
+ slotIndexes << i;
+ }
+
+ // we're faking sync pairs for sync replies
+ // --> we deliver to every slot starting with "receive"
+ QByteArray slotSignature;
+ QList<int>::iterator slotIter = slotIndexes.begin();
+ while(slotIter != slotIndexes.end()) {
+ slotSignature = QByteArray(_meta->method(*slotIter).signature());
+ if(slotSignature.startsWith("receive")) {
+ syncMap[slotSignature] = *slotIter;
+ slotIndexes.erase(slotIter);
+ } else {
+ slotIter++;
+ }
+ }
+
+ // find the proper sig -> slot matches
+ QMetaMethod signal, slot;
+ int matchIdx;
+ for(int signalIdx = 0; signalIdx < _meta->methodCount(); signalIdx++) {
+ signal = _meta->method(signalIdx);
+ if(signal.methodType() != QMetaMethod::Signal)
+ continue;
+
+ matchIdx = -1;
+ foreach(int slotIdx, slotIndexes) {
+ slot = _meta->method(slotIdx);
+ if(methodsMatch(signal, slot)) {
+ matchIdx = slotIdx;
+ break;
+ }
+ }
+ if(matchIdx != -1) {
+ slotIndexes.removeAll(matchIdx);
+ syncMap[QByteArray(signal.signature())] = matchIdx;
+ }
+ }
+ _syncMap = syncMap;
+ }
+ return _syncMap;
+}
+
+const QHash<int, int> &SignalProxy::ExtendedMetaObject::receiveMap() {
+ if(_receiveMap.isEmpty()) {
+ QHash<int, int> receiveMap;
+
+ QMetaMethod requestSlot;
+ QByteArray returnTypeName;
+ QByteArray signature;
+ QByteArray methodName;
+ QByteArray params;
+ int paramsPos;
+ int receiverId;
+ const int methodCount = _meta->methodCount();
+ for(int i = 0; i < methodCount; i++) {
+ requestSlot = _meta->method(i);
+ if(requestSlot.methodType() != QMetaMethod::Slot)
+ continue;
+
+ returnTypeName = requestSlot.typeName();
+ if(QMetaType::Void == (QMetaType::Type)returnType(i))
+ continue;
+
+ signature = QByteArray(requestSlot.signature());
+ if(!signature.startsWith("request"))
+ continue;
+
+ paramsPos = signature.indexOf('(');
+ if(paramsPos == -1)
+ continue;
+
+ methodName = signature.left(paramsPos);
+ params = signature.mid(paramsPos);
+
+ methodName = methodName.replace("request", "receive");
+ params = params.left(params.count() - 1) + ", " + returnTypeName + ")";
+
+ signature = QMetaObject::normalizedSignature(methodName + params);
+ receiverId = _meta->indexOfSlot(signature);
+
+ if(receiverId == -1) {
+ signature = QMetaObject::normalizedSignature(methodName + "(" + returnTypeName + ")");
+ receiverId = _meta->indexOfSlot(signature);
+ }
+
+ if(receiverId != -1)
+ receiveMap[i] = receiverId;
+ }
+ }
+ return _receiveMap;
+}
+
+int SignalProxy::ExtendedMetaObject::updatedRemotelyId() {
+ if(_updatedRemotelyId == -1) {
+ _updatedRemotelyId = _meta->indexOfSignal("updatedRemotely()");
+ }
+ return _updatedRemotelyId;
+}
+
+QByteArray SignalProxy::ExtendedMetaObject::methodName(const QMetaMethod &method) {
+ QByteArray sig(method.signature());
+ return sig.left(sig.indexOf("("));
+}
+
+bool SignalProxy::ExtendedMetaObject::methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) {
+ // if we don't even have the same basename it's a sure NO
+ QString baseName = methodBaseName(signal);
+ if(baseName != methodBaseName(slot))
+ return false;
+
+ // are the signatures compatible?
+ if(!QObject::staticMetaObject.checkConnectArgs(signal.signature(), slot.signature()))
+ return false;
+
+ // we take an educated guess if the signals and slots match
+ QString signalsuffix = methodName(signal);
+ QString slotprefix = methodName(slot);
+ if(!baseName.isEmpty()) {
+ signalsuffix = signalsuffix.mid(baseName.count()).toLower();
+ slotprefix = slotprefix.left(slotprefix.count() - baseName.count()).toLower();
+ }
+
+ uint sizediff = qAbs(slotprefix.size() - signalsuffix.size());
+ int ratio = editingDistance(slotprefix, signalsuffix) - sizediff;
+ return (ratio < 2);
+}
+
+QString SignalProxy::ExtendedMetaObject::methodBaseName(const QMetaMethod &method) {
+ QString methodname = QString(method.signature()).section("(", 0, 0);
+
+ // determine where we have to chop:
+ int upperCharPos;
+ if(method.methodType() == QMetaMethod::Slot) {
+ // we take evertyhing from the first uppercase char if it's slot
+ upperCharPos = methodname.indexOf(QRegExp("[A-Z]"));
+ if(upperCharPos == -1)
+ return QString();
+ methodname = methodname.mid(upperCharPos);
+ } else {
+ // and if it's a signal we discard everything from the last uppercase char
+ upperCharPos = methodname.lastIndexOf(QRegExp("[A-Z]"));
+ if(upperCharPos == -1)
+ return QString();
+ methodname = methodname.left(upperCharPos);
+ }
+
+ methodname[0] = methodname[0].toUpper();
+
+ return methodname;
+}
+
class AbstractPeer;
class IODevicePeer;
+ class SignalProxyPeer;
public:
enum ProxyMode {
bool addPeer(SignalProxy *proxy);
void removePeer(QObject *peer);
void removeAllPeers();
-
+
bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
// void setInitialized(SyncableObject *obj);
// bool isInitialized(SyncableObject *obj) const;
- void requestInit(SyncableObject *obj);
-
void detachObject(QObject *obj);
void detachSignals(QObject *sender);
void detachSlots(QObject *receiver);
*/
static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed = false);
- bool isSecure() const { return _secure; }
-
- static QString methodBaseName(const QMetaMethod &method);
-
- const QList<int> &argTypes(QObject *obj, int methodId);
- const int &returnType(QObject *obj, int methodId);
- const int &minArgCount(QObject *obj, int methodId);
- const QByteArray &methodName(QObject *obj, int methodId);
- const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
- const QHash<int, int> &receiveMap(SyncableObject *obj);
- int updatedRemotelyId(SyncableObject *obj);
-
- typedef QHash<int, QList<int> > ArgHash;
- typedef QHash<int, QByteArray> MethodNameHash;
- struct ClassInfo {
- ArgHash argTypes;
- QHash<int, int> returnType;
- QHash<int, int> minArgCount;
- MethodNameHash methodNames;
- int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
- QHash<QByteArray, int> syncMap;
- QHash<int, int> receiveMap;
- };
+ class ExtendedMetaObject;
+ ExtendedMetaObject *extendedMetaObject(const QObject *obj) const;
+ void createExtendedMetaObject(const QObject *obj);
+ bool isSecure() const { return _secure; }
void dumpProxyStats();
-
+
protected:
void customEvent(QEvent *event);
void init();
void initServer();
void initClient();
-
- const QMetaObject *metaObject(QObject *obj);
- void createClassInfo(QObject *obj);
- void setArgTypes(QObject *obj, int methodId);
- void setReturnType(QObject *obj, int methodId);
- void setMinArgCount(QObject *obj, int methodId);
- void setMethodName(QObject *obj, int methodId);
- void setSyncMap(SyncableObject *obj);
- void setReceiveMap(SyncableObject *obj);
- void setUpdatedRemotelyId(SyncableObject *obj);
-
- bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
+
+ static const QMetaObject *metaObject(const QObject *obj);
void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList ¶ms);
void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue);
bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
+ void requestInit(SyncableObject *obj);
QVariantMap initData(SyncableObject *obj) const;
void setInitData(SyncableObject *obj, const QVariantMap &properties);
public:
void dumpSyncMap(SyncableObject *object);
inline int peerCount() const { return _peers.size(); }
-
+
private:
static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
- class AbstractPeer {
- public:
- enum PeerType {
- NotAPeer = 0,
- IODevicePeer = 1,
- SignalProxyPeer = 2
- };
- AbstractPeer() : _type(NotAPeer) {}
- AbstractPeer(PeerType type) : _type(type) {}
- virtual ~AbstractPeer() {}
- inline PeerType type() const { return _type; }
- virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms) = 0;
- virtual bool isSecure() const = 0;
- private:
- PeerType _type;
- };
-
- class IODevicePeer : public AbstractPeer {
- public:
- IODevicePeer(QIODevice *device, bool compress) : AbstractPeer(AbstractPeer::IODevicePeer), _device(device), byteCount(0), usesCompression(compress), sentHeartBeats(0), lag(0) {}
- virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
- virtual bool isSecure() const;
- inline void dispatchPackedFunc(const QVariant &packedFunc) { SignalProxy::writeDataToDevice(_device, packedFunc, usesCompression); }
- QString address() const;
- inline bool isOpen() const { return _device->isOpen(); }
- inline void close() const { _device->close(); }
- inline bool readData(QVariant &item) { return SignalProxy::readDataFromDevice(_device, byteCount, item, usesCompression); }
- private:
- QIODevice *_device;
- quint32 byteCount;
- bool usesCompression;
- public:
- int sentHeartBeats;
- int lag;
- };
-
- class SignalProxyPeer : public AbstractPeer {
- public:
- SignalProxyPeer(SignalProxy *sender, SignalProxy *receiver) : AbstractPeer(AbstractPeer::SignalProxyPeer), sender(sender), receiver(receiver) {}
- virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
- virtual inline bool isSecure() const { return true; }
- private:
- SignalProxy *sender;
- SignalProxy *receiver;
- };
-
// a Hash of the actual used communication object to it's corresponding peer
// currently a communication object can either be an arbitrary QIODevice or another SignalProxy
typedef QHash<QObject *, AbstractPeer *> PeerHash;
PeerHash _peers;
// containg a list of argtypes for fast access
- QHash<const QMetaObject *, ClassInfo*> _classInfo;
+ QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
// we use one SignalRelay per QObject
QHash<QObject*, SignalRelay *> _relayHash;
QTimer _heartBeatTimer;
bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
-
+
friend class SignalRelay;
};
+
+// ==================================================
+// ExtendedMetaObject
+// ==================================================
+class SignalProxy::ExtendedMetaObject {
+public:
+ ExtendedMetaObject(const QMetaObject *meta);
+
+ const QList<int> &argTypes(int methodId);
+ const int &returnType(int methodId);
+ const int &minArgCount(int methodId);
+ const QByteArray &methodName(int methodId);
+ const QHash<QByteArray, int> &syncMap();
+ const QHash<int, int> &receiveMap();
+ int updatedRemotelyId();
+
+ static QByteArray methodName(const QMetaMethod &method);
+ static bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot);
+ static QString methodBaseName(const QMetaMethod &method);
+
+private:
+ typedef QHash<int, QList<int> > ArgHash;
+ typedef QHash<int, QByteArray> MethodNameHash;
+
+ const QMetaObject *_meta;
+ ArgHash _argTypes;
+ QHash<int, int> _returnType;
+ QHash<int, int> _minArgCount;
+ MethodNameHash _methodNames;
+ int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
+ QHash<QByteArray, int> _syncMap;
+ QHash<int, int> _receiveMap;
+};
+
+
+// ==================================================
+// Peers
+// ==================================================
+class SignalProxy::AbstractPeer {
+public:
+ enum PeerType {
+ NotAPeer = 0,
+ IODevicePeer = 1,
+ SignalProxyPeer = 2
+ };
+ AbstractPeer() : _type(NotAPeer) {}
+ AbstractPeer(PeerType type) : _type(type) {}
+ virtual ~AbstractPeer() {}
+ inline PeerType type() const { return _type; }
+ virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms) = 0;
+ virtual bool isSecure() const = 0;
+private:
+ PeerType _type;
+};
+
+class SignalProxy::IODevicePeer : public SignalProxy::AbstractPeer {
+public:
+ IODevicePeer(QIODevice *device, bool compress) : AbstractPeer(AbstractPeer::IODevicePeer), _device(device), byteCount(0), usesCompression(compress), sentHeartBeats(0), lag(0) {}
+ virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
+ virtual bool isSecure() const;
+ inline void dispatchPackedFunc(const QVariant &packedFunc) { SignalProxy::writeDataToDevice(_device, packedFunc, usesCompression); }
+ QString address() const;
+ inline bool isOpen() const { return _device->isOpen(); }
+ inline void close() const { _device->close(); }
+ inline bool readData(QVariant &item) { return SignalProxy::readDataFromDevice(_device, byteCount, item, usesCompression); }
+private:
+ QIODevice *_device;
+ quint32 byteCount;
+ bool usesCompression;
+public:
+ int sentHeartBeats;
+ int lag;
+};
+
+class SignalProxy::SignalProxyPeer : public SignalProxy::AbstractPeer {
+public:
+ SignalProxyPeer(SignalProxy *sender, SignalProxy *receiver) : AbstractPeer(AbstractPeer::SignalProxyPeer), sender(sender), receiver(receiver) {}
+ virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
+ virtual inline bool isSecure() const { return true; }
+private:
+ SignalProxy *sender;
+ SignalProxy *receiver;
+};
+
#endif