1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #ifndef _SIGNALPROXY_H_
22 #define _SIGNALPROXY_H_
27 #include <QVariantMap>
37 class SignalProxy : public QObject {
54 SignalProxy(QObject *parent);
55 SignalProxy(ProxyMode mode, QObject *parent);
56 SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
57 virtual ~SignalProxy();
59 void setProxyMode(ProxyMode mode);
60 ProxyMode proxyMode() const;
62 bool addPeer(QIODevice *iodev);
63 void removePeer(QIODevice *iodev = 0);
65 bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
66 bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
68 void synchronize(SyncableObject *obj);
70 void setInitialized(SyncableObject *obj);
71 bool isInitialized(SyncableObject *obj) const;
72 void requestInit(SyncableObject *obj);
74 void detachObject(QObject *obj);
75 void detachSignals(QObject *sender);
76 void detachSlots(QObject *receiver);
77 void stopSync(SyncableObject *obj);
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.
84 static void writeDataToDevice(QIODevice *dev, const QVariant &item);
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
90 static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item);
92 static QString methodBaseName(const QMetaMethod &method);
94 const QList<int> &argTypes(QObject *obj, int methodId);
95 const int &returnType(QObject *obj, int methodId);
96 const int &minArgCount(QObject *obj, int methodId);
97 const QByteArray &methodName(QObject *obj, int methodId);
98 const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
99 const QHash<int, int> &receiveMap(SyncableObject *obj);
100 int updatedRemotelyId(SyncableObject *obj);
102 typedef QHash<int, QList<int> > ArgHash;
103 typedef QHash<int, QByteArray> MethodNameHash;
106 QHash<int, int> returnType;
107 QHash<int, int> minArgCount;
108 MethodNameHash methodNames;
109 int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
110 QHash<QByteArray, int> syncMap;
111 QHash<int, int> receiveMap;
114 void dumpProxyStats();
117 void dataAvailable();
119 void removePeerBySender();
120 void objectRenamed(const QString &newname, const QString &oldname);
121 void objectRenamed(const QByteArray &classname, const QString &newname, const QString &oldname);
122 void sendHeartBeat();
125 void peerRemoved(QIODevice *obj);
128 void objectInitialized(SyncableObject *);
134 const QMetaObject *metaObject(QObject *obj);
135 void createClassInfo(QObject *obj);
136 void setArgTypes(QObject *obj, int methodId);
137 void setReturnType(QObject *obj, int methodId);
138 void setMinArgCount(QObject *obj, int methodId);
139 void setMethodName(QObject *obj, int methodId);
140 void setSyncMap(SyncableObject *obj);
141 void setReceiveMap(SyncableObject *obj);
142 void setUpdatedRemotelyId(SyncableObject *obj);
144 bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
146 void dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList ¶ms);
147 void dispatchSignal(const RequestType &requestType, const QVariantList ¶ms);
149 void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc);
150 void handleSync(QIODevice *sender, QVariantList params);
151 void handleInitRequest(QIODevice *sender, const QVariantList ¶ms);
152 void handleInitData(QIODevice *sender, const QVariantList ¶ms);
153 void handleSignal(const QByteArray &funcName, const QVariantList ¶ms);
155 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms, QVariant &returnValue);
156 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
158 QVariantMap initData(SyncableObject *obj) const;
159 void setInitData(SyncableObject *obj, const QVariantMap &properties);
162 void dumpSyncMap(SyncableObject *object);
165 // Hash of used QIODevices
166 QHash<QIODevice*, quint32> _peerByteCount;
168 // containg a list of argtypes for fast access
169 QHash<const QMetaObject *, ClassInfo*> _classInfo;
171 // we use one SignalRelay per QObject
172 QHash<QObject*, SignalRelay *> _relayHash;
174 // RPC function -> (object, slot ID)
175 typedef QPair<QObject*, int> MethodId;
176 typedef QMultiHash<QByteArray, MethodId> SlotHash;
177 SlotHash _attachedSlots;
180 typedef QHash<QString, SyncableObject *> ObjectId;
181 QHash<QByteArray, ObjectId> _syncSlave;
184 ProxyMode _proxyMode;
185 QTimer _heartBeatTimer;
187 friend class SignalRelay;