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