GammaRay  2.4.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 <QMetaMethod>
36 #include <QObject>
37 #include <QPointer>
38 
39 class QIODevice;
40 class QUrl;
41 
42 namespace GammaRay {
43 
44 class Message;
45 class PropertySyncer;
46 
53 class GAMMARAY_COMMON_EXPORT Endpoint : public QObject
54 {
55  Q_OBJECT
56 public:
57  ~Endpoint();
58 
60  static void send(const Message &msg);
61 
63  static bool isConnected();
64 
65  static quint16 defaultPort();
66  static quint16 broadcastPort();
67 
69  Protocol::ObjectAddress objectAddress(const QString &objectName) const;
70 
72  static Endpoint* instance();
73 
77  virtual Protocol::ObjectAddress registerObject(const QString &name, QObject *object);
78 
88  virtual void invokeObject(const QString &objectName, const char *method, const QVariantList &args = QVariantList()) const;
89 
95  void waitForMessagesWritten();
96 
100  QString label() const;
101 
105  void setLabel(const QString &label);
106 
110  virtual bool isRemoteClient() const = 0;
111 
116  virtual QUrl serverAddress() const = 0;
117 
121  virtual void registerMessageHandler(Protocol::ObjectAddress objectAddress, QObject *receiver, const char* messageHandlerName);
122 
124  virtual void unregisterMessageHandler(Protocol::ObjectAddress objectAddress);
125 
126 public slots:
128  void sendMessage(const GammaRay::Message &msg);
129 
130 signals:
132  void disconnected();
133 
135  void objectRegistered(const QString &objectName, Protocol::ObjectAddress objectAddress);
136  void objectUnregistered(const QString &objectName, Protocol::ObjectAddress objectAddress);
137 
138 protected:
139  Endpoint(QObject* parent = 0);
141  void setDevice(QIODevice* device);
142 
144  Protocol::ObjectAddress endpointAddress() const;
145 
149  virtual void messageReceived(const Message &msg) = 0;
150 
152  void addObjectNameAddressMapping(const QString &objectName, Protocol::ObjectAddress objectAddress);
154  void removeObjectNameAddressMapping(const QString& objectName);
155 
157  virtual void handlerDestroyed(Protocol::ObjectAddress objectAddress, const QString &objectName) = 0;
158 
160  virtual void objectDestroyed(Protocol::ObjectAddress objectAddress, const QString &objectName, QObject *object) = 0;
161 
163  void dispatchMessage(const GammaRay::Message& msg);
164 
166  QVector<QPair<Protocol::ObjectAddress, QString> > objectAddresses() const;
167 
170 
176  void invokeObjectLocal(QObject *object, const char *method, const QVariantList &args) const;
177 
178  PropertySyncer *m_propertySyncer;
179 
180 private slots:
181  void readyRead();
182  void connectionClosed();
183  void handlerDestroyed(QObject* obj);
184  void objectDestroyed(QObject* obj);
185 
186 private:
187  struct ObjectInfo
188  {
189  ObjectInfo()
190  : address(Protocol::InvalidObjectAddress)
191  , object(0)
192  , receiver(0)
193  {
194  }
195  QString name;
196  Protocol::ObjectAddress address;
197  // the locally registered object
198  QObject *object;
199 
200  // custom message handling support
201  QObject *receiver;
202  QMetaMethod 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:169
Definition: endpoint.h:42
Network protocol endpoint.
Definition: endpoint.h:53

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/