BoblightServer.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // system includes
  2. #include <stdexcept>
  3. // project includes
  4. #include <boblightserver/BoblightServer.h>
  5. #include "BoblightClientConnection.h"
  6. // hyperion includes
  7. #include <hyperion/Hyperion.h>
  8. // qt incl
  9. #include <QTcpServer>
  10. // netUtil
  11. #include <utils/NetUtils.h>
  12. using namespace hyperion;
  13. BoblightServer::BoblightServer(Hyperion* hyperion,const QJsonDocument& config)
  14. : QObject()
  15. , _hyperion(hyperion)
  16. , _server(new QTcpServer(this))
  17. , _openConnections()
  18. , _priority(0)
  19. , _log(nullptr)
  20. , _port(0)
  21. {
  22. QString subComponent = _hyperion->property("instance").toString();
  23. _log= Logger::getInstance("BOBLIGHT", subComponent);
  24. Debug(_log, "Instance created");
  25. // listen for component change
  26. connect(_hyperion, &Hyperion::compStateChangeRequest, this, &BoblightServer::compStateChangeRequest);
  27. // listen new connection signal from server
  28. connect(_server, &QTcpServer::newConnection, this, &BoblightServer::newConnection);
  29. // init
  30. handleSettingsUpdate(settings::BOBLSERVER, config);
  31. }
  32. BoblightServer::~BoblightServer()
  33. {
  34. stop();
  35. }
  36. void BoblightServer::start()
  37. {
  38. if ( _server->isListening() )
  39. return;
  40. if (NetUtils::portAvailable(_port, _log))
  41. _server->listen(QHostAddress::Any, _port);
  42. Info(_log, "Started on port: %d", _port);
  43. _hyperion->setNewComponentState(COMP_BOBLIGHTSERVER, _server->isListening());
  44. }
  45. void BoblightServer::stop()
  46. {
  47. if ( ! _server->isListening() )
  48. return;
  49. qDeleteAll(_openConnections);
  50. _server->close();
  51. Info(_log, "Stopped");
  52. _hyperion->setNewComponentState(COMP_BOBLIGHTSERVER, _server->isListening());
  53. }
  54. bool BoblightServer::active() const
  55. {
  56. return _server->isListening();
  57. }
  58. void BoblightServer::compStateChangeRequest(hyperion::Components component, bool enable)
  59. {
  60. if (component == COMP_BOBLIGHTSERVER)
  61. {
  62. if (_server->isListening() != enable)
  63. {
  64. if (enable) start();
  65. else stop();
  66. }
  67. }
  68. }
  69. uint16_t BoblightServer::getPort() const
  70. {
  71. return _server->serverPort();
  72. }
  73. void BoblightServer::newConnection()
  74. {
  75. QTcpSocket * socket = _server->nextPendingConnection();
  76. if (socket != nullptr)
  77. {
  78. Info(_log, "New connection from %s ", QSTRING_CSTR(QString("Boblight@%1").arg(socket->peerAddress().toString())));
  79. BoblightClientConnection * connection = new BoblightClientConnection(_hyperion, socket, _priority);
  80. _openConnections.insert(connection);
  81. // register slot for cleaning up after the connection closed
  82. connect(connection, &BoblightClientConnection::connectionClosed, this, &BoblightServer::closedConnection);
  83. }
  84. }
  85. void BoblightServer::closedConnection(BoblightClientConnection *connection)
  86. {
  87. Debug(_log, "Connection closed for %s", QSTRING_CSTR(QString("Boblight@%1").arg(connection->getClientAddress())));
  88. _openConnections.remove(connection);
  89. // schedule to delete the connection object
  90. connection->deleteLater();
  91. }
  92. void BoblightServer::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
  93. {
  94. if(type == settings::BOBLSERVER)
  95. {
  96. QJsonObject obj = config.object();
  97. _port = obj["port"].toInt();
  98. _priority = obj["priority"].toInt();
  99. stop();
  100. if(obj["enable"].toBool())
  101. start();
  102. }
  103. }