e5bd0f6c13326a1b6707abd1f876628529efd264
[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 class 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);
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);
91
92   static QString methodBaseName(const QMetaMethod &method);
93
94   const QList<int> &argTypes(QObject *obj, int methodId);
95   const int &minArgCount(QObject *obj, int methodId);
96   const QByteArray &methodName(QObject *obj, int methodId);
97   const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
98   int updatedRemotelyId(SyncableObject *obj);
99
100   typedef QHash<int, QList<int> > ArgHash;
101   typedef QHash<int, QByteArray> MethodNameHash;
102   struct ClassInfo {
103     ArgHash argTypes;
104     QHash<int, int> minArgCount;
105     MethodNameHash methodNames;
106     int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
107     QHash<QByteArray, int> syncMap;
108   };
109
110   void dumpProxyStats();
111   
112 private slots:
113   void dataAvailable();
114   void detachSender();
115   void removePeerBySender();
116   void objectRenamed(QString oldname, QString newname);
117   void objectRenamed(QByteArray classname, QString oldname, QString newname);
118   void sendHeartBeat();
119
120 signals:
121   void peerRemoved(QIODevice *obj);
122   void connected();
123   void disconnected();
124   void objectInitialized(SyncableObject *);
125   
126 private:
127   void initServer();
128   void initClient();
129   
130   void createClassInfo(QObject *obj);
131   void setArgTypes(QObject *obj, int methodId);
132   void setMinArgCount(QObject *obj, int methodId);
133   void setMethodName(QObject *obj, int methodId);
134   void setSyncMap(SyncableObject *obj);
135   void setUpdatedRemotelyId(QObject *obj);
136
137   bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
138
139   void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList &params);
140   void dispatchSignal(const RequestType &requestType, const QVariantList &params);
141   
142   void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc);
143   void handleSync(QVariantList params);
144   void handleInitRequest(QIODevice *sender, const QVariantList &params);
145   void handleInitData(QIODevice *sender, const QVariantList &params);
146   void handleSignal(const QByteArray &funcName, const QVariantList &params);
147
148   bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params = QVariantList());
149
150   QVariantMap initData(SyncableObject *obj) const;
151   void setInitData(SyncableObject *obj, const QVariantMap &properties);
152
153 public:
154   void dumpSyncMap(SyncableObject *object);
155   
156 private:
157   // Hash of used QIODevices
158   QHash<QIODevice*, quint32> _peerByteCount;
159
160   // containg a list of argtypes for fast access
161   QHash<const QMetaObject *, ClassInfo*> _classInfo;
162
163   // we use one SignalRelay per QObject
164   QHash<QObject*, SignalRelay *> _relayHash;
165
166   // RPC function -> (object, slot ID)
167   typedef QPair<QObject*, int> MethodId;
168   typedef QMultiHash<QByteArray, MethodId> SlotHash;
169   SlotHash _attachedSlots;
170
171   // slaves for sync
172   typedef QHash<QString, SyncableObject *> ObjectId;
173   QHash<QByteArray, ObjectId> _syncSlave;
174
175
176   ProxyMode _proxyMode;
177   QTimer _heartBeatTimer;
178   
179   friend class SignalRelay;
180 };
181
182 #endif