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)
|
2022-02-22 20:58:59 +01:00
|
|
|
, _log(nullptr)
|
2018-12-27 23:11:32 +01:00
|
|
|
, _port(0)
|
2013-11-08 22:18:10 +01:00
|
|
|
{
|
2022-02-22 20:58:59 +01:00
|
|
|
QString subComponent = _hyperion->property("instance").toString();
|
|
|
|
_log= Logger::getInstance("BOBLIGHT", subComponent);
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
Debug(_log, "Instance created");
|
|
|
|
|
|
|
|
// listen for component change
|
2020-08-02 22:32:00 +02:00
|
|
|
connect(_hyperion, &Hyperion::compStateChangeRequest, this, &BoblightServer::compStateChangeRequest);
|
2018-12-27 23:11:32 +01:00
|
|
|
// listen new connection signal from server
|
2020-08-02 22:32:00 +02:00
|
|
|
connect(_server, &QTcpServer::newConnection, this, &BoblightServer::newConnection);
|
2018-12-27 23:11:32 +01:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2022-02-22 20:58:59 +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
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
bool BoblightServer::active() const
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
|
|
|
return _server->isListening();
|
2013-11-08 22:18:10 +01:00
|
|
|
}
|
|
|
|
|
2020-08-08 13:09:15 +02:00
|
|
|
void BoblightServer::compStateChangeRequest(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)
|
|
|
|
{
|
2022-02-22 20:58:59 +01:00
|
|
|
Info(_log, "New connection from %s ", QSTRING_CSTR(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
|
2020-08-02 22:32:00 +02:00
|
|
|
connect(connection, &BoblightClientConnection::connectionClosed, this, &BoblightServer::closedConnection);
|
2013-11-08 22:18:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoblightServer::closedConnection(BoblightClientConnection *connection)
|
|
|
|
{
|
2022-02-22 20:58:59 +01:00
|
|
|
Debug(_log, "Connection closed for %s", QSTRING_CSTR(QString("Boblight@%1").arg(connection->getClientAddress())));
|
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
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|