1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
27 #include <QVariantMap>
37 class SignalProxy : public QObject {
58 SignalProxy(QObject *parent);
59 SignalProxy(ProxyMode mode, QObject *parent);
60 SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
61 virtual ~SignalProxy();
63 void setProxyMode(ProxyMode mode);
64 inline ProxyMode proxyMode() const { return _proxyMode; }
66 bool addPeer(QIODevice *iodev);
67 void removePeer(QIODevice *iodev);
68 bool addPeer(SignalProxy *proxy);
69 void removePeer(SignalProxy *proxy);
70 void removeAllPeers();
72 bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
73 bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
75 void synchronize(SyncableObject *obj);
77 // void setInitialized(SyncableObject *obj);
78 // bool isInitialized(SyncableObject *obj) const;
79 void requestInit(SyncableObject *obj);
81 void detachObject(QObject *obj);
82 void detachSignals(QObject *sender);
83 void detachSlots(QObject *receiver);
84 void stopSync(SyncableObject *obj);
86 //! Writes a QVariant to a device.
87 /** The data item is prefixed with the resulting blocksize,
88 * so the corresponding function readDataFromDevice() can check if enough data is available
89 * at the device to reread the item.
91 static void writeDataToDevice(QIODevice *dev, const QVariant &item, bool compressed = false);
93 //! Reads a data item from a device that has been written by writeDataToDevice().
94 /** If not enough data bytes are available, the function returns false and the QVariant reference
97 static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed = false);
99 static QString methodBaseName(const QMetaMethod &method);
101 const QList<int> &argTypes(QObject *obj, int methodId);
102 const int &returnType(QObject *obj, int methodId);
103 const int &minArgCount(QObject *obj, int methodId);
104 const QByteArray &methodName(QObject *obj, int methodId);
105 const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
106 const QHash<int, int> &receiveMap(SyncableObject *obj);
107 int updatedRemotelyId(SyncableObject *obj);
109 typedef QHash<int, QList<int> > ArgHash;
110 typedef QHash<int, QByteArray> MethodNameHash;
113 QHash<int, int> returnType;
114 QHash<int, int> minArgCount;
115 MethodNameHash methodNames;
116 int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
117 QHash<QByteArray, int> syncMap;
118 QHash<int, int> receiveMap;
121 void dumpProxyStats();
124 void dataAvailable();
126 void removePeerBySender();
127 void objectRenamed(const QString &newname, const QString &oldname);
128 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
129 void sendHeartBeat();
130 void receiveHeartBeat(AbstractPeer *peer, const QVariantList ¶ms);
131 void receiveHeartBeatReply(AbstractPeer *peer, const QVariantList ¶ms);
134 void peerRemoved(QIODevice *dev);
137 void objectInitialized(SyncableObject *);
138 void lagUpdated(int lag);
145 const QMetaObject *metaObject(QObject *obj);
146 void createClassInfo(QObject *obj);
147 void setArgTypes(QObject *obj, int methodId);
148 void setReturnType(QObject *obj, int methodId);
149 void setMinArgCount(QObject *obj, int methodId);
150 void setMethodName(QObject *obj, int methodId);
151 void setSyncMap(SyncableObject *obj);
152 void setReceiveMap(SyncableObject *obj);
153 void setUpdatedRemotelyId(SyncableObject *obj);
155 bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
157 void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList ¶ms);
158 void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
160 void receivePackedFunc(AbstractPeer *sender, const QVariant &packedFunc);
161 void receivePeerSignal(AbstractPeer *sender, const RequestType &requestType, const QVariantList ¶ms);
162 void handleSync(AbstractPeer *sender, QVariantList params);
163 void handleInitRequest(AbstractPeer *sender, const QVariantList ¶ms);
164 void handleInitData(AbstractPeer *sender, const QVariantList ¶ms);
165 void handleSignal(const QVariantList &data);
167 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue);
168 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
170 QVariantMap initData(SyncableObject *obj) const;
171 void setInitData(SyncableObject *obj, const QVariantMap &properties);
173 void updateLag(IODevicePeer *peer, int lag);
176 void dumpSyncMap(SyncableObject *object);
177 inline int peerCount() const { return _peers.size(); }
187 AbstractPeer() : _type(NotAPeer) {}
188 AbstractPeer(PeerType type) : _type(type) {}
189 virtual ~AbstractPeer() {}
190 inline PeerType type() const { return _type; }
191 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms) = 0;
196 class IODevicePeer : public AbstractPeer {
198 IODevicePeer(QIODevice *device, bool compress) : AbstractPeer(AbstractPeer::IODevicePeer), _device(device), byteCount(0), usesCompression(compress), sentHeartBeats(0), lag(0) {}
199 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
200 inline void dispatchPackedFunc(const QVariant &packedFunc) { SignalProxy::writeDataToDevice(_device, packedFunc, usesCompression); }
201 inline QIODevice *device() const { return _device; }
202 inline bool isOpen() const { return _device->isOpen(); }
203 inline bool readData(QVariant &item) { return SignalProxy::readDataFromDevice(_device, byteCount, item, usesCompression); }
207 bool usesCompression;
213 class SignalProxyPeer : public AbstractPeer {
215 SignalProxyPeer(SignalProxy *proxy) : AbstractPeer(AbstractPeer::SignalProxyPeer), proxy(proxy) {}
216 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
221 // a Hash of the actual used communication object to it's corresponding peer
222 // currently a communication object can either be an arbitrary QIODevice or another SignalProxy
223 typedef QHash<QObject *, AbstractPeer *> PeerHash;
226 // // Hash of used QIODevices
228 // quint32 byteCount;
229 // bool usesCompression;
230 // int sentHeartBeats;
232 // peerInfo() : byteCount(0), usesCompression(false), sentHeartBeats(0) {}
234 // QHash<QIODevice*, peerInfo> _peers;
236 // containg a list of argtypes for fast access
237 QHash<const QMetaObject *, ClassInfo*> _classInfo;
239 // we use one SignalRelay per QObject
240 QHash<QObject*, SignalRelay *> _relayHash;
242 // RPC function -> (object, slot ID)
243 typedef QPair<QObject*, int> MethodId;
244 typedef QMultiHash<QByteArray, MethodId> SlotHash;
245 SlotHash _attachedSlots;
248 typedef QHash<QString, SyncableObject *> ObjectId;
249 QHash<QByteArray, ObjectId> _syncSlave;
252 ProxyMode _proxyMode;
253 QTimer _heartBeatTimer;
255 friend class SignalRelay;