hyperion.ng/libsrc/boblightserver/BoblightServer.cpp

129 lines
2.9 KiB
C++
Raw Normal View History

// 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>
2018-12-27 23:11:32 +01:00
// qt incl
#include <QTcpServer>
// netUtil
#include <utils/NetUtils.h>
using namespace hyperion;
BoblightServer::BoblightServer(Hyperion* hyperion,const QJsonDocument& config)
: QObject()
, _hyperion(hyperion)
2018-12-27 23:11:32 +01:00
, _server(new QTcpServer(this))
, _openConnections()
2018-12-27 23:11:32 +01:00
, _priority(0)
, _log(Logger::getInstance("BOBLIGHT"))
2018-12-27 23:11:32 +01:00
, _port(0)
{
2018-12-27 23:11:32 +01:00
Debug(_log, "Instance created");
// listen for component change
connect(_hyperion, &Hyperion::compStateChangeRequest, this, &BoblightServer::compStateChangeRequest);
2018-12-27 23:11:32 +01:00
// listen new connection signal from server
connect(_server, &QTcpServer::newConnection, this, &BoblightServer::newConnection);
2018-12-27 23:11:32 +01:00
// init
handleSettingsUpdate(settings::BOBLSERVER, config);
}
BoblightServer::~BoblightServer()
{
stop();
}
void BoblightServer::start()
{
2018-12-27 23:11:32 +01:00
if ( _server->isListening() )
return;
2018-12-27 23:11:32 +01: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);
_hyperion->setNewComponentState(COMP_BOBLIGHTSERVER, _server->isListening());
}
void BoblightServer::stop()
{
2018-12-27 23:11:32 +01:00
if ( ! _server->isListening() )
return;
2018-12-27 23:11:32 +01:00
qDeleteAll(_openConnections);
2018-12-27 23:11:32 +01:00
_server->close();
2018-12-27 23:11:32 +01:00
Info(_log, "Stopped");
_hyperion->setNewComponentState(COMP_BOBLIGHTSERVER, _server->isListening());
2018-12-27 23:11:32 +01:00
}
bool BoblightServer::active() const
2018-12-27 23:11:32 +01:00
{
return _server->isListening();
}
2020-08-08 13:09:15 +02:00
void BoblightServer::compStateChangeRequest(hyperion::Components component, bool enable)
{
if (component == COMP_BOBLIGHTSERVER)
{
2018-12-27 23:11:32 +01:00
if (_server->isListening() != enable)
{
if (enable) start();
else stop();
}
}
}
uint16_t BoblightServer::getPort() const
{
2018-12-27 23:11:32 +01:00
return _server->serverPort();
}
void BoblightServer::newConnection()
{
2018-12-27 23:11:32 +01:00
QTcpSocket * socket = _server->nextPendingConnection();
if (socket != nullptr)
{
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()));
BoblightClientConnection * connection = new BoblightClientConnection(_hyperion, socket, _priority);
_openConnections.insert(connection);
// register slot for cleaning up after the connection closed
connect(connection, &BoblightClientConnection::connectionClosed, this, &BoblightServer::closedConnection);
}
}
void BoblightServer::closedConnection(BoblightClientConnection *connection)
{
Debug(_log, "connection closed");
_openConnections.remove(connection);
// schedule to delete the connection object
connection->deleteLater();
}
2018-12-27 23:11:32 +01:00
2020-08-08 13:09:15 +02:00
void BoblightServer::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
2018-12-27 23:11:32 +01:00
{
if(type == settings::BOBLSERVER)
{
QJsonObject obj = config.object();
_port = obj["port"].toInt();
_priority = obj["priority"].toInt();
stop();
if(obj["enable"].toBool())
start();
}
}