forward protobuf messages.

configure (send proto messages to two other hyperiond):

"protoServer" :
{
	"port"    : 19446,
	"forward" : [ "192.168.0.10:19445", "192.168.0.11:19445" ]
},


Former-commit-id: 33af219cfce99609ca7245d662dc0f0561013bbd
This commit is contained in:
redpanther 2016-02-08 16:56:23 +01:00
parent cf34f45daa
commit 5dc59344c4
6 changed files with 48 additions and 10 deletions

View File

@ -67,10 +67,6 @@ public:
/// ///
void clearAll(); void clearAll();
private:
/// Try to connect to the Hyperion host
void connectToHost();
/// ///
/// Send a command message and receive its reply /// Send a command message and receive its reply
/// ///
@ -78,6 +74,10 @@ private:
/// ///
void sendMessage(const proto::HyperionRequest & message); void sendMessage(const proto::HyperionRequest & message);
private:
/// Try to connect to the Hyperion host
void connectToHost();
/// ///
/// Parse a reply message /// Parse a reply message
/// ///

View File

@ -6,6 +6,7 @@
// Qt includes // Qt includes
#include <QTcpServer> #include <QTcpServer>
#include <QSet> #include <QSet>
#include <QStringList>
// Hyperion includes // Hyperion includes
#include <hyperion/Hyperion.h> #include <hyperion/Hyperion.h>
@ -27,7 +28,7 @@ public:
/// @param hyperion Hyperion instance /// @param hyperion Hyperion instance
/// @param port port number on which to start listening for connections /// @param port port number on which to start listening for connections
/// ///
ProtoServer(Hyperion * hyperion, uint16_t port = 19445); ProtoServer(Hyperion * hyperion, uint16_t port = 19445, QStringList * forwardClientList = new QStringList() );
~ProtoServer(); ~ProtoServer();
/// ///
@ -56,4 +57,5 @@ private:
/// List with open connections /// List with open connections
QSet<ProtoClientConnection *> _openConnections; QSet<ProtoClientConnection *> _openConnections;
QStringList _forwardClients;
}; };

View File

