2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-03-09 12:07:35 +01:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-03-09 12:07:35 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-03-09 12:07:35 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:58:39 +01:00
|
|
|
** 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.
|
2011-03-09 12:07:35 +01:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** 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.
|
2011-03-09 12:07:35 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-05-06 15:05:37 +02:00
|
|
|
|
2011-03-09 12:07:35 +01:00
|
|
|
#include "sshconnectionmanager.h"
|
|
|
|
|
|
|
|
|
|
#include "sshconnection.h"
|
2018-11-23 11:07:57 +01:00
|
|
|
#include "sshsettings.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
2011-03-09 12:07:35 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QList>
|
|
|
|
|
#include <QMutex>
|
|
|
|
|
#include <QMutexLocker>
|
|
|
|
|
#include <QObject>
|
|
|
|
|
#include <QThread>
|
2014-11-14 17:09:44 +01:00
|
|
|
#include <QTimer>
|
2011-03-09 12:07:35 +01:00
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
namespace QSsh {
|
2011-03-09 12:07:35 +01:00
|
|
|
namespace Internal {
|
2014-11-14 17:09:44 +01:00
|
|
|
class UnaquiredConnection {
|
|
|
|
|
public:
|
|
|
|
|
UnaquiredConnection(SshConnection *conn) : connection(conn), scheduledForRemoval(false) {}
|
|
|
|
|
|
|
|
|
|
SshConnection *connection;
|
|
|
|
|
bool scheduledForRemoval;
|
|
|
|
|
};
|
|
|
|
|
bool operator==(const UnaquiredConnection &c1, const UnaquiredConnection &c2) {
|
|
|
|
|
return c1.connection == c2.connection;
|
|
|
|
|
}
|
|
|
|
|
bool operator!=(const UnaquiredConnection &c1, const UnaquiredConnection &c2) {
|
|
|
|
|
return !(c1 == c2);
|
|
|
|
|
}
|
2011-03-09 12:07:35 +01:00
|
|
|
|
2013-09-10 18:52:17 +02:00
|
|
|
class SshConnectionManager : public QObject
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
public:
|
2013-09-10 18:52:17 +02:00
|
|
|
SshConnectionManager()
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
|
|
|
|
moveToThread(QCoreApplication::instance()->thread());
|
2014-11-14 17:09:44 +01:00
|
|
|
connect(&m_removalTimer, &QTimer::timeout,
|
|
|
|
|
this, &SshConnectionManager::removeInactiveConnections);
|
2018-11-23 11:07:57 +01:00
|
|
|
m_removalTimer.start(SshSettings::connectionSharingTimeout() * 1000 * 60 / 2);
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-10 18:52:17 +02:00
|
|
|
~SshConnectionManager()
|
2012-05-29 13:22:33 +02:00
|
|
|
{
|
2014-11-14 17:09:44 +01:00
|
|
|
foreach (const UnaquiredConnection &connection, m_unacquiredConnections) {
|
2019-07-31 17:21:41 +02:00
|
|
|
disconnect(connection.connection, nullptr, this, nullptr);
|
2014-11-14 17:09:44 +01:00
|
|
|
delete connection.connection;
|
2012-05-29 13:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-23 11:07:57 +01:00
|
|
|
QTC_CHECK(m_acquiredConnections.isEmpty());
|
|
|
|
|
QTC_CHECK(m_deprecatedConnections.isEmpty());
|
2012-05-29 13:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SshConnection *acquireConnection(const SshConnectionParameters &sshParams)
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
2011-12-12 15:11:45 +01:00
|
|
|
QMutexLocker locker(&m_listMutex);
|
|
|
|
|
|
|
|
|
|
// Check in-use connections:
|
2012-05-29 13:22:33 +02:00
|
|
|
foreach (SshConnection * const connection, m_acquiredConnections) {
|
2011-12-12 15:11:45 +01:00
|
|
|
if (connection->connectionParameters() != sshParams)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (connection->thread() != QThread::currentThread())
|
2014-12-18 14:15:12 +01:00
|
|
|
continue;
|
2011-12-12 15:11:45 +01:00
|
|
|
|
2018-11-23 11:07:57 +01:00
|
|
|
if (connection->sharingEnabled() != SshSettings::connectionSharingEnabled())
|
|
|
|
|
continue;
|
|
|
|
|
|
2011-12-12 15:11:45 +01:00
|
|
|
if (m_deprecatedConnections.contains(connection)) // we were asked to no longer use this one...
|
2014-12-18 14:15:12 +01:00
|
|
|
continue;
|
2011-12-12 15:11:45 +01:00
|
|
|
|
|
|
|
|
m_acquiredConnections.append(connection);
|
|
|
|
|
return connection;
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-14 17:09:44 +01:00
|
|
|
// Check cached open connections:
|
|
|
|
|
foreach (const UnaquiredConnection &c, m_unacquiredConnections) {
|
|
|
|
|
SshConnection * const connection = c.connection;
|
2011-12-12 15:11:45 +01:00
|
|
|
if (connection->state() != SshConnection::Connected
|
|
|
|
|
|| connection->connectionParameters() != sshParams)
|
|
|
|
|
continue;
|
|
|
|
|
|
2020-11-11 16:34:39 +01:00
|
|
|
auto currentThread = QThread::currentThread();
|
|
|
|
|
if (connection->thread() != currentThread) {
|
|
|
|
|
QMetaObject::invokeMethod(this, [this, connection, currentThread] {
|
|
|
|
|
switchToCallerThread(connection, currentThread);
|
|
|
|
|
}, Qt::BlockingQueuedConnection);
|
2011-12-12 15:11:45 +01:00
|
|
|
}
|
2011-03-09 12:07:35 +01:00
|
|
|
|
2014-11-14 17:09:44 +01:00
|
|
|
m_unacquiredConnections.removeOne(c);
|
2011-12-12 15:11:45 +01:00
|
|
|
m_acquiredConnections.append(connection);
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create a new connection:
|
2012-05-29 13:22:33 +02:00
|
|
|
SshConnection * const connection = new SshConnection(sshParams);
|
2015-02-04 10:11:46 +01:00
|
|
|
connect(connection, &SshConnection::disconnected,
|
|
|
|
|
this, &SshConnectionManager::cleanup);
|
2021-01-28 16:12:30 +01:00
|
|
|
if (SshSettings::connectionSharingEnabled())
|
|
|
|
|
m_acquiredConnections.append(connection);
|
2011-12-12 15:11:45 +01:00
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-29 13:22:33 +02:00
|
|
|
void releaseConnection(SshConnection *connection)
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_listMutex);
|
2011-12-12 15:11:45 +01:00
|
|
|
|
2012-05-29 13:22:33 +02:00
|
|
|
const bool wasAquired = m_acquiredConnections.removeOne(connection);
|
2021-01-28 16:12:30 +01:00
|
|
|
QTC_ASSERT(wasAquired == connection->sharingEnabled(), return);
|
2012-05-29 13:22:33 +02:00
|
|
|
if (m_acquiredConnections.contains(connection))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
bool doDelete = false;
|
|
|
|
|
connection->moveToThread(QCoreApplication::instance()->thread());
|
2021-01-28 16:12:30 +01:00
|
|
|
if (!connection->sharingEnabled()) {
|
|
|
|
|
doDelete = true;
|
|
|
|
|
} else if (m_deprecatedConnections.removeOne(connection)
|
2012-05-29 13:22:33 +02:00
|
|
|
|| connection->state() != SshConnection::Connected) {
|
|
|
|
|
doDelete = true;
|
|
|
|
|
} else {
|
2018-11-23 11:07:57 +01:00
|
|
|
QTC_ASSERT(!m_unacquiredConnections.contains(UnaquiredConnection(connection)), return);
|
2012-05-29 13:22:33 +02:00
|
|
|
|
|
|
|
|
// It can happen that two or more connections with the same parameters were acquired
|
|
|
|
|
// if the clients were running in different threads. Only keep one of them in
|
|
|
|
|
// such a case.
|
|
|
|
|
bool haveConnection = false;
|
2014-11-14 17:09:44 +01:00
|
|
|
foreach (const UnaquiredConnection &c, m_unacquiredConnections) {
|
|
|
|
|
if (c.connection->connectionParameters() == connection->connectionParameters()) {
|
2012-05-29 13:22:33 +02:00
|
|
|
haveConnection = true;
|
|
|
|
|
break;
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-11-23 11:07:57 +01:00
|
|
|
if (!haveConnection)
|
2014-11-14 17:09:44 +01:00
|
|
|
m_unacquiredConnections.append(UnaquiredConnection(connection));
|
2018-11-23 11:07:57 +01:00
|
|
|
else
|
2012-05-29 13:22:33 +02:00
|
|
|
doDelete = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doDelete) {
|
2019-07-31 17:21:41 +02:00
|
|
|
disconnect(connection, nullptr, this, nullptr);
|
2012-05-29 13:22:33 +02:00
|
|
|
m_deprecatedConnections.removeAll(connection);
|
2012-06-05 15:18:55 +02:00
|
|
|
connection->deleteLater();
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-12 15:11:45 +01:00
|
|
|
void forceNewConnection(const SshConnectionParameters &sshParams)
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_listMutex);
|
2011-12-12 15:11:45 +01:00
|
|
|
|
2012-05-29 13:22:33 +02:00
|
|
|
for (int i = 0; i < m_unacquiredConnections.count(); ++i) {
|
2014-11-14 17:09:44 +01:00
|
|
|
SshConnection * const connection = m_unacquiredConnections.at(i).connection;
|
2011-12-12 15:11:45 +01:00
|
|
|
if (connection->connectionParameters() == sshParams) {
|
2019-07-31 17:21:41 +02:00
|
|
|
disconnect(connection, nullptr, this, nullptr);
|
2012-05-29 13:22:33 +02:00
|
|
|
delete connection;
|
|
|
|
|
m_unacquiredConnections.removeAt(i);
|
2011-12-12 15:11:45 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-29 13:22:33 +02:00
|
|
|
foreach (SshConnection * const connection, m_acquiredConnections) {
|
|
|
|
|
if (connection->connectionParameters() == sshParams) {
|
|
|
|
|
if (!m_deprecatedConnections.contains(connection))
|
|
|
|
|
m_deprecatedConnections.append(connection);
|
2011-12-12 15:11:45 +01:00
|
|
|
}
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Q_INVOKABLE void switchToCallerThread(SshConnection *connection, QObject *threadObj)
|
|
|
|
|
{
|
|
|
|
|
connection->moveToThread(qobject_cast<QThread *>(threadObj));
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-12 15:11:45 +01:00
|
|
|
void cleanup()
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_listMutex);
|
2011-12-12 15:11:45 +01:00
|
|
|
|
2011-12-13 12:16:21 +01:00
|
|
|
SshConnection *currentConnection = qobject_cast<SshConnection *>(sender());
|
|
|
|
|
if (!currentConnection)
|
2011-12-12 15:11:45 +01:00
|
|
|
return;
|
|
|
|
|
|
2014-11-14 17:09:44 +01:00
|
|
|
if (m_unacquiredConnections.removeOne(UnaquiredConnection(currentConnection))) {
|
2019-07-31 17:21:41 +02:00
|
|
|
disconnect(currentConnection, nullptr, this, nullptr);
|
2012-05-29 13:22:33 +02:00
|
|
|
currentConnection->deleteLater();
|
2011-12-13 12:16:21 +01:00
|
|
|
}
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-14 17:09:44 +01:00
|
|
|
void removeInactiveConnections()
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_listMutex);
|
|
|
|
|
for (int i = m_unacquiredConnections.count() - 1; i >= 0; --i) {
|
|
|
|
|
UnaquiredConnection &c = m_unacquiredConnections[i];
|
|
|
|
|
if (c.scheduledForRemoval) {
|
2019-07-31 17:21:41 +02:00
|
|
|
disconnect(c.connection, nullptr, this, nullptr);
|
2014-11-14 17:09:44 +01:00
|
|
|
c.connection->deleteLater();
|
|
|
|
|
m_unacquiredConnections.removeAt(i);
|
|
|
|
|
} else {
|
|
|
|
|
c.scheduledForRemoval = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-12 15:11:45 +01:00
|
|
|
private:
|
2011-03-09 12:07:35 +01:00
|
|
|
// We expect the number of concurrently open connections to be small.
|
|
|
|
|
// If that turns out to not be the case, we can still use a data
|
|
|
|
|
// structure with faster access.
|
2014-11-14 17:09:44 +01:00
|
|
|
QList<UnaquiredConnection> m_unacquiredConnections;
|
2012-05-29 13:22:33 +02:00
|
|
|
|
|
|
|
|
// Can contain the same connection more than once; this acts as a reference count.
|
|
|
|
|
QList<SshConnection *> m_acquiredConnections;
|
|
|
|
|
|
|
|
|
|
QList<SshConnection *> m_deprecatedConnections;
|
2011-03-09 12:07:35 +01:00
|
|
|
QMutex m_listMutex;
|
2014-11-14 17:09:44 +01:00
|
|
|
QTimer m_removalTimer;
|
2011-03-09 12:07:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
|
2013-09-10 18:52:17 +02:00
|
|
|
static QMutex instanceMutex;
|
2011-03-09 12:07:35 +01:00
|
|
|
|
2013-09-10 18:52:17 +02:00
|
|
|
static Internal::SshConnectionManager &instance()
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
2013-09-10 18:52:17 +02:00
|
|
|
static Internal::SshConnectionManager manager;
|
|
|
|
|
return manager;
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-10 18:52:17 +02:00
|
|
|
SshConnection *acquireConnection(const SshConnectionParameters &sshParams)
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
2013-09-10 18:52:17 +02:00
|
|
|
QMutexLocker locker(&instanceMutex);
|
|
|
|
|
return instance().acquireConnection(sshParams);
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-10 18:52:17 +02:00
|
|
|
void releaseConnection(SshConnection *connection)
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
2013-09-10 18:52:17 +02:00
|
|
|
QMutexLocker locker(&instanceMutex);
|
|
|
|
|
instance().releaseConnection(connection);
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-10 18:52:17 +02:00
|
|
|
void forceNewConnection(const SshConnectionParameters &sshParams)
|
2011-12-12 15:11:45 +01:00
|
|
|
{
|
2013-09-10 18:52:17 +02:00
|
|
|
QMutexLocker locker(&instanceMutex);
|
|
|
|
|
instance().forceNewConnection(sshParams);
|
2011-12-12 15:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
} // namespace QSsh
|
2011-03-09 12:07:35 +01:00
|
|
|
|
|
|
|
|
#include "sshconnectionmanager.moc"
|