fix timestamp in Russian day change message
[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 <QEvent>
25 #include <QSet>
26
27 #include "protocol.h"
28
29 class 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
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);
126
127     template<class T>
128     void handle(Peer *peer, T) { Q_ASSERT(0); }
129
130     bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params, QVariant &returnValue);
131     bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params = QVariantList());
132
133     void requestInit(SyncableObject *obj);
134     QVariantMap initData(SyncableObject *obj) const;
135     void setInitData(SyncableObject *obj, const QVariantMap &properties);
136
137     static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
138
139     QSet<Peer *> _peers;
140
141     // containg a list of argtypes for fast access
142     QHash<const QMetaObject *, ExtendedMetaObject *> _extendedMetaObjects;
143
144     // SignalRelay for all manually attached signals
145     SignalRelay *_signalRelay;
146
147     // RPC function -> (object, slot ID)
148     typedef QPair<QObject *, int> MethodId;
149     typedef QMultiHash<QByteArray, MethodId> SlotHash;
150     SlotHash _attachedSlots;
151
152     // slaves for sync
153     typedef QHash<QString, SyncableObject *> ObjectId;
154     QHash<QByteArray, ObjectId> _syncSlave;
155
156     ProxyMode _proxyMode;
157     int _heartBeatInterval;
158     int _maxHeartBeatCount;
159
160     bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
161
162     friend class SignalRelay;
163     friend class SyncableObject;
164     friend class Peer;
165 };
166
167
168 // ==================================================
169 //  ExtendedMetaObject
170 // ==================================================
171 class SignalProxy::ExtendedMetaObject
172 {
173     class MethodDescriptor
174     {
175     public:
176         MethodDescriptor(const QMetaMethod &method);
177         MethodDescriptor() : _returnType(-1), _minArgCount(-1), _receiverMode(SignalProxy::Client) {}
178
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; }
184
185     private:
186         QByteArray _methodName;
187         QList<int> _argTypes;
188         int _returnType;
189         int _minArgCount;
190         SignalProxy::ProxyMode _receiverMode; // Only acceptable as a Sync Call if the receiving SignalProxy is in this mode.
191     };
192
193
194 public:
195     ExtendedMetaObject(const QMetaObject *meta, bool checkConflicts);
196
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(); }
202
203     inline int methodId(const QByteArray &methodName) { return _methodIds.contains(methodName) ? _methodIds[methodName] : -1; }
204
205     inline int updatedRemotelyId() { return _updatedRemotelyId; }
206
207     inline const QHash<QByteArray, int> &slotMap() { return _methodIds; }
208     const QHash<int, int> &receiveMap();
209
210     const QMetaObject *metaObject() const { return _meta; }
211
212     static QByteArray methodName(const QMetaMethod &method);
213     static QString methodBaseName(const QMetaMethod &method);
214
215 private:
216     const MethodDescriptor &methodDescriptor(int methodId);
217
218     const QMetaObject *_meta;
219     int _updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
220
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
224 };
225
226 #endif