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 bool addPeer(SignalProxy *proxy);
68 void removePeer(QObject *peer);
69 void removeAllPeers();
71 bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
72 bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
74 void synchronize(SyncableObject *obj);
76 // void setInitialized(SyncableObject *obj);
77 // bool isInitialized(SyncableObject *obj) const;
78 void requestInit(SyncableObject *obj);
80 void detachObject(QObject *obj);
81 void detachSignals(QObject *sender);
82 void detachSlots(QObject *receiver);
83 void stopSync(SyncableObject *obj);
85 //! Writes a QVariant to a device.
86 /** The data item is prefixed with the resulting blocksize,
87 * so the corresponding function readDataFromDevice() can check if enough data is available
88 * at the device to reread the item.
90 static void writeDataToDevice(QIODevice *dev, const QVariant &item, bool compressed = false);
92 //! Reads a data item from a device that has been written by writeDataToDevice().
93 /** If not enough data bytes are available, the function returns false and the QVariant reference
96 static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed = false);
98 static QString methodBaseName(const QMetaMethod &method);
100 const QList<int> &argTypes(QObject *obj, int methodId);
101 const int &returnType(QObject *obj, int methodId);
102 const int &minArgCount(QObject *obj, int methodId);
103 const QByteArray &methodName(QObject *obj, int methodId);
104 const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
105 const QHash<int, int> &receiveMap(SyncableObject *obj);
106 int updatedRemotelyId(SyncableObject *obj);
108 typedef QHash<int, QList<int> > ArgHash;
109 typedef QHash<int, QByteArray> MethodNameHash;
112 QHash<int, int> returnType;
113 QHash<int, int> minArgCount;
114 MethodNameHash methodNames;
115 int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
116 QHash<QByteArray, int> syncMap;
117 QHash<int, int> receiveMap;
120 void dumpProxyStats();
123 void customEvent(QEvent *event);
126 void dataAvailable();
128 void removePeerBySender();
129 void objectRenamed(const QString &newname, const QString &oldname);
130 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
131 void sendHeartBeat();
132 void receiveHeartBeat(AbstractPeer *peer, const QVariantList ¶ms);
133 void receiveHeartBeatReply(AbstractPeer *peer, const QVariantList ¶ms);
136 void peerRemoved(QIODevice *dev);
139 void objectInitialized(SyncableObject *);
140 void lagUpdated(int lag);
147 const QMetaObject *metaObject(QObject *obj);
148 void createClassInfo(QObject *obj);
149 void setArgTypes(QObject *obj, int methodId);
150 void setReturnType(QObject *obj, int methodId);
151 void setMinArgCount(QObject *obj, int methodId);
152 void setMethodName(QObject *obj, int methodId);
153 void setSyncMap(SyncableObject *obj);
154 void setReceiveMap(SyncableObject *obj);
155 void setUpdatedRemotelyId(SyncableObject *obj);
157 bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
159 void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList ¶ms);
160 void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
162 void receivePackedFunc(AbstractPeer *sender, const QVariant &packedFunc);
163 void receivePeerSignal(AbstractPeer *sender, const RequestType &requestType, const QVariantList ¶ms);
164 void receivePeerSignal(SignalProxy *sender, const RequestType &requestType, const QVariantList ¶ms);
165 void handleSync(AbstractPeer *sender, QVariantList params);
166 void handleInitRequest(AbstractPeer *sender, const QVariantList ¶ms);
167 void handleInitData(AbstractPeer *sender, const QVariantList ¶ms);
168 void handleSignal(const QVariantList &data);
170 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue);
171 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
173 QVariantMap initData(SyncableObject *obj) const;
174 void setInitData(SyncableObject *obj, const QVariantMap &properties);
176 void updateLag(IODevicePeer *peer, int lag);
179 void dumpSyncMap(SyncableObject *object);
180 inline int peerCount() const { return _peers.size(); }
190 AbstractPeer() : _type(NotAPeer) {}
191 AbstractPeer(PeerType type) : _type(type) {}
192 virtual ~AbstractPeer() {}
193 inline PeerType type() const { return _type; }
194 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms) = 0;
199 class IODevicePeer : public AbstractPeer {
201 IODevicePeer(QIODevice *device, bool compress) : AbstractPeer(AbstractPeer::IODevicePeer), _device(device), byteCount(0), usesCompression(compress), sentHeartBeats(0), lag(0) {}
202 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
203 inline void dispatchPackedFunc(const QVariant &packedFunc) { SignalProxy::writeDataToDevice(_device, packedFunc, usesCompression); }
204 QString address() const;
205 inline bool isOpen() const { return _device->isOpen(); }
206 inline void close() const { _device->close(); }
207 inline bool readData(QVariant &item) { return SignalProxy::readDataFromDevice(_device, byteCount, item, usesCompression); }
211 bool usesCompression;
217 class SignalProxyPeer : public AbstractPeer {
219 SignalProxyPeer(SignalProxy *sender, SignalProxy *receiver) : AbstractPeer(AbstractPeer::SignalProxyPeer), sender(sender), receiver(receiver) {}
220 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
223 SignalProxy *receiver;
226 // a Hash of the actual used communication object to it's corresponding peer
227 // currently a communication object can either be an arbitrary QIODevice or another SignalProxy
228 typedef QHash<QObject *, AbstractPeer *> PeerHash;
231 // // Hash of used QIODevices
233 // quint32 byteCount;
234 // bool usesCompression;
235 // int sentHeartBeats;
237 // peerInfo() : byteCount(0), usesCompression(false), sentHeartBeats(0) {}
239 // QHash<QIODevice*, peerInfo> _peers;
241 // containg a list of argtypes for fast access
242 QHash<const QMetaObject *, ClassInfo*> _classInfo;
244 // we use one SignalRelay per QObject
245 QHash<QObject*, SignalRelay *> _relayHash;
247 // RPC function -> (object, slot ID)
248 typedef QPair<QObject*, int> MethodId;
249 typedef QMultiHash<QByteArray, MethodId> SlotHash;
250 SlotHash _attachedSlots;
253 typedef QHash<QString, SyncableObject *> ObjectId;
254 QHash<QByteArray, ObjectId> _syncSlave;
257 ProxyMode _proxyMode;
258 QTimer _heartBeatTimer;
260 friend class SignalRelay;