File: servercontroller.cpp

package info (click to toggle)
kuserfeedback 1.3.0-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,496 kB
  • sloc: cpp: 13,251; php: 2,192; xml: 224; yacc: 90; lex: 82; sh: 17; makefile: 8
file content (69 lines) | stat: -rw-r--r-- 1,783 bytes parent folder | download | duplicates (4)
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
/*
    SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: MIT
*/

#include "test-config.h"

#include "servercontroller.h"

#include <rest/restapi.h>
#include <rest/restclient.h>

#include <QNetworkReply>
#include <QUrl>
#include <QSignalSpy>

using namespace KUserFeedback::Console;

ServerController::ServerController()
{
    m_process.setProgram(QStringLiteral(PHP_EXECUTABLE));
    m_process.setWorkingDirectory(QLatin1String(CURRENT_SOURCE_DIR "/../src/testserver"));
    m_process.setArguments({QStringLiteral("-S"), QStringLiteral("localhost:1984"), QStringLiteral("router.php")});
    m_process.setProcessChannelMode(QProcess::ForwardedChannels);
}

ServerController::~ServerController()
{
    if (m_process.state() == QProcess::Running)
        stop();
}

bool ServerController::start()
{
    m_process.start();
    if (!m_process.waitForStarted(1000)) {
        qWarning() << "Failed to launch:" << m_process.program() << m_process.arguments() << m_process.errorString();
        return false;
    }

    // wait for the server to become available, otherwise the HTTP access below fails
    m_process.waitForReadyRead(100);

    ServerInfo s;
    s.setUrl(url());
    RESTClient client;
    client.setServerInfo(s);
    auto reply = RESTApi::checkSchema(&client);

    QSignalSpy spy(reply, &QNetworkReply::finished);
    if (!spy.wait())
        return false;

    if (reply->error() != QNetworkReply::NoError)
        qWarning() << "Schema check failed:" << reply->errorString();
    return reply->error() == QNetworkReply::NoError;
}

void ServerController::stop()
{
    m_process.terminate();
    m_process.waitForFinished(1000);
}

QUrl ServerController::url() const
{
    return QUrl(QStringLiteral("http://localhost:1984"));
}