2013-10-11 10:06:24 +02:00
|
|
|
// system includes
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// qt incl
|
|
|
|
#include <QTcpServer>
|
2018-12-28 18:12:45 +01:00
|
|
|
#include <QJsonObject>
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2013-10-11 10:06:24 +02:00
|
|
|
// project includes
|
|
|
|
#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"
|
2018-12-27 23:11:32 +01:00
|
|
|
#include <bonjour/bonjourserviceregister.h>
|
|
|
|
#include <hyperion/ComponentRegister.h>
|
2013-10-11 10:06:24 +02:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
ProtoServer::ProtoServer(const QJsonDocument& config)
|
2016-07-16 22:51:31 +02:00
|
|
|
: QObject()
|
2018-12-27 23:11:32 +01:00
|
|
|
, _server(new QTcpServer(this))
|
2016-07-16 22:51:31 +02:00
|
|
|
, _openConnections()
|
|
|
|
, _log(Logger::getInstance("PROTOSERVER"))
|
2013-10-11 10:06:24 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
Debug(_log,"Instance created");
|
|
|
|
connect( _server, SIGNAL(newConnection()), this, SLOT(newConnection()));
|
|
|
|
handleSettingsUpdate(settings::PROTOSERVER, config);
|
2013-10-11 10:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ProtoServer::~ProtoServer()
|
|
|
|
{
|
|
|
|
foreach (ProtoClientConnection * connection, _openConnections) {
|
|
|
|
delete connection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void ProtoServer::start()
|
|
|
|
{
|
|
|
|
if(_server->isListening())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!_server->listen(QHostAddress::Any, _port))
|
|
|
|
{
|
|
|
|
Error(_log,"Could not bind to port '%d', please use an available port",_port);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Info(_log, "Started on port %d", _port);
|
|
|
|
|
|
|
|
if(_serviceRegister == nullptr)
|
|
|
|
{
|
2018-12-28 18:12:45 +01:00
|
|
|
_serviceRegister = new BonjourServiceRegister(this);
|
|
|
|
_serviceRegister->registerService("_hyperiond-proto._tcp", _port);
|
|
|
|
}
|
|
|
|
else if( _serviceRegister->getPort() != _port)
|
|
|
|
{
|
|
|
|
delete _serviceRegister;
|
|
|
|
_serviceRegister = new BonjourServiceRegister(this);
|
2018-12-27 23:11:32 +01:00
|
|
|
_serviceRegister->registerService("_hyperiond-proto._tcp", _port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoServer::stop()
|
|
|
|
{
|
|
|
|
if(!_server->isListening())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_server->close();
|
|
|
|
Info(_log, "Stopped");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoServer::handleSettingsUpdate(const settings::type& type, const QJsonDocument& config)
|
|
|
|
{
|
|
|
|
if(type == settings::PROTOSERVER)
|
|
|
|
{
|
|
|
|
QJsonObject obj = config.object();
|
|
|
|
if(obj["port"].toInt() != _port)
|
|
|
|
{
|
|
|
|
_port = obj["port"].toInt();
|
|
|
|
stop();
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 10:06:24 +02:00
|
|
|
uint16_t ProtoServer::getPort() const
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
return _port;
|
2013-10-11 10:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoServer::newConnection()
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
while(_server->hasPendingConnections())
|
2013-10-11 10:06:24 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
if(QTcpSocket * socket = _server->nextPendingConnection())
|
|
|
|
{
|
|
|
|
Debug(_log, "New connection");
|
|
|
|
ProtoClientConnection * connection = new ProtoClientConnection(socket);
|
|
|
|
_openConnections.insert(connection);
|
2013-10-11 10:06:24 +02:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// register slot for cleaning up after the connection closed
|
|
|
|
connect(connection, SIGNAL(connectionClosed(ProtoClientConnection*)), this, SLOT(closedConnection(ProtoClientConnection*)));
|
2018-12-28 18:12:45 +01:00
|
|
|
//connect(connection, SIGNAL(newMessage(const proto::HyperionRequest*)), this, SLOT(newMessage(const proto::HyperionRequest*)));
|
2018-12-27 23:11:32 +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();
|
|
|
|
}
|