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 ***************************************************************************/
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);
85 * This method allows to send a signal only to a limited set of peers
86 * @param peers A list of peers that should receive it
87 * @param closure Code you want to execute within of that restricted environment
89 void restrictTargetPeers(QSet<Peer*> peers, std::function<void()> closure);
90 void restrictTargetPeers(Peer *peer, std::function<void()> closure) {
93 restrictTargetPeers(set, std::move(closure));
96 //A better version, but only implemented on Qt5 if Initializer Lists exist
97 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
98 #ifdef Q_COMPILER_INITIALIZER_LISTS
99 void restrictTargetPeers(std::initializer_list<Peer*> peers, std::function<void()> closure) {
100 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
114 Peer *sourcePeer() { return _sourcePeer; }
117 void detachObject(QObject *obj);
118 void detachSignals(QObject *sender);
119 void detachSlots(QObject *receiver);
122 void customEvent(QEvent *event);
123 void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
124 void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
127 void removePeerBySender();
128 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
129 void updateSecureState();
132 void peerRemoved(Peer *peer);
135 void objectInitialized(SyncableObject *);
136 void heartBeatIntervalChanged(int secs);
137 void maxHeartBeatCountChanged(int max);
138 void lagUpdated(int lag);
139 void secureStateChanged(bool);
143 class PeerMessageEvent;
149 static const QMetaObject *metaObject(const QObject *obj);
151 void removePeer(Peer *peer);
152 void removeAllPeers();
155 return _lastPeerId++;
159 void dispatch(const T &protoMessage);
161 void dispatch(Peer *peer, const T &protoMessage);
163 void handle(Peer *peer, const Protocol::SyncMessage &syncMessage);
164 void handle(Peer *peer, const Protocol::RpcCall &rpcCall);
165 void handle(Peer *peer, const Protocol::InitRequest &initRequest);
166 void handle(Peer *peer, const Protocol::InitData &initData);
169 void handle(Peer *, T) { Q_ASSERT(0); }
171 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue, Peer *peer = 0);
172 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList(), Peer *peer = 0);
174 void requestInit(SyncableObject *obj);
175 QVariantMap initData(SyncableObject *obj) const;
176 void setInitData(SyncableObject *obj, const QVariantMap &properties);
178 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
180 QHash<int, Peer*> _peerMap;
182 // containg a list of argtypes for fast access
183 QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
185 // SignalRelay for all manually attached signals
186 SignalRelay *_signalRelay;
188 // RPC function -> (object, slot ID)
189 typedef QPair<QObject *, int> MethodId;
190 typedef QMultiHash<QByteArray, MethodId> SlotHash;
191 SlotHash _attachedSlots;
194 typedef QHash<QString, SyncableObject *> ObjectId;
195 QHash<QByteArray, ObjectId> _syncSlave;
197 ProxyMode _proxyMode;
198 int _heartBeatInterval;
199 int _maxHeartBeatCount;
201 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
205 QSet<Peer *> _restrictedTargets;
206 bool _restrictMessageTarget = false;
208 Peer *_sourcePeer = nullptr;
210 friend class SignalRelay;
211 friend class SyncableObject;
216 // ==================================================
217 // ExtendedMetaObject
218 // ==================================================
219 class SignalProxy::ExtendedMetaObject
221 class MethodDescriptor
224 MethodDescriptor(const QMetaMethod &method);
225 MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
227 inline const QByteArray &methodName() const { return _methodName; }
228 inline const QList<int> &argTypes() const { return _argTypes; }
229 inline int returnType() const { return _returnType; }
230 inline int minArgCount() const { return _minArgCount; }
231 inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
234 QByteArray _methodName;
235 QList<int> _argTypes;
238 SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
243 ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
245 inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
246 inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
247 inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
248 inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
249 inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
251 inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
253 inline int updatedRemotelyId() { return _updatedRemotelyId; }
255 inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
256 const QHash<int, int> &receiveMap();
258 const QMetaObject *metaObject() const { return _meta; }
260 static QByteArray methodName(const QMetaMethod &method);
261 static QString methodBaseName(const QMetaMethod &method);
264 const MethodDescriptor &methodDescriptor(int methodId);
266 const QMetaObject *_meta;
267 int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
269 QHash<int, MethodDescriptor> _methods;
270 QHash<QByteArray, int> _methodIds;
271 QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y