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