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-06-19 00:56:47 +02:00
|
|
|
ProtoServer::ProtoServer(uint16_t port) :
|
2013-10-11 10:06:24 +02:00
|
|
|
QObject(),
|
2016-06-19 00:56:47 +02:00
|
|
|
_hyperion(Hyperion::getInstance()),
|
2013-10-11 10:06:24 +02:00
|
|
|
_server(),
|
|
|
|
_openConnections()
|
|
|
|
{
|
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()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtoServer::~ProtoServer()
|
|
|
|
{
|
|
|
|
foreach (ProtoClientConnection * connection, _openConnections) {
|
|
|
|
delete connection;
|
|
|
|
}
|
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-03-23 17:40:34 +01:00
|
|
|
std::cout << "PROTOSERVER INFO: New connection" << std::endl;
|
2016-02-15 18:25:18 +01:00
|
|
|
ProtoClientConnection * connection = new ProtoClientConnection(socket, _hyperion);
|
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*)));
|
2016-06-07 23:12:18 +02:00
|
|
|
|
|
|
|
// register forward signal for xbmc checker
|
|
|
|
connect(this, SIGNAL(grabbingMode(GrabbingMode)), connection, SLOT(setGrabbingMode(GrabbingMode)));
|
|
|
|
connect(this, SIGNAL(videoMode(VideoMode)), connection, SLOT(setVideoMode(VideoMode)));
|
2016-02-15 18:25:18 +01:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _proxy_connections.size(); ++i)
|
|
|
|
_proxy_connections.at(i)->setImage(image, priority, duration_ms);
|
|
|
|
}
|
|
|
|
|
2013-10-11 10:06:24 +02:00
|
|
|
void ProtoServer::closedConnection(ProtoClientConnection *connection)
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "PROTOSERVER INFO: Connection closed" << std::endl;
|
2013-10-11 10:06:24 +02:00
|
|
|
_openConnections.remove(connection);
|
|
|
|
|
|
|
|
// schedule to delete the connection object
|
|
|
|
connection->deleteLater();
|
|
|
|
}
|