2013-11-08 22:18:10 +01:00
|
|
|
// system includes
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
// project includes
|
|
|
|
#include <boblightserver/BoblightServer.h>
|
|
|
|
#include "BoblightClientConnection.h"
|
|
|
|
|
2016-08-11 07:13:55 +02:00
|
|
|
using namespace hyperion;
|
|
|
|
|
2016-06-27 22:43:43 +02:00
|
|
|
BoblightServer::BoblightServer(const int priority, uint16_t port)
|
|
|
|
: QObject()
|
|
|
|
, _hyperion(Hyperion::getInstance())
|
|
|
|
, _server()
|
|
|
|
, _openConnections()
|
|
|
|
, _priority(priority)
|
|
|
|
, _log(Logger::getInstance("BOBLIGHT"))
|
|
|
|
, _isActive(false)
|
|
|
|
, _port(port)
|
2013-11-08 22:18:10 +01:00
|
|
|
{
|
2016-06-27 22:43:43 +02:00
|
|
|
// Set trigger for incoming connections
|
|
|
|
connect(&_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
|
|
|
|
}
|
|
|
|
|
|
|
|
BoblightServer::~BoblightServer()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoblightServer::start()
|
|
|
|
{
|
|
|
|
if ( active() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!_server.listen(QHostAddress::Any, _port))
|
2013-11-08 22:18:10 +01:00
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
throw std::runtime_error("BOBLIGHT ERROR: server could not bind to port");
|
2013-11-08 22:18:10 +01:00
|
|
|
}
|
2016-06-27 22:43:43 +02:00
|
|
|
Info(_log, "Boblight server started on port %d", _port);
|
2013-11-08 22:18:10 +01:00
|
|
|
|
2016-06-27 22:43:43 +02:00
|
|
|
_isActive = true;
|
|
|
|
emit statusChanged(_isActive);
|
2016-07-15 23:08:55 +02:00
|
|
|
|
|
|
|
_hyperion->registerPriority("Boblight", _priority);
|
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
|
|
|
{
|
2016-06-27 22:43:43 +02:00
|
|
|
if ( ! active() )
|
|
|
|
return;
|
|
|
|
|
2013-11-08 22:18:10 +01:00
|
|
|
foreach (BoblightClientConnection * connection, _openConnections) {
|
|
|
|
delete connection;
|
|
|
|
}
|
2016-08-11 07:13:55 +02:00
|
|
|
_server.close();
|
2016-06-27 22:43:43 +02:00
|
|
|
_isActive = false;
|
|
|
|
emit statusChanged(_isActive);
|
2016-07-15 23:08:55 +02:00
|
|
|
|
|
|
|
_hyperion->unRegisterPriority("Boblight");
|
|
|
|
|
2013-11-08 22:18:10 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 07:13:55 +02:00
|
|
|
void BoblightServer::componentStateChanged(const hyperion::Components component, bool enable)
|
|
|
|
{
|
2016-09-07 20:10:37 +02:00
|
|
|
if (component == COMP_BOBLIGHTSERVER)
|
2016-08-11 07:13:55 +02:00
|
|
|
{
|
2016-09-07 20:10:37 +02:00
|
|
|
if (_isActive != enable)
|
|
|
|
{
|
|
|
|
if (enable) start();
|
|
|
|
else stop();
|
|
|
|
Info(_log, "change state to %s", (_isActive ? "enabled" : "disabled") );
|
|
|
|
}
|
|
|
|
_hyperion->getComponentRegister().componentStateChanged(component, _isActive);
|
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
|
|
|
|
{
|
|
|
|
return _server.serverPort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoblightServer::newConnection()
|
|
|
|
{
|
|
|
|
QTcpSocket * socket = _server.nextPendingConnection();
|
|
|
|
|
|
|
|
if (socket != nullptr)
|
|
|
|
{
|
2016-06-27 22:43:43 +02:00
|
|
|
Info(_log, "new connection");
|
2016-07-15 23:08:55 +02:00
|
|
|
BoblightClientConnection * connection = new BoblightClientConnection(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();
|
|
|
|
}
|