summaryrefslogtreecommitdiff
path: root/src/libs/utils/ssh/sshremoteprocessrunner.cpp
blob: fcff1692f0a543348bf679eca0a29e9309b5459a (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
#include "sshremoteprocessrunner.h"

#define ASSERT_STATE(states) assertState(states, Q_FUNC_INFO)

namespace Utils {

class SshRemoteProcessRunnerPrivate : public QObject
{
    Q_OBJECT
public:
    SshRemoteProcessRunnerPrivate(const SshConnectionParameters &params,
        QObject *parent);
    SshRemoteProcessRunnerPrivate(const SshConnection::Ptr &connection,
        QObject *parent);
    void run(const QByteArray &command);
    QByteArray command() const { return m_command; }

    const SshConnection::Ptr m_connection;
    SshRemoteProcess::Ptr m_process;

signals:
    void connectionError(Utils::SshError);
    void processStarted();
    void processOutputAvailable(const QByteArray &output);
    void processErrorOutputAvailable(const QByteArray &output);
    void processClosed(int exitStatus);

private slots:
    void handleConnected();
    void handleConnectionError(Utils::SshError error);
    void handleDisconnected();
    void handleProcessStarted();
    void handleProcessFinished(int exitStatus);

private:
    enum State { Inactive, Connecting, Connected, ProcessRunning };

    void setState(State state);
    void assertState(const QList<State> &allowedStates, const char *func);
    void assertState(State allowedState, const char *func);

    State m_state;
    QByteArray m_command;
    const SshConnectionParameters m_params;
};


SshRemoteProcessRunnerPrivate::SshRemoteProcessRunnerPrivate(const SshConnectionParameters &params,
    QObject *parent)
    : QObject(parent),
      m_connection(SshConnection::create()),
      m_state(Inactive),
      m_params(params)
{
}

SshRemoteProcessRunnerPrivate::SshRemoteProcessRunnerPrivate(const SshConnection::Ptr &connection,
    QObject *parent)
    : QObject(parent),
      m_connection(connection),
      m_state(Inactive),
      m_params(connection->connectionParameters())
{
}

void SshRemoteProcessRunnerPrivate::run(const QByteArray &command)
{
    ASSERT_STATE(Inactive);
    setState(Connecting);

    m_command = command;
    connect(m_connection.data(), SIGNAL(error(Utils::SshError)),
        SLOT(handleConnectionError(Utils::SshError)));
    connect(m_connection.data(), SIGNAL(disconnected()),
        SLOT(handleDisconnected()));
    if (m_connection->state() == SshConnection::Connected) {
        handleConnected();
    } else {
        connect(m_connection.data(), SIGNAL(connected()),
            SLOT(handleConnected()));
        m_connection->connectToHost(m_params);
    }
}

void SshRemoteProcessRunnerPrivate::handleConnected()
{
    ASSERT_STATE(Connecting);
    setState(Connected);

    m_process = m_connection->createRemoteProcess(m_command);
    connect(m_process.data(), SIGNAL(started()), SLOT(handleProcessStarted()));
    connect(m_process.data(), SIGNAL(closed(int)),
        SLOT(handleProcessFinished(int)));
    connect(m_process.data(), SIGNAL(outputAvailable(QByteArray)),
        SIGNAL(processOutputAvailable(QByteArray)));
    connect(m_process.data(), SIGNAL(errorOutputAvailable(QByteArray)),
        SIGNAL(processErrorOutputAvailable(QByteArray)));
    m_process->start();
}

void SshRemoteProcessRunnerPrivate::handleConnectionError(Utils::SshError error)
{
    handleDisconnected();
    emit connectionError(error);
}

void SshRemoteProcessRunnerPrivate::handleDisconnected()
{
    ASSERT_STATE(QList<State>() << Connecting << Connected << ProcessRunning);
    setState(Inactive);
}

void SshRemoteProcessRunnerPrivate::handleProcessStarted()
{
    ASSERT_STATE(Connected);
    setState(ProcessRunning);

    emit processStarted();
}

void SshRemoteProcessRunnerPrivate::handleProcessFinished(int exitStatus)
{
    switch (exitStatus) {
    case SshRemoteProcess::FailedToStart:
        ASSERT_STATE(Connected);
        break;
    case SshRemoteProcess::KilledBySignal:
    case SshRemoteProcess::ExitedNormally:
        ASSERT_STATE(ProcessRunning);
        break;
    default:
        Q_ASSERT_X(false, Q_FUNC_INFO, "Impossible exit status.");
    }
    setState(Inactive);
    emit processClosed(exitStatus);
}

void SshRemoteProcessRunnerPrivate::setState(State state)
{
    if (m_state != state) {
        m_state = state;
        if (m_state == Inactive) {
            if (m_process)
                disconnect(m_process.data(), 0, this, 0);
            disconnect(m_connection.data(), 0, this, 0);
        }
    }
}

void SshRemoteProcessRunnerPrivate::assertState(const QList<State> &allowedStates,
    const char *func)
{
    if (!allowedStates.contains(m_state))
        qWarning("Unexpected state %d in function %s", m_state, func);
}

void SshRemoteProcessRunnerPrivate::assertState(State allowedState,
    const char *func)
{
    assertState(QList<State>() << allowedState, func);
}


SshRemoteProcessRunner::Ptr SshRemoteProcessRunner::create(const SshConnectionParameters &params)
{
    return SshRemoteProcessRunner::Ptr(new SshRemoteProcessRunner(params));
}

SshRemoteProcessRunner::Ptr SshRemoteProcessRunner::create(const SshConnection::Ptr &connection)
{
    return SshRemoteProcessRunner::Ptr(new SshRemoteProcessRunner(connection));
}

SshRemoteProcessRunner::SshRemoteProcessRunner(const SshConnectionParameters &params)
    : d(new SshRemoteProcessRunnerPrivate(params, this))
{
    init();
}

SshRemoteProcessRunner::SshRemoteProcessRunner(const SshConnection::Ptr &connection)
    : d(new SshRemoteProcessRunnerPrivate(connection, this))
{
    init();
}

void SshRemoteProcessRunner::init()
{
    connect(d, SIGNAL(connectionError(Utils::SshError)),
        SIGNAL(connectionError(Utils::SshError)));
    connect(d, SIGNAL(processStarted()), SIGNAL(processStarted()));
    connect(d, SIGNAL(processClosed(int)), SIGNAL(processClosed(int)));
    connect(d, SIGNAL(processOutputAvailable(QByteArray)),
        SIGNAL(processOutputAvailable(QByteArray)));
    connect(d, SIGNAL(processErrorOutputAvailable(QByteArray)),
        SIGNAL(processErrorOutputAvailable(QByteArray)));
}

void SshRemoteProcessRunner::run(const QByteArray &command)
{
    d->run(command);
}

QByteArray SshRemoteProcessRunner::command() const { return d->command(); }
SshConnection::Ptr SshRemoteProcessRunner::connection() const { return d->m_connection; }
SshRemoteProcess::Ptr SshRemoteProcessRunner::process() const { return d->m_process; }

} // namespace Utils


#include "sshremoteprocessrunner.moc"