summaryrefslogtreecommitdiffstats
path: root/qdb/server/hostservlet.cpp
blob: 4bd5c2d886793834009a21ec1080167bdbb9a026 (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
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
/******************************************************************************
**
** 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 "hostservlet.h"

#include "hostmessages.h"

#include <QtCore/qjsonarray.h>
#include <QtCore/qjsonobject.h>
#include <QtNetwork/qlocalsocket.h>

QJsonObject deviceInformationToJsonObject(const DeviceInformation &deviceInfo)
{
    QJsonObject info;
    info["serial"] = deviceInfo.serial;
    info["hostMac"] = deviceInfo.hostMac;
    info["ipAddress"] = deviceInfo.ipAddress;
    return info;
}

ServletId newServletId()
{
    static ServletId nextId = 0;
    return ++nextId;
}

HostServlet::HostServlet(QLocalSocket *socket, DeviceManager &deviceManager)
    : m_id{newServletId()},
      m_socket{socket},
      m_deviceManager{deviceManager}

{

}

HostServlet::~HostServlet()
{
    m_socket->deleteLater();
}

void HostServlet::close()
{
    m_socket->waitForBytesWritten();
    m_socket->disconnectFromServer();
}

ServletId HostServlet::id() const
{
    return m_id;
}

void HostServlet::handleDisconnection()
{
    emit done(m_id);
}

void HostServlet::handleRequest()
{
    const auto requestBytes = m_socket->readLine();
    const auto request = QJsonDocument::fromJson(requestBytes);
    const auto type = requestType(request.object());

    switch (type) {
    case RequestType::Devices:
        replyDevices();
        break;
    case RequestType::WatchDevices:
        startWatchingDevices();
        break;
    case RequestType::StopServer:
        stopServer();
        break;
    case RequestType::Unknown:
        qWarning() << "Got invalid request from client:" << requestBytes;
        const QJsonObject response = initializeResponse(ResponseType::InvalidRequest);
        m_socket->write(serialiseResponse(response));
        close();
        break;
    }
}

void HostServlet::replyDevices()
{
    QJsonArray infoArray;
    const auto deviceInfos = m_deviceManager.listDevices();
    for (const auto &deviceInfo : deviceInfos)
        infoArray << deviceInformationToJsonObject(deviceInfo);

    QJsonObject response = initializeResponse(ResponseType::Devices);
    response["devices"] = infoArray;

    if (!m_socket || !m_socket->isWritable()) {
        qWarning() << "Could not reply to the client";
        return;
    }
    m_socket->write(serialiseResponse(response));
    qDebug() << "Replied device information to the client";
    close();
}

void HostServlet::replyNewDevice(const DeviceInformation &deviceInfo)
{
    QJsonObject response = initializeResponse(ResponseType::NewDevice);
    response["device"] = deviceInformationToJsonObject(deviceInfo);

    if (!m_socket || !m_socket->isWritable()) {
        qWarning() << "Could not reply to the client";
        return;
    }
    m_socket->write(serialiseResponse(response));
    qDebug() << "Sent new device information to the client";
}

void HostServlet::replyDisconnectedDevice(const QString &serial)
{
    QJsonObject response = initializeResponse(ResponseType::DisconnectedDevice);
    response["serial"] = serial;

    if (!m_socket || !m_socket->isWritable()) {
        qWarning() << "Could not reply to the client";
        return;
    }
    m_socket->write(serialiseResponse(response));
    qDebug() << "Sent disconnected device information to the client";
}

void HostServlet::startWatchingDevices()
{
    qDebug() << "Starting to watch devices";
    connect(&m_deviceManager, &DeviceManager::newDeviceInfo, this, &HostServlet::replyNewDevice);
    connect(&m_deviceManager, &DeviceManager::disconnectedDevice, this, &HostServlet::replyDisconnectedDevice);

    const auto deviceInfos = m_deviceManager.listDevices();
    for (const auto &deviceInfo : deviceInfos)
        replyNewDevice(deviceInfo);
    qDebug() << "Reported initial devices to watcher";
}

void HostServlet::stopServer()
{
    QJsonObject response = initializeResponse(ResponseType::Stopping);

    if (!m_socket || !m_socket->isWritable()) {
        qWarning() << "Could not reply to the client";
        return;
    }
    m_socket->write(serialiseResponse(response));
    qDebug() << "Acknowledged stopping";

    emit serverStopRequested();
    // All servlets, including this one will be closed during shutdown
}