aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/boot2qt/device-detection/qdbwatcher.cpp
blob: 8a18a3b5ac93b732ef6b1b1a50c85549f48ef01c (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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qdbwatcher.h"

#include "hostmessages.h"
#include "../qdbtr.h"
#include "../qdbutils.h"

#include <utils/filepath.h>
#include <utils/qtcprocess.h>

#include <QFile>
#include <QTimer>

namespace Qdb::Internal {

const int startupDelay = 500; // time in ms to wait for host server startup before retrying
const QString qdbSocketName = "qdb.socket";

QMutex QdbWatcher::s_startMutex;
bool QdbWatcher::s_startedServer = false;

QdbWatcher::QdbWatcher(QObject *parent)
    : QObject(parent)
    , m_requestType(RequestType::Unknown)
{
}

QdbWatcher::~QdbWatcher()
{
    stop();
}

void QdbWatcher::start(RequestType requestType)
{
    m_requestType = requestType;
    startPrivate();
}

void QdbWatcher::startPrivate()
{
    m_socket = std::unique_ptr<QLocalSocket>(new QLocalSocket());
    connect(m_socket.get(), &QLocalSocket::connected,
            this, &QdbWatcher::handleWatchConnection);
    connect(m_socket.get(), &QLocalSocket::errorOccurred,
            this, &QdbWatcher::handleWatchError);
    m_socket->connectToServer(qdbSocketName);
}

void QdbWatcher::stop()
{
    m_shuttingDown = true;
    if (m_socket)
        m_socket->disconnectFromServer();
}

void QdbWatcher::handleWatchConnection()
{
    m_retried = false;
    {
        QMutexLocker lock(&s_startMutex);
        s_startedServer = false;
    }
    connect(m_socket.get(), &QIODevice::readyRead, this, &QdbWatcher::handleWatchMessage);
    m_socket->write(createRequest(m_requestType));
}

void QdbWatcher::handleWatchError(QLocalSocket::LocalSocketError error)
{
    if (m_shuttingDown)
        return;

    if (error == QLocalSocket::PeerClosedError) {
        retry();
        return;
    }

    if (error != QLocalSocket::ServerNotFoundError
            && error != QLocalSocket::ConnectionRefusedError) {
        stop();
        emit watcherError(Tr::tr("Unexpected QLocalSocket error: %1")
                          .arg(m_socket->errorString()));
        return;
    }

    if (m_retried) {
        stop();
        emit watcherError(Tr::tr("Could not connect to QDB host server even after trying to start it."));
        return;
    }
    retry();
}

void QdbWatcher::handleWatchMessage()
{
    while (m_socket->bytesAvailable() > 0) {
        const QByteArray responseBytes = m_socket->readLine();
        const auto document = QJsonDocument::fromJson(responseBytes);
        if (document.isNull()) {
            const QString message =
                    Tr::tr("Invalid JSON response received from QDB server: %1");
            emit watcherError(message.arg(QString::fromUtf8(responseBytes)));
            return;
        }
        emit incomingMessage(document);
    }
}

void QdbWatcher::forkHostServer()
{
    Utils::FilePath qdbFilePath = findTool(QdbTool::Qdb);
    QFile executable(qdbFilePath.toString());
    if (!executable.exists()) {
        const QString message = Tr::tr("Could not find QDB host server executable. "
                                   "You can set the location with environment variable %1.")
                                    .arg(overridingEnvironmentVariable(QdbTool::Qdb));
        showMessage(message, true);
        return;
    }
    if (Utils::Process::startDetached({qdbFilePath, {"server"}}))
        showMessage(Tr::tr("QDB host server started."), false);
    else
        showMessage(Tr::tr("Could not start QDB host server in %1").arg(qdbFilePath.toString()), true);
}

void QdbWatcher::retry()
{
    m_retried = true;
    {
        QMutexLocker lock(&s_startMutex);
        if (!s_startedServer) {
            showMessage(Tr::tr("Starting QDB host server."), false);
            forkHostServer();
            s_startedServer = true;
        }
    }
    QTimer::singleShot(startupDelay, this, &QdbWatcher::startPrivate);
}

} // Qdb::Internal