From 809ab825249dbd56b932aaef144f15df3a94f5e9 Mon Sep 17 00:00:00 2001 From: redPanther Date: Mon, 27 Jun 2016 22:43:43 +0200 Subject: [PATCH] migrate boblight to new logger and make it start/stoppable during runtime (#62) --- include/boblightserver/BoblightServer.h | 27 ++++++++++ .../BoblightClientConnection.cpp | 23 +++++---- .../boblightserver/BoblightClientConnection.h | 4 ++ libsrc/boblightserver/BoblightServer.cpp | 50 ++++++++++++++----- src/hyperiond/hyperiond.cpp | 15 +++--- 5 files changed, 88 insertions(+), 31 deletions(-) diff --git a/include/boblightserver/BoblightServer.h b/include/boblightserver/BoblightServer.h index e324a715..9e6ff9e1 100644 --- a/include/boblightserver/BoblightServer.h +++ b/include/boblightserver/BoblightServer.h @@ -9,6 +9,7 @@ // Hyperion includes #include +#include class BoblightClientConnection; @@ -32,6 +33,24 @@ public: /// @return the port number on which this TCP listens for incoming connections /// 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: /// @@ -57,4 +76,12 @@ private: /// hyperion priority const int _priority; + + /// Logger instance + Logger * _log; + + /// state of connection + bool _isActive; + + uint16_t _port; }; diff --git a/libsrc/boblightserver/BoblightClientConnection.cpp b/libsrc/boblightserver/BoblightClientConnection.cpp index 4f314c39..5ae424b4 100644 --- a/libsrc/boblightserver/BoblightClientConnection.cpp +++ b/libsrc/boblightserver/BoblightClientConnection.cpp @@ -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) diff --git a/libsrc/boblightserver/BoblightClientConnection.h b/libsrc/boblightserver/BoblightClientConnection.h index 22ddfc20..d73c4960 100644 --- a/libsrc/boblightserver/BoblightClientConnection.h +++ b/libsrc/boblightserver/BoblightClientConnection.h @@ -10,6 +10,7 @@ // Hyperion includes #include +#include class ImageProcessor; @@ -100,4 +101,7 @@ private: /// The latest led color data std::vector _ledColors; + + /// logger instance + Logger * _log; }; diff --git a/libsrc/boblightserver/BoblightServer.cpp b/libsrc/boblightserver/BoblightServer.cpp index d06c2909..46f54e27 100644 --- a/libsrc/boblightserver/BoblightServer.cpp +++ b/libsrc/boblightserver/BoblightServer.cpp @@ -5,29 +5,53 @@ #include #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 diff --git a/src/hyperiond/hyperiond.cpp b/src/hyperiond/hyperiond.cpp index cb2d8364..016a9417 100644 --- a/src/hyperiond/hyperiond.cpp +++ b/src/hyperiond/hyperiond.cpp @@ -231,13 +231,15 @@ void HyperionDaemon::startNetworkServices() if (_config.isMember("boblightServer")) { const Json::Value & boblightServerConfig = _config["boblightServer"]; + _boblightServer = new BoblightServer( + boblightServerConfig.get("priority",900).asInt(), + boblightServerConfig["port"].asUInt() + ); + Debug(_log, "Boblight server created"); + 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()); + _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(); } }