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