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

@@ -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