From df91527557342e7925c57de81610b67485d3d9ce Mon Sep 17 00:00:00 2001 From: redpanther Date: Mon, 15 Feb 2016 20:53:03 +0100 Subject: [PATCH] implement json forwarder Former-commit-id: 5519118304bd5690e6b512481347579339e78ac9 --- include/hyperion/MessageForwarder.h | 13 ++++++++--- libsrc/hyperion/Hyperion.cpp | 6 ++++++ libsrc/hyperion/MessageForwarder.cpp | 25 ++++++++++++++++------ libsrc/jsonserver/JsonClientConnection.cpp | 14 ++++++++---- libsrc/protoserver/ProtoServer.cpp | 1 - 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/include/hyperion/MessageForwarder.h b/include/hyperion/MessageForwarder.h index 467d0d35..0a3c1b19 100644 --- a/include/hyperion/MessageForwarder.h +++ b/include/hyperion/MessageForwarder.h @@ -9,12 +9,19 @@ // QT includes #include #include +#include // Utils includes #include class MessageForwarder { public: + + struct JsonSlaveAddress { + QHostAddress addr; + quint16 port = 19444; + }; + MessageForwarder(); ~MessageForwarder(); @@ -23,9 +30,9 @@ public: void sendMessage(); QStringList getProtoSlaves(); + QList getJsonSlaves(); private: - bool _running; - - QStringList _protoSlaves; + QStringList _protoSlaves; + QList _jsonSlaves; }; diff --git a/libsrc/hyperion/Hyperion.cpp b/libsrc/hyperion/Hyperion.cpp index ed23fb30..2463ed15 100644 --- a/libsrc/hyperion/Hyperion.cpp +++ b/libsrc/hyperion/Hyperion.cpp @@ -275,13 +275,19 @@ MessageForwarder * Hyperion::createMessageForwarder(const Json::Value & forwarde if ( ! forwarderConfig["json"].isNull() && forwarderConfig["json"].isArray() ) { for (const Json::Value& addr : forwarderConfig["json"]) + { + std::cout << "Json forward to " << addr.asString() << std::endl; forwarder->addJsonSlave(addr.asString()); + } } if ( ! forwarderConfig["proto"].isNull() && forwarderConfig["proto"].isArray() ) { for (const Json::Value& addr : forwarderConfig["proto"]) + { + std::cout << "Proto forward to " << addr.asString() << std::endl; forwarder->addProtoSlave(addr.asString()); + } } } diff --git a/libsrc/hyperion/MessageForwarder.cpp b/libsrc/hyperion/MessageForwarder.cpp index 8da277e2..be9c9825 100644 --- a/libsrc/hyperion/MessageForwarder.cpp +++ b/libsrc/hyperion/MessageForwarder.cpp @@ -1,8 +1,7 @@ #include -MessageForwarder::MessageForwarder() : -_running(false) +MessageForwarder::MessageForwarder() { } @@ -13,7 +12,19 @@ MessageForwarder::~MessageForwarder() void MessageForwarder::addJsonSlave(std::string slave) { - std::cout << slave << std::endl; + QStringList parts = QString(slave.c_str()).split(":"); + if (parts.size() != 2) + throw std::runtime_error(QString("Wrong address: unable to parse address (%1)").arg(slave.c_str()).toStdString()); + + bool ok; + quint16 port = parts[1].toUShort(&ok); + if (!ok) + throw std::runtime_error(QString("Wrong address: Unable to parse the port number (%1)").arg(parts[1]).toStdString()); + + JsonSlaveAddress c; + c.addr = QHostAddress(parts[0]); + c.port = port; + _jsonSlaves << c; } void MessageForwarder::addProtoSlave(std::string slave) @@ -23,12 +34,14 @@ void MessageForwarder::addProtoSlave(std::string slave) void MessageForwarder::sendMessage() { - if ( ! _running ) - return; - } QStringList MessageForwarder::getProtoSlaves() { return _protoSlaves; } + +QList MessageForwarder::getJsonSlaves() +{ + return _jsonSlaves; +} diff --git a/libsrc/jsonserver/JsonClientConnection.cpp b/libsrc/jsonserver/JsonClientConnection.cpp index 1b700b25..e4dbc096 100644 --- a/libsrc/jsonserver/JsonClientConnection.cpp +++ b/libsrc/jsonserver/JsonClientConnection.cpp @@ -16,6 +16,7 @@ // hyperion util includes #include #include +#include #include #include @@ -254,11 +255,16 @@ void JsonClientConnection::handleMessage(const std::string &messageString) void JsonClientConnection::forwardJsonMessage(const Json::Value & message) { QTcpSocket client; - client.connectToHost(QHostAddress("127.0.0.1"), 19444); - if ( client.waitForConnected(500) ) + QList list = _hyperion->getForwarder()->getJsonSlaves(); + + for ( int i=0; igetProtoSlaves(); for (int i = 0; i < slaves.size(); ++i) { - std::cout << "Proto forward to " << slaves.at(i).toLocal8Bit().constData() << std::endl; ProtoConnection* p = new ProtoConnection(slaves.at(i).toLocal8Bit().constData()); p->setSkipReply(true); _proxy_connections << p;