1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
27 #include <initializer_list>
37 class SignalProxy : public QObject
50 RemovePeerEvent = QEvent::User
53 SignalProxy(QObject *parent);
54 SignalProxy(ProxyMode mode, QObject *parent);
55 virtual ~SignalProxy();
57 void setProxyMode(ProxyMode mode);
58 inline ProxyMode proxyMode() const { return _proxyMode; }
60 void setHeartBeatInterval(int secs);
61 inline int heartBeatInterval() const { return _heartBeatInterval; }
62 void setMaxHeartBeatCount(int max);
63 inline int maxHeartBeatCount() const { return _maxHeartBeatCount; }
65 bool addPeer(Peer *peer);
67 bool attachSignal(QObject *sender, const char *signal, const QByteArray &sigName = QByteArray());
68 bool attachSlot(const QByteArray &sigName, QObject *recv, const char *slot);
70 void synchronize(SyncableObject *obj);
71 void stopSynchronize(SyncableObject *obj);
73 class ExtendedMetaObject;
74 ExtendedMetaObject *extendedMetaObject(const QMetaObject *meta) const;
75 ExtendedMetaObject *createExtendedMetaObject(const QMetaObject *meta, bool checkConflicts = false);
76 inline ExtendedMetaObject *extendedMetaObject(const QObject *obj) const { return extendedMetaObject(metaObject(obj)); }
77 inline ExtendedMetaObject *createExtendedMetaObject(const QObject *obj, bool checkConflicts = false) { return createExtendedMetaObject(metaObject(obj), checkConflicts); }
79 bool isSecure() const { return _secure; }
80 void dumpProxyStats();
81 void dumpSyncMap(SyncableObject *object);
83 static SignalProxy *current() {
89 * This method allows to send a signal only to a limited set of peers
90 * @param peers A list of peers that should receive it
91 * @param closure Code you want to execute within of that restricted environment
93 void restrictTargetPeers(QSet<Peer*> peers, std::function<void()> closure);
94 void restrictTargetPeers(Peer *peer, std::function<void()> closure) {
97 restrictTargetPeers(set, std::move(closure));
100 //A better version, but only implemented on Qt5 if Initializer Lists exist
101 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
102 #ifdef Q_COMPILER_INITIALIZER_LISTS
103 void restrictTargetPeers(std::initializer_list<Peer*> peers, std::function<void()> closure) {
104 restrictTargetPeers(QSet<Peer*>(peers), std::move(closure));
110 inline int peerCount() const { return _peerMap.size(); }
111 QVariantList peerData();
113 Peer *peerById(int peerId);
116 * @return If handling a signal, the Peer from which the current signal originates
119 void setSourcePeer(Peer *sourcePeer);
122 * @return If sending a signal, the Peer to which the current signal is directed
125 void setTargetPeer(Peer *targetPeer);
128 void detachObject(QObject *obj);
129 void detachSignals(QObject *sender);
130 void detachSlots(QObject *receiver);
133 void customEvent(QEvent *event);
134 void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
135 void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
138 void removePeerBySender();
139 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
140 void updateSecureState();
143 void peerRemoved(Peer *peer);
146 void objectInitialized(SyncableObject *);
147 void heartBeatIntervalChanged(int secs);
148 void maxHeartBeatCountChanged(int max);
149 void lagUpdated(int lag);
150 void secureStateChanged(bool);
154 class PeerMessageEvent;
160 static const QMetaObject *metaObject(const QObject *obj);
162 void removePeer(Peer *peer);
163 void removeAllPeers();
166 return _lastPeerId++;
170 void dispatch(const T &protoMessage);
172 void dispatch(Peer *peer, const T &protoMessage);
174 void handle(Peer *peer, const Protocol::SyncMessage &syncMessage);
175 void handle(Peer *peer, const Protocol::RpcCall &rpcCall);
176 void handle(Peer *peer, const Protocol::InitRequest &initRequest);
177 void handle(Peer *peer, const Protocol::InitData &initData);
180 void handle(Peer *, T) { Q_ASSERT(0); }
182 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue, Peer *peer = 0);
183 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList(), Peer *peer = 0);
185 void requestInit(SyncableObject *obj);
186 QVariantMap initData(SyncableObject *obj) const;
187 void setInitData(SyncableObject *obj, const QVariantMap &properties);
189 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
191 QHash<int, Peer*> _peerMap;
193 // containg a list of argtypes for fast access
194 QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
196 // SignalRelay for all manually attached signals
197 SignalRelay *_signalRelay;
199 // RPC function -> (object, slot ID)
200 typedef QPair<QObject *, int> MethodId;
201 typedef QMultiHash<QByteArray, MethodId> SlotHash;
202 SlotHash _attachedSlots;
205 typedef QHash<QString, SyncableObject *> ObjectId;
206 QHash<QByteArray, ObjectId> _syncSlave;
208 ProxyMode _proxyMode;
209 int _heartBeatInterval;
210 int _maxHeartBeatCount;
212 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
216 QSet<Peer *> _restrictedTargets;
217 bool _restrictMessageTarget = false;
219 Peer *_sourcePeer = nullptr;
220 Peer *_targetPeer = nullptr;
222 thread_local static SignalProxy *_current;
224 friend class SignalRelay;
225 friend class SyncableObject;
230 // ==================================================
231 // ExtendedMetaObject
232 // ==================================================
233 class SignalProxy::ExtendedMetaObject
235 class MethodDescriptor
238 MethodDescriptor(const QMetaMethod &method);
239 MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
241 inline const QByteArray &methodName() const { return _methodName; }
242 inline const QList<int> &argTypes() const { return _argTypes; }
243 inline int returnType() const { return _returnType; }
244 inline int minArgCount() const { return _minArgCount; }
245 inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
248 QByteArray _methodName;
249 QList<int> _argTypes;
252 SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
257 ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
259 inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
260 inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
261 inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
262 inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
263 inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
265 inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
267 inline int updatedRemotelyId() { return _updatedRemotelyId; }
269 inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
270 const QHash<int, int> &receiveMap();
272 const QMetaObject *metaObject() const { return _meta; }
274 static QByteArray methodName(const QMetaMethod &method);
275 static QString methodBaseName(const QMetaMethod &method);
278 const MethodDescriptor &methodDescriptor(int methodId);
280 const QMetaObject *_meta;
281 int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
283 QHash<int, MethodDescriptor> _methods;
284 QHash<QByteArray, int> _methodIds;
285 QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y