summaryrefslogtreecommitdiffstats
path: root/qdb/server/hostserver.cpp
blob: 93feec2c8507eee58a718d45feb6ef0fd51a6aeb (plain)
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
/******************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Debug Bridge.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
******************************************************************************/
#include "hostserver.h"

#include "libqdb/interruptsignalhandler.h"
#include "libqdb/qdbconstants.h"
#include "logging.h"

#include <QtCore/qcommandlineparser.h>
#include <QtCore/qdir.h>
#include <QtCore/qfile.h>
#include <QtCore/qloggingcategory.h>
#include <QtNetwork/qlocalsocket.h>

Q_LOGGING_CATEGORY(hostServerC, "qdb.hostserver");

int execHostServer(const QCoreApplication &app, const QCommandLineParser &parser)
{
    setupLogging();

    QString filterRules;
    if (!parser.isSet("debug-transport"))
        filterRules.append("transport=false\n");
    if (!parser.isSet("debug-connection"))
        filterRules.append("connection=false\n");
    QLoggingCategory::setFilterRules(filterRules);

    InterruptSignalHandler signalHandler;
    HostServer hostServer;
    QObject::connect(&signalHandler, &InterruptSignalHandler::interrupted, &hostServer, &HostServer::close);
    QObject::connect(&hostServer, &HostServer::closed, &app, &QCoreApplication::quit);
    QTimer::singleShot(0, &hostServer, &HostServer::listen);

    return app.exec();
}

HostServer::HostServer(QObject *parent)
    : QObject{parent},
      m_localServer{},
      m_servlets{},
      m_deviceManager{}
{

}

void HostServer::listen()
{
#ifdef Q_OS_UNIX
    QString socketPath = QDir::cleanPath(QDir::tempPath()) + QChar{'/'};
    socketPath += qdbSocketName;
    QFile::remove(socketPath);
#endif
    if (!m_localServer.listen(qdbSocketName)) {
        qCCritical(hostServerC) << "Could not start listening with QLocalServer: "
                                << m_localServer.errorString();
        close();
        return;
    }
    connect(&m_localServer, &QLocalServer::newConnection, this, &HostServer::handleClient);
    qCDebug(hostServerC) << "Started listening";

    connect(&m_deviceManager, &DeviceManager::newDeviceInfo, this, &HostServer::handleNewDeviceInfo);
    connect(&m_deviceManager, &DeviceManager::disconnectedDevice, this, &HostServer::handleDisconnectedDevice);
    m_deviceManager.start();
}

void HostServer::close()
{
    qCDebug(hostServerC) << "Shutting down";
    m_localServer.close();
    while (!m_servlets.empty())
        m_servlets.front().close(); // closing results in being erased from the list
    emit closed();
}

void HostServer::handleClient()
{
    QLocalSocket *socket = m_localServer.nextPendingConnection();
    if (!socket) {
        qCCritical(hostServerC) << "Did not get a connection from client";
        close();
        return;
    }
    m_servlets.emplace_back(socket, m_deviceManager);
    auto servlet = &m_servlets.back();

    connect(socket, &QLocalSocket::disconnected, servlet, &HostServlet::handleDisconnection);
    connect(socket, &QIODevice::readyRead, servlet, &HostServlet::handleRequest);

    connect(servlet, &HostServlet::done, this, &HostServer::handleDoneClient);
    connect(servlet, &HostServlet::serverStopRequested, this, &HostServer::close);
}

void HostServer::handleDoneClient(ServletId servletId)
{
    auto iter = std::find_if(m_servlets.begin(), m_servlets.end(),
                             [=](const HostServlet &servlet) {
                                 return servlet.id() == servletId;
                             });
    if (iter != m_servlets.end())
        m_servlets.erase(iter);
    else
        qCWarning(hostServerC) << "Could not find done servlet" << servletId << "when trying to remove it";
}

void HostServer::handleNewDeviceInfo(DeviceInformation info)
{
    qCDebug(hostServerC) << "New device information about" << info.serial;
}

void HostServer::handleDisconnectedDevice(QString serial)
{
    qCDebug(hostServerC) << "Disconnected" << serial;
}