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 bool isSecure() const { return _secure; }
111 static QString methodBaseName(const QMetaMethod &method);
113 const QList<int> &argTypes(QObject *obj, int methodId);
114 const int &returnType(QObject *obj, int methodId);
115 const int &minArgCount(QObject *obj, int methodId);
116 const QByteArray &methodName(QObject *obj, int methodId);
117 const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
118 const QHash<int, int> &receiveMap(SyncableObject *obj);
119 int updatedRemotelyId(SyncableObject *obj);
121 typedef QHash<int, QList<int> > ArgHash;
122 typedef QHash<int, QByteArray> MethodNameHash;
125 QHash<int, int> returnType;
126 QHash<int, int> minArgCount;
127 MethodNameHash methodNames;
128 int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
129 QHash<QByteArray, int> syncMap;
130 QHash<int, int> receiveMap;
133 void dumpProxyStats();
136 void customEvent(QEvent *event);
139 void dataAvailable();
141 void removePeerBySender();
142 void objectRenamed(const QString &newname, const QString &oldname);
143 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
144 void sendHeartBeat();
145 void receiveHeartBeat(AbstractPeer *peer, const QVariantList ¶ms);
146 void receiveHeartBeatReply(AbstractPeer *peer, const QVariantList ¶ms);
148 void updateSecureState();
151 void peerRemoved(QIODevice *dev);
154 void objectInitialized(SyncableObject *);
155 void lagUpdated(int lag);
156 void securityChanged(bool);
157 void secureStateChanged(bool);
164 const QMetaObject *metaObject(QObject *obj);
165 void createClassInfo(QObject *obj);
166 void setArgTypes(QObject *obj, int methodId);
167 void setReturnType(QObject *obj, int methodId);
168 void setMinArgCount(QObject *obj, int methodId);
169 void setMethodName(QObject *obj, int methodId);
170 void setSyncMap(SyncableObject *obj);
171 void setReceiveMap(SyncableObject *obj);
172 void setUpdatedRemotelyId(SyncableObject *obj);
174 bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
176 void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList ¶ms);
177 void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
179 void receivePackedFunc(AbstractPeer *sender, const QVariant &packedFunc);
180 void receivePeerSignal(AbstractPeer *sender, const RequestType &requestType, const QVariantList ¶ms);
181 void receivePeerSignal(SignalProxy *sender, const RequestType &requestType, const QVariantList ¶ms);
182 void handleSync(AbstractPeer *sender, QVariantList params);
183 void handleInitRequest(AbstractPeer *sender, const QVariantList ¶ms);
184 void handleInitData(AbstractPeer *sender, const QVariantList ¶ms);
185 void handleSignal(const QVariantList &data);
187 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue);
188 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
190 QVariantMap initData(SyncableObject *obj) const;
191 void setInitData(SyncableObject *obj, const QVariantMap &properties);
193 void updateLag(IODevicePeer *peer, int lag);
196 void dumpSyncMap(SyncableObject *object);
197 inline int peerCount() const { return _peers.size(); }
200 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
209 AbstractPeer() : _type(NotAPeer) {}
210 AbstractPeer(PeerType type) : _type(type) {}
211 virtual ~AbstractPeer() {}
212 inline PeerType type() const { return _type; }
213 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms) = 0;
214 virtual bool isSecure() const = 0;
219 class IODevicePeer : public AbstractPeer {
221 IODevicePeer(QIODevice *device, bool compress) : AbstractPeer(AbstractPeer::IODevicePeer), _device(device), byteCount(0), usesCompression(compress), sentHeartBeats(0), lag(0) {}
222 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
223 virtual bool isSecure() const;
224 inline void dispatchPackedFunc(const QVariant &packedFunc) { SignalProxy::writeDataToDevice(_device, packedFunc, usesCompression); }
225 QString address() const;
226 inline bool isOpen() const { return _device->isOpen(); }
227 inline void close() const { _device->close(); }
228 inline bool readData(QVariant &item) { return SignalProxy::readDataFromDevice(_device, byteCount, item, usesCompression); }
232 bool usesCompression;
238 class SignalProxyPeer : public AbstractPeer {
240 SignalProxyPeer(SignalProxy *sender, SignalProxy *receiver) : AbstractPeer(AbstractPeer::SignalProxyPeer), sender(sender), receiver(receiver) {}
241 virtual void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
242 virtual inline bool isSecure() const { return true; }
245 SignalProxy *receiver;
248 // a Hash of the actual used communication object to it's corresponding peer
249 // currently a communication object can either be an arbitrary QIODevice or another SignalProxy
250 typedef QHash<QObject *, AbstractPeer *> PeerHash;
253 // containg a list of argtypes for fast access
254 QHash<const QMetaObject *, ClassInfo*> _classInfo;
256 // we use one SignalRelay per QObject
257 QHash<QObject*, SignalRelay *> _relayHash;
259 // RPC function -> (object, slot ID)
260 typedef QPair<QObject*, int> MethodId;
261 typedef QMultiHash<QByteArray, MethodId> SlotHash;
262 SlotHash _attachedSlots;
265 typedef QHash<QString, SyncableObject *> ObjectId;
266 QHash<QByteArray, ObjectId> _syncSlave;
269 ProxyMode _proxyMode;
270 QTimer _heartBeatTimer;
272 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
274 friend class SignalRelay;