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();
87 * This method allows to send a signal only to a limited set of peers
88 * @param peers A list of peers that should receive it
89 * @param closure Code you want to execute within of that restricted environment
91 void restrictTargetPeers(QSet<Peer*> peers, std::function<void()> closure);
92 void restrictTargetPeers(Peer *peer, std::function<void()> closure) {
95 restrictTargetPeers(set, std::move(closure));
98 //A better version, but only implemented on Qt5 if Initializer Lists exist
99 #ifdef Q_COMPILER_INITIALIZER_LISTS
100 void restrictTargetPeers(std::initializer_list<Peer*> peers, std::function<void()> closure) {
101 restrictTargetPeers(QSet<Peer*>(peers), std::move(closure));
106 inline int peerCount() const { return _peerMap.size(); }
107 QVariantList peerData();
109 Peer *peerById(int peerId);
112 * @return If handling a signal, the Peer from which the current signal originates
115 void setSourcePeer(Peer *sourcePeer);
118 * @return If sending a signal, the Peer to which the current signal is directed
121 void setTargetPeer(Peer *targetPeer);
124 void detachObject(QObject *obj);
125 void detachSignals(QObject *sender);
126 void detachSlots(QObject *receiver);
129 void customEvent(QEvent *event);
130 void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
131 void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
134 void removePeerBySender();
135 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
136 void updateSecureState();
139 void peerRemoved(Peer *peer);
142 void objectInitialized(SyncableObject *);
143 void heartBeatIntervalChanged(int secs);
144 void maxHeartBeatCountChanged(int max);
145 void lagUpdated(int lag);
146 void secureStateChanged(bool);
150 class PeerMessageEvent;
156 static const QMetaObject *metaObject(const QObject *obj);
158 void removePeer(Peer *peer);
159 void removeAllPeers();
162 return _lastPeerId++;
166 void dispatch(const T &protoMessage);
168 void dispatch(Peer *peer, const T &protoMessage);
170 void handle(Peer *peer, const Protocol::SyncMessage &syncMessage);
171 void handle(Peer *peer, const Protocol::RpcCall &rpcCall);
172 void handle(Peer *peer, const Protocol::InitRequest &initRequest);
173 void handle(Peer *peer, const Protocol::InitData &initData);
176 void handle(Peer *, T) { Q_ASSERT(0); }
178 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue, Peer *peer = 0);
179 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList(), Peer *peer = 0);
181 void requestInit(SyncableObject *obj);
182 QVariantMap initData(SyncableObject *obj) const;
183 void setInitData(SyncableObject *obj, const QVariantMap &properties);
185 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
187 QHash<int, Peer*> _peerMap;
189 // containg a list of argtypes for fast access
190 QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
192 // SignalRelay for all manually attached signals
193 SignalRelay *_signalRelay;
195 // RPC function -> (object, slot ID)
196 typedef QPair<QObject *, int> MethodId;
197 typedef QMultiHash<QByteArray, MethodId> SlotHash;
198 SlotHash _attachedSlots;
201 typedef QHash<QString, SyncableObject *> ObjectId;
202 QHash<QByteArray, ObjectId> _syncSlave;
204 ProxyMode _proxyMode;
205 int _heartBeatInterval;
206 int _maxHeartBeatCount;
208 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
212 QSet<Peer *> _restrictedTargets;
213 bool _restrictMessageTarget = false;
215 Peer *_sourcePeer = nullptr;
216 Peer *_targetPeer = nullptr;
218 friend class SignalRelay;
219 friend class SyncableObject;
224 // ==================================================
225 // ExtendedMetaObject
226 // ==================================================
227 class SignalProxy::ExtendedMetaObject
229 class MethodDescriptor
232 MethodDescriptor(const QMetaMethod &method);
233 MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
235 inline const QByteArray &methodName() const { return _methodName; }
236 inline const QList<int> &argTypes() const { return _argTypes; }
237 inline int returnType() const { return _returnType; }
238 inline int minArgCount() const { return _minArgCount; }
239 inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
242 QByteArray _methodName;
243 QList<int> _argTypes;
246 SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
251 ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
253 inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
254 inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
255 inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
256 inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
257 inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
259 inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
261 inline int updatedRemotelyId() { return _updatedRemotelyId; }
263 inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
264 const QHash<int, int> &receiveMap();
266 const QMetaObject *metaObject() const { return _meta; }
268 static QByteArray methodName(const QMetaMethod &method);
269 static QString methodBaseName(const QMetaMethod &method);
272 const MethodDescriptor &methodDescriptor(int methodId);
274 const QMetaObject *_meta;
275 int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
277 QHash<int, MethodDescriptor> _methods;
278 QHash<QByteArray, int> _methodIds;
279 QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y