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 ***************************************************************************/
28 #include <QVariantMap>
38 class SignalProxy : public QObject {
59 enum ClientConnectionType {
60 SignalProxyConnection,
65 PeerSignal = QEvent::User,
69 SignalProxy(QObject *parent);
70 SignalProxy(ProxyMode mode, QObject *parent);
71 SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
72 virtual ~SignalProxy();
74 void setProxyMode(ProxyMode mode);
75 inline ProxyMode proxyMode() const { return _proxyMode; }
77 bool addPeer(QIODevice *iodev);
78 bool addPeer(SignalProxy *proxy);
79 void removePeer(QObject *peer);
80 void removeAllPeers();
82 bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
83 bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
85 void synchronize(SyncableObject *obj);
87 // void setInitialized(SyncableObject *obj);
88 // bool isInitialized(SyncableObject *obj) const;
89 void requestInit(SyncableObject *obj);
91 void detachObject(QObject *obj);
92 void detachSignals(QObject *sender);
93 void detachSlots(QObject *receiver);
94 void stopSync(SyncableObject *obj);
96 //! Writes a QVariant to a device.
97 /** The data item is prefixed with the resulting blocksize,
98 * so the corresponding function readDataFromDevice() can check if enough data is available
99 * at the device to reread the item.
101 static void writeDataToDevice(QIODevice *dev, const QVariant &item, bool compressed = false);
103 //! Reads a data item from a device that has been written by writeDataToDevice().
104 /** If not enough data bytes are available, the function returns false and the QVariant reference
107 static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed = false);
109 static QString methodBaseName(const QMetaMethod &method);
111 const QList<int> &argTypes(QObject *obj, int methodId);
112 const int &returnType(QObject *obj, int methodId);
113 const int &minArgCount(QObject *obj, int methodId);
114 const QByteArray &methodName(QObject *obj, int methodId);
115 const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
116 const QHash<int, int> &receiveMap(SyncableObject *obj);
117 int updatedRemotelyId(SyncableObject *obj);
119 typedef QHash<int, QList<int> > ArgHash;
120 typedef QHash<int, QByteArray> MethodNameHash;
123 QHash<int, int> returnType;
124 QHash<int, int> minArgCount;
125 MethodNameHash methodNames;
126 int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
127 QHash<QByteArray, int> syncMap;
128 QHash<int, int> receiveMap;
131 void dumpProxyStats();
134 void customEvent(QEvent *event);
137 void dataAvailable();
139 void removePeerBySender();
140 void objectRenamed(const QString &newname, const QString &oldname);
141 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
142 void sendHeartBeat();
143 void receiveHeartBeat(AbstractPeer *peer, const QVariantList ¶ms);
144 void receiveHeartBeatReply(AbstractPeer *peer, const QVariantList ¶ms);
147 void peerRemoved(QIODevice *dev);
150 void objectInitialized(SyncableObject *);
151 void lagUpdated(int lag);
158 const QMetaObject *metaObject(QObject *obj);
159 void createClassInfo(QObject *obj);
160 void setArgTypes(QObject *obj, int methodId);
161 void setReturnType(QObject *obj, int methodId);
162 void setMinArgCount(QObject *obj, int methodId);
163 void setMethodName(QObject *obj, int methodId);
164 void setSyncMap(SyncableObject *obj);
165 void setReceiveMap(SyncableObject *obj);
166 void setUpdatedRemotelyId(SyncableObject *obj);
168 bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
170 void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList ¶ms);
171 void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
173 void receivePackedFunc(AbstractPeer *sender, const QVariant &packedFunc);
174 void receivePeerSignal(AbstractPeer *sender, const RequestType &requestType, const QVariantList ¶ms);
175 void receivePeerSignal(SignalProxy *sender, const RequestType &requestType, const QVariantList ¶ms);
176 void handleSync(AbstractPeer *sender, QVariantList params);
177 void handleInitRequest(AbstractPeer *sender, const QVariantList ¶ms);
178 void handleInitData(AbstractPeer *sender, const QVariantList ¶ms);
179 void handleSignal(const QVariantList &data);
181 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue);
182 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
184 QVariantMap initData(SyncableObject *obj) const;
185 void setInitData(SyncableObject *obj, const QVariantMap &properties);
187 void updateLag(IODevicePeer *peer, int lag);
190 void dumpSyncMap(SyncableObject *object);
191 inline int peerCount() const { return _peers.size(); }
194 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
203 AbstractPeer() : _type(NotAPeer) {}
204 AbstractPeer(PeerType type) : _type(type) {}
205 virtual ~AbstractPeer() {}
206 inline PeerType type() const { return _type; }
207 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms) = 0;
212 class IODevicePeer : public AbstractPeer {
214 IODevicePeer(QIODevice *device, bool compress) : AbstractPeer(AbstractPeer::IODevicePeer), _device(device), byteCount(0), usesCompression(compress), sentHeartBeats(0), lag(0) {}
215 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
216 inline void dispatchPackedFunc(const QVariant &packedFunc) { SignalProxy::writeDataToDevice(_device, packedFunc, usesCompression); }
217 QString address() const;
218 inline bool isOpen() const { return _device->isOpen(); }
219 inline void close() const { _device->close(); }
220 inline bool readData(QVariant &item) { return SignalProxy::readDataFromDevice(_device, byteCount, item, usesCompression); }
224 bool usesCompression;
230 class SignalProxyPeer : public AbstractPeer {
232 SignalProxyPeer(SignalProxy *sender, SignalProxy *receiver) : AbstractPeer(AbstractPeer::SignalProxyPeer), sender(sender), receiver(receiver) {}
233 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
236 SignalProxy *receiver;
239 // a Hash of the actual used communication object to it's corresponding peer
240 // currently a communication object can either be an arbitrary QIODevice or another SignalProxy
241 typedef QHash<QObject *, AbstractPeer *> PeerHash;
244 // containg a list of argtypes for fast access
245 QHash<const QMetaObject *, ClassInfo*> _classInfo;
247 // we use one SignalRelay per QObject
248 QHash<QObject*, SignalRelay *> _relayHash;
250 // RPC function -> (object, slot ID)
251 typedef QPair<QObject*, int> MethodId;
252 typedef QMultiHash<QByteArray, MethodId> SlotHash;
253 SlotHash _attachedSlots;
256 typedef QHash<QString, SyncableObject *> ObjectId;
257 QHash<QByteArray, ObjectId> _syncSlave;
260 ProxyMode _proxyMode;
261 QTimer _heartBeatTimer;
263 friend class SignalRelay;