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
|
/*
server.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 <config-gammaray.h>
#include "server.h"
#include "serverdevice.h"
#include "probe.h"
#include "probesettings.h"
#include "multisignalmapper.h"
#include <common/protocol.h>
#include <common/message.h>
#include <common/propertysyncer.h>
#ifdef Q_OS_ANDROID
#include <QDir>
#endif
#include <QDebug>
#include <QIODevice>
#include <QTimer>
#include <QMetaMethod>
#include <iostream>
using namespace GammaRay;
using namespace std;
Server::Server(QObject *parent)
: Endpoint(parent)
, m_serverDevice(nullptr)
, m_nextAddress(endpointAddress())
, m_broadcastTimer(new QTimer(this))
, m_signalMapper(new MultiSignalMapper(this))
{
Message::resetNegotiatedDataVersion();
if (!ProbeSettings::value(QStringLiteral("RemoteAccessEnabled"), true).toBool())
return;
const auto serverAddress = serverAddress_impl();
m_serverDevice = ServerDevice::create(serverAddress, this);
if (!m_serverDevice)
return;
connect(m_serverDevice, &ServerDevice::newConnection, this, &Server::newConnection);
connect(m_serverDevice, &ServerDevice::externalAddressChanged, this, &Server::externalAddressChanged);
m_broadcastTimer->setInterval(5 * 1000);
m_broadcastTimer->setSingleShot(false);
if (serverAddress.scheme() == QLatin1String("tcp")) {
m_broadcastTimer->start();
}
connect(m_broadcastTimer, &QTimer::timeout, this, &Server::broadcast);
connect(this, &Server::disconnected, m_broadcastTimer, [this] { m_broadcastTimer->start(); });
connect(m_signalMapper, &MultiSignalMapper::signalEmitted,
this, &Server::forwardSignal);
Endpoint::addObjectNameAddressMapping(QStringLiteral(
"com.kdab.GammaRay.PropertySyncer"),
++m_nextAddress);
m_propertySyncer->setAddress(m_nextAddress);
Endpoint::registerObject(QStringLiteral("com.kdab.GammaRay.PropertySyncer"), m_propertySyncer);
registerMessageHandler(m_nextAddress, m_propertySyncer, "handleMessage");
}
Server::~Server() = default;
bool Server::listen()
{
Q_ASSERT(!m_serverDevice->isListening());
if (!m_serverDevice->listen()) {
return false;
}
return true;
}
bool Server::isListening() const
{
return m_serverDevice->isListening();
}
Server *Server::instance()
{
Q_ASSERT(s_instance);
return static_cast<Server *>(s_instance);
}
bool Server::isRemoteClient() const
{
return false;
}
QUrl Server::serverAddress() const
{
return serverAddress_impl();
}
QUrl Server::serverAddress_impl() const
{
#ifdef Q_OS_ANDROID
const QString defaultServerAddr = QLatin1String("local://") + QDir::homePath() + QLatin1String("/+gammaray_socket");
#else
const QString defaultServerAddr = QString::fromUtf8(GAMMARAY_DEFAULT_ANY_TCP_URL);
#endif
QUrl url(ProbeSettings::value(QStringLiteral("ServerAddress"), defaultServerAddr).toString());
if (url.scheme().isEmpty())
url.setScheme(QStringLiteral("tcp"));
if (url.port() <= 0)
url.setPort(defaultPort());
return url;
}
void Server::newConnection()
{
if (isConnected()) {
cerr << Q_FUNC_INFO << " connected already, refusing incoming connection." << endl;
auto con = m_serverDevice->nextPendingConnection();
con->close();
con->deleteLater();
return;
}
m_broadcastTimer->stop();
auto con = m_serverDevice->nextPendingConnection();
// FIXME Use proper type for m_serverDevice->nextPendingConnection, instead
// of relying on runtime-connect to a slot which doesn't exist in QIODevice
connect(con, SIGNAL(disconnected()), con, SLOT(deleteLater()));
setDevice(con);
sendServerGreeting();
emit connectionEstablished();
}
void Server::sendServerGreeting()
{
// send greeting message for protocol version check
{
Message msg(endpointAddress(), Protocol::ServerVersion);
msg << Protocol::version();
send(msg);
}
{
Message msg(endpointAddress(), Protocol::ServerInfo);
msg << label() << key() << pid() << Message::highestSupportedDataVersion(); // TODO: expand with anything else needed here: Qt/GammaRay version, hostname, that kind of stuff
send(msg);
}
{
Message msg(endpointAddress(), Protocol::ObjectMapReply);
msg << objectAddresses();
send(msg);
}
}
void Server::messageReceived(const Message &msg)
{
if (msg.address() == endpointAddress()) {
switch (msg.type()) {
case Protocol::ClientDataVersionNegotiated: {
quint8 version;
msg >> version;
{
Message msg(endpointAddress(), Protocol::ServerDataVersionNegotiated);
msg << version;
send(msg);
}
Message::setNegotiatedDataVersion(version);
break;
}
case Protocol::ObjectMonitored:
case Protocol::ObjectUnmonitored: {
Protocol::ObjectAddress addr;
msg >> addr;
Q_ASSERT(addr > Protocol::InvalidObjectAddress);
m_propertySyncer->setObjectEnabled(addr, msg.type() == Protocol::ObjectMonitored);
auto it = m_monitorNotifiers.constFind(addr);
if (it == m_monitorNotifiers.constEnd())
break;
// cout << Q_FUNC_INFO << " un/monitor " << (int)addr << endl;
QMetaObject::invokeMethod(it.value().first, it.value().second,
Q_ARG(bool, msg.type() == Protocol::ObjectMonitored));
break;
}
}
} else {
dispatchMessage(msg);
}
}
void Server::invokeObject(const QString &objectName, const char *method,
const QVariantList &args) const
{
Endpoint::invokeObject(objectName, method, args);
QObject *object = ObjectBroker::objectInternal(objectName);
Q_ASSERT(object);
// also invoke locally for in-process mode
invokeObjectLocal(object, method, args);
}
Protocol::ObjectAddress Server::registerObject(const QString &name, QObject *object)
{
return registerObject(name, object, ExportEverything);
}
static bool isNotifySignal(const QMetaObject *mo, const QMetaMethod &method)
{
for (int i = 0; i < mo->propertyCount(); ++i) {
const auto prop = mo->property(i);
if (!prop.hasNotifySignal())
continue;
if (prop.notifySignal().methodIndex() == method.methodIndex())
return true;
}
return false;
}
Protocol::ObjectAddress Server::registerObject(const QString &name, QObject *object,
Server::ObjectExportOptions exportOptions)
{
addObjectNameAddressMapping(name, ++m_nextAddress);
Protocol::ObjectAddress address = Endpoint::registerObject(name, object);
Q_ASSERT(m_nextAddress);
Q_ASSERT(m_nextAddress == address);
if (isConnected()) {
Message msg(endpointAddress(), Protocol::ObjectAdded);
msg << name << m_nextAddress;
send(msg);
}
if (exportOptions & ExportSignals) {
const QMetaObject *meta = object->metaObject();
for (int i = 0; i < meta->methodCount(); ++i) {
const QMetaMethod method = meta->method(i);
if (method.methodType() != QMetaMethod::Signal)
continue;
if ((exportOptions & ExportProperties) && isNotifySignal(meta, method))
continue; // no need to forward property change signals if we forward the property already
m_signalMapper->connectToSignal(object, method);
}
}
if (exportOptions & ExportProperties)
m_propertySyncer->addObject(address, object);
return address;
}
void Server::forwardSignal(QObject *sender, int signalIndex, const QVector<QVariant> &args)
{
if (!isConnected())
return;
Q_ASSERT(sender);
Q_ASSERT(signalIndex >= 0);
const QMetaMethod signal = sender->metaObject()->method(signalIndex);
Q_ASSERT(signal.methodType() == QMetaMethod::Signal);
QByteArray name = signal.methodSignature();
// get the name of the function to invoke, excluding the parens and function arguments.
name = name.mid(0, name.indexOf('('));
QVariantList v;
v.reserve(args.size());
foreach (const QVariant &arg, args)
v.push_back(arg);
Endpoint::invokeObject(sender->objectName(), name, v);
}
void Server::registerMonitorNotifier(Protocol::ObjectAddress address, QObject *receiver,
const char *monitorNotifier)
{
Q_ASSERT(address != Protocol::InvalidObjectAddress);
Q_ASSERT(receiver);
Q_ASSERT(monitorNotifier);
m_monitorNotifiers.insert(address, qMakePair<QObject *, QByteArray>(std::forward<QObject *>(receiver), monitorNotifier));
}
void Server::handlerDestroyed(Protocol::ObjectAddress objectAddress, const QString &objectName)
{
removeObjectNameAddressMapping(objectName);
m_monitorNotifiers.remove(objectAddress);
if (isConnected()) {
Message msg(endpointAddress(), Protocol::ObjectRemoved);
msg << objectName;
send(msg);
}
}
void Server::objectDestroyed(Protocol::ObjectAddress /*objectAddress*/, const QString &objectName,
QObject *object)
{
Q_UNUSED(object);
removeObjectNameAddressMapping(objectName);
if (isConnected()) {
Message msg(endpointAddress(), Protocol::ObjectRemoved);
msg << objectName;
send(msg);
}
}
void Server::broadcast()
{
if (!Server::instance()->isListening())
return;
QByteArray datagram;
QDataStream stream(&datagram, QIODevice::WriteOnly);
stream << Protocol::broadcastFormatVersion();
stream << Protocol::version();
stream << externalAddress();
stream << label(); // TODO integrate hostname
m_serverDevice->broadcast(datagram);
}
QUrl Server::externalAddress() const
{
if (!m_serverDevice)
return QUrl();
return m_serverDevice->externalAddress();
}
QString Server::errorString() const
{
if (!m_serverDevice)
return QString();
return m_serverDevice->errorString();
}
|