diff options
author | kh1 <karsten.heimrich@digia.com> | 2014-06-03 10:59:40 +0200 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@digia.com> | 2014-06-03 13:04:45 +0200 |
commit | 7c07130119722e2e0afcf423f23a449f93931dfa (patch) | |
tree | f82909d57cfec2da8e7377376d6dffac75b05aa4 /src/libs/installer/remoteclient.cpp | |
parent | 00c6f94d1d6b7bce7c7339870bcad3d76b7cc390 (diff) |
Introduce new classes for client-server communication.
Still based on what we had already, though more separated.
Change-Id: I4cce298003a4ffc2ebcec01fea1a07adfbfdf990
Reviewed-by: Tim Jenssen <tim.jenssen@digia.com>
Reviewed-by: Niels Weber <niels.weber@digia.com>
Diffstat (limited to 'src/libs/installer/remoteclient.cpp')
-rw-r--r-- | src/libs/installer/remoteclient.cpp | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/src/libs/installer/remoteclient.cpp b/src/libs/installer/remoteclient.cpp new file mode 100644 index 000000000..60ccb3409 --- /dev/null +++ b/src/libs/installer/remoteclient.cpp @@ -0,0 +1,160 @@ +/************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Installer Framework. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +**************************************************************************/ + +#include "remoteclient.h" + +#include "protocol.h" +#include "remoteclient_p.h" + +#include <QElapsedTimer> +#include <QUuid> + +namespace QInstaller { + +RemoteClient::RemoteClient() + : d_ptr(new RemoteClientPrivate(this)) +{ + Q_D(RemoteClient); + d->m_key = QUuid::createUuid().toString(); +} + +RemoteClient::~RemoteClient() +{ + Q_D(RemoteClient); + d->m_quit = true; +} + +RemoteClient &RemoteClient::instance() +{ + static RemoteClient instance; + return instance; +} + +QString RemoteClient::authorizationKey() const +{ + Q_D(const RemoteClient); + return d->m_key; +} + +void RemoteClient::setAuthorizationKey(const QString &key) +{ + Q_D(RemoteClient); + if (d->m_serverStarted) + return; + d->m_key = key; +} + +void RemoteClient::init(quint16 port, const QHostAddress &address, Mode mode) +{ + Q_D(RemoteClient); + d->init(port, address, mode); +} + +QTcpSocket *RemoteClient::connect() const +{ + Q_D(const RemoteClient); + if (d->m_quit) + return 0; + + int tries = 3; + QScopedPointer<QTcpSocket> socket(new QTcpSocket); + while ((tries > 0) && (!d->m_quit)) { + socket->connectToHost(d->m_address, d->m_port); + + QElapsedTimer stopWatch; + stopWatch.start(); + while ((socket->state() == QAbstractSocket::ConnectingState) + && (stopWatch.elapsed() < 10000) && (!d->m_quit)) { + --tries; + qApp->processEvents(); + continue; + } + if ((socket->state() != QAbstractSocket::ConnectedState) || d->m_quit) + return 0; + + QDataStream stream; + stream.setDevice(socket.data()); + stream << QString::fromLatin1(Protocol::Authorize) << d->m_key; + + socket->waitForBytesWritten(-1); + if (!socket->bytesAvailable()) + socket->waitForReadyRead(-1); + + quint32 size; stream >> size; + bool authorized; stream >> authorized; + if (authorized && (!d->m_quit)) + return socket.take(); + } + return 0; +} + +bool RemoteClient::isActive() const +{ + Q_D(const RemoteClient); + return d->m_active; +} + +void RemoteClient::setActive(bool active) +{ + Q_D(RemoteClient); + d->m_active = active; + if (d->m_active) { + d->maybeStartServer(); + d->m_active = d->m_serverStarted; + } +} + +void RemoteClient::setStartServerCommand(const QString &command, StartAs startAs) +{ + setStartServerCommand(command, QStringList(), startAs); +} + +void RemoteClient::setStartServerCommand(const QString &command, const QStringList &arguments, + StartAs startAs) +{ + Q_D(RemoteClient); + d->maybeStopServer(); + d->m_serverCommand = command; + d->m_serverArguments = arguments; + d->m_startServerAsAdmin = startAs; +} + +} // namespace QInstaller |