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 #ifdef Q_COMPILER_INITIALIZER_LISTS
102 void restrictTargetPeers(std::initializer_list<Peer*> peers, std::function<void()> closure) {
103 restrictTargetPeers(QSet<Peer*>(peers), std::move(closure));
108 inline int peerCount() const { return _peerMap.size(); }
109 QVariantList peerData();
111 Peer *peerById(int peerId);
114 * @return If handling a signal, the Peer from which the current signal originates
117 void setSourcePeer(Peer *sourcePeer);
120 * @return If sending a signal, the Peer to which the current signal is directed
123 void setTargetPeer(Peer *targetPeer);
126 void detachObject(QObject *obj);
127 void detachSignals(QObject *sender);
128 void detachSlots(QObject *receiver);
131 void customEvent(QEvent *event);
132 void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
133 void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
136 void removePeerBySender();
137 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
138 void updateSecureState();
141 void peerRemoved(Peer *peer);
144 void objectInitialized(SyncableObject *);
145 void heartBeatIntervalChanged(int secs);
146 void maxHeartBeatCountChanged(int max);
147 void lagUpdated(int lag);
148 void secureStateChanged(bool);
152 class PeerMessageEvent;
158 static const QMetaObject *metaObject(const QObject *obj);
160 void removePeer(Peer *peer);
161 void removeAllPeers();
164 return _lastPeerId++;
168 void dispatch(const T &protoMessage);
170 void dispatch(Peer *peer, const T &protoMessage);
172 void handle(Peer *peer, const Protocol::SyncMessage &syncMessage);
173 void handle(Peer *peer, const Protocol::RpcCall &rpcCall);
174 void handle(Peer *peer, const Protocol::InitRequest &initRequest);
175 void handle(Peer *peer, const Protocol::InitData &initData);
178 void handle(Peer *, T) { Q_ASSERT(0); }
180 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue, Peer *peer = 0);
181 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList(), Peer *peer = 0);
183 void requestInit(SyncableObject *obj);
184 QVariantMap initData(SyncableObject *obj) const;
185 void setInitData(SyncableObject *obj, const QVariantMap &properties);
187 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
189 QHash<int, Peer*> _peerMap;
191 // containg a list of argtypes for fast access
192 QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
194 // SignalRelay for all manually attached signals
195 SignalRelay *_signalRelay;
197 // RPC function -> (object, slot ID)
198 typedef QPair<QObject *, int> MethodId;
199 typedef QMultiHash<QByteArray, MethodId> SlotHash;
200 SlotHash _attachedSlots;
203 typedef QHash<QString, SyncableObject *> ObjectId;
204 QHash<QByteArray, ObjectId> _syncSlave;
206 ProxyMode _proxyMode;
207 int _heartBeatInterval;
208 int _maxHeartBeatCount;
210 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
214 QSet<Peer *> _restrictedTargets;
215 bool _restrictMessageTarget = false;
217 Peer *_sourcePeer = nullptr;
218 Peer *_targetPeer = nullptr;
220 thread_local static SignalProxy *_current;
222 friend class SignalRelay;
223 friend class SyncableObject;
228 // ==================================================
229 // ExtendedMetaObject
230 // ==================================================
231 class SignalProxy::ExtendedMetaObject
233 class MethodDescriptor
236 MethodDescriptor(const QMetaMethod &method);
237 MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
239 inline const QByteArray &methodName() const { return _methodName; }
240 inline const QList<int> &argTypes() const { return _argTypes; }
241 inline int returnType() const { return _returnType; }
242 inline int minArgCount() const { return _minArgCount; }
243 inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
246 QByteArray _methodName;
247 QList<int> _argTypes;
250 SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
255 ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
257 inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
258 inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
259 inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
260 inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
261 inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
263 inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
265 inline int updatedRemotelyId() { return _updatedRemotelyId; }
267 inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
268 const QHash<int, int> &receiveMap();
270 const QMetaObject *metaObject() const { return _meta; }
272 static QByteArray methodName(const QMetaMethod &method);
273 static QString methodBaseName(const QMetaMethod &method);
276 const MethodDescriptor &methodDescriptor(int methodId);
278 const QMetaObject *_meta;
279 int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
281 QHash<int, MethodDescriptor> _methods;
282 QHash<QByteArray, int> _methodIds;
283 QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y