Remove unused enum
[quassel.git] / src / common / signalproxy.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2012 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef SIGNALPROXY_H
22 #define SIGNALPROXY_H
23
24 #include <QEvent>
25 #include <QList>
26 #include <QHash>
27 #include <QVariant>
28 #include <QVariantMap>
29 #include <QPair>
30 #include <QString>
31 #include <QByteArray>
32 #include <QTimer>
33
34 class SyncableObject;
35 struct QMetaObject;
36
37 class SignalProxy : public QObject
38 {
39     Q_OBJECT
40
41     class AbstractPeer;
42     class IODevicePeer;
43     class SignalProxyPeer;
44
45     class SignalRelay;
46
47 public:
48     enum ProxyMode {
49         Server,
50         Client
51     };
52
53     enum RequestType {
54         Sync = 1,
55         RpcCall,
56         InitRequest,
57         InitData,
58         HeartBeat,
59         HeartBeatReply
60     };
61
62     enum CustomEvents {
63         PeerSignal = QEvent::User,
64         RemovePeer
65     };
66
67     SignalProxy(QObject *parent);
68     SignalProxy(ProxyMode mode, QObject *parent);
69     SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
70     virtual ~SignalProxy();
71
72     void setProxyMode(ProxyMode mode);
73     inline ProxyMode proxyMode() const { return _proxyMode; }
74
75     void setHeartBeatInterval(int secs);
76     inline int heartBeatInterval() const { return _heartBeatInterval; }
77     void setMaxHeartBeatCount(int max);
78     inline int maxHeartBeatCount() const { return _maxHeartBeatCount; }
79
80     bool addPeer(QIODevice *iodev);
81     bool addPeer(SignalProxy *proxy);
82     void removePeer(QObject *peer);
83     void removeAllPeers();
84
85     bool attachSignal(QObject *sender, const char *signal, const QByteArray &sigName = QByteArray());
86     bool attachSlot(const QByteArray &sigName, QObject *recv, const char *slot);
87
88     void synchronize(SyncableObject *obj);
89     void stopSynchronize(SyncableObject *obj);
90
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.
95      */
96     static void writeDataToDevice(QIODevice *dev, const QVariant &item, bool compressed = false);
97
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
100      *  remains untouched.
101      */
102     static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed = false);
103
104     class ExtendedMetaObject;
105     ExtendedMetaObject *extendedMetaObject(const QMetaObject *meta) const;
106     ExtendedMetaObject *createExtendedMetaObject(const QMetaObject *meta, bool checkConflicts = false);
107     inline ExtendedMetaObject *extendedMetaObject(const QObject *obj) const { return extendedMetaObject(metaObject(obj)); }
108     inline ExtendedMetaObject *createExtendedMetaObject(const QObject *obj, bool checkConflicts = false) { return createExtendedMetaObject(metaObject(obj), checkConflicts); }
109
110     bool isSecure() const { return _secure; }
111     void dumpProxyStats();
112
113 public slots:
114     void detachObject(QObject *obj);
115     void detachSignals(QObject *sender);
116     void detachSlots(QObject *receiver);
117
118 protected:
119     void customEvent(QEvent *event);
120     void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
121     void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
122
123 private slots:
124     void dataAvailable();
125     void removePeerBySender();
126     void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
127     void sendHeartBeat();
128     void receiveHeartBeat(AbstractPeer *peer, const QVariantList &params);
129     void receiveHeartBeatReply(AbstractPeer *peer, const QVariantList &params);
130
131     void updateSecureState();
132
133 signals:
134     void peerRemoved(QIODevice *dev);
135     void connected();
136     void disconnected();
137     void objectInitialized(SyncableObject *);
138     void lagUpdated(int lag);
139     void securityChanged(bool);
140     void secureStateChanged(bool);
141
142 private:
143     void init();
144     void initServer();
145     void initClient();
146
147     static const QMetaObject *metaObject(const QObject *obj);
148
149     void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList &params);
150     void dispatchSignal(const RequestType &requestType, const QVariantList &params);
151
152     void receivePackedFunc(AbstractPeer *sender, const QVariant &packedFunc);
153     void receivePeerSignal(AbstractPeer *sender, const RequestType &requestType, const QVariantList &params);
154     void receivePeerSignal(SignalProxy *sender, const RequestType &requestType, const QVariantList &params);
155     void handleSync(AbstractPeer *sender, QVariantList params);
156     void handleInitRequest(AbstractPeer *sender, const QVariantList &params);
157     void handleInitData(AbstractPeer *sender, const QVariantList &params);
158     void handleSignal(const QVariantList &data);
159
160     bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params, QVariant &returnValue);
161     bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params = QVariantList());
162
163     void requestInit(SyncableObject *obj);
164     QVariantMap initData(SyncableObject *obj) const;
165     void setInitData(SyncableObject *obj, const QVariantMap &properties);
166
167     void updateLag(IODevicePeer *peer, int lag);
168
169 public:
170     void dumpSyncMap(SyncableObject *object);
171     inline int peerCount() const { return _peers.size(); }
172
173 private:
174     static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
175
176     // a Hash of the actual used communication object to it's corresponding peer
177     // currently a communication object can either be an arbitrary QIODevice or another SignalProxy
178     typedef QHash<QObject *, AbstractPeer *> PeerHash;
179     PeerHash _peers;
180
181     // containg a list of argtypes for fast access
182     QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
183
184     // SignalRelay for all manually attached signals
185     SignalRelay *_signalRelay;
186
187     // RPC function -> (object, slot ID)
188     typedef QPair<QObject *, int> MethodId;
189     typedef QMultiHash<QByteArray, MethodId> SlotHash;
190     SlotHash _attachedSlots;
191
192     // slaves for sync
193     typedef QHash<QString, SyncableObject *> ObjectId;
194     QHash<QByteArray, ObjectId> _syncSlave;
195
196     ProxyMode _proxyMode;
197     QTimer _heartBeatTimer;
198     int _heartBeatInterval;
199     int _maxHeartBeatCount;
200
201     bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
202
203     friend class SignalRelay;
204     friend class SyncableObject;
205 };
206
207
208 // ==================================================
209 //  ExtendedMetaObject
210 // ==================================================
211 class SignalProxy::ExtendedMetaObject
212 {
213     class MethodDescriptor
214     {
215 public:
216         MethodDescriptor(const QMetaMethod &method);
217         MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
218
219         inline const QByteArray &methodName() const { return _methodName; }
220         inline const QList<int> &argTypes() const { return _argTypes; }
221         inline int returnType() const { return _returnType; }
222         inline int minArgCount() const { return _minArgCount; }
223         inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
224
225 private:
226         QByteArray _methodName;
227         QList<int> _argTypes;
228         int _returnType;
229         int _minArgCount;
230         SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
231     };
232
233
234 public:
235     ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
236
237     inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
238     inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
239     inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
240     inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
241     inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
242
243     inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
244
245     inline int updatedRemotelyId() { return _updatedRemotelyId; }
246
247     inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
248     const QHash<int, int> &receiveMap();
249
250     const QMetaObject *metaObject() const { return _meta; }
251
252     static QByteArray methodName(const QMetaMethod &method);
253     static QString methodBaseName(const QMetaMethod &method);
254
255 private:
256     const MethodDescriptor &methodDescriptor(int methodId);
257
258     const QMetaObject *_meta;
259     int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
260
261     QHash<int, MethodDescriptor> _methods;
262     QHash<QByteArray, int> _methodIds;
263     QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y
264 };
265
266
267 // ==================================================
268 //  Peers
269 // ==================================================
270 class SignalProxy::AbstractPeer
271 {
272 public:
273     enum PeerType {
274         NotAPeer = 0,
275         IODevicePeer = 1,
276         SignalProxyPeer = 2
277     };
278     AbstractPeer() : _type(NotAPeer) {}
279     AbstractPeer(PeerType type) : _type(type) {}
280     virtual ~AbstractPeer() {}
281     inline PeerType type() const { return _type; }
282     virtual void dispatchSignal(const RequestType &requestType, const QVariantList &params) = 0;
283     virtual bool isSecure() const = 0;
284 private:
285     PeerType _type;
286 };
287
288
289 class SignalProxy::IODevicePeer : public SignalProxy::AbstractPeer
290 {
291 public:
292     IODevicePeer(QIODevice *device, bool compress) : AbstractPeer(AbstractPeer::IODevicePeer), _device(device), byteCount(0), usesCompression(compress), sentHeartBeats(0), lag(0) {}
293     virtual void dispatchSignal(const RequestType &requestType, const QVariantList &params);
294     virtual bool isSecure() const;
295     inline void dispatchPackedFunc(const QVariant &packedFunc) { SignalProxy::writeDataToDevice(_device, packedFunc, usesCompression); }
296     QString address() const;
297     inline bool isOpen() const { return _device->isOpen(); }
298     inline void close() const { _device->close(); }
299     inline bool readData(QVariant &item) { return SignalProxy::readDataFromDevice(_device, byteCount, item, usesCompression); }
300 private:
301     QIODevice *_device;
302     quint32 byteCount;
303     bool usesCompression;
304 public:
305     int sentHeartBeats;
306     int lag;
307 };
308
309
310 class SignalProxy::SignalProxyPeer : public SignalProxy::AbstractPeer
311 {
312 public:
313     SignalProxyPeer(SignalProxy *sender, SignalProxy *receiver) : AbstractPeer(AbstractPeer::SignalProxyPeer), sender(sender), receiver(receiver) {}
314     virtual void dispatchSignal(const RequestType &requestType, const QVariantList &params);
315     virtual inline bool isSecure() const { return true; }
316 private:
317     SignalProxy *sender;
318     SignalProxy *receiver;
319 };
320
321
322 #endif