Say hello to compression!
[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 public:
41   enum ProxyMode {
42     Server,
43     Client
44   };
45
46   enum RequestType {
47     Sync = 1,
48     RpcCall,
49     InitRequest,
50     InitData,
51     HeartBeat
52   };
53
54   SignalProxy(QObject *parent);
55   SignalProxy(ProxyMode mode, QObject *parent);
56   SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
57   virtual ~SignalProxy();
58
59   void setProxyMode(ProxyMode mode);
60   ProxyMode proxyMode() const;
61
62   bool addPeer(QIODevice *iodev);
63   void removePeer(QIODevice *iodev = 0);
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
70   void setInitialized(SyncableObject *obj);
71   bool isInitialized(SyncableObject *obj) const;
72   void requestInit(SyncableObject *obj);
73
74   void detachObject(QObject *obj);
75   void detachSignals(QObject *sender);
76   void detachSlots(QObject *receiver);
77   void stopSync(SyncableObject *obj);
78
79   //! Writes a QVariant to a device.
80   /** The data item is prefixed with the resulting blocksize,
81    *  so the corresponding function readDataFromDevice() can check if enough data is available
82    *  at the device to reread the item.
83    */
84   static void writeDataToDevice(QIODevice *dev, const QVariant &item, bool compressed = false);
85
86   //! Reads a data item from a device that has been written by writeDataToDevice().
87   /** If not enough data bytes are available, the function returns false and the QVariant reference
88    *  remains untouched.
89    */
90   static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed = false);
91
92   static QString methodBaseName(const QMetaMethod &method);
93
94   const QList<int> &argTypes(QObject *obj, int methodId);
95   const int &returnType(QObject *obj, int methodId);
96   const int &minArgCount(QObject *obj, int methodId);
97   const QByteArray &methodName(QObject *obj, int methodId);
98   const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
99   const QHash<int, int> &receiveMap(SyncableObject *obj);
100   int updatedRemotelyId(SyncableObject *obj);
101
102   typedef QHash<int, QList<int> > ArgHash;
103   typedef QHash<int, QByteArray> MethodNameHash;
104   struct ClassInfo {
105     ArgHash argTypes;
106     QHash<int, int> returnType;
107     QHash<int, int> minArgCount;
108     MethodNameHash methodNames;
109     int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
110     QHash<QByteArray, int> syncMap;
111     QHash<int, int> receiveMap;
112   };
113
114   void dumpProxyStats();
115   
116 private slots:
117   void dataAvailable();
118   void detachSender();
119   void removePeerBySender();
120   void objectRenamed(const QString &newname, const QString &oldname);
121   void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
122   void sendHeartBeat();
123
124 signals:
125   void peerRemoved(QIODevice *obj);
126   void connected();
127   void disconnected();
128   void objectInitialized(SyncableObject *);
129   
130 private:
131   void initServer();
132   void initClient();
133   
134   const QMetaObject *metaObject(QObject *obj);
135   void createClassInfo(QObject *obj);
136   void setArgTypes(QObject *obj, int methodId);
137   void setReturnType(QObject *obj, int methodId);
138   void setMinArgCount(QObject *obj, int methodId);
139   void setMethodName(QObject *obj, int methodId);
140   void setSyncMap(SyncableObject *obj);
141   void setReceiveMap(SyncableObject *obj);
142   void setUpdatedRemotelyId(SyncableObject *obj);
143
144   bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
145
146   void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList &params);
147   void dispatchSignal(const RequestType &requestType, const QVariantList &params);
148   
149   void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc);
150   void handleSync(QIODevice *sender, QVariantList params);
151   void handleInitRequest(QIODevice *sender, const QVariantList &params);
152   void handleInitData(QIODevice *sender, const QVariantList &params);
153   void handleSignal(const QByteArray &funcName, const QVariantList &params);
154
155   bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params, QVariant &returnValue);
156   bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params = QVariantList());
157
158   QVariantMap initData(SyncableObject *obj) const;
159   void setInitData(SyncableObject *obj, const QVariantMap &properties);
160
161 public:
162   void dumpSyncMap(SyncableObject *object);
163   
164 private:
165   // Hash of used QIODevices
166   struct peerInfo {
167     quint32 byteCount;
168     bool usesCompression;
169     peerInfo() : byteCount(0), usesCompression(false) {};
170   };
171   //QHash<QIODevice*, peerInfo> _peerByteCount;
172   QHash<QIODevice*, peerInfo> _peers;
173
174   // containg a list of argtypes for fast access
175   QHash<const QMetaObject *, ClassInfo*> _classInfo;
176
177   // we use one SignalRelay per QObject
178   QHash<QObject*, SignalRelay *> _relayHash;
179
180   // RPC function -> (object, slot ID)
181   typedef QPair<QObject*, int> MethodId;
182   typedef QMultiHash<QByteArray, MethodId> SlotHash;
183   SlotHash _attachedSlots;
184
185   // slaves for sync
186   typedef QHash<QString, SyncableObject *> ObjectId;
187   QHash<QByteArray, ObjectId> _syncSlave;
188
189
190   ProxyMode _proxyMode;
191   QTimer _heartBeatTimer;
192   
193   friend class SignalRelay;
194 };
195
196 #endif