2013-11-08 22:18:10 +01:00
|
|
|
// system includes
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
// project includes
|
|
|
|
#include <boblightserver/BoblightServer.h>
|
|
|
|
#include "BoblightClientConnection.h"
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// hyperion includes
|
|
|
|
#include <hyperion/Hyperion.h>
|
2019-07-20 11:28:16 +02:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// qt incl
|
|
|
|
#include <QTcpServer>
|
|
|
|
|
2019-07-20 11:28:16 +02:00
|
|
|
// netUtil
|
|
|
|
#include <utils/NetUtils.h>
|
|
|
|
|
2016-08-11 07:13:55 +02:00
|
|
|
using namespace hyperion;
|
|
|
|
|
2018-12-28 18:12:45 +01:00
|
|
|
BoblightServer::BoblightServer(Hyperion* hyperion,const QJsonDocument& config)
|
2016-06-27 22:43:43 +02:00
|
|
|
: QObject()
|
2018-12-28 18:12:45 +01:00
|
|
|
, _hyperion(hyperion)
|
2018-12-27 23:11:32 +01:00
|
|
|
, _server(new QTcpServer(this))
|
2016-06-27 22:43:43 +02:00
|
|
|
, _openConnections()
|
2018-12-27 23:11:32 +01:00
|
|
|
, _priority(0)
|
2016-06-27 22:43:43 +02:00
|
|
|
, _log(Logger::getInstance("BOBLIGHT"))
|
2018-12-27 23:11:32 +01:00
|
|
|
, _port(0)
|
2013-11-08 22:18:10 +01:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
Debug(_log, "Instance created");
|
|
|
|
|
|
|
|
// listen for component change
|
2020-02-26 18:54:56 +01:00
|
|
|
connect(_hyperion, SIGNAL(compStateChangeRequest(hyperion::Components,bool)), this, SLOT(compStateChangeRequest(hyperion::Components,bool)));
|
2018-12-27 23:11:32 +01:00
|
|
|
// listen new connection signal from server
|
|
|
|
connect(_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
|
|
|
|
|
|
|
|
// init
|
|
|
|
handleSettingsUpdate(settings::BOBLSERVER, config);
|
2016-06-27 22:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BoblightServer::~BoblightServer()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoblightServer::start()
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
if ( _server->isListening() )
|
2016-06-27 22:43:43 +02:00
|
|
|
return;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2019-07-20 11:28:16 +02:00
|
|
|
if (NetUtils::portAvailable(_port, _log))
|
|
|
|
_server->listen(QHostAddress::Any, _port);
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
Info(_log, "Started on port %d", _port);
|
2016-07-15 23:08:55 +02:00
|
|
|
|
2020-02-26 18:54:56 +01:00
|
|
|
_hyperion->setNewComponentState(COMP_BOBLIGHTSERVER, _server->isListening());
|
2013-11-08 22:18:10 +01:00
|
|
|
}
|
|
|
|
|
2016-06-27 22:43:43 +02:00
|
|
|
void BoblightServer::stop()
|
2013-11-08 22:18:10 +01:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
if ( ! _server->isListening() )
|
2016-06-27 22:43:43 +02:00
|
|
|
return;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2020-07-12 09:19:59 +02:00
|
|
|
qDeleteAll(_openConnections);
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
_server->close();
|
2016-07-15 23:08:55 +02:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
Info(_log, "Stopped");
|
2020-02-26 18:54:56 +01:00
|
|
|
_hyperion->setNewComponentState(COMP_BOBLIGHTSERVER, _server->isListening());
|
2018-12-27 23:11:32 +01:00
|
|
|
}
|
2016-07-15 23:08:55 +02:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
bool BoblightServer::active()
|
|
|
|
{
|
|
|
|
return _server->isListening();
|
2013-11-08 22:18:10 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 18:54:56 +01:00
|
|
|
void BoblightServer::compStateChangeRequest(const hyperion::Components component, bool enable)
|
2016-08-11 07:13:55 +02:00
|
|
|
{
|
2016-09-07 20:10:37 +02:00
|
|
|
if (component == COMP_BOBLIGHTSERVER)
|
2016-08-11 07:13:55 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
if (_server->isListening() != enable)
|
2016-09-07 20:10:37 +02:00
|
|
|
{
|
|
|
|
if (enable) start();
|
|
|
|
else stop();
|
|
|
|
}
|
2016-08-11 07:13:55 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-27 22:43:43 +02:00
|
|
|
|
2013-11-08 22:18:10 +01:00
|
|
|
uint16_t BoblightServer::getPort() const
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
return _server->serverPort();
|
2013-11-08 22:18:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BoblightServer::newConnection()
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
QTcpSocket * socket = _server->nextPendingConnection();
|
2013-11-08 22:18:10 +01:00
|
|
|
|
|
|
|
if (socket != nullptr)
|
|
|
|
{
|
2016-06-27 22:43:43 +02:00
|
|
|
Info(_log, "new connection");
|
2018-12-27 23:11:32 +01:00
|
|
|
_hyperion->registerInput(_priority, hyperion::COMP_BOBLIGHTSERVER, QString("Boblight@%1").arg(socket->peerAddress().toString()));
|
2018-12-28 18:12:45 +01:00
|
|
|
BoblightClientConnection * connection = new BoblightClientConnection(_hyperion, socket, _priority);
|
2013-11-08 22:18:10 +01:00
|
|
|
_openConnections.insert(connection);
|
|
|
|
|
|
|
|
// register slot for cleaning up after the connection closed
|
|
|
|
connect(connection, SIGNAL(connectionClosed(BoblightClientConnection*)), this, SLOT(closedConnection(BoblightClientConnection*)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoblightServer::closedConnection(BoblightClientConnection *connection)
|
|
|
|
{
|
2016-06-27 22:43:43 +02:00
|
|
|
Debug(_log, "connection closed");
|
2013-11-08 22:18:10 +01:00
|
|
|
_openConnections.remove(connection);
|
|
|
|
|
|
|
|
// schedule to delete the connection object
|
|
|
|
connection->deleteLater();
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
|
|
|
|
void BoblightServer::handleSettingsUpdate(const settings::type& type, const QJsonDocument& config)
|
|
|
|
{
|
|
|
|
if(type == settings::BOBLSERVER)
|
|
|
|
{
|
|
|
|
QJsonObject obj = config.object();
|
|
|
|
_port = obj["port"].toInt();
|
|
|
|
_priority = obj["priority"].toInt();
|
|
|
|
stop();
|
|
|
|
if(obj["enable"].toBool())
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
}
|