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