Add client pingpong functionality for longtime connection tests

This commit is contained in:
Simon Stürz 2018-08-16 23:03:28 +02:00
parent 60c3775dca
commit a0d5aadc6a
19 changed files with 103 additions and 27 deletions

View File

@ -107,6 +107,10 @@ int main(int argc, char *argv[])
QCommandLineOption insecureOption(QStringList() << "i" << "igore-ssl", "Ignore SSL certificate errors.");
parser.addOption(insecureOption);
QCommandLineOption pingPongOption(QStringList() << "p" << "pingpong", "Start a ping pong traffic trough the remote connection.");
parser.addOption(pingPongOption);
QCommandLineOption nameOption(QStringList() << "name", "The name of the client. Default nymea-remoteproxyclient", "name");
nameOption.setDefaultValue("nymea-remoteproxyclient");
parser.addOption(nameOption);
@ -154,6 +158,7 @@ int main(int argc, char *argv[])
ProxyClient client(parser.value(nameOption), uuid);
client.setInsecure(parser.isSet(insecureOption));
client.setPingpong(parser.isSet(pingPongOption));
client.start(serverUrl, parser.value(tokenOption));
return application.exec();

View File

@ -21,6 +21,8 @@
#include "proxyclient.h"
#include <QTimer>
Q_LOGGING_CATEGORY(dcProxyClient, "ProxyClient")
ProxyClient::ProxyClient(const QString &name, const QUuid &uuid, QObject *parent) :
@ -33,6 +35,7 @@ ProxyClient::ProxyClient(const QString &name, const QUuid &uuid, QObject *parent
connect(m_connection, &RemoteProxyConnection::ready, this, &ProxyClient::onClientReady);
connect(m_connection, &RemoteProxyConnection::authenticated, this, &ProxyClient::onAuthenticationFinished);
connect(m_connection, &RemoteProxyConnection::remoteConnectionEstablished, this, &ProxyClient::onRemoteConnectionEstablished);
connect(m_connection, &RemoteProxyConnection::dataReady, this, &ProxyClient::onDataReady);
connect(m_connection, &RemoteProxyConnection::errorOccured, this, &ProxyClient::onErrorOccured);
connect(m_connection, &RemoteProxyConnection::disconnected, this, &ProxyClient::onClientDisconnected);
connect(m_connection, &RemoteProxyConnection::sslErrors, this, &ProxyClient::onSslErrors);
@ -43,6 +46,11 @@ void ProxyClient::setInsecure(bool insecure)
m_insecure = insecure;
}
void ProxyClient::setPingpong(bool enable)
{
m_pingpong = enable;
}
void ProxyClient::onErrorOccured(RemoteProxyConnection::Error error)
{
qCWarning(dcProxyClient()) << "Error occured" << error << m_connection->errorString();
@ -66,6 +74,16 @@ void ProxyClient::onRemoteConnectionEstablished()
qCDebug(dcProxyClient()) << "----------------------------------------------------------------------------------";
qCDebug(dcProxyClient()) << "Remote connection established with" << m_connection->tunnelPartnerName() << m_connection->tunnelPartnerUuid();
qCDebug(dcProxyClient()) << "----------------------------------------------------------------------------------";
if (m_pingpong) sendPing();
}
void ProxyClient::onDataReady(const QByteArray &data)
{
qCDebug(dcProxyClient()) << "<--" << qUtf8Printable(data);
if (m_pingpong) {
QTimer::singleShot(1000, this, &ProxyClient::sendPing);
}
}
void ProxyClient::onClientDisconnected()
@ -87,6 +105,13 @@ void ProxyClient::onSslErrors(const QList<QSslError> errors)
}
}
void ProxyClient::sendPing()
{
QByteArray data(QString("Ping from " + m_name + "!").toUtf8());
qCDebug(dcProxyClient()) << "-->" << qUtf8Printable(data);
m_connection->sendData(data);
}
void ProxyClient::start(const QUrl &url, const QString &token)
{
m_token = token;

View File

@ -38,12 +38,14 @@ public:
explicit ProxyClient(const QString &name, const QUuid &uuid, QObject *parent = nullptr);
void setInsecure(bool insecure);
void setPingpong(bool enable);
private:
QString m_name;
QUuid m_uuid;
QString m_token;
bool m_insecure = false;
bool m_pingpong = false;
RemoteProxyConnection *m_connection = nullptr;
@ -52,9 +54,12 @@ private slots:
void onClientReady();
void onAuthenticationFinished();
void onRemoteConnectionEstablished();
void onDataReady(const QByteArray &data);
void onClientDisconnected();
void onSslErrors(const QList<QSslError> errors);
void sendPing();
public slots:
void start(const QUrl &url, const QString &token);

View File

@ -1,3 +1,24 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2018 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of nymea-remoteproxy. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "proxyclient.h"
#include "authenticator.h"
#include "authenticationreply.h"

View File

@ -129,6 +129,11 @@ void Engine::setDeveloperModeEnabled(bool enabled)
m_developerMode = enabled;
}
ProxyConfiguration *Engine::configuration() const
{
return m_configuration;
}
Authenticator *Engine::authenticator() const
{
return m_authenticator;

View File

@ -55,6 +55,7 @@ public:
void setAuthenticator(Authenticator *authenticator);
void setDeveloperModeEnabled(bool enabled);
ProxyConfiguration *configuration() const;
Authenticator *authenticator() const;
ProxyServer *proxyServer() const;
WebSocketServer *webSocketServer() const;

View File

@ -19,6 +19,7 @@
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "engine.h"
#include "monitorserver.h"
#include "loggingcategories.h"
@ -52,7 +53,10 @@ bool MonitorServer::running() const
QVariantMap MonitorServer::createMonitorData()
{
QVariantMap monitorData;
monitorData.insert("serverName", Engine::instance()->configuration()->serverName());
monitorData.insert("serverVersion", SERVER_VERSION_STRING);
monitorData.insert("apiVersion", API_VERSION_STRING);
monitorData.insert("proxyStatistic", Engine::instance()->proxyServer()->currentStatistics());
return monitorData;
}

View File

@ -59,6 +59,14 @@ void ProxyServer::registerTransportInterface(TransportInterface *interface)
m_transportInterfaces.append(interface);
}
QVariantMap ProxyServer::currentStatistics()
{
QVariantMap statisticsMap;
statisticsMap.insert("clientCount", m_proxyClients.count());
statisticsMap.insert("tunnelCount", m_tunnels.count());
return statisticsMap;
}
void ProxyServer::setRunning(bool running)
{
if (m_running == running)

View File

@ -43,6 +43,8 @@ public:
bool running() const;
void registerTransportInterface(TransportInterface *interface);
QVariantMap currentStatistics();
private:
JsonRpcServer *m_jsonRpcServer = nullptr;
QList<TransportInterface *> m_transportInterfaces;

View File

@ -149,22 +149,6 @@ QString RemoteProxyConnection::tunnelPartnerUuid() const
return m_tunnelPartnerUuid;
}
bool RemoteProxyConnection::sendData(const QByteArray &data)
{
if (!isConnected()) {
qCWarning(dcRemoteProxyClientConnection()) << "Could not send data. Not connected.";
return false;
}
if (!isRemoteConnected()) {
qCWarning(dcRemoteProxyClientConnection()) << "Could not send data. The remote client is not connected yet.";
return false;
}
m_connection->sendData(data);
return true;
}
void RemoteProxyConnection::cleanUp()
{
if (m_jsonClient) {
@ -381,4 +365,21 @@ void RemoteProxyConnection::disconnectServer()
}
}
bool RemoteProxyConnection::sendData(const QByteArray &data)
{
if (!isConnected()) {
qCWarning(dcRemoteProxyClientConnection()) << "Could not send data. Not connected.";
return false;
}
if (!isRemoteConnected()) {
qCWarning(dcRemoteProxyClientConnection()) << "Could not send data. The remote client is not connected yet.";
return false;
}
m_connection->sendData(data);
return true;
}
}

