1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 ***************************************************************************/
26 #include <QThreadStorage>
39 class SignalProxy : public QObject
52 RemovePeerEvent = QEvent::User
55 SignalProxy(QObject *parent);
56 SignalProxy(ProxyMode mode, QObject *parent);
57 virtual ~SignalProxy();
59 void setProxyMode(ProxyMode mode);
60 inline ProxyMode proxyMode() const { return _proxyMode; }
62 void setHeartBeatInterval(int secs);
63 inline int heartBeatInterval() const { return _heartBeatInterval; }
64 void setMaxHeartBeatCount(int max);
65 inline int maxHeartBeatCount() const { return _maxHeartBeatCount; }
67 bool addPeer(Peer *peer);
69 bool attachSignal(QObject *sender, const char *signal, const QByteArray &sigName = QByteArray());
70 bool attachSlot(const QByteArray &sigName, QObject *recv, const char *slot);
72 void synchronize(SyncableObject *obj);
73 void stopSynchronize(SyncableObject *obj);
75 class ExtendedMetaObject;
76 ExtendedMetaObject *extendedMetaObject(const QMetaObject *meta) const;
77 ExtendedMetaObject *createExtendedMetaObject(const QMetaObject *meta, bool checkConflicts = false);
78 inline ExtendedMetaObject *extendedMetaObject(const QObject *obj) const { return extendedMetaObject(metaObject(obj)); }
79 inline ExtendedMetaObject *createExtendedMetaObject(const QObject *obj, bool checkConflicts = false) { return createExtendedMetaObject(metaObject(obj), checkConflicts); }
81 bool isSecure() const { return _secure; }
82 void dumpProxyStats();
83 void dumpSyncMap(SyncableObject *object);
85 static SignalProxy *current() {
91 * This method allows to send a signal only to a limited set of peers
92 * @param peers A list of peers that should receive it
93 * @param closure Code you want to execute within of that restricted environment
95 void restrictTargetPeers(QSet<Peer*> peers, std::function<void()> closure);
96 void restrictTargetPeers(Peer *peer, std::function<void()> closure) {
99 restrictTargetPeers(set, std::move(closure));
102 //A better version, but only implemented on Qt5 if Initializer Lists exist
103 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
104 #ifdef Q_COMPILER_INITIALIZER_LISTS
105 void restrictTargetPeers(std::initializer_list<Peer*> peers, std::function<void()> closure) {
106 restrictTargetPeers(QSet<Peer*>(peers), std::move(closure));
112 inline int peerCount() const { return _peerMap.size(); }
113 QVariantList peerData();
115 Peer *peerById(int peerId);
118 * @return If handling a signal, the Peer from which the current signal originates
121 void setSourcePeer(Peer *sourcePeer);
124 * @return If sending a signal, the Peer to which the current signal is directed
127 void setTargetPeer(Peer *targetPeer);
130 void detachObject(QObject *obj);
131 void detachSignals(QObject *sender);
132 void detachSlots(QObject *receiver);
135 void customEvent(QEvent *event);
136 void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
137 void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
140 void removePeerBySender();
141 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
142 void updateSecureState();
145 void peerRemoved(Peer *peer);
148 void objectInitialized(SyncableObject *);
149 void heartBeatIntervalChanged(int secs);
150 void maxHeartBeatCountChanged(int max);
151 void lagUpdated(int lag);
152 void secureStateChanged(bool);
156 class PeerMessageEvent;
162 static const QMetaObject *metaObject(const QObject *obj);
164 void removePeer(Peer *peer);
165 void removeAllPeers();
168 return _lastPeerId++;
172 void dispatch(const T &protoMessage);
174 void dispatch(Peer *peer, const T &protoMessage);
176 void handle(Peer *peer, const Protocol::SyncMessage &syncMessage);
177 void handle(Peer *peer, const Protocol::RpcCall &rpcCall);
178 void handle(Peer *peer, const Protocol::InitRequest &initRequest);
179 void handle(Peer *peer, const Protocol::InitData &initData);
182 void handle(Peer *, T) { Q_ASSERT(0); }
184 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue, Peer *peer = 0);
185 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList(), Peer *peer = 0);
187 void requestInit(SyncableObject *obj);
188 QVariantMap initData(SyncableObject *obj) const;
189 void setInitData(SyncableObject *obj, const QVariantMap &properties);
191 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
193 QHash<int, Peer*> _peerMap;
195 // containg a list of argtypes for fast access
196 QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
198 // SignalRelay for all manually attached signals
199 SignalRelay *_signalRelay;
201 // RPC function -> (object, slot ID)
202 typedef QPair<QObject *, int> MethodId;
203 typedef QMultiHash<QByteArray, MethodId> SlotHash;
204 SlotHash _attachedSlots;
207 typedef QHash<QString, SyncableObject *> ObjectId;
208 QHash<QByteArray, ObjectId> _syncSlave;
210 ProxyMode _proxyMode;
211 int _heartBeatInterval;
212 int _maxHeartBeatCount;
214 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
218 QSet<Peer *> _restrictedTargets;
219 bool _restrictMessageTarget = false;
221 Peer *_sourcePeer = nullptr;
222 Peer *_targetPeer = nullptr;
224 thread_local static SignalProxy *_current;
226 friend class SignalRelay;
227 friend class SyncableObject;
232 // ==================================================
233 // ExtendedMetaObject
234 // ==================================================
235 class SignalProxy::ExtendedMetaObject
237 class MethodDescriptor
240 MethodDescriptor(const QMetaMethod &method);
241 MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
243 inline const QByteArray &methodName() const { return _methodName; }
244 inline const QList<int> &argTypes() const { return _argTypes; }
245 inline int returnType() const { return _returnType; }
246 inline int minArgCount() const { return _minArgCount; }
247 inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
250 QByteArray _methodName;
251 QList<int> _argTypes;
254 SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
259 ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
261 inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
262 inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
263 inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
264 inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
265 inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
267 inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
269 inline int updatedRemotelyId() { return _updatedRemotelyId; }
271 inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
272 const QHash<int, int> &receiveMap();
274 const QMetaObject *metaObject() const { return _meta; }
276 static QByteArray methodName(const QMetaMethod &method);
277 static QString methodBaseName(const QMetaMethod &method);
280 const MethodDescriptor &methodDescriptor(int methodId);
282 const QMetaObject *_meta;
283 int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
285 QHash<int, MethodDescriptor> _methods;
286 QHash<QByteArray, int> _methodIds;
287 QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y