GammaRay  2.2.0
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
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-2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8  Author: Volker Krause <volker.krause@kdab.com>
9 
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #ifndef GAMMARAY_ENDPOINT_H
25 #define GAMMARAY_ENDPOINT_H
26 
27 #include "gammaray_common_export.h"
28 #include "protocol.h"
29 
30 #include <QObject>
31 #include <QPointer>
32 
33 class QIODevice;
34 
35 namespace GammaRay {
36 
37 class Message;
38 
45 class GAMMARAY_COMMON_EXPORT Endpoint : public QObject
46 {
47  Q_OBJECT
48 public:
49  ~Endpoint();
50 
52  static void send(const Message &msg);
53 
55  static bool isConnected();
56 
57  static quint16 defaultPort();
58  static quint16 broadcastPort();
59 
61  Protocol::ObjectAddress objectAddress(const QString &objectName) const;
62 
64  static Endpoint* instance();
65 
69  virtual Protocol::ObjectAddress registerObject(const QString &name, QObject *object);
70 
80  virtual void invokeObject(const QString &objectName, const char *method, const QVariantList &args = QVariantList()) const;
81 
87  void waitForMessagesWritten();
88 
92  QString label() const;
93 
97  void setLabel(const QString &label);
98 
102  virtual bool isRemoteClient() const = 0;
103 
108  virtual QString serverAddress() const = 0;
109 
110 signals:
112  void disconnected();
113 
115  void objectRegistered(const QString &objectName, Protocol::ObjectAddress objectAddress);
116  void objectUnregistered(const QString &objectName, Protocol::ObjectAddress objectAddress);
117 
118 protected:
119  Endpoint(QObject* parent = 0);
121  void setDevice(QIODevice* device);
122 
124  Protocol::ObjectAddress endpointAddress() const;
125 
129  virtual void messageReceived(const Message &msg) = 0;
130 
132  void registerObjectInternal(const QString &objectName, Protocol::ObjectAddress objectAddress);
134  void unregisterObjectInternal(const QString& objectName);
135 
139  void registerMessageHandlerInternal(Protocol::ObjectAddress objectAddress, QObject *receiver, const char* messageHandlerName);
140 
142  void unregisterMessageHandlerInternal(Protocol::ObjectAddress objectAddress);
143 
145  virtual void handlerDestroyed(Protocol::ObjectAddress objectAddress, const QString &objectName) = 0;
146 
148  virtual void objectDestroyed(Protocol::ObjectAddress objectAddress, const QString &objectName, QObject *object) = 0;
149 
151  void dispatchMessage(const GammaRay::Message& msg);
152 
154  QVector<QPair<Protocol::ObjectAddress, QString> > objectAddresses() const;
155 
158 
164  void invokeObjectLocal(QObject *object, const char *method, const QVariantList &args) const;
165 
166 private slots:
167  void readyRead();
168  void connectionClosed();
169  void handlerDestroyed(QObject* obj);
170  void objectDestroyed(QObject* obj);
171 
172 private:
173  struct ObjectInfo
174  {
175  ObjectInfo()
176  : address(Protocol::InvalidObjectAddress)
177  , object(0)
178  , receiver(0)
179  {
180  }
181  QString name;
182  Protocol::ObjectAddress address;
183  // the locally registered object
184  QObject *object;
185 
186  // custom message handling support
187  // TODO: obsolete this
188  QObject *receiver;
189  QByteArray messageHandler;
190  };
191 
193  void insertObjectInfo(ObjectInfo *oi);
195  void removeObjectInfo(ObjectInfo *oi);
196 
197  QHash<QString, ObjectInfo*> m_nameMap;
198  QHash<Protocol::ObjectAddress, ObjectInfo*> m_addressMap;
199  QHash<QObject*, ObjectInfo*> m_objectMap;
200  QMultiHash<QObject*, ObjectInfo*> m_handlerMap;
201 
202  QPointer<QIODevice> m_socket;
203  Protocol::ObjectAddress m_myAddress;
204 
205  QString m_label;
206 };
207 
208 }
209 
210 #endif // GAMMARAY_ENDPOINT_H

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/