silence some warnings
[quassel.git] / src / common / signalproxy.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #ifndef SIGNALPROXY_H
22 #define SIGNALPROXY_H
23
24 #include <QEvent>
25 #include <QSet>
26
27 #include "protocol.h"
28
29 struct QMetaObject;
30
31 class Peer;
32 class SyncableObject;
33
34 class SignalProxy : public QObject
35 {
36     Q_OBJECT
37
38     class SignalRelay;
39
40 public:
41     enum ProxyMode {
42         Server,
43         Client
44     };
45
46     enum EventType {
47         RemovePeerEvent = QEvent::User
48     };
49
50     SignalProxy(QObject *parent);
51     SignalProxy(ProxyMode mode, QObject *parent);
52     virtual ~SignalProxy();
53
54     void setProxyMode(ProxyMode mode);
55     inline ProxyMode proxyMode() const { return _proxyMode; }
56
57     void setHeartBeatInterval(int secs);
58     inline int heartBeatInterval() const { return _heartBeatInterval; }
59     void setMaxHeartBeatCount(int max);
60     inline int maxHeartBeatCount() const { return _maxHeartBeatCount; }
61
62     bool addPeer(Peer *peer);
63
64     bool attachSignal(QObject *sender, const char *signal, const QByteArray &sigName = QByteArray());
65     bool attachSlot(const QByteArray &sigName, QObject *recv, const char *slot);
66
67     void synchronize(SyncableObject *obj);
68     void stopSynchronize(SyncableObject *obj);
69
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); }
75
76     bool isSecure() const { return _secure; }
77     void dumpProxyStats();
78     void dumpSyncMap(SyncableObject *object);
79     inline int peerCount() const { return _peers.size(); }
80
81 public slots:
82     void detachObject(QObject *obj);
83     void detachSignals(QObject *sender);
84     void detachSlots(QObject *receiver);
85
86 protected:
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);
90
91 private slots:
92     void removePeerBySender();
93     void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
94     void updateSecureState();
95
96 signals:
97     void peerRemoved(Peer *peer);
98     void connected();
99     void disconnected();
100     void objectInitialized(SyncableObject *);
101     void heartBeatIntervalChanged(int secs);
102     void maxHeartBeatCountChanged(int max);
103     void lagUpdated(int lag);
104     void secureStateChanged(bool);
105
106 private:
107     template<class T>
108     class PeerMessageEvent;
109
110     void init();
111     void initServer();
112     void initClient();
113
114     static const QMetaObject *metaObject(const QObject *obj);
115
116     void removePeer(Peer *peer);
117     void removeAllPeers();
118
119     template<class T>
120     void dispatch(const T &protoMessage);
121     template<class T>
122     void dispatch(Peer *peer, const T &protoMessage);
123
124     void handle(Peer *peer, const Protocol::SyncMessage &syncMessage);
125     void handle(Peer *peer, const Protocol::RpcCall &rpcCall);
126     void handle(Peer *peer, const Protocol::InitRequest &initRequest);
127     void handle(Peer *peer, const Protocol::InitData &initData);
128
129     template<class T>
130     void handle(Peer *, T) { Q_ASSERT(0); }
131
132     bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params, QVariant &returnValue, Peer *peer = 0);
133     bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params = QVariantList(), Peer *peer = 0);
134
135     void requestInit(SyncableObject *obj);
136     QVariantMap initData(SyncableObject *obj) const;
137     void setInitData(SyncableObject *obj, const QVariantMap &properties);
138
139     static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
140
141     QSet<Peer *> _peers;
142
143     // containg a list of argtypes for fast access
144     QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
145
146     // SignalRelay for all manually attached signals
147     SignalRelay *_signalRelay;
148
149     // RPC function -> (object, slot ID)
150     typedef QPair<QObject *, int> MethodId;
151     typedef QMultiHash<QByteArray, MethodId> SlotHash;
152     SlotHash _attachedSlots;
153
154     // slaves for sync
155     typedef QHash<QString, SyncableObject *> ObjectId;
156     QHash<QByteArray, ObjectId> _syncSlave;
157
158     ProxyMode _proxyMode;
159     int _heartBeatInterval;
160     int _maxHeartBeatCount;
161
162     bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
163
164     friend class SignalRelay;
165     friend class SyncableObject;
166     friend class Peer;
167 };
168
169
170 // ==================================================
171 //  ExtendedMetaObject
172 // ==================================================
173 class SignalProxy::ExtendedMetaObject
174 {
175     class MethodDescriptor
176     {
177     public:
178         MethodDescriptor(const QMetaMethod &method);
179         MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
180
181         inline const QByteArray &methodName() const { return _methodName; }
182         inline const QList<int> &argTypes() const { return _argTypes; }
183         inline int returnType() const { return _returnType; }
184         inline int minArgCount() const { return _minArgCount; }
185         inline SignalProxy::ProxyMode receiverMode() const { return _receiverMode; }
186
187     private:
188         QByteArray _methodName;
189         QList<int> _argTypes;
190         int _returnType;
191         int _minArgCount;
192         SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
193     };
194
195
196 public:
197     ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
198
199     inline const QByteArray &methodName(int methodId) { return methodDescriptor(methodId).methodName(); }
200     inline const QList<int> &argTypes(int methodId) { return methodDescriptor(methodId).argTypes(); }
201     inline int returnType(int methodId) { return methodDescriptor(methodId).returnType(); }
202     inline int minArgCount(int methodId) { return methodDescriptor(methodId).minArgCount(); }
203     inline SignalProxy::ProxyMode receiverMode(int methodId) { return methodDescriptor(methodId).receiverMode(); }
204
205     inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
206
207     inline int updatedRemotelyId() { return _updatedRemotelyId; }
208
209     inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
210     const QHash<int, int> &receiveMap();
211
212     const QMetaObject *metaObject() const { return _meta; }
213
214     static QByteArray methodName(const QMetaMethod &method);
215     static QString methodBaseName(const QMetaMethod &method);
216
217 private:
218     const MethodDescriptor &methodDescriptor(int methodId);
219
220     const QMetaObject *_meta;
221     int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
222
223     QHash<int, MethodDescriptor> _methods;
224     QHash<QByteArray, int> _methodIds;
225     QHash<int, int> _receiveMap; // if slot x is called then hand over the result to slot y
226 };
227
228 #endif