aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cmakeprojectmanager/servermode.cpp
blob: 8e217499276e7b055656bb153489ac36b2b6b5e3 (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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "servermode.h"

#include <coreplugin/icore.h>
#include <coreplugin/reaper.h>

#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/temporarydirectory.h>

#include <QByteArray>
#include <QCryptographicHash>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLocalSocket>
#include <QUuid>

using namespace Utils;

namespace CMakeProjectManager {
namespace Internal {

const char COOKIE_KEY[] = "cookie";
const char IN_REPLY_TO_KEY[] = "inReplyTo";
const char NAME_KEY[] = "name";
const char TYPE_KEY[] = "type";

const char ERROR_TYPE[] = "error";
const char HANDSHAKE_TYPE[] = "handshake";

const char START_MAGIC[] = "\n[== \"CMake Server\" ==[\n";
const char END_MAGIC[] = "\n]== \"CMake Server\" ==]\n";

Q_LOGGING_CATEGORY(cmakeServerMode, "qtc.cmake.serverMode", QtWarningMsg);

// ----------------------------------------------------------------------
// Helpers:
// ----------------------------------------------------------------------

bool isValid(const QVariant &v)
{
    if (v.isNull())
        return false;
    if (v.type() == QVariant::String) // Workaround signals sending an empty string cookie
        return !v.toString().isEmpty();
    return true;
}

// --------------------------------------------------------------------
// ServerMode:
// --------------------------------------------------------------------


ServerMode::ServerMode(const Environment &env,
                       const FileName &sourceDirectory, const FileName &buildDirectory,
                       const FileName &cmakeExecutable,
                       const QString &generator, const QString &extraGenerator,
                       const QString &platform, const QString &toolset,
                       bool experimental, int major, int minor,
                       QObject *parent) :
    QObject(parent),
#if defined(Q_OS_UNIX)
    // Some unixes (e.g. Darwin) limit the length of a local socket to about 100 char (or less).
    // Since some unixes (e.g. Darwin) also point TMPDIR to /really/long/paths we need to create
    // our own socket in a place closer to '/'.
    m_socketDir("/tmp/cmake-"),
#endif
    m_sourceDirectory(sourceDirectory), m_buildDirectory(buildDirectory),
    m_cmakeExecutable(cmakeExecutable),
    m_generator(generator), m_extraGenerator(extraGenerator),
    m_platform(platform), m_toolset(toolset),
    m_useExperimental(experimental), m_majorProtocol(major), m_minorProtocol(minor)
{
    QTC_ASSERT(!m_sourceDirectory.isEmpty() && m_sourceDirectory.exists(), return);
    QTC_ASSERT(!m_buildDirectory.isEmpty() && m_buildDirectory.exists(), return);

    m_connectionTimer.setInterval(100);
    connect(&m_connectionTimer, &QTimer::timeout, this, &ServerMode::connectToServer);

    m_cmakeProcess = std::make_unique<QtcProcess>();

    m_cmakeProcess->setEnvironment(env);
    m_cmakeProcess->setWorkingDirectory(buildDirectory.toString());

#if defined(Q_OS_UNIX)
    m_socketName = m_socketDir.path() + "/socket";
#else
    m_socketName = QString::fromLatin1("\\\\.\\pipe\\") + QUuid::createUuid().toString();
#endif

    const QStringList args = QStringList({"-E", "server", "--pipe=" + m_socketName});

    connect(m_cmakeProcess.get(), &QtcProcess::started, this, [this]() { m_connectionTimer.start(); });
    connect(m_cmakeProcess.get(),
            static_cast<void(QtcProcess::*)(int, QProcess::ExitStatus)>(&QtcProcess::finished),
            this, &ServerMode::handleCMakeFinished);

    QString argumentString;
    QtcProcess::addArgs(&argumentString, args);
    if (m_useExperimental)
        QtcProcess::addArg(&argumentString, "--experimental");

    qCInfo(cmakeServerMode)
            << "Preparing cmake:" << cmakeExecutable.toString() << argumentString
            << "in" << m_buildDirectory.toString();
    m_cmakeProcess->setCommand(cmakeExecutable.toString(), argumentString);

    // Delay start:
    QTimer::singleShot(0, this, [argumentString, this] {
        emit message(tr("Running \"%1 %2\" in %3.")
                     .arg(m_cmakeExecutable.toUserOutput())
                     .arg(argumentString)
                     .arg(m_buildDirectory.toUserOutput()));

        m_cmakeProcess->start();
    });
}

ServerMode::~ServerMode()
{
    if (m_cmakeProcess)
        m_cmakeProcess->disconnect();
    if (m_cmakeSocket) {
        m_cmakeSocket->disconnect();
        m_cmakeSocket->abort();
        delete(m_cmakeSocket);
    }
    m_cmakeSocket = nullptr;
    Core::Reaper::reap(m_cmakeProcess.release());

    qCDebug(cmakeServerMode) << "Server-Mode closed.";
}

void ServerMode::sendRequest(const QString &type, const QVariantMap &extra, const QVariant &cookie)
{
    QTC_ASSERT(m_cmakeSocket, return);
    ++m_requestCounter;

    qCInfo(cmakeServerMode) << "Sending Request" << type << "(" << cookie << ")";

    QVariantMap data = extra;
    data.insert(TYPE_KEY, type);
    const QVariant realCookie = cookie.isNull() ? QVariant(m_requestCounter) : cookie;
    data.insert(COOKIE_KEY, realCookie);
    m_expectedReplies.push_back({type, realCookie});

    QJsonObject object = QJsonObject::fromVariantMap(data);
    QJsonDocument document;
    document.setObject(object);

    const QByteArray rawData = START_MAGIC + document.toJson(QJsonDocument::Compact) + END_MAGIC;
    qCDebug(cmakeServerMode) << ">>>" << rawData;
    m_cmakeSocket->write(rawData);
    m_cmakeSocket->flush();
}

bool ServerMode::isConnected()
{
    return m_cmakeSocket && m_isConnected;
}

void ServerMode::connectToServer()
{
    QTC_ASSERT(m_cmakeProcess, return);
    if (m_cmakeSocket)
        return; // We connected in the meantime...

    static int counter = 0;
    ++counter;

    if (counter > 50) {
        counter = 0;
        m_cmakeProcess->disconnect();
        qCInfo(cmakeServerMode) << "Timeout waiting for pipe" << m_socketName;
        reportError(tr("Running \"%1\" failed: Timeout waiting for pipe \"%2\".")
                    .arg(m_cmakeExecutable.toUserOutput())
                    .arg(m_socketName));

        Core::Reaper::reap(m_cmakeProcess.release());
        emit disconnected();
        return;
    }

    QTC_ASSERT(!m_cmakeSocket, return);

    auto socket = new QLocalSocket(m_cmakeProcess.get());
    connect(socket, &QLocalSocket::readyRead, this, &ServerMode::handleRawCMakeServerData);
    connect(socket, static_cast<void(QLocalSocket::*)(QLocalSocket::LocalSocketError)>(&QLocalSocket::error),
            this, [this, socket]() {
        reportError(socket->errorString());
        m_cmakeSocket = nullptr;
        socket->disconnect();
        socket->deleteLater();
    });
    connect(socket, &QLocalSocket::connected, this, [this, socket]() { m_cmakeSocket = socket; });
    connect(socket, &QLocalSocket::disconnected, this, [this, socket]() {
        if (m_cmakeSocket)
            emit disconnected();
        m_cmakeSocket = nullptr;
        socket->disconnect();
        socket->deleteLater();
    });

    socket->connectToServer(m_socketName);
    m_connectionTimer.start();
}

void ServerMode::handleCMakeFinished(int code, QProcess::ExitStatus status)
{
    qCInfo(cmakeServerMode) << "CMake has finished" << code << status;
    QString msg;
    if (status != QProcess::NormalExit)
        msg = tr("CMake process \"%1\" crashed.").arg(m_cmakeExecutable.toUserOutput());
    else if (code != 0)
        msg = tr("CMake process \"%1\" quit with exit code %2.").arg(m_cmakeExecutable.toUserOutput()).arg(code);

    if (!msg.isEmpty()) {
        reportError(msg);
    } else {
        emit message(tr("CMake process \"%1\" quit normally.").arg(m_cmakeExecutable.toUserOutput()));
    }

    if (m_cmakeSocket) {
        m_cmakeSocket->disconnect();
        delete m_cmakeSocket;
        m_cmakeSocket = nullptr;
    }

    if (!HostOsInfo::isWindowsHost())
        QFile::remove(m_socketName);

    emit disconnected();
}

void ServerMode::handleRawCMakeServerData()
{
    const static QByteArray startNeedle(START_MAGIC);
    const static QByteArray endNeedle(END_MAGIC);

    if (!m_cmakeSocket) // might happen during shutdown
        return;

    m_buffer.append(m_cmakeSocket->readAll());

    while (true) {
        const int startPos = m_buffer.indexOf(startNeedle);
        if (startPos >= 0) {
            const int afterStartNeedle = startPos + startNeedle.count();
            const int endPos = m_buffer.indexOf(endNeedle, afterStartNeedle);
            if (endPos > afterStartNeedle) {
                // Process JSON, remove junk and JSON-part, continue to loop with shorter buffer
                parseBuffer(m_buffer.mid(afterStartNeedle, endPos - afterStartNeedle));
                m_buffer.remove(0, endPos + endNeedle.count());
            } else {
                // Remove junk up to the start needle and break out of the loop
                if (startPos > 0)
                    m_buffer.remove(0, startPos);
                break;
            }
        } else {
            // Keep at last startNeedle.count() characters (as that might be a
            // partial startNeedle), break out of the loop
            if (m_buffer.count() > startNeedle.count())
                m_buffer.remove(0, m_buffer.count() - startNeedle.count());
            break;
        }
    }
}

void ServerMode::parseBuffer(const QByteArray &buffer)
{
    qCDebug(cmakeServerMode) << "<<<" << buffer;
    QJsonDocument document = QJsonDocument::fromJson(buffer);
    if (document.isNull()) {
        reportError(tr("Failed to parse JSON from CMake server."));
        return;
    }
    QJsonObject rootObject = document.object();
    if (rootObject.isEmpty()) {
        reportError(tr("JSON data from CMake server was not a JSON object."));
        return;
    }

    parseJson(rootObject.toVariantMap());
}

void ServerMode::parseJson(const QVariantMap &data)
{
    QString type = data.value(TYPE_KEY).toString();
    if (type == "hello") {
        qCInfo(cmakeServerMode) << "Got \"hello\" message.";
        if (m_gotHello) {
            reportError(tr("Unexpected hello received from CMake server."));
            return;
        } else {
            handleHello(data);
            m_gotHello = true;
            return;
        }
    }
    if (!m_gotHello && type != ERROR_TYPE) {
        reportError(tr("Unexpected type \"%1\" received while waiting for \"hello\".").arg(type));
        return;
    }

    if (type == "reply") {
        if (m_expectedReplies.empty()) {
            reportError(tr("Received a reply even though no request is open."));
            return;
        }
        const QString replyTo = data.value(IN_REPLY_TO_KEY).toString();
        const QVariant cookie = data.value(COOKIE_KEY);
        qCInfo(cmakeServerMode) << "Got \"reply\" message." << replyTo << "(" << cookie << ")";

        const auto expected = m_expectedReplies.begin();
        if (expected->type != replyTo) {
            reportError(tr("Received a reply to a request of type \"%1\", when a request of type \"%2\" was sent.")
                        .arg(replyTo).arg(expected->type));
            return;
        }
        if (expected->cookie != cookie) {
            reportError(tr("Received a reply with cookie \"%1\", when \"%2\" was expected.")
                        .arg(cookie.toString()).arg(expected->cookie.toString()));
            return;
        }

        m_expectedReplies.erase(expected);
        if (replyTo != HANDSHAKE_TYPE)
            emit cmakeReply(data, replyTo, cookie);
        else {
            m_isConnected = true;
            emit connected();
        }
        return;
    }
    if (type == "error") {
        if (m_expectedReplies.empty()) {
            reportError(tr("An error was reported even though no request is open."));
            return;
        }
        const QString replyTo = data.value(IN_REPLY_TO_KEY).toString();
        const QVariant cookie = data.value(COOKIE_KEY);
        qCInfo(cmakeServerMode) << "Got \"error\" message." << replyTo << "(" << cookie << ")";

        const auto expected = m_expectedReplies.begin();
        if (expected->type != replyTo) {
            reportError(tr("Received an error in response to a request of type \"%1\", when a request of type \"%2\" was sent.")
                        .arg(replyTo).arg(expected->type));
            return;
        }
        if (expected->cookie != cookie) {
            reportError(tr("Received an error with cookie \"%1\", when \"%2\" was expected.")
                        .arg(cookie.toString()).arg(expected->cookie.toString()));
            return;
        }

        m_expectedReplies.erase(expected);

        emit cmakeError(data.value("errorMessage").toString(), replyTo, cookie);
        if (replyTo == HANDSHAKE_TYPE) {
            Core::Reaper::reap(m_cmakeProcess.release());
            m_cmakeSocket->disconnect();
            m_cmakeSocket->disconnectFromServer();
            m_cmakeSocket = nullptr;
            emit disconnected();
        }
        return;
    }
    if (type == "message") {
        const QString replyTo = data.value(IN_REPLY_TO_KEY).toString();
        const QVariant cookie = data.value(COOKIE_KEY);
        qCInfo(cmakeServerMode) << "Got \"message\" message." << replyTo << "(" << cookie << ")";

        const auto expected = m_expectedReplies.begin();
        if (expected->type != replyTo) {
            reportError(tr("Received a message in response to a request of type \"%1\", when a request of type \"%2\" was sent.")
                        .arg(replyTo).arg(expected->type));
            return;
        }
        if (expected->cookie != cookie) {
            reportError(tr("Received a message with cookie \"%1\", when \"%2\" was expected.")
                        .arg(cookie.toString()).arg(expected->cookie.toString()));
            return;
        }

        emit cmakeMessage(data.value("message").toString(), replyTo, cookie);
        return;
    }
    if (type == "progress") {
        const QString replyTo = data.value(IN_REPLY_TO_KEY).toString();
        const QVariant cookie = data.value(COOKIE_KEY);
        qCInfo(cmakeServerMode) << "Got \"progress\" message." << replyTo << "(" << cookie << ")";

        const auto expected = m_expectedReplies.begin();
        if (expected->type != replyTo) {
            reportError(tr("Received a progress report in response to a request of type \"%1\", when a request of type \"%2\" was sent.")
                        .arg(replyTo).arg(expected->type));
            return;
        }
        if (expected->cookie != cookie) {
            reportError(tr("Received a progress report with cookie \"%1\", when \"%2\" was expected.")
                        .arg(cookie.toString()).arg(expected->cookie.toString()));
            return;
        }

        emit cmakeProgress(data.value("progressMinimum").toInt(),
                           data.value("progressCurrent").toInt(),
                           data.value("progressMaximum").toInt(), replyTo, cookie);
        return;
    }
    if (type == "signal") {
        const QString replyTo = data.value(IN_REPLY_TO_KEY).toString();
        const QString cookie = data.value(COOKIE_KEY).toString();
        const QString name = data.value(NAME_KEY).toString();
        qCInfo(cmakeServerMode) << "Got \"signal\" message." << name << replyTo << "(" << cookie << ")";

        if (name.isEmpty()) {
            reportError(tr("Received a signal without a name."));
            return;
        }
        if (!replyTo.isEmpty() || isValid(cookie)) {
            reportError(tr("Received a signal in reply to a request."));
            return;
        }

        emit cmakeSignal(name, data);
        return;
    }
    reportError("Got a message of an unknown type.");
}

void ServerMode::handleHello(const QVariantMap &data)
{
    Q_UNUSED(data);
    QVariantMap extra;
    QVariantMap version;
    version.insert("major", m_majorProtocol);
    if (m_minorProtocol >= 0)
        version.insert("minor", m_minorProtocol);
    extra.insert("protocolVersion", version);
    extra.insert("sourceDirectory", m_sourceDirectory.toString());
    extra.insert("buildDirectory", m_buildDirectory.toString());
    extra.insert("generator", m_generator);
    if (!m_platform.isEmpty())
        extra.insert("platform", m_platform);
    if (!m_toolset.isEmpty())
        extra.insert("toolset", m_toolset);
    if (!m_extraGenerator.isEmpty())
        extra.insert("extraGenerator", m_extraGenerator);
    if (!m_platform.isEmpty())
        extra.insert("platform", m_platform);
    if (!m_toolset.isEmpty())
        extra.insert("toolset", m_toolset);

    sendRequest(HANDSHAKE_TYPE, extra);
}

void ServerMode::reportError(const QString &msg)
{
    qCWarning(cmakeServerMode) << "Report Error:" << msg;
    emit message(msg);
    emit errorOccured(msg);
}

} // namespace Internal
} // namespace CMakeProjectManager