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