1 /***************************************************************************
2 * Copyright (C) 2005-2012 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>
42 class SignalProxy : public QObject
57 RemovePeerEvent = QEvent::User
60 SignalProxy(QObject *parent);
61 SignalProxy(ProxyMode mode, QObject *parent);
62 virtual ~SignalProxy();
64 void setProxyMode(ProxyMode mode);
65 inline ProxyMode proxyMode() const { return _proxyMode; }
67 void setHeartBeatInterval(int secs);
68 inline int heartBeatInterval() const { return _heartBeatInterval; }
69 void setMaxHeartBeatCount(int max);
70 inline int maxHeartBeatCount() const { return _maxHeartBeatCount; }
72 bool addPeer(AbstractPeer *peer);
74 bool attachSignal(QObject *sender, const char *signal, const QByteArray &sigName = QByteArray());
75 bool attachSlot(const QByteArray &sigName, QObject *recv, const char *slot);
77 void synchronize(SyncableObject *obj);
78 void stopSynchronize(SyncableObject *obj);
80 class ExtendedMetaObject;
81 ExtendedMetaObject *extendedMetaObject(const QMetaObject *meta) const;
82 ExtendedMetaObject *createExtendedMetaObject(const QMetaObject *meta, bool checkConflicts = false);
83 inline ExtendedMetaObject *extendedMetaObject(const QObject *obj) const { return extendedMetaObject(metaObject(obj)); }
84 inline ExtendedMetaObject *createExtendedMetaObject(const QObject *obj, bool checkConflicts = false) { return createExtendedMetaObject(metaObject(obj), checkConflicts); }
86 bool isSecure() const { return _secure; }
87 void dumpProxyStats();
88 void dumpSyncMap(SyncableObject *object);
89 inline int peerCount() const { return _peers.size(); }
92 void detachObject(QObject *obj);
93 void detachSignals(QObject *sender);
94 void detachSlots(QObject *receiver);
97 void customEvent(QEvent *event);
98 void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
99 void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
102 void removePeerBySender();
103 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
104 void updateSecureState();
107 void peerRemoved(SignalProxy::AbstractPeer *peer);
110 void objectInitialized(SyncableObject *);
111 void heartBeatIntervalChanged(int secs);
112 void maxHeartBeatCountChanged(int max);
113 void lagUpdated(int lag);
114 void secureStateChanged(bool);
118 class PeerMessageEvent;
124 static const QMetaObject *metaObject(const QObject *obj);
126 void removePeer(AbstractPeer *peer);
127 void removeAllPeers();
130 void dispatch(const T &protoMessage);
132 void handle(AbstractPeer *peer, const Protocol::SyncMessage &syncMessage);
133 void handle(AbstractPeer *peer, const Protocol::RpcCall &rpcCall);
134 void handle(AbstractPeer *peer, const Protocol::InitRequest &initRequest);
135 void handle(AbstractPeer *peer, const Protocol::InitData &initData);
137 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue);
138 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
140 void requestInit(SyncableObject *obj);
141 QVariantMap initData(SyncableObject *obj) const;
142 void setInitData(SyncableObject *obj, const QVariantMap &properties);
144 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
146 QSet<AbstractPeer *> _peers;
148 // containg a list of argtypes for fast access
149 QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
151 // SignalRelay for all manually attached signals
152 SignalRelay *_signalRelay;
154 // RPC function -> (object, slot ID)
155 typedef QPair<QObject *, int> MethodId;
156 typedef QMultiHash<QByteArray, MethodId> SlotHash;
157 SlotHash _attachedSlots;
160 typedef QHash<QString, SyncableObject *> ObjectId;
161 QHash<QByteArray, ObjectId> _syncSlave;
163 ProxyMode _proxyMode;
164 int _heartBeatInterval;
165 int _maxHeartBeatCount;
167 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
169 friend class SignalRelay;
170 friend class SyncableObject;
171 friend class InternalConnection;
172 friend class RemoteConnection;
176 // ==================================================
177 // ExtendedMetaObject
178 // ==================================================
179 class SignalProxy::ExtendedMetaObject
181 class MethodDescriptor
184 MethodDescriptor(const QMetaMethod &method);
185 MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
187 inline const QByteArray &methodName() const { return _methodName; }
188 inline const QList<int> &argTypes() const { return _argTypes; }
189 inline int returnType() const { return _returnType; }
190 inline int minArgCount() const { return _minArgCount; }
191 inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
194 QByteArray _methodName;
195 QList<int> _argTypes;
198 SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
203 ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
205 inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
206 inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
207 inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
208 inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
209 inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
211 inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
213 inline int updatedRemotelyId() { return _updatedRemotelyId; }
215 inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
216 const QHash<int, int> &receiveMap();
218 const QMetaObject *metaObject() const { return _meta; }
220 static QByteArray methodName(const QMetaMethod &method);
221 static QString methodBaseName(const QMetaMethod &method);
224 const MethodDescriptor &methodDescriptor(int methodId);
226 const QMetaObject *_meta;
227 int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
229 QHash<int, MethodDescriptor> _methods;
230 QHash<QByteArray, int> _methodIds;
231 QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y
235 // ==================================================
237 // ==================================================
238 class SignalProxy::AbstractPeer : public QObject
243 AbstractPeer(QObject *parent = 0) : QObject(parent) {}
245 virtual QString description() const = 0;
247 virtual void setSignalProxy(SignalProxy *proxy) = 0;
249 virtual bool isOpen() const = 0;
250 virtual bool isSecure() const = 0;
251 virtual bool isLocal() const = 0;
253 virtual QString errorString() const { return QString(); }
255 virtual int lag() const = 0;
258 virtual void dispatch(const Protocol::SyncMessage &msg) = 0;
259 virtual void dispatch(const Protocol::RpcCall &msg) = 0;
260 virtual void dispatch(const Protocol::InitRequest &msg) = 0;
261 virtual void dispatch(const Protocol::InitData &msg) = 0;
263 virtual void close(const QString &reason = QString()) = 0;
267 void error(QAbstractSocket::SocketError);
268 void secureStateChanged(bool secure = true);
269 void lagUpdated(int msecs);