File: endpoint.cpp

package info (click to toggle)
gammaray 3.1.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 24,796 kB
  • sloc: cpp: 109,174; ansic: 2,156; sh: 336; python: 274; yacc: 90; lex: 82; xml: 61; makefile: 28; javascript: 9; ruby: 5
file content (435 lines) | stat: -rw-r--r-- 12,256 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
  endpoint.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2013 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Volker Krause <volker.krause@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "endpoint.h"
#include "message.h"
#include "methodargument.h"
#include "propertysyncer.h"

#include <iostream>

#include <QIODevice>
#include <QLoggingCategory>
// we use qCWarning, which we turn off by default, but which is not compiled out in releasebuilds
Q_LOGGING_CATEGORY(networkstatistics, "gammaray.network.statistics", QtMsgType::QtCriticalMsg)

using namespace GammaRay;
using namespace std;

Endpoint *Endpoint::s_instance = nullptr;

Endpoint::Endpoint(QObject *parent)
    : QObject(parent)
    , m_propertySyncer(new PropertySyncer(this))
    , m_socket(nullptr)
    , m_myAddress(Protocol::InvalidObjectAddress + 1)
    , m_bytesRead(0)
    , m_bytesWritten(0)
    , m_pid(-1)
{
    if (s_instance) {
        qCritical(
            "Found existing GammaRay::Endpoint instance - trying to attach to a GammaRay client?");
    }

    Q_ASSERT(!s_instance);
    s_instance = this;

    auto *endpointObj = new ObjectInfo;
    endpointObj->address = m_myAddress;
    endpointObj->name = QStringLiteral("com.kdab.GammaRay.Server");
    // TODO: we could set this as message handler here and use the same dispatch mechanism
    insertObjectInfo(endpointObj);

    m_bandwidthMeasurementTimer = new QTimer(this);
    connect(m_bandwidthMeasurementTimer, &QTimer::timeout, this, &Endpoint::doLogTransmissionRate);
    m_bandwidthMeasurementTimer->start(1000);

    connect(m_propertySyncer, &PropertySyncer::message, this,
            &Endpoint::sendMessage);
}

Endpoint::~Endpoint()
{
    for (auto it = m_addressMap.constBegin(); it != m_addressMap.constEnd(); ++it) {
        delete it.value();
    }

    if (m_socket) {
        connectionClosed();
    }

    s_instance = nullptr;
}

Endpoint *Endpoint::instance()
{
    return s_instance;
}

void Endpoint::send(const Message &msg)
{
    Q_ASSERT(s_instance);
    s_instance->doSendMessage(msg);
}

void Endpoint::sendMessage(const Message &msg)
{
    if (!isConnected())
        return;

    doSendMessage(msg);
}

void Endpoint::doSendMessage(const GammaRay::Message &msg)
{
    Q_ASSERT(msg.address() != Protocol::InvalidObjectAddress);
    msg.write(m_socket);
    m_bytesWritten += msg.size();
}

void Endpoint::waitForMessagesWritten()
{
    m_socket->waitForBytesWritten(-1);
}

bool Endpoint::isConnected()
{
    return s_instance && s_instance->m_socket;
}

quint16 Endpoint::defaultPort()
{
    return 11732;
}

quint16 Endpoint::broadcastPort()
{
    return 13325;
}

void Endpoint::doLogTransmissionRate()
{
    emit logTransmissionRate(m_bytesRead, m_bytesWritten);

    if (!isRemoteClient()) {
        if (m_bytesRead != 0 || m_bytesWritten != 0) {
            const float transmissionRateRX = (m_bytesRead * 8 / 1024.0 / 1024.0); // in Mpbs
            const float transmissionRateTX = (m_bytesWritten * 8 / 1024.0 / 1024.0); // in Mpbs
            qCWarning(networkstatistics, "RX %7.3f Mbps | TX %7.3f Mbps", transmissionRateRX, transmissionRateTX);
        }
    }
    m_bytesRead = 0;
    m_bytesWritten = 0;
}

void Endpoint::setDevice(QIODevice *device)
{
    Q_ASSERT(!m_socket);
    Q_ASSERT(device);
    m_socket = device;
    connect(m_socket.data(), &QIODevice::readyRead, this, &Endpoint::readyRead);
    // FIXME Use proper type for m_socket, instead of relying on runtime-connect
    // to a slot which doesn't exist in QIODevice
    connect(m_socket.data(), SIGNAL(disconnected()), SLOT(connectionClosed()));
    if (m_socket->bytesAvailable())
        readyRead();
}

Protocol::ObjectAddress Endpoint::endpointAddress() const
{
    return m_myAddress;
}

