bufferviews are now saved periodically
[quassel.git] / src / common / signalproxy.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef SIGNALPROXY_H
22 #define SIGNALPROXY_H
23
24 #include <QEvent>
25 #include <QList>
26 #include <QHash>
27 #include <QVariant>
28 #include <QVariantMap>
29 #include <QPair>
30 #include <QString>
31 #include <QByteArray>
32 #include <QTimer>
33
34 class SignalRelay;
35 class SyncableObject;
36 struct QMetaObject;
37
38 class SignalProxy : public QObject {
39   Q_OBJECT
40
41   class AbstractPeer;
42   class IODevicePeer;
43
44 public:
45   enum ProxyMode {
46     Server,
47     Client
48   };
49
50   enum RequestType {
51     Sync = 1,
52     RpcCall,
53     InitRequest,
54     InitData,
55     HeartBeat,
56     HeartBeatReply
57   };
58
59   enum ClientConnectionType {
60     SignalProxyConnection,
61     IODeviceConnection
62   };
63
64   enum CustomEvents {
65     PeerSignal = QEvent::User,
66     RemovePeer
67   };
68
69   SignalProxy(QObject *parent);
70   SignalProxy(ProxyMode mode, QObject *parent);
71   SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
72   virtual ~SignalProxy();
73
74   void setProxyMode(ProxyMode mode);
75   inline ProxyMode proxyMode() const { return _proxyMode; }
76
77   bool addPeer(QIODevice *iodev);
78   bool addPeer(SignalProxy *proxy);
79   void removePeer(QObject *peer);
80   void removeAllPeers();
81   
82   bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
83   bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
84
85   void synchronize(SyncableObject *obj);
86
87 //   void setInitialized(SyncableObject *obj);
88 //   bool isInitialized(SyncableObject *obj) const;
89   void requestInit(SyncableObject *obj);
90
91   void detachObject(QObject *obj);
92   void detachSignals(QObject *sender);
93   void detachSlots(QObject *receiver);
94   void stopSync(SyncableObject *obj);
95
96   //! Writes a QVariant to a device.
97   /** The data item is prefixed with the resulting blocksize,
98    *  so the corresponding function readDataFromDevice() can check if enough data is available
99    *  at the device to reread the item.
100    */
101   static void writeDataToDevice(QIODevice *dev, const QVariant &item, bool compressed = false);
102
103   //! Reads a data item from a device that has been written by writeDataToDevice().
104   /** If not enough data bytes are available, the function returns false and the QVariant reference
105    *  remains untouched.
106    */
107   static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed = false);
108
109   bool isSecure() const { return _secure; }
110   
111   static QString methodBaseName(const QMetaMethod &method);
112
113   const QList<int> &argTypes(QObject *obj, int methodId);
114   const int &returnType(QObject *obj, int methodId);
115   const int &minArgCount(QObject *obj, int methodId);
116   const QByteArray &methodName(QObject *obj, int methodId);
117   const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
118   const QHash<int, int> &receiveMap(SyncableObject *obj);
119   int updatedRemotelyId(SyncableObject *obj);
120
121   typedef QHash<int, QList<int> > ArgHash;
122   typedef QHash<int, QByteArray> MethodNameHash;
123   struct ClassInfo {
124     ArgHash argTypes;
125     QHash<int, int> returnType;
126     QHash<int, int> minArgCount;
127     MethodNameHash methodNames;
128     int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
129     QHash<QByteArray, int> syncMap;
130     QHash<int, int> receiveMap;
131   };
132
133   void dumpProxyStats();
134   
135 protected:
136   void customEvent(QEvent *event);
137
138 private slots:
139   void dataAvailable();
140   void detachSender();
141   void removePeerBySender();
142   void objectRenamed(const QString &newname, const QString &oldname);
143   void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
144   void sendHeartBeat();
145   void receiveHeartBeat(AbstractPeer *peer, const QVariantList &params);
146   void receiveHeartBeatReply(AbstractPeer *peer, const QVariantList &params);
147
148   void updateSecureState();
149
150 signals:
151   void peerRemoved(QIODevice *dev);
152   void connected();
153   void disconnected();
154   void objectInitialized(SyncableObject *);
155   void lagUpdated(int lag);
156   void securityChanged(bool);
157   void secureStateChanged(bool);
158
159 private:
160   void init();
161   void initServer();
162   void initClient();
163   
164   const QMetaObject *metaObject(QObject *obj);
165   void createClassInfo(QObject *obj);
166   void setArgTypes(QObject *obj, int methodId);
167   void setReturnType(QObject *obj, int methodId);
168   void setMinArgCount(QObject *obj, int methodId);
169   void setMethodName(QObject *obj, int methodId);
170   void setSyncMap(SyncableObject *obj);
171   void setReceiveMap(SyncableObject *obj);
172   void setUpdatedRemotelyId(SyncableObject *obj);
173
174   bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
175
176   void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList &params);
177   void dispatchSignal(const RequestType &requestType, const QVariantList &params);
178
179   void receivePackedFunc(AbstractPeer *sender, const QVariant &packedFunc);
180   void receivePeerSignal(AbstractPeer *sender, const RequestType &requestType, const QVariantList &params);
181   void receivePeerSignal(SignalProxy *sender, const RequestType &requestType, const QVariantList &params);
182   void handleSync(AbstractPeer *sender, QVariantList params);
183   void handleInitRequest(AbstractPeer *sender, const QVariantList &params);
184   void handleInitData(AbstractPeer *sender, const QVariantList &params);
185   void handleSignal(const QVariantList &data);
186
187   bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params, QVariant &returnValue);
188   bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params = QVariantList());
189
190   QVariantMap initData(SyncableObject *obj) const;
191   void setInitData(SyncableObject *obj, const QVariantMap &properties);
192
193   void updateLag(IODevicePeer *peer, int lag);
194
195 public:
196   void dumpSyncMap(SyncableObject *object);
197   inline int peerCount() const { return _peers.size(); }
198   
199 private:
200   static void disconnectDevice(QIODevice *dev, const QString &reason = QString());
201
202   class AbstractPeer {
203   public:
204     enum PeerType {
205       NotAPeer = 0,
206       IODevicePeer = 1,
207       SignalProxyPeer = 2
208     };
209     AbstractPeer() : _type(NotAPeer) {}
210     AbstractPeer(PeerType type) : _type(type) {}
211     virtual ~AbstractPeer() {}
212     inline PeerType type() const { return _type; }
213     virtual void dispatchSignal(const RequestType &requestType, const QVariantList &params) = 0;
214     virtual bool isSecure() const = 0;
215   private:
216     PeerType _type;
217   };
218
219   class IODevicePeer : public AbstractPeer {
220   public:
221     IODevicePeer(QIODevice *device, bool compress) : AbstractPeer(AbstractPeer::IODevicePeer), _device(device), byteCount(0), usesCompression(compress), sentHeartBeats(0), lag(0) {}
222     virtual void dispatchSignal(const RequestType &requestType, const QVariantList &params);
223     virtual bool isSecure() const;
224     inline void dispatchPackedFunc(const QVariant &packedFunc) { SignalProxy::writeDataToDevice(_device, packedFunc, usesCompression); }
225     QString address() const;
226     inline bool isOpen() const { return _device->isOpen(); }
227     inline void close() const { _device->close(); }
228     inline bool readData(QVariant &item) { return SignalProxy::readDataFromDevice(_device, byteCount, item, usesCompression); }
229   private:
230     QIODevice *_device;
231     quint32 byteCount;
232     bool usesCompression;
233   public:
234     int sentHeartBeats;
235     int lag;
236   };
237
238   class SignalProxyPeer : public AbstractPeer {
239   public:
240     SignalProxyPeer(SignalProxy *sender, SignalProxy *receiver) : AbstractPeer(AbstractPeer::SignalProxyPeer), sender(sender), receiver(receiver) {}
241     virtual void dispatchSignal(const RequestType &requestType, const QVariantList &params);
242     virtual inline bool isSecure() const { return true; }
243   private:
244     SignalProxy *sender;
245     SignalProxy *receiver;
246   };
247
248   // a Hash of the actual used communication object to it's corresponding peer
249   // currently a communication object can either be an arbitrary QIODevice or another SignalProxy
250   typedef QHash<QObject *, AbstractPeer *> PeerHash;
251   PeerHash _peers;
252
253   // containg a list of argtypes for fast access
254   QHash<const QMetaObject *, ClassInfo*> _classInfo;
255
256   // we use one SignalRelay per QObject
257   QHash<QObject*, SignalRelay *> _relayHash;
258
259   // RPC function -> (object, slot ID)
260   typedef QPair<QObject*, int> MethodId;
261   typedef QMultiHash<QByteArray, MethodId> SlotHash;
262   SlotHash _attachedSlots;
263
264   // slaves for sync
265   typedef QHash<QString, SyncableObject *> ObjectId;
266   QHash<QByteArray, ObjectId> _syncSlave;
267
268
269   ProxyMode _proxyMode;
270   QTimer _heartBeatTimer;
271
272   bool _secure; // determines if all connections are in a secured state (using ssl or internal connections)
273   
274   friend class SignalRelay;
275 };
276
277 #endif