2013-10-11 10:06:24 +02:00
|
|
|
// system includes
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
// project includes
|
2016-02-15 18:25:18 +01:00
|
|
|
#include <hyperion/MessageForwarder.h>
|
2013-10-11 10:06:24 +02:00
|
|
|
#include <protoserver/ProtoServer.h>
|
2016-02-15 18:25:18 +01:00
|
|
|
#include "protoserver/ProtoConnection.h"
|
2013-10-11 10:06:24 +02:00
|
|
|
#include "ProtoClientConnection.h"
|
|
|
|
|
2016-07-16 22:51:31 +02:00
|
|
|
ProtoServer::ProtoServer(uint16_t port)
|
|
|
|
: QObject()
|
|
|
|
, _hyperion(Hyperion::getInstance())
|
|
|
|
, _server()
|
|
|
|
, _openConnections()
|
|
|
|
, _log(Logger::getInstance("PROTOSERVER"))
|
2016-08-11 07:13:55 +02:00
|
|
|
, _forwarder_enabled(true)
|
2013-10-11 10:06:24 +02:00
|
|
|
{
|
2016-02-15 18:25:18 +01:00
|
|
|
|
2016-06-19 00:56:47 +02:00
|
|
|
MessageForwarder * forwarder = _hyperion->getForwarder();
|
2016-02-15 18:25:18 +01:00
|
|
|
QStringList slaves = forwarder->getProtoSlaves();
|
|
|
|
|
|
|
|
for (int i = 0; i < slaves.size(); ++i) {
|
2016-02-18 10:32:38 +01:00
|
|
|
if ( QString("127.0.0.1:%1").arg(port) == slaves.at(i) ) {
|
2016-03-23 17:40:34 +01:00
|
|
|
throw std::runtime_error("PROTOSERVER ERROR: Loop between proto server and forwarder detected. Fix your config!");
|
2016-02-18 10:32:38 +01:00
|
|
|
}
|
|
|
|
|
2016-02-15 18:25:18 +01:00
|
|
|
ProtoConnection* p = new ProtoConnection(slaves.at(i).toLocal8Bit().constData());
|
|
|
|
p->setSkipReply(true);
|
|
|
|
_proxy_connections << p;
|
|
|
|
}
|
2016-02-08 16:56:23 +01:00
|
|
|
|
2013-10-11 10:06:24 +02:00
|
|
|
if (!_server.listen(QHostAddress::Any, port))
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
throw std::runtime_error("PROTOSERVER ERROR: Could not bind to port");
|
2013-10-11 10:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set trigger for incoming connections
|
|
|
|
connect(&_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
|
2016-08-11 07:13:55 +02:00
|
|
|
connect( _hyperion, SIGNAL(componentStateChanged(hyperion::Components,bool)), this, SLOT(componentStateChanged(hyperion::Components,bool)));
|
|
|
|
|
2013-10-11 10:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ProtoServer::~ProtoServer()
|
|
|
|
{
|
|
|
|
foreach (ProtoClientConnection * connection, _openConnections) {
|
|
|
|
delete connection;
|
|
|
|
}
|
2017-11-22 00:52:55 +01:00
|
|
|
|
2016-02-15 18:25:18 +01:00
|
|
|
while (!_proxy_connections.isEmpty())
|
|
|
|
delete _proxy_connections.takeFirst();
|
2013-10-11 10:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t ProtoServer::getPort() const
|
|
|
|
{
|
|
|
|
return _server.serverPort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoServer::newConnection()
|
|
|
|
{
|
|
|
|
QTcpSocket * socket = _server.nextPendingConnection();
|
|
|
|
|
|
|
|
if (socket != nullptr)
|
|
|
|
{
|
2016-07-11 17:08:22 +02:00
|
|
|
Debug(_log, "New connection");
|
2016-07-16 22:51:31 +02:00
|
|
|
ProtoClientConnection * connection = new ProtoClientConnection(socket);
|
2013-10-11 10:06:24 +02:00
|
|
|
_openConnections.insert(connection);
|
|
|
|
|
|
|
|
// register slot for cleaning up after the connection closed
|
|
|
|
connect(connection, SIGNAL(connectionClosed(ProtoClientConnection*)), this, SLOT(closedConnection(ProtoClientConnection*)));
|
2016-02-15 18:25:18 +01:00
|
|
|
connect(connection, SIGNAL(newMessage(const proto::HyperionRequest*)), this, SLOT(newMessage(const proto::HyperionRequest*)));
|
|
|
|
|
2017-11-22 00:52:55 +01:00
|
|
|
// register forward signal for video mode
|
|
|
|
connect(this, SIGNAL(videoMode(VideoMode)), connection, SLOT(setVideoMode(VideoMode)));
|
2013-10-11 10:06:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:25:18 +01:00
|
|
|
void ProtoServer::newMessage(const proto::HyperionRequest * message)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _proxy_connections.size(); ++i)
|
|
|
|
_proxy_connections.at(i)->sendMessage(*message);
|
|
|
|
}
|
|
|
|
|
2016-02-16 15:41:40 +01:00
|
|
|
void ProtoServer::sendImageToProtoSlaves(int priority, const Image<ColorRgb> & image, int duration_ms)
|
|
|
|
{
|
2016-08-11 07:13:55 +02:00
|
|
|
if ( _forwarder_enabled )
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _proxy_connections.size(); ++i)
|
|
|
|
_proxy_connections.at(i)->setImage(image, priority, duration_ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoServer::componentStateChanged(const hyperion::Components component, bool enable)
|
|
|
|
{
|
2016-09-07 20:10:37 +02:00
|
|
|
if (component == hyperion::COMP_FORWARDER)
|
2016-08-11 07:13:55 +02:00
|
|
|
{
|
2016-09-07 20:10:37 +02:00
|
|
|
if (_forwarder_enabled != enable)
|
|
|
|
{
|
|
|
|
_forwarder_enabled = enable;
|
|
|
|
Info(_log, "forwarder change state to %s", (_forwarder_enabled ? "enabled" : "disabled") );
|
|
|
|
}
|
|
|
|
_hyperion->getComponentRegister().componentStateChanged(component, _forwarder_enabled);
|
2016-08-11 07:13:55 +02:00
|
|
|
}
|
2016-02-16 15:41:40 +01:00
|
|
|
}
|
|
|
|
|
2013-10-11 10:06:24 +02:00
|
|
|
void ProtoServer::closedConnection(ProtoClientConnection *connection)
|
|
|
|
{
|
2016-07-11 17:08:22 +02:00
|
|
|
Debug(_log, "Connection closed");
|
2013-10-11 10:06:24 +02:00
|
|
|
_openConnections.remove(connection);
|
|
|
|
|
|
|
|
// schedule to delete the connection object
|
|
|
|
connection->deleteLater();
|
|
|
|
}
|