X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fsignalproxy.h;h=1c991aeb993e867de2bff1173dfca1eb7e727da3;hp=ed8b46b0b94b5a93571646ff3aedd6129e555349;hb=9f5481c93c3676f765333d4d9c36c559a339f3aa;hpb=06a03c2c69ee934aaeec83512bae2fffee83a340 diff --git a/src/common/signalproxy.h b/src/common/signalproxy.h index ed8b46b0..1c991aeb 100644 --- a/src/common/signalproxy.h +++ b/src/common/signalproxy.h @@ -1,11 +1,11 @@ /*************************************************************************** - * Copyright (C) 2005-07 by The Quassel IRC Development 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 * * 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 * @@ -18,70 +18,165 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#ifndef _RPCPEER_H_ -#define _RPCPEER_H_ +#ifndef _SIGNALPROXY_H_ +#define _SIGNALPROXY_H_ -#include "qxtrpcpeer.h" -#include +#include +#include +#include +#include +#include +#include +#include +#include + +class SignalRelay; +class SyncableObject; +class QMetaObject; class SignalProxy : public QObject { Q_OBJECT - public: - - enum ProxyType { Client, Server }; - - SignalProxy(ProxyType type, QIODevice *device = 0, QObject *parent = 0); - ~SignalProxy(); - - void attachSignal(QObject* sender, const char* signal, const QByteArray& rpcFunction = QByteArray()); - void attachSlot(const QByteArray& rpcFunction, QObject* recv, const char* slot); - - void detachObject(QObject* obj); - - public slots: - void addPeer(QIODevice *device); - - void sendSignal(const char *signal, QVariant p1 = QVariant(), QVariant p2 = QVariant(), QVariant p3 = QVariant(), QVariant p4 = QVariant(), - QVariant p5 = QVariant(), QVariant p6 = QVariant(), QVariant p7 = QVariant(), QVariant p8 = QVariant(), QVariant p9 = QVariant()); - - //void detachSender(); - - signals: - void peerDisconnected(); - - private slots: - void socketDisconnected(); - - private: - struct Connection { - QPointer peer; - QPointer device; - }; - - struct SignalDesc { - QObject *sender; - const char *signal; - QByteArray rpcFunction; - - SignalDesc(QObject *sndr, const char *sig, const QByteArray &func) : sender(sndr), signal(sig), rpcFunction(func) {} - }; - - struct SlotDesc { - QByteArray rpcFunction; - QObject *recv; - const char *slot; - - SlotDesc(const QByteArray& func, QObject* r, const char* s) : rpcFunction(func), recv(r), slot(s) {} - }; - - ProxyType type; - QList peers; - QList attachedSignals; - QList attachedSlots; - +public: + enum ProxyMode { + Server, + Client + }; + + enum RequestType { + Sync = 1, + RpcCall, + InitRequest, + InitData, + HeartBeat + }; + + SignalProxy(QObject *parent); + SignalProxy(ProxyMode mode, QObject *parent); + SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent); + virtual ~SignalProxy(); + + void setProxyMode(ProxyMode mode); + ProxyMode proxyMode() const; + + bool addPeer(QIODevice *iodev); + void removePeer(QIODevice *iodev = 0); + + bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray()); + bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot); + + void synchronize(SyncableObject *obj); + + 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); + void stopSync(SyncableObject *obj); + + //! Writes a QVariant to a device. + /** The data item is prefixed with the resulting blocksize, + * so the corresponding function readDataFromDevice() can check if enough data is available + * at the device to reread the item. + */ + static void writeDataToDevice(QIODevice *dev, const QVariant &item); + + //! Reads a data item from a device that has been written by writeDataToDevice(). + /** If not enough data bytes are available, the function returns false and the QVariant reference + * remains untouched. + */ + static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item); + + static QString methodBaseName(const QMetaMethod &method); + + const QList &argTypes(QObject *obj, int methodId); + const int &minArgCount(QObject *obj, int methodId); + const QByteArray &methodName(QObject *obj, int methodId); + const QHash &syncMap(SyncableObject *obj); + int updatedRemotelyId(SyncableObject *obj); + + typedef QHash > ArgHash; + typedef QHash MethodNameHash; + struct ClassInfo { + ArgHash argTypes; + QHash minArgCount; + MethodNameHash methodNames; + int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster + QHash syncMap; + }; + + void dumpProxyStats(); + +private slots: + void dataAvailable(); + void detachSender(); + void removePeerBySender(); + void objectRenamed(QString oldname, QString newname); + void objectRenamed(QByteArray classname, QString oldname, QString newname); + void sendHeartBeat(); + +signals: + void peerRemoved(QIODevice *obj); + void connected(); + void disconnected(); + void objectInitialized(SyncableObject *); + +private: + void initServer(); + void initClient(); + + void createClassInfo(QObject *obj); + void setArgTypes(QObject *obj, int methodId); + void setMinArgCount(QObject *obj, int methodId); + void setMethodName(QObject *obj, int methodId); + void setSyncMap(SyncableObject *obj); + void setUpdatedRemotelyId(QObject *obj); + + bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const; + + void dispatchSignal(QIODevice *receiver, const QVariant &identifier, const QVariantList ¶ms); + void dispatchSignal(const QVariant &identifier, const QVariantList ¶ms); + + void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc); + void handleSync(QVariantList params); + void handleInitRequest(QIODevice *sender, const QVariantList ¶ms); + void handleInitData(QIODevice *sender, const QVariantList ¶ms); + void handleSignal(const QByteArray &funcName, const QVariantList ¶ms); + + bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList()); + + QVariantMap initData(SyncableObject *obj) const; + void setInitData(SyncableObject *obj, const QVariantMap &properties); + +public: + void dumpSyncMap(SyncableObject *object); + +private: + // Hash of used QIODevices + QHash _peerByteCount; + + // containg a list of argtypes for fast access + QHash _classInfo; + + // we use one SignalRelay per QObject + QHash _relayHash; + + // RPC function -> (object, slot ID) + typedef QPair MethodId; + typedef QMultiHash SlotHash; + SlotHash _attachedSlots; + + // slaves for sync + typedef QHash ObjectId; + QHash _syncSlave; + + + ProxyMode _proxyMode; + QTimer _heartBeatTimer; + + friend class SignalRelay; }; - - #endif