migrate boblight to new logger and make it start/stoppable during runtime (#62)

This commit is contained in:
redPanther 2016-06-27 22:43:43 +02:00 committed by brindosch
parent 96037da1cf
commit 809ab82524
5 changed files with 88 additions and 31 deletions

View File

@ -9,6 +9,7 @@
// Hyperion includes
#include <hyperion/Hyperion.h>
#include <utils/Logger.h>
class BoblightClientConnection;
@ -33,6 +34,24 @@ public:
///
uint16_t getPort() const;
/// @return true if server is active (bind to a port)
///
bool active() { return _isActive; };
public slots:
///
/// bind server to network
///
void start();
///
/// close server
///
void stop();
signals:
void statusChanged(bool isActive);
private slots:
///
/// Slot which is called when a client tries to create a new connection
@ -57,4 +76,12 @@ private:
/// hyperion priority
const int _priority;
/// Logger instance
Logger * _log;
/// state of connection
bool _isActive;
uint16_t _port;
};

View File

@ -22,15 +22,16 @@
// project includes
#include "BoblightClientConnection.h"
BoblightClientConnection::BoblightClientConnection(QTcpSocket *socket, const int priority, Hyperion * hyperion) :
QObject(),
_locale(QLocale::C),
_socket(socket),
_imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor()),
_hyperion(hyperion),
_receiveBuffer(),
_priority(priority),
_ledColors(hyperion->getLedCount(), ColorRgb::BLACK)
BoblightClientConnection::BoblightClientConnection(QTcpSocket *socket, const int priority, Hyperion * hyperion)
: QObject()
, _locale(QLocale::C)
, _socket(socket)
, _imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor())
, _hyperion(hyperion)
, _receiveBuffer()
, _priority(priority)
, _ledColors(hyperion->getLedCount(), ColorRgb::BLACK)
, _log(Logger::getInstance("BOBLIGHT"))
{
// initalize the locale. Start with the default C-locale
_locale.setNumberOptions(QLocale::OmitGroupSeparator | QLocale::RejectGroupSeparator);
@ -70,7 +71,7 @@ void BoblightClientConnection::readData()
// drop messages if the buffer is too full
if (_receiveBuffer.size() > 100*1024)
{
std::cout << "BOBLIGHT INFO: server drops messages (buffer full)" << std::endl;
Debug(_log, "server drops messages (buffer full)");
_receiveBuffer.clear();
}
@ -208,7 +209,7 @@ void BoblightClientConnection::handleMessage(const QString & message)
}
}
std::cout << "BOBLIGHT INFO: unknown boblight message: " << message.toStdString() << std::endl;
Debug(_log, "unknown boblight message: %s", message.toStdString().c_str());
}
void BoblightClientConnection::sendMessage(const std::string & message)

View File

@ -10,6 +10,7 @@
// Hyperion includes
#include <hyperion/Hyperion.h>
#include <utils/Logger.h>
class ImageProcessor;
@ -100,4 +101,7 @@ private:
/// The latest led color data
std::vector<ColorRgb> _ledColors;
/// logger instance
Logger * _log;
};

View File

@ -5,29 +5,53 @@
#include <boblightserver/BoblightServer.h>
#include "BoblightClientConnection.h"
BoblightServer::BoblightServer(const int priority,uint16_t port) :
QObject(),
_hyperion(Hyperion::getInstance()),
_server(),
_openConnections(),
_priority(priority)
BoblightServer::BoblightServer(const int priority, uint16_t port)
: QObject()
, _hyperion(Hyperion::getInstance())
, _server()
, _openConnections()
, _priority(priority)
, _log(Logger::getInstance("BOBLIGHT"))
, _isActive(false)
, _port(port)
{
if (!_server.listen(QHostAddress::Any, port))
{
throw std::runtime_error("BOBLIGHT ERROR: server could not bind to port");
}
// 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))
{
throw std::runtime_error("BOBLIGHT ERROR: server could not bind to port");
}
Info(_log, "Boblight server started on port %d", _port);
_isActive = true;
emit statusChanged(_isActive);
}
void BoblightServer::stop()
{
if ( ! active() )
return;
foreach (BoblightClientConnection * connection, _openConnections) {
delete connection;
}
_isActive = false;
emit statusChanged(_isActive);
}
uint16_t BoblightServer::getPort() const
{
return _server.serverPort();
@ -39,7 +63,7 @@ void BoblightServer::newConnection()
if (socket != nullptr)
{
std::cout << "BOBLIGHT INFO: new connection" << std::endl;
Info(_log, "new connection");
BoblightClientConnection * connection = new BoblightClientConnection(socket, _priority, _hyperion);
_openConnections.insert(connection);
@ -50,7 +74,7 @@ void BoblightServer::newConnection()
void BoblightServer::closedConnection(BoblightClientConnection *connection)
{
std::cout << "BOBLIGHT INFO: connection closed" << std::endl;
Debug(_log, "connection closed");
_openConnections.remove(connection);
// schedule to delete the connection object

View File

@ -231,13 +231,15 @@ void HyperionDaemon::startNetworkServices()
if (_config.isMember("boblightServer"))
{
const Json::Value & boblightServerConfig = _config["boblightServer"];
if ( boblightServerConfig.get("enable", true).asBool() )
{
_boblightServer = new BoblightServer(
boblightServerConfig.get("priority",900).asInt(),
boblightServerConfig["port"].asUInt()
);
Info(_log, "Boblight server created and started on port %d", _boblightServer->getPort());
Debug(_log, "Boblight server created");
if ( boblightServerConfig.get("enable", true).asBool() )
{
_boblightServer->start();
}
}
@ -252,11 +254,10 @@ void HyperionDaemon::startNetworkServices()
udpListenerConfig.get("port", 2801).asUInt(),
udpListenerConfig.get("shared", false).asBool()
);
Info(_log, "UDP listener created on port %d", _udpListener->getPort());
Debug(_log, "UDP listener created");
if ( udpListenerConfig.get("enable", true).asBool() )
{
Info(_log, "UDP listener started" );
_udpListener->start();
}
}