GammaRay  2.3.0
endpoint.h
1 /*
2  endpoint.h
3 
4  This file is part of GammaRay, the Qt application inspection and
5  manipulation tool.
6 
7  Copyright (C) 2013-2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8  Author: Volker Krause <volker.krause@kdab.com>
9 
10  Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11  acuordance with GammaRay Commercial License Agreement provided with the Software.
12 
13  Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15  This program is free software; you can redistribute it and/or modify
16  it under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 2 of the License, or
18  (at your option) any later version.
19 
20  This program is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with this program. If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #ifndef GAMMARAY_ENDPOINT_H
30 #define GAMMARAY_ENDPOINT_H
31 
32 #include "gammaray_common_export.h"
33 #include "protocol.h"
34 
35 #include <QObject>
36 #include <QPointer>
37 
38 class QIODevice;
39 class QUrl;
40 
41 namespace GammaRay {
42 
43 class Message;
44 class PropertySyncer;
45 
52 class GAMMARAY_COMMON_EXPORT Endpoint : public QObject
53 {
54  Q_OBJECT
55 public:
56  ~Endpoint();
57 
59  static void send(const Message &msg);
60 
62  static bool isConnected();
63 
64  static quint16 defaultPort();
65  static quint16 broadcastPort();
66 
68  Protocol::ObjectAddress objectAddress(const QString &objectName) const;
69 
71  static Endpoint* instance();
72 
76  virtual Protocol::ObjectAddress registerObject(const QString &name, QObject *object);
77 
87  virtual void invokeObject(const QString &objectName, const char *method, const QVariantList &args = QVariantList()) const;
88 
94  void waitForMessagesWritten();
95 
99  QString label() const;
100 
104  void setLabel(const QString &label);
105 
109  virtual bool isRemoteClient() const = 0;
110 
115  virtual QUrl serverAddress() const = 0;
116 
117 public slots:
119  void sendMessage(const GammaRay::Message &msg);
120 
121 signals:
123  void disconnected();
124 
126  void objectRegistered(const QString &objectName, Protocol::ObjectAddress objectAddress);
127  void objectUnregistered(const QString &objectName, Protocol::ObjectAddress objectAddress);
128 
129 protected:
130  Endpoint(QObject* parent = 0);
132  void setDevice(QIODevice* device);
133 
135  Protocol::ObjectAddress endpointAddress() const;
136 
140  virtual void messageReceived(const Message &msg) = 0;
141 
143  void registerObjectInternal(const QString &objectName, Protocol::ObjectAddress objectAddress);
145  void unregisterObjectInternal(const QString& objectName);
146 
150  void registerMessageHandlerInternal(Protocol::ObjectAddress objectAddress, QObject *receiver, const char* messageHandlerName);
151 
153  void unregisterMessageHandlerInternal(Protocol::ObjectAddress objectAddress);
154 
156  virtual void handlerDestroyed(Protocol::ObjectAddress objectAddress, const QString &objectName) = 0;
157 
159  virtual void objectDestroyed(Protocol::ObjectAddress objectAddress, const QString &objectName, QObject *object) = 0;
160 
162  void dispatchMessage(const GammaRay::Message& msg);
163 
165  QVector<QPair<Protocol::ObjectAddress, QString> > objectAddresses() const;
166 
169 
175  void invokeObjectLocal(QObject *object, const char *method, const QVariantList &args) const;
176 
177  PropertySyncer *m_propertySyncer;
178 
179 private slots:
180  void readyRead();
181  void connectionClosed();
182  void handlerDestroyed(QObject* obj);
183  void objectDestroyed(QObject* obj);
184 
185 private:
186  struct ObjectInfo
187  {
188  ObjectInfo()
189  : address(Protocol::InvalidObjectAddress)
190  , object(0)
191  , receiver(0)
192  {
193  }
194  QString name;
195  Protocol::ObjectAddress address;
196  // the locally registered object
197  QObject *object;
198 
199  // custom message handling support
200  // TODO: obsolete this
201  QObject *receiver;
202  QByteArray messageHandler;
203  };
204 
206  void insertObjectInfo(ObjectInfo *oi);
208  void removeObjectInfo(ObjectInfo *oi);
209 
210  QHash<QString, ObjectInfo*> m_nameMap;
211  QHash<Protocol::ObjectAddress, ObjectInfo*> m_addressMap;
212  QHash<QObject*, ObjectInfo*> m_objectMap;
213  QMultiHash<QObject*, ObjectInfo*> m_handlerMap;
214 
215  QPointer<QIODevice> m_socket;
216  Protocol::ObjectAddress m_myAddress;
217 
218  QString m_label;
219 };
220 
221 }
222 
223 #endif // GAMMARAY_ENDPOINT_H
static Endpoint * s_instance
Definition: endpoint.h:168
Definition: endpoint.h:41
Network protocol endpoint.
Definition: endpoint.h:52

Klarälvdalens Datakonsult AB (KDAB)
"The Qt Experts"
http://www.kdab.com/
GammaRay
Qt-application inspection and manipulation tool
http://www.kdab.com/kdab-products/gammaray/