void Endpoint::readyRead()
{
    while (Message::canReadMessage(m_socket.data())) {
        const auto msg = Message::readMessage(m_socket.data());
        m_bytesRead += msg.size();
        messageReceived(msg);
    }
}

void Endpoint::connectionClosed()
{
    disconnect(m_socket.data(), &QIODevice::readyRead, this, &Endpoint::readyRead);
    disconnect(m_socket.data(), SIGNAL(disconnected()), this, SLOT(connectionClosed()));
    m_socket = nullptr;
    emit disconnected();
}

Protocol::ObjectAddress Endpoint::objectAddress(const QString &objectName) const
{
    auto it = m_nameMap.constFind(objectName);
    if (it != m_nameMap.constEnd())
        return it.value()->address;

    return Protocol::InvalidObjectAddress;
}

Protocol::ObjectAddress Endpoint::registerObject(const QString &name, QObject *object)
{
    ObjectInfo *obj = m_nameMap.value(name, nullptr);
#if !defined(NDEBUG)
    Q_ASSERT(obj);
    Q_ASSERT(!obj->object);
    Q_ASSERT(obj->address != Protocol::InvalidObjectAddress);
#else
    if (!obj || obj->object || obj->address == Protocol::InvalidObjectAddress)
        return 0;
#endif

    obj->object = object;

    Q_ASSERT(!m_objectMap.contains(object));
    m_objectMap[object] = obj;

    connect(object, &QObject::destroyed, this, &Endpoint::slotObjectDestroyed);

    return obj->address;
}

void Endpoint::invokeObject(const QString &objectName, const char *method,
                            const QVariantList &args) const
{
    if (!isConnected())
        return;

    ObjectInfo *obj = m_nameMap.value(objectName, nullptr);
#if !defined(NDEBUG)
    Q_ASSERT(obj);
    Q_ASSERT(obj->address != Protocol::InvalidObjectAddress);
#else
    if (!obj || obj->address == Protocol::InvalidObjectAddress)
        return;
#endif

    Message msg(obj->address, Protocol::MethodCall);
    const QByteArray name(method);
    Q_ASSERT(!name.isEmpty());
    msg << name << args;
    send(msg);
}

void Endpoint::invokeObjectLocal(QObject *object, const char *method,
                                 const QVariantList &args)
{
    Q_ASSERT(args.size() <= 10);
    MethodArgument m[10] = {};
    QGenericArgument a[10] = {};
    for (int i = 0; i < args.size(); ++i) {
        m[i] = MethodArgument(args.at(i));
        a[i] = QGenericArgument(m[i]);
    }

    QMetaObject::invokeMethod(object, method, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8],
                              a[9]);
}

void Endpoint::addObjectNameAddressMapping(const QString &objectName,
                                           Protocol::ObjectAddress objectAddress)
{
    Q_ASSERT(objectAddress != Protocol::InvalidObjectAddress);

    auto *obj = new ObjectInfo;
    obj->address = objectAddress;
    obj->name = objectName;
    insertObjectInfo(obj);

    emit objectRegistered(objectName, objectAddress);
}

void Endpoint::removeObjectNameAddressMapping(const QString &objectName)
{
    Q_ASSERT(m_nameMap.contains(objectName));
    ObjectInfo *obj = m_nameMap.value(objectName);

    emit objectUnregistered(objectName, obj->address);
    removeObjectInfo(obj);
}

void Endpoint::registerMessageHandler(Protocol::ObjectAddress objectAddress, QObject *receiver,
                                      const char *messageHandlerName)
{
    Q_ASSERT(m_addressMap.contains(objectAddress));
    ObjectInfo *obj = m_addressMap.value(objectAddress);
    Q_ASSERT(obj);
    Q_ASSERT(!obj->receiver);
    Q_ASSERT(!obj->messageHandler.isValid());
    obj->receiver = receiver;

    QByteArray signature(messageHandlerName);
    signature += "(GammaRay::Message)";
    auto idx = receiver->metaObject()->indexOfMethod(signature);
    Q_ASSERT(idx >= 0);
    obj->messageHandler = receiver->metaObject()->method(idx);

    Q_ASSERT(!m_handlerMap.contains(receiver, obj));
    m_handlerMap.insert(receiver, obj);
    if (obj->receiver != obj->object)
        connect(receiver, &QObject::destroyed, this, &Endpoint::slotHandlerDestroyed);
}

