1 /***************************************************************************
2 * Copyright (C) 2005-2013 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 ***************************************************************************/
24 #include <QAbstractSocket>
29 #include <QVariantMap>
43 class SignalProxy : public QObject
56 RemovePeerEvent = QEvent::User
59 SignalProxy(QObject *parent);
60 SignalProxy(ProxyMode mode, QObject *parent);
61 virtual ~SignalProxy();
63 void setProxyMode(ProxyMode mode);
64 inline ProxyMode proxyMode() const { return _proxyMode; }
66 void setHeartBeatInterval(int secs);
67 inline int heartBeatInterval() const { return _heartBeatInterval; }
68 void setMaxHeartBeatCount(int max);
69 inline int maxHeartBeatCount() const { return _maxHeartBeatCount; }
71 bool addPeer(Peer *peer);
73 bool attachSignal(QObject *sender, const char *signal, const QByteArray &sigName = QByteArray());
74 bool attachSlot(const QByteArray &sigName, QObject *recv, const char *slot);
76 void synchronize(SyncableObject *obj);
77 void stopSynchronize(SyncableObject *obj);
79 class ExtendedMetaObject;
80 ExtendedMetaObject *extendedMetaObject(const QMetaObject *meta) const;
81 ExtendedMetaObject *createExtendedMetaObject(const QMetaObject *meta, bool checkConflicts = false);
82 inline ExtendedMetaObject *extendedMetaObject(const QObject *obj) const { return extendedMetaObject(metaObject(obj)); }
83 inline ExtendedMetaObject *createExtendedMetaObject(const QObject *obj, bool checkConflicts = false) { return createExtendedMetaObject(metaObject(obj), checkConflicts); }
85 bool isSecure() const { return _secure; }
86 void dumpProxyStats();
87 void dumpSyncMap(SyncableObject *object);
88 inline int peerCount() const { return _peers.size(); }
91 void detachObject(QObject *obj);
92 void detachSignals(QObject *sender);
93 void detachSlots(QObject *receiver);
96 void customEvent(QEvent *event);
97 void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
98 void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
101 void removePeerBySender();
102 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
103 void updateSecureState();
106 void peerRemoved(Peer *peer);
109 void objectInitialized(SyncableObject *);
110 void heartBeatIntervalChanged(int secs);
111 void maxHeartBeatCountChanged(int max);
112 void lagUpdated(int lag);
113 void secureStateChanged(bool);
117 class PeerMessageEvent;
123 static const QMetaObject *metaObject(const QObject *obj);
125 void removePeer(Peer *peer);
126 void removeAllPeers();
129 void dispatch(const T &protoMessage);
131 void handle(Peer *peer, const Protocol::SyncMessage &syncMessage);
132 void handle(Peer *peer, const Protocol::RpcCall &rpcCall);
133 void handle(Peer *peer, const Protocol::InitRequest &initRequest);
134 void handle(Peer *peer, const Protocol::InitData &initData);
137 void handle(Peer *peer, T) { Q_ASSERT(0); }
139 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue);
140 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
142 void requestInit(SyncableObject *obj);
143 QVariantMap initData(SyncableObject *obj) const;
144 void setInitData(SyncableObject *obj, const QVariantMap &properties);
146 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
150 // containg a list of argtypes for fast access
151 QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
153 // SignalRelay for all manually attached signals
154 SignalRelay *_signalRelay;
156 // RPC function -> (object, slot ID)
157 typedef QPair<QObject *, int> MethodId;
158 typedef QMultiHash<QByteArray, MethodId> SlotHash;
159 SlotHash _attachedSlots;
162 typedef QHash<QString, SyncableObject *> ObjectId;
163 QHash<QByteArray, ObjectId> _syncSlave;
165 ProxyMode _proxyMode;
166 int _heartBeatInterval;
167 int _maxHeartBeatCount;
169 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
171 friend class SignalRelay;
172 friend class SyncableObject;
177 // ==================================================
178 // ExtendedMetaObject
179 // ==================================================
180 class SignalProxy::ExtendedMetaObject
182 class MethodDescriptor
185 MethodDescriptor(const QMetaMethod &method);
186 MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
188 inline const QByteArray &methodName() const { return _methodName; }
189 inline const QList<int> &argTypes() const { return _argTypes; }
190 inline int returnType() const { return _returnType; }
191 inline int minArgCount() const { return _minArgCount; }
192 inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
195 QByteArray _methodName;
196 QList<int> _argTypes;
199 SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
204 ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
206 inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
207 inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
208 inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
209 inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
210 inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
212 inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
214 inline int updatedRemotelyId() { return _updatedRemotelyId; }
216 inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
217 const QHash<int, int> &receiveMap();
219 const QMetaObject *metaObject() const { return _meta; }
221 static QByteArray methodName(const QMetaMethod &method);
222 static QString methodBaseName(const QMetaMethod &method);
225 const MethodDescriptor &methodDescriptor(int methodId);
227 const QMetaObject *_meta;
228 int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
230 QHash<int, MethodDescriptor> _methods;
231 QHash<QByteArray, int> _methodIds;
232 QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y