Finaly got rid of the synchronizers, making Quassel quite a bit more lightweight...
[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) any later version.                                   *
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  *   SignalProxy has been inspired by QxtRPCPeer, part of libqxt,          *
21  *   the Qt eXTension Library <http://www.libqxt.org>. We would like to    *
22  *   thank Arvid "aep" Picciani and Adam "ahigerd" Higerd for providing    *
23  *   QxtRPCPeer, valuable input and the genius idea to (ab)use Qt's        *
24  *   Meta Object System for transmitting signals over the network.         *
25  *                                                                         *
26  *   To make contribution back into libqxt possible, redistribution and    *
27  *   modification of this file is additionally allowed under the terms of  *
28  *   the Common Public License, version 1.0, as published by IBM.          *
29  ***************************************************************************/
30
31 #ifndef _SIGNALPROXY_H_
32 #define _SIGNALPROXY_H_
33
34 #include <QList>
35 #include <QHash>
36 #include <QVariant>
37 #include <QVariantMap>
38 #include <QPair>
39 #include <QString>
40 #include <QByteArray>
41
42 class SignalRelay;
43 class QMetaObject;
44
45 class SignalProxy : public QObject {
46   Q_OBJECT
47
48 public:
49   enum ProxyMode {
50     Server,
51     Client
52   };
53
54   enum RequestType {
55     Sync = 0,
56     InitRequest,
57     InitData
58   };
59
60   SignalProxy(QObject *parent);
61   SignalProxy(ProxyMode mode, QObject *parent);
62   SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
63   virtual ~SignalProxy();
64
65   void setProxyMode(ProxyMode mode);
66   ProxyMode proxyMode() const;
67
68   bool addPeer(QIODevice *iodev);
69   void removePeer(QIODevice *iodev = 0);
70
71   bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
72   bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
73
74   void synchronize(QObject *obj);
75   void synchronizeAsMaster(QObject *obj);
76   void synchronizeAsSlave(QObject *obj);
77
78   void setInitialized(QObject *obj);
79   bool initialized(QObject *obj);
80   void requestInit(QObject *obj);
81   
82   void detachObject(QObject *obj);
83   void detachSignals(QObject *sender);
84   void detachSlots(QObject *receiver);
85   
86   static void writeDataToDevice(QIODevice *dev, const QVariant &item);
87   static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item);
88
89   static QString methodBaseName(const QMetaMethod &method);
90   
91   const QList<int> &argTypes(QObject *obj, int methodId);
92   const QByteArray &methodName(QObject *obj, int methodId);
93   const QHash<int, int> &syncMap(QObject *obj);
94
95   typedef QHash<int, QList<int> > ArgHash;
96   typedef QHash<int, QByteArray> MethodNameHash;
97   struct ClassInfo {
98     ArgHash argTypes;
99     MethodNameHash methodNames;
100     QHash<int, int> syncMap;
101   };
102
103   void dumpProxyStats();
104   
105 private slots:
106   void dataAvailable();
107   void detachSender();
108   void removePeerBySender();
109   void objectRenamed(QString oldname, QString newname);
110   void objectRenamed(QByteArray classname, QString oldname, QString newname);
111
112 signals:
113   void peerRemoved(QIODevice *obj);
114   void connected();
115   void disconnected();
116   
117 private:
118   void initServer();
119   void initClient();
120   
121   void createClassInfo(QObject *obj);
122   void setArgTypes(QObject *obj, int methodId);
123   void setMethodName(QObject *obj, int methodId);
124   void setSyncMap(QObject *obj);
125
126   bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
127
128   void dispatchSignal(QIODevice *receiver, const QVariant &identifier, const QVariantList &params);
129   void dispatchSignal(const QVariant &identifier, const QVariantList &params);
130   
131   void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc);
132   void handleSync(QVariantList params);
133   void handleInitRequest(QIODevice *sender, const QVariantList &params);
134   void handleInitData(QIODevice *sender, const QVariantList &params);
135   void handleSignal(const QByteArray &funcName, const QVariantList &params);
136
137   bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params);
138
139   QVariantMap initData(QObject *obj) const;
140   void setInitData(QObject *obj, const QVariantMap &properties);
141   bool setInitValue(QObject *obj, const QString &property, const QVariant &value);
142
143   void _detachSignals(QObject *sender);
144   void _detachSlots(QObject *receiver);
145
146   // containg a list of argtypes for fast access
147   QHash<const QMetaObject *, ClassInfo*> _classInfo;
148
149   // we use one SignalRelay per QObject
150   QHash<QObject*, SignalRelay *> _relayHash;
151
152   // RPC function -> (object, slot ID)
153   typedef QPair<QObject*, int> MethodId;
154   typedef QMultiHash<QByteArray, MethodId> SlotHash;
155   SlotHash _attachedSlots;
156
157   // slaves for sync
158   typedef QHash<QString, QObject *> ObjectId;
159   QHash<QByteArray, ObjectId> _syncSlave;
160
161   // Hash of used QIODevices
162   QHash<QIODevice*, quint32> _peerByteCount;
163
164   ProxyMode _proxyMode;
165
166   friend class SignalRelay;
167 };
168
169 #endif