void Endpoint::unregisterMessageHandler(Protocol::ObjectAddress objectAddress)
{
    Q_ASSERT(m_addressMap.contains(objectAddress));
    ObjectInfo *obj = m_addressMap.value(objectAddress);
    Q_ASSERT(obj);
    Q_ASSERT(obj->receiver);
    disconnect(obj->receiver, &QObject::destroyed, this,
               &Endpoint::slotHandlerDestroyed);
    m_handlerMap.remove(obj->receiver, obj);
    obj->receiver = nullptr;
    obj->messageHandler = QMetaMethod();
}

void Endpoint::slotObjectDestroyed(QObject *obj)
{
    ObjectInfo *info = m_objectMap.value(obj, nullptr);
#if !defined(NDEBUG)
    Q_ASSERT(info);
    Q_ASSERT(info->object == obj);
#else
    if (!info || info->object != obj)
        return;
#endif

    info->object = nullptr;
    m_objectMap.remove(obj);
    // copy the name, in case unregisterMessageHandlerInternal() is called inside
    objectDestroyed(info->address, QString(info->name), obj);
}

void Endpoint::slotHandlerDestroyed(QObject *obj)
{
    // copy, the virtual method below likely changes the maps.
    const QList<ObjectInfo *> objs = m_handlerMap.values(obj);
    m_handlerMap.remove(obj);
    for (ObjectInfo *obj : objs) {
        obj->receiver = nullptr;
        obj->messageHandler = QMetaMethod();
        // copy the name, in case unregisterMessageHandlerInternal() is called inside
        handlerDestroyed(obj->address, QString(obj->name));
    }
}

void Endpoint::dispatchMessage(const Message &msg)
{
    auto it = m_addressMap.constFind(msg.address());
    if (it == m_addressMap.constEnd()) {
        cerr << "message for unknown object address received: " << quint64(msg.address()) << endl;
        return;
    }

    ObjectInfo *obj = it.value();
    if (msg.type() == Protocol::MethodCall) {
        QByteArray method;
        msg >> method;
        if (obj->object) {
            Q_ASSERT(!method.isEmpty());
            QVariantList args;
            msg >> args;

            invokeObjectLocal(obj->object, method.constData(), args);
        } else {
            cerr << "cannot call method " << method.constData() << " on unknown object of name "
                 << qPrintable(obj->name) << " with address " << quint64(obj->address)
                 << " - did you forget to register it?" << endl;
        }
    }

    if (obj->receiver)
        obj->messageHandler.invoke(obj->receiver, Q_ARG(GammaRay::Message, msg));

    if (!obj->receiver && (msg.type() != Protocol::MethodCall || !obj->object)) {
        cerr << "Cannot dispatch message " << quint64(msg.type()) << " - no handler registered."
             << " Receiver: " << qPrintable(obj->name) << ", address " << quint64(obj->address)
             << endl;
    }
}

QVector<QPair<Protocol::ObjectAddress, QString>> Endpoint::objectAddresses() const
{
    QVector<QPair<Protocol::ObjectAddress, QString>> addrs;
    addrs.reserve(m_addressMap.size());
    for (auto it = m_addressMap.constBegin(); it != m_addressMap.constEnd(); ++it) {
        addrs.push_back(qMakePair(it.key(), it.value()->name));
    }
    return addrs;
}

void Endpoint::insertObjectInfo(Endpoint::ObjectInfo *oi)
{
    Q_ASSERT(!m_addressMap.contains(oi->address));
    m_addressMap.insert(oi->address, oi);
    Q_ASSERT(!m_nameMap.contains(oi->name));
    m_nameMap.insert(oi->name, oi);

    if (oi->receiver)
        m_handlerMap.insert(oi->receiver, oi);

    if (oi->object)
        m_objectMap.insert(oi->object, oi);
}

void Endpoint::removeObjectInfo(Endpoint::ObjectInfo *oi)
{
    Q_ASSERT(m_addressMap.contains(oi->address));
    m_addressMap.remove(oi->address);
    Q_ASSERT(m_nameMap.contains(oi->name));
    m_nameMap.remove(oi->name);

    if (oi->receiver) {
        disconnect(oi->receiver, &QObject::destroyed, this,
                   &Endpoint::slotHandlerDestroyed);
        m_handlerMap.remove(oi->receiver, oi);
    }

    if (oi->object) {
        disconnect(oi->object, &QObject::destroyed, this,
                   &Endpoint::slotObjectDestroyed);
        m_objectMap.remove(oi->object);
    }

    delete oi;
}

QString Endpoint::label() const
{
    return m_label;
}

void Endpoint::setLabel(const QString &label)
{
    m_label = label;
}

QString Endpoint::key() const
{
    return m_key;
}

void Endpoint::setKey(const QString &key)
{
    m_key = key;
}

qint64 Endpoint::pid() const
{
    return m_pid;
}

void Endpoint::setPid(qint64 pid)
{
    m_pid = pid;
}