From f28ac0f4e358748e4d91c9add01f660b45ed5206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Fri, 17 Aug 2018 15:54:59 +0200 Subject: [PATCH] Update socket error and documentation --- README.md | 6 ++-- client/proxyclient.cpp | 1 - libnymea-remoteproxy/monitorserver.cpp | 2 +- libnymea-remoteproxy/proxyclient.cpp | 28 +++++++++++++--- libnymea-remoteproxy/proxyclient.h | 12 ++++++- libnymea-remoteproxy/proxyserver.cpp | 6 ++-- libnymea-remoteproxy/proxyserver.h | 2 +- libnymea-remoteproxy/transportinterface.h | 3 +- libnymea-remoteproxy/websocketserver.cpp | 2 +- libnymea-remoteproxyclient/proxyconnection.h | 2 +- .../remoteproxyconnection.cpp | 6 ++-- .../remoteproxyconnection.h | 4 +-- .../websocketconnection.cpp | 2 +- .../websocketconnection.h | 1 + .../nymea-remoteproxy-tests-offline.cpp | 33 +++++++++++-------- .../nymea-remoteproxy-tests-online.h | 1 + 16 files changed, 74 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index bb046e6..e3f3ec3 100644 --- a/README.md +++ b/README.md @@ -391,15 +391,15 @@ Once the server is up and running with the dummy authenticator, you can try to c > *Note:* assuming you are starting the client on the same system as the server: - $ nymea-remoteproxy-client -u wss://127.0.0.1:443 -t "dummytoken" + $ nymea-remoteproxy-client -i -u wss://127.0.0.1:443 -t "dummytoken" Open a second terminal and start the same command again. > *Note:* assuming you are starting the client on the same system as the server: - $ nymea-remoteproxy-client -u wss://127.0.0.1:443 -t "dummytoken" + $ nymea-remoteproxy-client -i -u wss://127.0.0.1:443 -t "dummytoken" -You can follow the connection flow on all parts using the `--very-verbose` option. +You can follow the connection flow on both sides using the `--very-verbose` option. # License diff --git a/client/proxyclient.cpp b/client/proxyclient.cpp index 4ffd656..20f1d7b 100644 --- a/client/proxyclient.cpp +++ b/client/proxyclient.cpp @@ -80,7 +80,6 @@ void ProxyClient::onRemoteConnectionEstablished() void ProxyClient::onDataReady(const QByteArray &data) { qCDebug(dcProxyClient()) << "<--" << qUtf8Printable(data); - if (m_pingpong) { QTimer::singleShot(1000, this, &ProxyClient::sendPing); } diff --git a/libnymea-remoteproxy/monitorserver.cpp b/libnymea-remoteproxy/monitorserver.cpp index 18dc0c8..d6428b9 100644 --- a/libnymea-remoteproxy/monitorserver.cpp +++ b/libnymea-remoteproxy/monitorserver.cpp @@ -103,7 +103,7 @@ void MonitorServer::onMonitorDisconnected() } void MonitorServer::startServer() -{ +{ qCDebug(dcMonitorServer()) << "Starting server on" << m_serverName; m_server = new QLocalServer(this); if (!m_server->listen(m_serverName)) { diff --git a/libnymea-remoteproxy/proxyclient.cpp b/libnymea-remoteproxy/proxyclient.cpp index 76ebe40..bf660e7 100644 --- a/libnymea-remoteproxy/proxyclient.cpp +++ b/libnymea-remoteproxy/proxyclient.cpp @@ -21,14 +21,17 @@ #include "proxyclient.h" +#include + namespace remoteproxy { -ProxyClient::ProxyClient(TransportInterface *interface, const QUuid &clientId, QObject *parent) : +ProxyClient::ProxyClient(TransportInterface *interface, const QUuid &clientId, const QHostAddress &address, QObject *parent) : QObject(parent), m_interface(interface), - m_clientId(clientId) + m_clientId(clientId), + m_peerAddress(address) { - + m_creationTimeStamp = QDateTime::currentDateTime().toTime_t(); } QUuid ProxyClient::clientId() const @@ -36,6 +39,21 @@ QUuid ProxyClient::clientId() const return m_clientId; } +QHostAddress ProxyClient::peerAddress() const +{ + return m_peerAddress; +} + +uint ProxyClient::creationTime() const +{ + return m_creationTimeStamp; +} + +QString ProxyClient::creationTimeString() const +{ + return QDateTime::fromTime_t(creationTime()).toString("dd.MM.yyyy hh:mm:ss"); +} + bool ProxyClient::isAuthenticated() const { return m_authenticated; @@ -102,7 +120,9 @@ void ProxyClient::setToken(const QString &token) QDebug operator<<(QDebug debug, ProxyClient *proxyClient) { debug.nospace() << "ProxyClient(" << proxyClient->interface()->serverName(); - debug.nospace() << ", " << proxyClient->clientId().toString() << ") "; + debug.nospace() << ", " << proxyClient->clientId().toString(); + debug.nospace() << ", " << proxyClient->peerAddress().toString() << ") "; + debug.nospace() << ", " << proxyClient->creationTimeString() << ") "; return debug; } diff --git a/libnymea-remoteproxy/proxyclient.h b/libnymea-remoteproxy/proxyclient.h index c7c6009..3fd7d6f 100644 --- a/libnymea-remoteproxy/proxyclient.h +++ b/libnymea-remoteproxy/proxyclient.h @@ -25,6 +25,7 @@ #include #include #include +#include #include "transportinterface.h" @@ -35,9 +36,13 @@ class ProxyClient : public QObject Q_OBJECT public: - explicit ProxyClient(TransportInterface *interface, const QUuid &clientId, QObject *parent = nullptr); + explicit ProxyClient(TransportInterface *interface, const QUuid &clientId, const QHostAddress &address, QObject *parent = nullptr); QUuid clientId() const; + QHostAddress peerAddress() const; + + uint creationTime() const; + QString creationTimeString() const; bool isAuthenticated() const; void setAuthenticated(bool isAuthenticated); @@ -60,8 +65,13 @@ public: private: TransportInterface *m_interface = nullptr; QUuid m_clientId; + QHostAddress m_peerAddress; + + uint m_creationTimeStamp = 0; + bool m_authenticated = false; bool m_tunnelConnected = false; + QString m_uuid; QString m_name; QString m_token; diff --git a/libnymea-remoteproxy/proxyserver.cpp b/libnymea-remoteproxy/proxyserver.cpp index b065491..485e050 100644 --- a/libnymea-remoteproxy/proxyserver.cpp +++ b/libnymea-remoteproxy/proxyserver.cpp @@ -140,13 +140,13 @@ void ProxyServer::establishTunnel(ProxyClient *firstClient, ProxyClient *secondC Q_ARG(ProxyClient *, tunnel.clientTwo())); } -void ProxyServer::onClientConnected(const QUuid &clientId) +void ProxyServer::onClientConnected(const QUuid &clientId, const QHostAddress &address) { TransportInterface *interface = static_cast(sender()); - qCDebug(dcProxyServer()) << "New client connected" << interface->serverName() << clientId.toString(); + qCDebug(dcProxyServer()) << "New client connected" << interface->serverName() << clientId.toString() << address.toString(); - ProxyClient *proxyClient = new ProxyClient(interface, clientId, this); + ProxyClient *proxyClient = new ProxyClient(interface, clientId, address, this); connect(proxyClient, &ProxyClient::authenticated, this, &ProxyServer::onProxyClientAuthenticated); connect(proxyClient, &ProxyClient::tunnelConnected, this, &ProxyServer::onProxyClientTunnelConnected); diff --git a/libnymea-remoteproxy/proxyserver.h b/libnymea-remoteproxy/proxyserver.h index 9879d3f..166baa0 100644 --- a/libnymea-remoteproxy/proxyserver.h +++ b/libnymea-remoteproxy/proxyserver.h @@ -71,7 +71,7 @@ signals: void runningChanged(); private slots: - void onClientConnected(const QUuid &clientId); + void onClientConnected(const QUuid &clientId, const QHostAddress &address); void onClientDisconnected(const QUuid &clientId); void onClientDataAvailable(const QUuid &clientId, const QByteArray &data); diff --git a/libnymea-remoteproxy/transportinterface.h b/libnymea-remoteproxy/transportinterface.h index b9e5a98..b17dfac 100644 --- a/libnymea-remoteproxy/transportinterface.h +++ b/libnymea-remoteproxy/transportinterface.h @@ -23,6 +23,7 @@ #define TRANSPORTINTERFACE_H #include +#include namespace remoteproxy { @@ -41,7 +42,7 @@ public: virtual void killClientConnection(const QUuid &clientId, const QString &killReason) = 0; signals: - void clientConnected(const QUuid &clientId); + void clientConnected(const QUuid &clientId, const QHostAddress &address); void clientDisconnected(const QUuid &clientId); void dataAvailable(const QUuid &clientId, const QByteArray &data); diff --git a/libnymea-remoteproxy/websocketserver.cpp b/libnymea-remoteproxy/websocketserver.cpp index c2edc5a..c73f1ff 100644 --- a/libnymea-remoteproxy/websocketserver.cpp +++ b/libnymea-remoteproxy/websocketserver.cpp @@ -116,7 +116,7 @@ void WebSocketServer::onClientConnected() connect(client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onClientError(QAbstractSocket::SocketError))); connect(client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected())); - emit clientConnected(clientId); + emit clientConnected(clientId, client->peerAddress()); } void WebSocketServer::onClientDisconnected() diff --git a/libnymea-remoteproxyclient/proxyconnection.h b/libnymea-remoteproxyclient/proxyconnection.h index eaaf29e..f71de17 100644 --- a/libnymea-remoteproxyclient/proxyconnection.h +++ b/libnymea-remoteproxyclient/proxyconnection.h @@ -53,7 +53,7 @@ protected: signals: void connectedChanged(bool connected); void dataReceived(const QByteArray &data); - void errorOccured(); + void errorOccured(QAbstractSocket::SocketError error); void sslErrors(const QList &errors); public slots: diff --git a/libnymea-remoteproxyclient/remoteproxyconnection.cpp b/libnymea-remoteproxyclient/remoteproxyconnection.cpp index 58e4f63..d70d773 100644 --- a/libnymea-remoteproxyclient/remoteproxyconnection.cpp +++ b/libnymea-remoteproxyclient/remoteproxyconnection.cpp @@ -244,8 +244,9 @@ void RemoteProxyConnection::onConnectionDataAvailable(const QByteArray &data) } } -void RemoteProxyConnection::onConnectionSocketError() +void RemoteProxyConnection::onConnectionSocketError(QAbstractSocket::SocketError error) { + emit socketErrorOccured(error); setError(ErrorSocketError); } @@ -305,9 +306,8 @@ void RemoteProxyConnection::onTunnelEstablished(const QString &clientName, const bool RemoteProxyConnection::connectServer(const QUrl &url) { - // Verify url - // FIXME: support also tcp if (url.scheme() != "wss") { + // FIXME: support also tcp qCWarning(dcRemoteProxyClientConnection()) << "Unsupported connection type" << url.scheme() << "Default to wss"; m_serverUrl.setScheme("wss"); } diff --git a/libnymea-remoteproxyclient/remoteproxyconnection.h b/libnymea-remoteproxyclient/remoteproxyconnection.h index 1c8fc67..972db89 100644 --- a/libnymea-remoteproxyclient/remoteproxyconnection.h +++ b/libnymea-remoteproxyclient/remoteproxyconnection.h @@ -49,7 +49,6 @@ public: Q_ENUM(ConnectionType) enum State { - StateHostLookup, StateConnecting, StateConnected, StateInitializing, @@ -137,6 +136,7 @@ signals: void stateChanged(RemoteProxyConnection::State state); void errorOccured(RemoteProxyConnection::Error error); + void socketErrorOccured(QAbstractSocket::SocketError error); void sslErrors(const QList &errors); void dataReady(const QByteArray &data); @@ -144,7 +144,7 @@ signals: private slots: void onConnectionChanged(bool isConnected); void onConnectionDataAvailable(const QByteArray &data); - void onConnectionSocketError(); + void onConnectionSocketError(QAbstractSocket::SocketError error); void onHelloFinished(); void onAuthenticateFinished(); diff --git a/libnymea-remoteproxyclient/websocketconnection.cpp b/libnymea-remoteproxyclient/websocketconnection.cpp index 16c94b8..157c85d 100644 --- a/libnymea-remoteproxyclient/websocketconnection.cpp +++ b/libnymea-remoteproxyclient/websocketconnection.cpp @@ -73,7 +73,7 @@ void WebSocketConnection::onDisconnected() void WebSocketConnection::onError(QAbstractSocket::SocketError error) { qCDebug(dcRemoteProxyClientWebSocket()) << "Socket error occured" << error << m_webSocket->errorString(); - emit errorOccured(); + emit errorOccured(error); } void WebSocketConnection::onStateChanged(QAbstractSocket::SocketState state) diff --git a/libnymea-remoteproxyclient/websocketconnection.h b/libnymea-remoteproxyclient/websocketconnection.h index e0ec4cc..31b9186 100644 --- a/libnymea-remoteproxyclient/websocketconnection.h +++ b/libnymea-remoteproxyclient/websocketconnection.h @@ -61,6 +61,7 @@ private slots: public slots: void connectServer(const QUrl &serverUrl) override; void disconnectServer() override; + }; } diff --git a/tests/test-offline/nymea-remoteproxy-tests-offline.cpp b/tests/test-offline/nymea-remoteproxy-tests-offline.cpp index c2d1abb..f9e8515 100644 --- a/tests/test-offline/nymea-remoteproxy-tests-offline.cpp +++ b/tests/test-offline/nymea-remoteproxy-tests-offline.cpp @@ -287,11 +287,21 @@ void RemoteProxyOfflineTests::remoteConnection() m_mockAuthenticator->setTimeoutDuration(100); m_mockAuthenticator->setExpectedAuthenticationError(); + QString nameConnectionOne = "Test client one"; + QUuid uuidConnectionOne = QUuid::createUuid(); + + QString nameConnectionTwo = "Test client two"; + QUuid uuidConnectionTwo = QUuid::createUuid(); + + QByteArray dataOne = "Hello from client one :-)"; + QByteArray dataTwo = "Hello from client two :-)"; + + // Create two connection - RemoteProxyConnection *connectionOne = new RemoteProxyConnection(QUuid::createUuid(), "Test client one", this); + RemoteProxyConnection *connectionOne = new RemoteProxyConnection(uuidConnectionOne, nameConnectionOne, this); connect(connectionOne, &RemoteProxyConnection::sslErrors, this, &BaseTest::ignoreConnectionSslError); - RemoteProxyConnection *connectionTwo = new RemoteProxyConnection(QUuid::createUuid(), "Test client two", this); + RemoteProxyConnection *connectionTwo = new RemoteProxyConnection(uuidConnectionTwo, nameConnectionTwo, this); connect(connectionTwo, &RemoteProxyConnection::sslErrors, this, &BaseTest::ignoreConnectionSslError); // Connect one @@ -336,27 +346,25 @@ void RemoteProxyOfflineTests::remoteConnection() QVERIFY(connectionOne->state() == RemoteProxyConnection::StateRemoteConnected); QVERIFY(connectionTwo->state() == RemoteProxyConnection::StateRemoteConnected); + QCOMPARE(connectionOne->tunnelPartnerName(), nameConnectionTwo); + QCOMPARE(connectionOne->tunnelPartnerUuid(), uuidConnectionTwo.toString()); + QCOMPARE(connectionTwo->tunnelPartnerName(), nameConnectionOne); + QCOMPARE(connectionTwo->tunnelPartnerUuid(), uuidConnectionOne.toString()); + // Pipe data trought the tunnel QSignalSpy remoteConnectionDataOne(connectionOne, &RemoteProxyConnection::dataReady); QSignalSpy remoteConnectionDataTwo(connectionTwo, &RemoteProxyConnection::dataReady); - QByteArray dataOne = "Hello from client one :-)"; - QByteArray dataTwo = "Hello from client two :-)"; - connectionOne->sendData(dataOne); remoteConnectionDataTwo.wait(500); QVERIFY(remoteConnectionDataTwo.count() == 1); QCOMPARE(remoteConnectionDataTwo.at(0).at(0).toByteArray(), dataOne); - // verify if data is the same - connectionTwo->sendData(dataTwo); remoteConnectionDataOne.wait(500); QVERIFY(remoteConnectionDataOne.count() == 1); QCOMPARE(remoteConnectionDataOne.at(0).at(0).toByteArray(), dataTwo); - // verify if data is the same - connectionOne->deleteLater(); connectionTwo->deleteLater(); @@ -380,7 +388,7 @@ void RemoteProxyOfflineTests::trippleConnection() RemoteProxyConnection *connectionTwo = new RemoteProxyConnection(QUuid::createUuid(), "Test client two", this); connect(connectionTwo, &RemoteProxyConnection::sslErrors, this, &BaseTest::ignoreConnectionSslError); - RemoteProxyConnection *connectionThree = new RemoteProxyConnection(QUuid::createUuid(), "Token thief ^w^", this); + RemoteProxyConnection *connectionThree = new RemoteProxyConnection(QUuid::createUuid(), "Token thief ^v^", this); connect(connectionThree, &RemoteProxyConnection::sslErrors, this, &BaseTest::ignoreConnectionSslError); // Connect one @@ -479,10 +487,6 @@ void RemoteProxyOfflineTests::timeout() // Start the server startServer(); - // Configure result - // Start the server - startServer(); - // Configure result m_mockAuthenticator->setExpectedAuthenticationError(); m_mockAuthenticator->setTimeoutDuration(6000); @@ -495,6 +499,7 @@ void RemoteProxyOfflineTests::timeout() QVariant response = invokeApiCall("Authentication.Authenticate", params); qDebug() << qUtf8Printable(QJsonDocument::fromVariant(response).toJson(QJsonDocument::Indented)); + // Clean up stopServer(); } diff --git a/tests/test-online/nymea-remoteproxy-tests-online.h b/tests/test-online/nymea-remoteproxy-tests-online.h index 77c488f..c945e9c 100644 --- a/tests/test-online/nymea-remoteproxy-tests-online.h +++ b/tests/test-online/nymea-remoteproxy-tests-online.h @@ -41,6 +41,7 @@ class RemoteProxyOnlineTests : public BaseTest Q_OBJECT public: explicit RemoteProxyOnlineTests(QObject *parent = nullptr); + ~RemoteProxyOnlineTests() = default; private slots: void awsStaticCredentials();