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;
61 enum ClientConnectionType {
62 SignalProxyConnection,
67 PeerSignal = QEvent::User,
71 SignalProxy(QObject *parent);
72 SignalProxy(ProxyMode mode, QObject *parent);
73 SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
74 virtual ~SignalProxy();
76 void setProxyMode(ProxyMode mode);
77 inline ProxyMode proxyMode() const { return _proxyMode; }
79 bool addPeer(QIODevice *iodev);
80 bool addPeer(SignalProxy *proxy);
81 void removePeer(QObject *peer);
82 void removeAllPeers();
84 bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
85 bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
87 void synchronize(SyncableObject *obj);
88 void stopSynchronize(SyncableObject *obj);
90 //! Writes a QVariant to a device.
91 /** The data item is prefixed with the resulting blocksize,
92 * so the corresponding function readDataFromDevice() can check if enough data is available
93 * at the device to reread the item.
95 static void writeDataToDevice(QIODevice *dev, const QVariant &item, bool compressed = false);
97 //! Reads a data item from a device that has been written by writeDataToDevice().
98 /** If not enough data bytes are available, the function returns false and the QVariant reference
101 static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed = false);
103 class ExtendedMetaObject;
104 ExtendedMetaObject *extendedMetaObject(const QMetaObject *meta) const;
105 ExtendedMetaObject *createExtendedMetaObject(const QMetaObject *meta, bool checkConflicts = false);
106 inline ExtendedMetaObject *extendedMetaObject(const QObject *obj) const { return extendedMetaObject(metaObject(obj)); }
107 inline ExtendedMetaObject *createExtendedMetaObject(const QObject *obj, bool checkConflicts = false) { return createExtendedMetaObject(metaObject(obj), checkConflicts); }
109 bool isSecure() const { return _secure; }
110 void dumpProxyStats();
113 void detachObject(QObject *obj);
114 void detachSignals(QObject *sender);
115 void detachSlots(QObject *receiver);
118 void customEvent(QEvent *event);
119 void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
120 void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
123 void dataAvailable();
124 void removePeerBySender();
125 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
126 void sendHeartBeat();
127 void receiveHeartBeat(AbstractPeer *peer, const QVariantList ¶ms);
128 void receiveHeartBeatReply(AbstractPeer *peer, const QVariantList ¶ms);
130 void updateSecureState();
133 void peerRemoved(QIODevice *dev);
136 void objectInitialized(SyncableObject *);
137 void lagUpdated(int lag);
138 void securityChanged(bool);
139 void secureStateChanged(bool);
146 static const QMetaObject *metaObject(const QObject *obj);
148 void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList ¶ms);
149 void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
151 void receivePackedFunc(AbstractPeer *sender, const QVariant &packedFunc);
152 void receivePeerSignal(AbstractPeer *sender, const RequestType &requestType, const QVariantList ¶ms);
153 void receivePeerSignal(SignalProxy *sender, const RequestType &requestType, const QVariantList ¶ms);
154 void handleSync(AbstractPeer *sender, QVariantList params);
155 void handleInitRequest(AbstractPeer *sender, const QVariantList ¶ms);
156 void handleInitData(AbstractPeer *sender, const QVariantList ¶ms);
157 void handleSignal(const QVariantList &data);
159 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue);
160 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
162 void requestInit(SyncableObject *obj);
163 QVariantMap initData(SyncableObject *obj) const;
164 void setInitData(SyncableObject *obj, const QVariantMap &properties);
166 void updateLag(IODevicePeer *peer, int lag);
169 void dumpSyncMap(SyncableObject *object);
170 inline int peerCount() const { return _peers.size(); }
173 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
175 // a Hash of the actual used communication object to it's corresponding peer
176 // currently a communication object can either be an arbitrary QIODevice or another SignalProxy
177 typedef QHash<QObject *, AbstractPeer *> PeerHash;
180 // containg a list of argtypes for fast access
181 QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
183 // SignalRelay for all manually attached signals
184 SignalRelay *_signalRelay;
186 // RPC function -> (object, slot ID)
187 typedef QPair<QObject*, int> MethodId;
188 typedef QMultiHash<QByteArray, MethodId> SlotHash;
189 SlotHash _attachedSlots;
192 typedef QHash<QString, SyncableObject *> ObjectId;
193 QHash<QByteArray, ObjectId> _syncSlave;
196 ProxyMode _proxyMode;
197 QTimer _heartBeatTimer;
199 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
201 friend class SignalRelay;
202 friend class SyncableObject;
206 // ==================================================
207 // ExtendedMetaObject
208 // ==================================================
209 class SignalProxy::ExtendedMetaObject {
210 class MethodDescriptor {
212 MethodDescriptor(const QMetaMethod &method);
213 MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
215 inline const QByteArray &methodName() const { return _methodName; }
216 inline const QList<int> &argTypes() const { return _argTypes; }
217 inline int returnType() const { return _returnType; }
218 inline int minArgCount() const { return _minArgCount; }
219 inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
222 QByteArray _methodName;
223 QList<int> _argTypes;
226 SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
230 ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
232 inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
233 inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
234 inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
235 inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
236 inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
238 inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
240 inline int updatedRemotelyId() { return _updatedRemotelyId; }
242 inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
243 const QHash<int, int> &receiveMap();
245 const QMetaObject *metaObject() const { return _meta; }
247 static QByteArray methodName(const QMetaMethod &method);
248 static QString methodBaseName(const QMetaMethod &method);
251 const MethodDescriptor &methodDescriptor(int methodId);
253 const QMetaObject *_meta;
254 int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
256 QHash<int, MethodDescriptor> _methods;
257 QHash<QByteArray, int> _methodIds;
258 QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y
262 // ==================================================
264 // ==================================================
265 class SignalProxy::AbstractPeer {
272 AbstractPeer() : _type(NotAPeer) {}
273 AbstractPeer(PeerType type) : _type(type) {}
274 virtual ~AbstractPeer() {}
275 inline PeerType type() const { return _type; }
276 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms) = 0;
277 virtual bool isSecure() const = 0;
282 class SignalProxy::IODevicePeer : public SignalProxy::AbstractPeer {
284 IODevicePeer(QIODevice *device, bool compress) : AbstractPeer(AbstractPeer::IODevicePeer), _device(device), byteCount(0), usesCompression(compress), sentHeartBeats(0), lag(0) {}
285 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
286 virtual bool isSecure() const;
287 inline void dispatchPackedFunc(const QVariant &packedFunc) { SignalProxy::writeDataToDevice(_device, packedFunc, usesCompression); }
288 QString address() const;
289 inline bool isOpen() const { return _device->isOpen(); }
290 inline void close() const { _device->close(); }
291 inline bool readData(QVariant &item) { return SignalProxy::readDataFromDevice(_device, byteCount, item, usesCompression); }
295 bool usesCompression;
301 class SignalProxy::SignalProxyPeer : public SignalProxy::AbstractPeer {
303 SignalProxyPeer(SignalProxy *sender, SignalProxy *receiver) : AbstractPeer(AbstractPeer::SignalProxyPeer), sender(sender), receiver(receiver) {}
304 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
305 virtual inline bool isSecure() const { return true; }
308 SignalProxy *receiver;