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