Improvements to fix the build and for Doxygen
[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     /**@{*/
82     /**
83      * This method allows to send a signal only to a limited set of peers
84      * @param peerIds A list of peers that should receive it
85      * @param closure Code you want to execute within of that restricted environment
86      */
87     void restrictTargetPeers(QSet<Peer*> peers, std::function<void()> closure);
88     void restrictTargetPeers(Peer *peer, std::function<void()> closure) {
89         QSet<Peer*> set;
90         set.insert(peer);
91         restrictTargetPeers(set, std::move(closure));
92     }
93
94     //A better version, but only implemented on Qt5 if Initializer Lists exist
95 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
96 #ifdef Q_COMPILER_INITIALIZER_LISTS
97     void restrictTargetPeers(std::initializer_list<Peer*> peers, std::function<void()> closure) {
98         restrictTargetPeers(QSet<Peer*>(peers), std::move(closure));
99     }
100 #endif
101 #endif
102     /**}@*/
103
104     inline int peerCount() const { return _peers.size(); }
105     QVariantList peerData();
106
107     Peer *peerById(int peerId);
108
109     /**
110      * @return If handling a signal, the Peer from which the current signal originates
111      */
112     Peer *sourcePeer() { return _sourcePeer; }
113
114 public slots:
115     void detachObject(QObject *obj);
116     void detachSignals(QObject *sender);
117     void detachSlots(QObject *receiver);
118
119 protected:
120     void customEvent(QEvent *event);
121     void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
122     void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
123
124 private slots:
125     void removePeerBySender();
126     void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
127     void updateSecureState();
128
129 signals:
130     void peerRemoved(Peer *peer);
131     void connected();
132     void disconnected();
133     void objectInitialized(SyncableObject *);
134     void heartBeatIntervalChanged(int secs);
135     void maxHeartBeatCountChanged(int max);
136     void lagUpdated(int lag);
137     void secureStateChanged(bool);
138
139 private:
140     template<class T>
141     class PeerMessageEvent;
142
143     void init();
144     void initServer();
145     void initClient();
146
147     static const QMetaObject *metaObject(const QObject *obj);
148
149     void removePeer(Peer *peer);
150     void removeAllPeers();
151
152     int nextPeerId() {
153         return _lastPeerId++;
154     }
155
156     template<class T>
157     void dispatch(const T &protoMessage);
158     template<class T>
159     void dispatch(Peer *peer, const T &protoMessage);
160
161     void handle(Peer *peer, const Protocol::SyncMessage &syncMessage);
162     void handle(Peer *peer, const Protocol::RpcCall &rpcCall);
163     void handle(Peer *peer, const Protocol::InitRequest &initRequest);
164     void handle(Peer *peer, const Protocol::InitData &initData);
165
166     template<class T>
167     void handle(Peer *, T) { Q_ASSERT(0); }
168
169     bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params, QVariant &returnValue, Peer *peer = 0);
170     bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params = QVariantList(), Peer *peer = 0);
171
172     void requestInit(SyncableObject *obj);
173     QVariantMap initData(SyncableObject *obj) const;
174     void setInitData(SyncableObject *obj, const QVariantMap &properties);
175
176     static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
177
178     QSet<Peer *> _peers;
179     QHash<int, Peer*> _peerMap;
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     int _heartBeatInterval;
198     int _maxHeartBeatCount;
199
200     bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
201
202     int _lastPeerId = 0;
203
204     QSet<Peer *> _restrictedTargets;
205     bool _restrictMessageTarget = false;
206
207     Peer *_sourcePeer;
208
209     friend class SignalRelay;
210     friend class SyncableObject;
211     friend class Peer;
212 };
213
214
215 // ==================================================
216 //  ExtendedMetaObject
217 // ==================================================
218 class SignalProxy::ExtendedMetaObject
219 {
220     class MethodDescriptor
221     {
222     public:
223         MethodDescriptor(const QMetaMethod &method);
224         MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
225
226         inline const QByteArray &methodName() const { return _methodName; }
227         inline const QList<int> &argTypes() const { return _argTypes; }
228         inline int returnType() const { return _returnType; }
229         inline int minArgCount() const { return _minArgCount; }
230         inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
231
232     private:
233         QByteArray _methodName;
234         QList<int> _argTypes;
235         int _returnType;
236         int _minArgCount;
237         SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
238     };
239
240
241 public:
242     ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
243
244     inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
245     inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
246     inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
247     inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
248     inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
249
250     inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
251
252     inline int updatedRemotelyId() { return _updatedRemotelyId; }
253
254     inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
255     const QHash<int, int> &receiveMap();
256
257     const QMetaObject *metaObject() const { return _meta; }
258
259     static QByteArray methodName(const QMetaMethod &method);
260     static QString methodBaseName(const QMetaMethod &method);
261
262 private:
263     const MethodDescriptor &methodDescriptor(int methodId);
264
265     const QMetaObject *_meta;
266     int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
267
268     QHash<int, MethodDescriptor> _methods;
269     QHash<QByteArray, int> _methodIds;
270     QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y
271 };
272
273 #endif