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