- prepare general way to send (proto) messages. currently only incomming protomessages are forwarded

- begin impl. of json server


Former-commit-id: 8f9237cd57ada1e84dc05e60b9ad723e47fd57b1
This commit is contained in:
redpanther
2016-02-15 18:25:18 +01:00
parent 5dc59344c4
commit b01b5eb005
12 changed files with 287 additions and 104 deletions

View File

@@ -2,17 +2,27 @@
#include <stdexcept>
// project includes
#include <hyperion/MessageForwarder.h>
#include <protoserver/ProtoServer.h>
#include "protoserver/ProtoConnection.h"
#include "ProtoClientConnection.h"
ProtoServer::ProtoServer(Hyperion *hyperion, uint16_t port, QStringList * forwardClientList) :
ProtoServer::ProtoServer(Hyperion *hyperion, uint16_t port) :
QObject(),
_hyperion(hyperion),
_server(),
_openConnections()
{
for (int i = 0; i < forwardClientList->size(); ++i)
_forwardClients << forwardClientList->at(i);
MessageForwarder * forwarder = hyperion->getForwarder();
QStringList slaves = forwarder->getProtoSlaves();
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;
}
if (!_server.listen(QHostAddress::Any, port))
{
@@ -28,6 +38,9 @@ ProtoServer::~ProtoServer()
foreach (ProtoClientConnection * connection, _openConnections) {
delete connection;
}
while (!_proxy_connections.isEmpty())
delete _proxy_connections.takeFirst();
}
uint16_t ProtoServer::getPort() const
@@ -42,14 +55,22 @@ void ProtoServer::newConnection()
if (socket != nullptr)
{
std::cout << "New proto connection" << std::endl;
ProtoClientConnection * connection = new ProtoClientConnection(socket, _hyperion, _forwardClients);
ProtoClientConnection * connection = new ProtoClientConnection(socket, _hyperion);
_openConnections.insert(connection);
// register slot for cleaning up after the connection closed
connect(connection, SIGNAL(connectionClosed(ProtoClientConnection*)), this, SLOT(closedConnection(ProtoClientConnection*)));
connect(connection, SIGNAL(newMessage(const proto::HyperionRequest*)), this, SLOT(newMessage(const proto::HyperionRequest*)));
}
}
void ProtoServer::newMessage(const proto::HyperionRequest * message)
{
for (int i = 0; i < _proxy_connections.size(); ++i)
_proxy_connections.at(i)->sendMessage(*message);
}
void ProtoServer::closedConnection(ProtoClientConnection *connection)
{
std::cout << "Proto connection closed" << std::endl;