View File

@ -97,8 +97,6 @@ public:
QString tunnelPartnerName() const;
QString tunnelPartnerUuid() const;
bool sendData(const QByteArray &data);
private:
ConnectionType m_connectionType = ConnectionTypeWebSocket;
QUuid m_clientUuid;
@ -156,6 +154,7 @@ public slots:
bool connectServer(const QUrl &url);
bool authenticate(const QString &token);
void disconnectServer();
bool sendData(const QByteArray &data);
};

View File

@ -2,7 +2,7 @@
* *
* Copyright (C) 2018 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh-cloudproxy. *
* This file is part of nymea-remoteproxy. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *

View File

@ -2,7 +2,7 @@
* *
* Copyright (C) 2018 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh-cloudproxy. *
* This file is part of nymea-remoteproxy. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *

View File

@ -2,7 +2,7 @@
* *
* Copyright (C) 2018 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh-cloudproxy. *
* This file is part of nymea-remoteproxy. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *

View File

@ -2,7 +2,7 @@
* *
* Copyright (C) 2018 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh-cloudproxy. *
* This file is part of nymea-remoteproxy. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *

View File

@ -2,7 +2,7 @@
* *
* Copyright (C) 2018 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh-cloudproxy. *
* This file is part of nymea-remoteproxy. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *

View File

@ -2,7 +2,7 @@
* *
* Copyright (C) 2018 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh-cloudproxy. *
* This file is part of nymea-remoteproxy. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *

View File

@ -2,7 +2,7 @@
* *
* Copyright (C) 2018 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh-cloudproxy. *
* This file is part of nymea-remoteproxy. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *

View File

@ -2,7 +2,7 @@
* *
* Copyright (C) 2018 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh-cloudproxy. *
* This file is part of nymea-remoteproxy. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *