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>
|
2021-12-15 21:25:24 +01:00
|
|
|
#include <QHash>
|
2012-02-15 10:42:41 +01:00
|
|
|
#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 {
|
2021-09-09 15:29:28 +02:00
|
|
|
|
|
|
|
|
class SshConnectionManagerPrivate : public QObject
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2021-09-09 15:29:28 +02:00
|
|
|
SshConnectionManagerPrivate()
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
2014-11-14 17:09:44 +01:00
|
|
|
connect(&m_removalTimer, &QTimer::timeout,
|
2021-09-09 15:29:28 +02:00
|
|
|
this, &SshConnectionManagerPrivate::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
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:29:28 +02:00
|
|
|
~SshConnectionManagerPrivate() override
|
2012-05-29 13:22:33 +02:00
|
|
|
{
|
2021-12-15 21:25:24 +01:00
|
|
|
for (auto it = m_connections.cbegin(); it != m_connections.cend(); ++it) {
|
|
|
|
|
SshConnection * const connection = it.key();
|
|
|
|
|
const SshConnectionState &state = it.value();
|
|
|
|
|
QTC_CHECK(state.refCount() == 0);
|
|
|
|
|
QTC_CHECK(!state.isStale());
|
|
|
|
|
disconnect(connection, nullptr, this, nullptr);
|
|
|
|
|
delete connection;
|
2012-05-29 13:22:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SshConnection *acquireConnection(const SshConnectionParameters &sshParams)
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
2021-12-15 21:25:24 +01:00
|
|
|
if (SshSettings::connectionSharingEnabled()) {
|
|
|
|
|
for (auto it = m_connections.begin(); it != m_connections.end(); ++it) {
|
|
|
|
|
SshConnection * const connection = it.key();
|
|
|
|
|
if (connection->connectionParameters() != sshParams)
|
|
|
|
|
continue;
|
2011-12-12 15:11:45 +01:00
|
|
|
|
2021-12-15 21:25:24 +01:00
|
|
|
SshConnectionState &state = it.value();
|
|
|
|
|
if (state.isStale())
|
|
|
|
|
continue;
|
2011-03-09 12:07:35 +01:00
|
|
|
|
2021-12-15 21:25:24 +01:00
|
|
|
if (state.refCount() == 0 && connection->state() != SshConnection::Connected)
|
|
|
|
|
continue;
|
2011-12-12 15:11:45 +01:00
|
|
|
|
2021-12-15 21:25:24 +01:00
|
|
|
state.ref();
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
2011-12-12 15:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-29 13:22:33 +02:00
|
|
|
SshConnection * const connection = new SshConnection(sshParams);
|
2021-12-15 21:25:24 +01:00
|
|
|
if (SshSettings::connectionSharingEnabled()) {
|
|
|
|
|
connect(connection, &SshConnection::disconnected,
|
|
|
|
|
this, [this, connection] { cleanup(connection); });
|
|
|
|
|
m_connections.insert(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
|
|
|
{
|
2021-12-15 21:25:24 +01:00
|
|
|
auto it = m_connections.find(connection);
|
2012-05-29 13:22:33 +02:00
|
|
|
bool doDelete = false;
|
2021-12-15 21:25:24 +01:00
|
|
|
if (it == m_connections.end()) {
|
|
|
|
|
QTC_ASSERT(!connection->sharingEnabled(), return);
|
2012-05-29 13:22:33 +02:00
|
|
|
doDelete = true;
|
|
|
|
|
} else {
|
2021-12-15 21:25:24 +01:00
|
|
|
SshConnectionState &state = it.value();
|
|
|
|
|
if (state.deref())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (state.isStale() || connection->state() != SshConnection::Connected) {
|
|
|
|
|
doDelete = true;
|
|
|
|
|
m_connections.erase(it);
|
|
|
|
|
}
|
2012-05-29 13:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doDelete) {
|
2019-07-31 17:21:41 +02:00
|
|
|
disconnect(connection, nullptr, this, nullptr);
|
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
|
|
|
{
|
2021-12-15 21:25:24 +01:00
|
|
|
auto it = m_connections.begin();
|
|
|
|
|
while (it != m_connections.end()) {
|
|
|
|
|
SshConnection * const connection = it.key();
|
|
|
|
|
if (connection->connectionParameters() != sshParams) {
|
|
|
|
|
++it;
|
|
|
|
|
continue;
|
2011-12-12 15:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 21:25:24 +01:00
|
|
|
SshConnectionState &state = it.value();
|
|
|
|
|
if (state.refCount()) {
|
|
|
|
|
state.makeStale();
|
|
|
|
|
++it;
|
|
|
|
|
continue;
|
2011-12-12 15:11:45 +01:00
|
|
|
}
|
2021-12-15 21:25:24 +01:00
|
|
|
|
|
|
|
|
disconnect(connection, nullptr, this, nullptr);
|
|
|
|
|
delete connection;
|
|
|
|
|
it = m_connections.erase(it);
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2021-12-15 21:25:24 +01:00
|
|
|
void cleanup(SshConnection *connection)
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
2021-12-15 21:25:24 +01:00
|
|
|
auto it = m_connections.find(connection);
|
|
|
|
|
if (it == m_connections.end())
|
2011-12-12 15:11:45 +01:00
|
|
|
return;
|
|
|
|
|
|
2021-12-15 21:25:24 +01:00
|
|
|
SshConnectionState &state = it.value();
|
|
|
|
|
if (state.refCount())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
disconnect(connection, nullptr, this, nullptr);
|
|
|
|
|
connection->deleteLater();
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-14 17:09:44 +01:00
|
|
|
void removeInactiveConnections()
|
|
|
|
|
{
|
2021-12-15 21:25:24 +01:00
|
|
|
auto it = m_connections.begin();
|
|
|
|
|
while (it != m_connections.end()) {
|
|
|
|
|
SshConnection * const connection = it.key();
|
|
|
|
|
SshConnectionState &state = it.value();
|
|
|
|
|
if (state.refCount() == 0 && state.scheduleForRemoval()) {
|
|
|
|
|
disconnect(connection, nullptr, this, nullptr);
|
|
|
|
|
connection->deleteLater();
|
|
|
|
|
it = m_connections.erase(it);
|
2014-11-14 17:09:44 +01:00
|
|
|
} else {
|
2021-12-15 21:25:24 +01:00
|
|
|
++it;
|
2014-11-14 17:09:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-12 15:11:45 +01:00
|
|
|
private:
|
2021-12-15 21:25:24 +01:00
|
|
|
struct SshConnectionState {
|
|
|
|
|
void ref() { ++m_ref; m_scheduledForRemoval = false; }
|
|
|
|
|
bool deref() { QTC_ASSERT(m_ref, return false); return --m_ref; }
|
|
|
|
|
int refCount() const { return m_ref; }
|
|
|
|
|
|
|
|
|
|
void makeStale() { m_isStale = true; }
|
|
|
|
|
bool isStale() const { return m_isStale; }
|
|
|
|
|
|
|
|
|
|
bool scheduleForRemoval()
|
|
|
|
|
{
|
|
|
|
|
const bool ret = m_scheduledForRemoval;
|
|
|
|
|
m_scheduledForRemoval = true;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
int m_ref = 1; // 0 means unacquired connection
|
|
|
|
|
bool m_isStale = false;
|
|
|
|
|
bool m_scheduledForRemoval = false;
|
|
|
|
|
};
|
2012-05-29 13:22:33 +02:00
|
|
|
|
2021-12-15 21:25:24 +01:00
|
|
|
QHash<SshConnection *, SshConnectionState> m_connections;
|
2014-11-14 17:09:44 +01:00
|
|
|
QTimer m_removalTimer;
|
2011-03-09 12:07:35 +01:00
|
|
|
};
|
|
|
|
|
|
2021-09-09 15:29:28 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
|
|
SshConnectionManager::SshConnectionManager()
|
|
|
|
|
: d(new Internal::SshConnectionManagerPrivate())
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
2021-09-17 16:32:50 +02:00
|
|
|
QTC_CHECK(QThread::currentThread() == qApp->thread());
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:29:28 +02:00
|
|
|
SshConnectionManager::~SshConnectionManager()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
2021-09-09 15:29:28 +02:00
|
|
|
|
2021-09-09 15:29:28 +02:00
|
|
|
SshConnection *SshConnectionManager::acquireConnection(const SshConnectionParameters &sshParams)
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
2021-09-17 16:32:50 +02:00
|
|
|
return instance()->d->acquireConnection(sshParams);
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:29:28 +02:00
|
|
|
void SshConnectionManager::releaseConnection(SshConnection *connection)
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
2021-09-17 16:32:50 +02:00
|
|
|
instance()->d->releaseConnection(connection);
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:29:28 +02:00
|
|
|
void SshConnectionManager::forceNewConnection(const SshConnectionParameters &sshParams)
|
2011-12-12 15:11:45 +01:00
|
|
|
{
|
2021-09-17 16:32:50 +02:00
|
|
|
instance()->d->forceNewConnection(sshParams);
|
2011-12-12 15:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
} // namespace QSsh
|