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 ***************************************************************************/
34 class SignalProxy : public QObject
47 RemovePeerEvent = QEvent::User
50 SignalProxy(QObject *parent);
51 SignalProxy(ProxyMode mode, QObject *parent);
52 virtual ~SignalProxy();
54 void setProxyMode(ProxyMode mode);
55 inline ProxyMode proxyMode() const { return _proxyMode; }
57 void setHeartBeatInterval(int secs);
58 inline int heartBeatInterval() const { return _heartBeatInterval; }
59 void setMaxHeartBeatCount(int max);
60 inline int maxHeartBeatCount() const { return _maxHeartBeatCount; }
62 bool addPeer(Peer *peer);
64 bool attachSignal(QObject *sender, const char *signal, const QByteArray &sigName = QByteArray());
65 bool attachSlot(const QByteArray &sigName, QObject *recv, const char *slot);
67 void synchronize(SyncableObject *obj);
68 void stopSynchronize(SyncableObject *obj);
70 class ExtendedMetaObject;
71 ExtendedMetaObject *extendedMetaObject(const QMetaObject *meta) const;
72 ExtendedMetaObject *createExtendedMetaObject(const QMetaObject *meta, bool checkConflicts = false);
73 inline ExtendedMetaObject *extendedMetaObject(const QObject *obj) const { return extendedMetaObject(metaObject(obj)); }
74 inline ExtendedMetaObject *createExtendedMetaObject(const QObject *obj, bool checkConflicts = false) { return createExtendedMetaObject(metaObject(obj), checkConflicts); }
76 bool isSecure() const { return _secure; }
77 void dumpProxyStats();
78 void dumpSyncMap(SyncableObject *object);
79 inline int peerCount() const { return _peers.size(); }
82 void detachObject(QObject *obj);
83 void detachSignals(QObject *sender);
84 void detachSlots(QObject *receiver);
87 void customEvent(QEvent *event);
88 void sync_call__(const SyncableObject *obj, ProxyMode modeType, const char *funcname, va_list ap);
89 void renameObject(const SyncableObject *obj, const QString &newname, const QString &oldname);
92 void removePeerBySender();
93 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
94 void updateSecureState();
97 void peerRemoved(Peer *peer);
100 void objectInitialized(SyncableObject *);
101 void heartBeatIntervalChanged(int secs);
102 void maxHeartBeatCountChanged(int max);
103 void lagUpdated(int lag);
104 void secureStateChanged(bool);
108 class PeerMessageEvent;
114 static const QMetaObject *metaObject(const QObject *obj);
116 void removePeer(Peer *peer);
117 void removeAllPeers();
120 void dispatch(const T &protoMessage);
121 void dispatch(Peer *peer, const Protocol::RpcCall &rpcCall);
123 void handle(Peer *peer, const Protocol::SyncMessage &syncMessage);
124 void handle(Peer *peer, const Protocol::RpcCall &rpcCall);
125 void handle(Peer *peer, const Protocol::InitRequest &initRequest);
126 void handle(Peer *peer, const Protocol::InitData &initData);
129 void handle(Peer *, T) { Q_ASSERT(0); }
131 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue, Peer *peer = 0);
132 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList(), Peer *peer = 0);
134 void requestInit(SyncableObject *obj);
135 QVariantMap initData(SyncableObject *obj) const;
136 void setInitData(SyncableObject *obj, const QVariantMap &properties);
138 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
142 // containg a list of argtypes for fast access
143 QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
145 // SignalRelay for all manually attached signals
146 SignalRelay *_signalRelay;
148 // RPC function -> (object, slot ID)
149 typedef QPair<QObject *, int> MethodId;
150 typedef QMultiHash<QByteArray, MethodId> SlotHash;
151 SlotHash _attachedSlots;
154 typedef QHash<QString, SyncableObject *> ObjectId;
155 QHash<QByteArray, ObjectId> _syncSlave;
157 ProxyMode _proxyMode;
158 int _heartBeatInterval;
159 int _maxHeartBeatCount;
161 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
163 friend class SignalRelay;
164 friend class SyncableObject;
169 // ==================================================
170 // ExtendedMetaObject
171 // ==================================================
172 class SignalProxy::ExtendedMetaObject
174 class MethodDescriptor
177 MethodDescriptor(const QMetaMethod &method);
178 MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
180 inline const QByteArray &methodName() const { return _methodName; }
181 inline const QList<int> &argTypes() const { return _argTypes; }
182 inline int returnType() const { return _returnType; }
183 inline int minArgCount() const { return _minArgCount; }
184 inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
187 QByteArray _methodName;
188 QList<int> _argTypes;
191 SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
196 ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
198 inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
199 inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
200 inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
201 inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
202 inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
204 inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
206 inline int updatedRemotelyId() { return _updatedRemotelyId; }
208 inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
209 const QHash<int, int> &receiveMap();
211 const QMetaObject *metaObject() const { return _meta; }
213 static QByteArray methodName(const QMetaMethod &method);
214 static QString methodBaseName(const QMetaMethod &method);
217 const MethodDescriptor &methodDescriptor(int methodId);
219 const QMetaObject *_meta;
220 int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
222 QHash<int, MethodDescriptor> _methods;
223 QHash<QByteArray, int> _methodIds;
224 QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y