it's somewhat working

This commit is contained in:
Michael Zanetti 2018-08-15 00:37:28 +02:00
parent c5f4c9dc51
commit 4e3ab48e58
4 changed files with 69 additions and 14 deletions

View File

@ -514,7 +514,8 @@ ResponseCode AWSConnector::onSubscriptionReceivedCallback(util::String topic_nam
// silently drop our own things (should not be subscribed to that in the first place) // silently drop our own things (should not be subscribed to that in the first place)
} else if (topic.startsWith(QString("%1/eu-west-1:").arg(connector->m_clientId)) && topic.contains("proxy")) { } else if (topic.startsWith(QString("%1/eu-west-1:").arg(connector->m_clientId)) && topic.contains("proxy")) {
qCDebug(dcAWS) << "Proxy remote connection request received"; qCDebug(dcAWS) << "Proxy remote connection request received";
connector->staticMetaObject.invokeMethod(connector, "proxyConnectionRequestReceived", Qt::QueuedConnection); QString token = jsonDoc.toVariant().toMap().value("token").toString();
connector->staticMetaObject.invokeMethod(connector, "proxyConnectionRequestReceived", Qt::QueuedConnection, Q_ARG(QString, token));
} else if (topic == QString("%1/notify/response").arg(connector->m_clientId)) { } else if (topic == QString("%1/notify/response").arg(connector->m_clientId)) {
int transactionId = jsonDoc.toVariant().toMap().value("id").toInt(); int transactionId = jsonDoc.toVariant().toMap().value("id").toInt();
int status = jsonDoc.toVariant().toMap().value("status").toInt(); int status = jsonDoc.toVariant().toMap().value("status").toInt();

View File

@ -69,7 +69,7 @@ signals:
void pushNotificationSent(int id, int status); void pushNotificationSent(int id, int status);
void turnCredentialsReceived(const QVariantMap &turnCredentials); void turnCredentialsReceived(const QVariantMap &turnCredentials);
void proxyConnectionRequestReceived(); void proxyConnectionRequestReceived(const QString &token);
private slots: private slots:
void doConnect(); void doConnect();

View File

@ -1,22 +1,25 @@
#include "cloudtransport.h" #include "cloudtransport.h"
#include "loggingcategories.h" #include "loggingcategories.h"
#include "libnymea-remoteproxyclient/remoteproxyconnection.h"
using namespace remoteproxyclient; using namespace remoteproxyclient;
namespace nymeaserver { namespace nymeaserver {
CloudTransport::CloudTransport(const ServerConfiguration &config, QObject *parent): CloudTransport::CloudTransport(const ServerConfiguration &config, QObject *parent):
TransportInterface(config, parent), TransportInterface(config, parent)
m_remoteProxy(new RemoteProxyConnection(QUuid::createUuid(), "nymea:core", RemoteProxyConnection::ConnectionTypeWebSocket, this))
{ {
} }
void CloudTransport::sendData(const QUuid &clientId, const QByteArray &data) void CloudTransport::sendData(const QUuid &clientId, const QByteArray &data)
{ {
qCDebug(dcCloud) << "Should send data" << clientId << data; qCDebug(dcCloud) << "Should send data" << clientId << data;
foreach (const ConnectionContext &ctx, m_connections) {
if (ctx.clientId == clientId) {
ctx.proxyConnection->sendData(data);
return;
}
}
qCWarning(dcCloud()) << "Error sending data. No such clientId";
} }
void CloudTransport::sendData(const QList<QUuid> &clientIds, const QByteArray &data) void CloudTransport::sendData(const QList<QUuid> &clientIds, const QByteArray &data)
@ -38,10 +41,51 @@ bool CloudTransport::stopServer()
return true; return true;
} }
void CloudTransport::connectToCloud() void CloudTransport::connectToCloud(const QString &token)
{ {
qCDebug(dcCloud) << "Should connect to cloud"; qCDebug(dcCloud) << "Should connect to cloud";
m_remoteProxy->connectServer(QHostAddress("127.0.0.1"), 1212); ConnectionContext context;
context.clientId = QUuid::createUuid();
context.token = token;
context.proxyConnection = new RemoteProxyConnection(QUuid::createUuid(), "nymea:core", RemoteProxyConnection::ConnectionTypeWebSocket, this);
m_connections.insert(context.proxyConnection, context);
context.proxyConnection->setInsecureConnection(true);
connect(context.proxyConnection, &RemoteProxyConnection::ready, this, &CloudTransport::transportReady);
connect(context.proxyConnection, &RemoteProxyConnection::stateChanged, this, &CloudTransport::remoteConnectionStateChanged);
connect(context.proxyConnection, &RemoteProxyConnection::dataReady, this, &CloudTransport::transportDataReady);
context.proxyConnection->connectServer(QHostAddress("127.0.0.1"), 1212);
}
void CloudTransport::remoteConnectionStateChanged(RemoteProxyConnection::State state)
{
qCDebug(dcCloud) << "Remote connection state changed" << state;
RemoteProxyConnection *proxyConnection = qobject_cast<RemoteProxyConnection*>(sender());
ConnectionContext context = m_connections.value(proxyConnection);
if (state == RemoteProxyConnection::StateRemoteConnected) {
emit clientConnected(context.clientId);
} else if (state ==RemoteProxyConnection::StateDisconnected) {
emit clientDisconnected(context.clientId);
}
}
void CloudTransport::transportReady()
{
qCDebug(dcCloud) << "Transport ready";
RemoteProxyConnection *proxyConnection = qobject_cast<RemoteProxyConnection*>(sender());
ConnectionContext context = m_connections.value(proxyConnection);
context.proxyConnection->authenticate(context.token);
}
void CloudTransport::transportDataReady(const QByteArray &data)
{
RemoteProxyConnection *proxyConnection = qobject_cast<RemoteProxyConnection*>(sender());
ConnectionContext context = m_connections.value(proxyConnection);
emit dataAvailable(context.clientId, data);
} }
} }

View File

@ -3,10 +3,8 @@
#include <QObject> #include <QObject>
#include "../transportinterface.h" #include "../transportinterface.h"
#include "libnymea-remoteproxyclient/remoteproxyconnection.h"
namespace remoteproxyclient {
class RemoteProxyConnection;
}
namespace nymeaserver { namespace nymeaserver {
class CloudTransport : public TransportInterface class CloudTransport : public TransportInterface
@ -23,10 +21,22 @@ public:
signals: signals:
public slots: public slots:
void connectToCloud(); void connectToCloud(const QString &token);
void remoteConnectionStateChanged(remoteproxyclient::RemoteProxyConnection::State state);
private slots:
void transportReady();
void transportDataReady(const QByteArray &data);
private: private:
remoteproxyclient::RemoteProxyConnection *m_remoteProxy = nullptr; class ConnectionContext {
public:
QUuid clientId;
QString token;
remoteproxyclient::RemoteProxyConnection* proxyConnection;
};
QHash<remoteproxyclient::RemoteProxyConnection*, ConnectionContext> m_connections;
}; };
} }