maemosshthread.cpp 6.48 KB
Newer Older
ck's avatar
ck committed
1
2
/****************************************************************************
**
hjk's avatar
hjk committed
3
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
ck's avatar
ck committed
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
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "maemosshthread.h"

namespace Qt4ProjectManager {
namespace Internal {

47
48
template <class SshConnection> MaemoSshThread<SshConnection>::MaemoSshThread(const Core::SshServerInfo &server)
    : m_server(server), m_stopRequested(false)
ck's avatar
ck committed
49
50
51
{
}

52
template <class SshConnection> MaemoSshThread<SshConnection>::~MaemoSshThread()
ck's avatar
ck committed
53
54
55
56
57
{
    stop();
    wait();
}

58
template <class SshConnection> void MaemoSshThread<SshConnection>::run()
ck's avatar
ck committed
59
{
60
61
62
63
64
    if (m_stopRequested)
        return;

    if (!runInternal())
        m_error = m_connection->error();
ck's avatar
ck committed
65
66
}

67
template<class SshConnection> void MaemoSshThread<SshConnection>::stop()
ck's avatar
ck committed
68
{
ck's avatar
ck committed
69
70
    m_mutex.lock();
    m_stopRequested = true;
71
    m_waitCond.wakeAll();
ck's avatar
ck committed
72
73
    const bool hasConnection = !m_connection.isNull();
    if (hasConnection)
74
75
        m_connection->quit();
    m_mutex.unlock();
ck's avatar
ck committed
76
77
}

78
template<class SshConnection> void MaemoSshThread<SshConnection>::waitForStop()
ck's avatar
ck committed
79
{
80
81
82
83
84
85
86
87
88
    m_mutex.lock();
    while (!stopRequested())
        m_waitCond.wait(&m_mutex);
    m_mutex.unlock();
}

template<class SshConnection> void MaemoSshThread<SshConnection>::createConnection()
{
    typename SshConnection::Ptr connection = SshConnection::create(m_server);
ck's avatar
ck committed
89
    m_mutex.lock();
ck's avatar
ck committed
90
    m_connection = connection;
ck's avatar
ck committed
91
    m_mutex.unlock();
ck's avatar
ck committed
92
}
ck's avatar
ck committed
93

94
MaemoSshRunner::MaemoSshRunner(const Core::SshServerInfo &server,
ck's avatar
ck committed
95
                               const QString &command)
96
97
    : MaemoSshThread<Core::InteractiveSshConnection>(server),
      m_command(command)
ck's avatar
ck committed
98
{
99
    m_prompt = server.uname == QLatin1String("root") ? "#" : "$";
ck's avatar
ck committed
100
101
}

102
bool MaemoSshRunner::runInternal()
ck's avatar
ck committed
103
{
104
    createConnection();
ck's avatar
ck committed
105
106
    connect(m_connection.data(), SIGNAL(remoteOutputAvailable()),
            this, SLOT(handleRemoteOutput()));
ck's avatar
ck committed
107
    initState();
108
109
    if (!m_connection->start())
        return false;
ck's avatar
ck committed
110
    if (stopRequested())
111
112
113
114
115
116
        return true;

    waitForStop();
    return !m_connection->hasError();
}

ck's avatar
ck committed
117
118
119
120
121
122
123
void MaemoSshRunner::initState()
{
    m_endMarkerCount = 0;
    m_promptEncountered = false;
    m_potentialEndMarkerPrefix.clear();
}

ck's avatar
ck committed
124
void MaemoSshRunner::handleRemoteOutput()
125
{
126
127
    const QByteArray output
        = m_potentialEndMarkerPrefix + m_connection->waitForRemoteOutput(0);
ck's avatar
ck committed
128

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    // Wait for a prompt before sending the command.
    if (!m_promptEncountered) {
        if (output.indexOf(m_prompt) != -1) {
            m_promptEncountered = true;

            /*
             * We don't have access to the remote process management, so we
             * try to track the lifetime of the process by adding a second command
             * that prints a rare character. When it occurs for the second time (the
             * first one is the echo from the remote terminal), we assume the
             * process has finished. If anyone actually prints this special character
             * in their application, they are out of luck.
             */
            const QString finalCommand = m_command + QLatin1String(";echo ")
                + QString::fromUtf8(EndMarker) + QLatin1Char('\n');
            if (!m_connection->sendInput(finalCommand.toUtf8()))
                stop();
        }
ck's avatar
ck committed
147
        return;
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    }

    /*
     * The output the user should see is everything after the first
     * and before the last occurrence of our marker string.
     */
    int firstCharToEmit;
    int charsToEmitCount;
    const int endMarkerPos = output.indexOf(EndMarker);
    if (endMarkerPos != -1) {
        if (m_endMarkerCount++ == 0) {
            firstCharToEmit = endMarkerPos + EndMarker.count() + 1;
            int endMarkerPos2
                    = output.indexOf(EndMarker, firstCharToEmit);
            if (endMarkerPos2 != -1) {
                ++ m_endMarkerCount;
                charsToEmitCount = endMarkerPos2 - firstCharToEmit;
            } else {
                charsToEmitCount = -1;
            }
        } else {
169
170
            firstCharToEmit = m_potentialEndMarkerPrefix.count();
            charsToEmitCount = endMarkerPos - firstCharToEmit;
171
172
173
174
175
        }
    } else {
        if (m_endMarkerCount == 0) {
            charsToEmitCount = 0;
        } else {
176
            firstCharToEmit = m_potentialEndMarkerPrefix.count();
177
178
179
180
            charsToEmitCount = -1;
        }
    }

181
    if (charsToEmitCount != 0) {
182
183
        emit remoteOutput(QString::fromUtf8(output.data() + firstCharToEmit,
                                            charsToEmitCount));
184
    }
185
186
    if (m_endMarkerCount == 2)
        stop();
187
188

    m_potentialEndMarkerPrefix = output.right(EndMarker.count() - 1);
ck's avatar
ck committed
189
190
}

191
192
193
194
195
196
197
const QByteArray MaemoSshRunner::EndMarker(QString(QChar(0x13a0)).toUtf8());


MaemoSshDeployer::MaemoSshDeployer(const Core::SshServerInfo &server,
    const QList<Core::SftpTransferInfo> &deploySpecs)
    : MaemoSshThread<Core::SftpConnection>(server),
      m_deploySpecs(deploySpecs)
ck's avatar
ck committed
198
199
200
{
}

201
bool MaemoSshDeployer::runInternal()
ck's avatar
ck committed
202
{
203
204
205
    createConnection();
    if (!m_connection->start())
        return false;
ck's avatar
ck committed
206
    if (stopRequested())
207
208
209
        return true;

    connect(m_connection.data(), SIGNAL(fileCopied(QString)),
ck's avatar
ck committed
210
            this, SIGNAL(fileCopied(QString)));
211
    return m_connection->transferFiles(m_deploySpecs);
ck's avatar
ck committed
212
213
214
215
}

} // namespace Internal
} // namespace Qt4ProjectManager