1 /***************************************************************************
2 * Copyright (C) 2005-09 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>
37 class SignalProxy : public QObject {
42 class SignalProxyPeer;
63 enum ClientConnectionType {
64 SignalProxyConnection,
69 PeerSignal = QEvent::User,
73 SignalProxy(QObject *parent);
74 SignalProxy(ProxyMode mode, QObject *parent);
75 SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
76 virtual ~SignalProxy();
78 void setProxyMode(ProxyMode mode);
79 inline ProxyMode proxyMode() const { return _proxyMode; }
81 bool addPeer(QIODevice *iodev);
82 bool addPeer(SignalProxy *proxy);
83 void removePeer(QObject *peer);
84 void removeAllPeers();
86 bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
87 bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
89 void synchronize(SyncableObject *obj);
91 //! Writes a QVariant to a device.
92 /** The data item is prefixed with the resulting blocksize,
93 * so the corresponding function readDataFromDevice() can check if enough data is available
94 * at the device to reread the item.
96 static void writeDataToDevice(QIODevice *dev, const QVariant &item, bool compressed = false);
98 //! Reads a data item from a device that has been written by writeDataToDevice().
99 /** If not enough data bytes are available, the function returns false and the QVariant reference
102 static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed = false);
104 class ExtendedMetaObject;
105 ExtendedMetaObject *extendedMetaObject(const QMetaObject *meta) const;
106 ExtendedMetaObject *createExtendedMetaObject(const QMetaObject *meta);
107 inline ExtendedMetaObject *extendedMetaObject(const QObject *obj) const { return extendedMetaObject(metaObject(obj)); }
108 inline ExtendedMetaObject *createExtendedMetaObject(const QObject *obj) { return createExtendedMetaObject(metaObject(obj)); }
110 bool isSecure() const { return _secure; }
111 void dumpProxyStats();
114 void detachObject(QObject *obj);
115 void detachSignals(QObject *sender);
116 void detachSlots(QObject *receiver);
117 void stopSync(QObject *obj);
120 void customEvent(QEvent *event);
121 void syncCall(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
124 void dataAvailable();
125 void removePeerBySender();
126 void objectRenamed(const QString &newname, const QString &oldname);
127 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
128 void sendHeartBeat();
129 void receiveHeartBeat(AbstractPeer *peer, const QVariantList ¶ms);
130 void receiveHeartBeatReply(AbstractPeer *peer, const QVariantList ¶ms);
132 void updateSecureState();
135 void peerRemoved(QIODevice *dev);
138 void objectInitialized(SyncableObject *);
139 void lagUpdated(int lag);
140 void securityChanged(bool);
141 void secureStateChanged(bool);
148 static const QMetaObject *metaObject(const QObject *obj);
150 void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList ¶ms);
151 void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
153 void receivePackedFunc(AbstractPeer *sender, const QVariant &packedFunc);
154 void receivePeerSignal(AbstractPeer *sender, const RequestType &requestType, const QVariantList ¶ms);
155 void receivePeerSignal(SignalProxy *sender, const RequestType &requestType, const QVariantList ¶ms);
156 void handleSync(AbstractPeer *sender, QVariantList params);
157 void handleInitRequest(AbstractPeer *sender, const QVariantList ¶ms);
158 void handleInitData(AbstractPeer *sender, const QVariantList ¶ms);
159 void handleSignal(const QVariantList &data);
161 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue);
162 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
164 void requestInit(SyncableObject *obj);
165 QVariantMap initData(SyncableObject *obj) const;
166 void setInitData(SyncableObject *obj, const QVariantMap &properties);
168 void updateLag(IODevicePeer *peer, int lag);
171 void dumpSyncMap(SyncableObject *object);
172 inline int peerCount() const { return _peers.size(); }
175 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
177 // a Hash of the actual used communication object to it's corresponding peer
178 // currently a communication object can either be an arbitrary QIODevice or another SignalProxy
179 typedef QHash<QObject *, AbstractPeer *> PeerHash;
182 // containg a list of argtypes for fast access
183 QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
185 // SignalRelay for all manually attached signals
186 SignalRelay *_signalRelay;
187 // one SyncRelay per class
188 QHash<const QMetaObject *, SyncRelay *> _syncRelays;
190 // RPC function -> (object, slot ID)
191 typedef QPair<QObject*, int> MethodId;
192 typedef QMultiHash<QByteArray, MethodId> SlotHash;
193 SlotHash _attachedSlots;
196 typedef QHash<QString, SyncableObject *> ObjectId;
197 QHash<QByteArray, ObjectId> _syncSlave;
200 ProxyMode _proxyMode;
201 QTimer _heartBeatTimer;
203 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
205 friend class SignalRelay;
206 friend class SyncableObject;
210 // ==================================================
211 // ExtendedMetaObject
212 // ==================================================
213 class SignalProxy::ExtendedMetaObject {
215 ExtendedMetaObject(const QMetaObject *meta);
217 const QList<int> &argTypes(int methodId);
218 int returnType(int methodId);
219 int minArgCount(int methodId);
220 const QByteArray &methodName(int methodId);
221 int methodId(const QByteArray &methodName);
222 const QHash<QByteArray, int> &syncMap();
223 const QHash<int, int> &receiveMap();
224 int updatedRemotelyId();
226 const QMetaObject *metaObject() const { return _meta; }
228 static QByteArray methodName(const QMetaMethod &method);
229 static bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot);
230 static QString methodBaseName(const QMetaMethod &method);
233 typedef QHash<int, QList<int> > ArgHash;
234 typedef QHash<int, QByteArray> MethodNameHash;
235 typedef QHash<QByteArray, int> MethodIdHash;
237 const QMetaObject *_meta;
239 QHash<int, int> _returnType;
240 QHash<int, int> _minArgCount;
241 MethodNameHash _methodNames;
242 MethodIdHash _methodIds;
243 int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
244 QHash<QByteArray, int> _syncMap;
245 QHash<int, int> _receiveMap;
249 // ==================================================
251 // ==================================================
252 class SignalProxy::AbstractPeer {
259 AbstractPeer() : _type(NotAPeer) {}
260 AbstractPeer(PeerType type) : _type(type) {}
261 virtual ~AbstractPeer() {}
262 inline PeerType type() const { return _type; }
263 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms) = 0;
264 virtual bool isSecure() const = 0;
269 class SignalProxy::IODevicePeer : public SignalProxy::AbstractPeer {
271 IODevicePeer(QIODevice *device, bool compress) : AbstractPeer(AbstractPeer::IODevicePeer), _device(device), byteCount(0), usesCompression(compress), sentHeartBeats(0), lag(0) {}
272 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
273 virtual bool isSecure() const;
274 inline void dispatchPackedFunc(const QVariant &packedFunc) { SignalProxy::writeDataToDevice(_device, packedFunc, usesCompression); }
275 QString address() const;
276 inline bool isOpen() const { return _device->isOpen(); }
277 inline void close() const { _device->close(); }
278 inline bool readData(QVariant &item) { return SignalProxy::readDataFromDevice(_device, byteCount, item, usesCompression); }
282 bool usesCompression;
288 class SignalProxy::SignalProxyPeer : public SignalProxy::AbstractPeer {
290 SignalProxyPeer(SignalProxy *sender, SignalProxy *receiver) : AbstractPeer(AbstractPeer::SignalProxyPeer), sender(sender), receiver(receiver) {}
291 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
292 virtual inline bool isSecure() const { return true; }
295 SignalProxy *receiver;