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);
122 void handle(Peer *peer, const Protocol::SyncMessage &syncMessage);
123 void handle(Peer *peer, const Protocol::RpcCall &rpcCall);
124 void handle(Peer *peer, const Protocol::InitRequest &initRequest);
125 void handle(Peer *peer, const Protocol::InitData &initData);
128 void handle(Peer *peer, T) { Q_ASSERT(0); }
130 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue);
131 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
133 void requestInit(SyncableObject *obj);
134 QVariantMap initData(SyncableObject *obj) const;
135 void setInitData(SyncableObject *obj, const QVariantMap &properties);
137 static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
141 // containg a list of argtypes for fast access
142 QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
144 // SignalRelay for all manually attached signals
145 SignalRelay *_signalRelay;
147 // RPC function -> (object, slot ID)
148 typedef QPair<QObject *, int> MethodId;
149 typedef QMultiHash<QByteArray, MethodId> SlotHash;
150 SlotHash _attachedSlots;
153 typedef QHash<QString, SyncableObject *> ObjectId;
154 QHash<QByteArray, ObjectId> _syncSlave;
156 ProxyMode _proxyMode;
157 int _heartBeatInterval;
158 int _maxHeartBeatCount;
160 bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
162 friend class SignalRelay;
163 friend class SyncableObject;
168 // ==================================================
169 // ExtendedMetaObject
170 // ==================================================
171 class SignalProxy::ExtendedMetaObject
173 class MethodDescriptor
176 MethodDescriptor(const QMetaMethod &method);
177 MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
179 inline const QByteArray &methodName() const { return _methodName; }
180 inline const QList<int> &argTypes() const { return _argTypes; }
181 inline int returnType() const { return _returnType; }
182 inline int minArgCount() const { return _minArgCount; }
183 inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
186 QByteArray _methodName;
187 QList<int> _argTypes;
190 SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
195 ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
197 inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
198 inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
199 inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
200 inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
201 inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
203 inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
205 inline int updatedRemotelyId() { return _updatedRemotelyId; }
207 inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
208 const QHash<int, int> &receiveMap();
210 const QMetaObject *metaObject() const { return _meta; }
212 static QByteArray methodName(const QMetaMethod &method);
213 static QString methodBaseName(const QMetaMethod &method);
216 const MethodDescriptor &methodDescriptor(int methodId);
218 const QMetaObject *_meta;
219 int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
221 QHash<int, MethodDescriptor> _methods;
222 QHash<QByteArray, int> _methodIds;
223 QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y