dddb785bdaa83d548f0af13c56f85bb36dc528ed
[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 <QAbstractSocket>
25 #include <QEvent>
26 #include <QList>
27 #include <QHash>
28 #include <QVariant>
29 #include <QVariantMap>
30 #include <QPair>
31 #include <QSet>
32 #include <QString>
33 #include <QByteArray>
34 #include <QTimer>
35
36 #include "protocol.h"
37
38 class SyncableObject;
39 struct QMetaObject;
40
41
42 class SignalProxy : public QObject
43 {
44     Q_OBJECT
45
46     class SignalRelay;
47
48 public:
49     class AbstractPeer;
50
51     enum ProxyMode {
52         Server,
53         Client
54     };
55
56     enum EventType {
57         RemovePeerEvent = QEvent::User
58     };
59
60     SignalProxy(QObject *parent);
61     SignalProxy(ProxyMode mode, QObject *parent);
62     virtual ~SignalProxy();
63
64     void setProxyMode(ProxyMode mode);
65     inline ProxyMode proxyMode() const { return _proxyMode; }
66
67     void setHeartBeatInterval(int secs);
68     inline int heartBeatInterval() const { return _heartBeatInterval; }
69     void setMaxHeartBeatCount(int max);
70     inline int maxHeartBeatCount() const { return _maxHeartBeatCount; }
71
72     bool addPeer(AbstractPeer *peer);
73
74     bool attachSignal(QObject *sender, const char *signal, const QByteArray &sigName = QByteArray());
75     bool attachSlot(const QByteArray &sigName, QObject *recv, const char *slot);
76
77     void synchronize(SyncableObject *obj);
78     void stopSynchronize(SyncableObject *obj);
79
80     class ExtendedMetaObject;
81     ExtendedMetaObject *extendedMetaObject(const QMetaObject *meta) const;
82     ExtendedMetaObject *createExtendedMetaObject(const QMetaObject *meta, bool checkConflicts = false);
83     inline ExtendedMetaObject *extendedMetaObject(const QObject *obj) const { return extendedMetaObject(metaObject(obj)); }
84     inline ExtendedMetaObject *createExtendedMetaObject(const QObject *obj, bool checkConflicts = false) { return createExtendedMetaObject(metaObject(obj), checkConflicts); }
85
86     bool isSecure() const { return _secure; }
87     void dumpProxyStats();
88     void dumpSyncMap(SyncableObject *object);
89     inline int peerCount() const { return _peers.size(); }
90
91 public slots:
92     void detachObject(QObject *obj);
93     void detachSignals(QObject *sender);
94     void detachSlots(QObject *receiver);
95
96 protected:
97     void customEvent(QEvent *event);
98     void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
99     void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
100
101 private slots:
102     void removePeerBySender();
103     void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
104     void updateSecureState();
105
106 signals:
107     void peerRemoved(SignalProxy::AbstractPeer *peer);
108     void connected();
109     void disconnected();
110     void objectInitialized(SyncableObject *);
111     void heartBeatIntervalChanged(int secs);
112     void maxHeartBeatCountChanged(int max);
113     void lagUpdated(int lag);
114     void secureStateChanged(bool);
115
116 private:
117     template<class T>
118     class PeerMessageEvent;
119
120     void init();
121     void initServer();
122     void initClient();
123
124     static const QMetaObject *metaObject(const QObject *obj);
125
126     void removePeer(AbstractPeer *peer);
127     void removeAllPeers();
128
129     template<class T>
130     void dispatch(const T &protoMessage);
131
132     void handle(AbstractPeer *peer, const Protocol::SyncMessage &syncMessage);
133     void handle(AbstractPeer *peer, const Protocol::RpcCall &rpcCall);
134     void handle(AbstractPeer *peer, const Protocol::InitRequest &initRequest);
135     void handle(AbstractPeer *peer, const Protocol::InitData &initData);
136
137     bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params, QVariant &returnValue);
138     bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params = QVariantList());
139
140     void requestInit(SyncableObject *obj);
141     QVariantMap initData(SyncableObject *obj) const;
142     void setInitData(SyncableObject *obj, const QVariantMap &properties);
143
144     static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
145
146     QSet<AbstractPeer *> _peers;
147
148     // containg a list of argtypes for fast access
149     QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
150
151     // SignalRelay for all manually attached signals
152     SignalRelay *_signalRelay;
153
154     // RPC function -> (object, slot ID)
155     typedef QPair<QObject *, int> MethodId;
156     typedef QMultiHash<QByteArray, MethodId> SlotHash;
157     SlotHash _attachedSlots;
158
159     // slaves for sync
160     typedef QHash<QString, SyncableObject *> ObjectId;
161     QHash<QByteArray, ObjectId> _syncSlave;
162
163     ProxyMode _proxyMode;
164     int _heartBeatInterval;
165     int _maxHeartBeatCount;
166
167     bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
168
169     friend class SignalRelay;
170     friend class SyncableObject;
171     friend class InternalConnection;
172     friend class RemoteConnection;
173 };
174
175
176 // ==================================================
177 //  ExtendedMetaObject
178 // ==================================================
179 class SignalProxy::ExtendedMetaObject
180 {
181     class MethodDescriptor
182     {
183     public:
184         MethodDescriptor(const QMetaMethod &method);
185         MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
186
187         inline const QByteArray &methodName() const { return _methodName; }
188         inline const QList<int> &argTypes() const { return _argTypes; }
189         inline int returnType() const { return _returnType; }
190         inline int minArgCount() const { return _minArgCount; }
191         inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
192
193     private:
194         QByteArray _methodName;
195         QList<int> _argTypes;
196         int _returnType;
197         int _minArgCount;
198         SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
199     };
200
201
202 public:
203     ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
204
205     inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
206     inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
207     inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
208     inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
209     inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
210
211     inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
212
213     inline int updatedRemotelyId() { return _updatedRemotelyId; }
214
215     inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
216     const QHash<int, int> &receiveMap();
217
218     const QMetaObject *metaObject() const { return _meta; }
219
220     static QByteArray methodName(const QMetaMethod &method);
221     static QString methodBaseName(const QMetaMethod &method);
222
223 private:
224     const MethodDescriptor &methodDescriptor(int methodId);
225
226     const QMetaObject *_meta;
227     int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
228
229     QHash<int, MethodDescriptor> _methods;
230     QHash<QByteArray, int> _methodIds;
231     QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y
232 };
233
234
235 // ==================================================
236 //  AbstractPeer
237 // ==================================================
238 class SignalProxy::AbstractPeer : public QObject
239 {
240     Q_OBJECT
241
242 public:
243     AbstractPeer(QObject *parent = 0) : QObject(parent) {}
244
245     virtual QString description() const = 0;
246
247     virtual void setSignalProxy(SignalProxy *proxy) = 0;
248
249     virtual bool isOpen() const = 0;
250     virtual bool isSecure() const = 0;
251     virtual bool isLocal() const = 0;
252
253     virtual QString errorString() const { return QString(); }
254
255     virtual int lag() const = 0;
256
257 public slots:
258     virtual void dispatch(const Protocol::SyncMessage &msg) = 0;
259     virtual void dispatch(const Protocol::RpcCall &msg) = 0;
260     virtual void dispatch(const Protocol::InitRequest &msg) = 0;
261     virtual void dispatch(const Protocol::InitData &msg) = 0;
262
263     virtual void close(const QString &reason = QString()) = 0;
264
265 signals:
266     void disconnected();
267     void error(QAbstractSocket::SocketError);
268     void secureStateChanged(bool secure = true);
269     void lagUpdated(int msecs);
270 };
271
272 #endif