@ -20,7 +20,7 @@
// project includes // project includes
#include "ProtoClientConnection.h" #include "ProtoClientConnection.h"
ProtoClientConnection::ProtoClientConnection(QTcpSocket *socket, Hyperion * hyperion) : ProtoClientConnection::ProtoClientConnection(QTcpSocket *socket, Hyperion * hyperion, QStringList forwardClientList) :
QObject(), QObject(),
_socket(socket), _socket(socket),
_imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor()), _imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor()),
@ -30,11 +30,22 @@ ProtoClientConnection::ProtoClientConnection(QTcpSocket *socket, Hyperion * hype
// connect internal signals and slots // connect internal signals and slots
connect(_socket, SIGNAL(disconnected()), this, SLOT(socketClosed())); connect(_socket, SIGNAL(disconnected()), this, SLOT(socketClosed()));
connect(_socket, SIGNAL(readyRead()), this, SLOT(readData())); connect(_socket, SIGNAL(readyRead()), this, SLOT(readData()));
for (int i = 0; i < forwardClientList.size(); ++i) {
std::cout << "Proto forward to " << forwardClientList.at(i).toLocal8Bit().constData() << std::endl;
ProtoConnection* p = new ProtoConnection("127.0.0.1:19445");
p->setSkipReply(true);
_proxy_connections << p;
}
} }
ProtoClientConnection::~ProtoClientConnection() ProtoClientConnection::~ProtoClientConnection()
{ {
delete _socket; delete _socket;
while (!_proxy_connections.isEmpty())
delete _proxy_connections.takeFirst();
} }
void ProtoClientConnection::readData() void ProtoClientConnection::readData()
@ -81,6 +92,10 @@ void ProtoClientConnection::socketClosed()
void ProtoClientConnection::handleMessage(const proto::HyperionRequest & message) void ProtoClientConnection::handleMessage(const proto::HyperionRequest & message)
{ {
// forward messages
for (int i = 0; i < _proxy_connections.size(); ++i)
_proxy_connections.at(i)->sendMessage(message);
switch (message.command()) switch (message.command())
{ {
case proto::HyperionRequest::COLOR: case proto::HyperionRequest::COLOR:

View File

@ -6,12 +6,14 @@
// Qt includes // Qt includes
#include <QByteArray> #include <QByteArray>
#include <QTcpSocket> #include <QTcpSocket>
#include <QStringList>
// Hyperion includes // Hyperion includes
#include <hyperion/Hyperion.h> #include <hyperion/Hyperion.h>
// proto includes // proto includes
#include "message.pb.h" #include "message.pb.h"
#include "protoserver/ProtoConnection.h"
class ImageProcessor; class ImageProcessor;
@ -28,7 +30,7 @@ public:
/// @param socket The Socket object for this connection /// @param socket The Socket object for this connection
/// @param hyperion The Hyperion server /// @param hyperion The Hyperion server
/// ///
ProtoClientConnection(QTcpSocket * socket, Hyperion * hyperion); ProtoClientConnection(QTcpSocket * socket, Hyperion * hyperion, QStringList forwardClientList);
/// ///
/// Destructor /// Destructor
@ -123,4 +125,8 @@ private:
/// The buffer used for reading data from the socket /// The buffer used for reading data from the socket
QByteArray _receiveBuffer; QByteArray _receiveBuffer;
/// Hyperion proto connection object for forwarding
QList<ProtoConnection*> _proxy_connections;
}; };

View File

@ -5,12 +5,15 @@
#include <protoserver/ProtoServer.h> #include <protoserver/ProtoServer.h>
#include "ProtoClientConnection.h" #include "ProtoClientConnection.h"
ProtoServer::ProtoServer(Hyperion *hyperion, uint16_t port) : ProtoServer::ProtoServer(Hyperion *hyperion, uint16_t port, QStringList * forwardClientList) :
QObject(), QObject(),
_hyperion(hyperion), _hyperion(hyperion),
_server(), _server(),
_openConnections() _openConnections()
{ {
for (int i = 0; i < forwardClientList->size(); ++i)
_forwardClients << forwardClientList->at(i);
if (!_server.listen(QHostAddress::Any, port)) if (!_server.listen(QHostAddress::Any, port))
{ {
throw std::runtime_error("Proto server could not bind to port"); throw std::runtime_error("Proto server could not bind to port");
@ -39,7 +42,7 @@ void ProtoServer::newConnection()
if (socket != nullptr) if (socket != nullptr)
{ {
std::cout << "New proto connection" << std::endl; std::cout << "New proto connection" << std::endl;
ProtoClientConnection * connection = new ProtoClientConnection(socket, _hyperion); ProtoClientConnection * connection = new ProtoClientConnection(socket, _hyperion, _forwardClients);
_openConnections.insert(connection); _openConnections.insert(connection);
// register slot for cleaning up after the connection closed // register slot for cleaning up after the connection closed

View File

@ -1,6 +1,7 @@
// C++ includes // C++ includes
#include <cassert> #include <cassert>
#include <csignal> #include <csignal>
#include <vector>
// QT includes // QT includes
#include <QCoreApplication> #include <QCoreApplication>
@ -368,7 +369,18 @@ int main(int argc, char** argv)
if (config.isMember("protoServer")) if (config.isMember("protoServer"))
{ {
const Json::Value & protoServerConfig = config["protoServer"]; const Json::Value & protoServerConfig = config["protoServer"];
protoServer = new ProtoServer(&hyperion, protoServerConfig["port"].asUInt()); QStringList forwardClientList;
if ( ! protoServerConfig["forward"].isNull() && protoServerConfig["forward"].isArray() )
{
for (const Json::Value& client : protoServerConfig["forward"])
{
forwardClientList << client.asString().c_str();
std::cout << client.asString() << std::endl;
}
}
protoServer = new ProtoServer(&hyperion, protoServerConfig["port"].asUInt(), &forwardClientList );
std::cout << "Proto server created and started on port " << protoServer->getPort() << std::endl; std::cout << "Proto server created and started on port " << protoServer->getPort() << std::endl;
} }
#endif #endif