diff --git a/include/udplistener/UDPListener.h b/include/udplistener/UDPListener.h index 9b8f5154..2ee9c5ec 100644 --- a/include/udplistener/UDPListener.h +++ b/include/udplistener/UDPListener.h @@ -9,6 +9,7 @@ // Hyperion includes #include +#include class UDPClientConnection; @@ -25,7 +26,7 @@ public: /// @param hyperion Hyperion instance /// @param port port number on which to start listening for connections /// - UDPListener(const int priority, const int timeout, uint16_t port = 2801); + UDPListener(const int priority, const int timeout, const std::string& address, quint16 listenPort); ~UDPListener(); /// @@ -59,4 +60,7 @@ private: /// The latest led color data std::vector _ledColors; + + /// Logger instance + Logger * _log; }; diff --git a/libsrc/udplistener/UDPListener.cpp b/libsrc/udplistener/UDPListener.cpp index 46d8c9ea..577ad446 100644 --- a/libsrc/udplistener/UDPListener.cpp +++ b/libsrc/udplistener/UDPListener.cpp @@ -10,19 +10,38 @@ #include "utils/ColorRgb.h" #include "HyperionConfig.h" -UDPListener::UDPListener(const int priority, const int timeout, uint16_t port) : +UDPListener::UDPListener(const int priority, const int timeout, const std::string& address, quint16 listenPort) : QObject(), _hyperion(Hyperion::getInstance()), _server(), _openConnections(), _priority(priority), _timeout(timeout), - _ledColors(Hyperion::getInstance()->getLedCount(), ColorRgb::BLACK) + _ledColors(Hyperion::getInstance()->getLedCount(), ColorRgb::BLACK), + _log(Logger::getInstance("UDPLISTENER")) { _server = new QUdpSocket(this); - if (!_server->bind(QHostAddress::Any, port)) + QHostAddress listenAddress = QHostAddress(QHostAddress::Any); + + if (address.empty()) { + listenAddress = QHostAddress::Any; + } else { + listenAddress = QHostAddress( QString::fromStdString(address) ); + } + + if (!_server->bind(listenAddress, listenPort)) { - throw std::runtime_error("UDPLISTENER ERROR: server could not bind to port"); + Warning(_log, "Could not bind to %s:%d parsed from %s", listenAddress.toString().toStdString().c_str(), listenPort, address.c_str()); + } else { + Info(_log, "Started, listening on %s:%d", listenAddress.toString().toStdString().c_str(), listenPort); +// if (listenAddress.QHostAddress::isMulticast() ) { // needs qt >= 5.6 + if (listenAddress.isInSubnet(QHostAddress::parseSubnet("224.0.0.0/4"))) { + if (_server->joinMulticastGroup(listenAddress)) { + Info(_log, "Multicast enabled"); + } else { + Warning(_log, "Multicast failed"); + } + } } // Set trigger for incoming connections @@ -52,7 +71,6 @@ void UDPListener::readPendingDatagrams() _server->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); -// std::cout << "UDPLISTENER INFO: new packet from " << std::endl; processTheDatagram(&datagram); } @@ -61,11 +79,8 @@ void UDPListener::readPendingDatagrams() void UDPListener::processTheDatagram(const QByteArray * datagram) { -// std::cout << "udp message: " << datagram->data() << std::endl; - int packlen = datagram->size()/3; int ledlen = _ledColors.size(); -// int maxled = std::min(datagram->size()/3, _ledColors.size()); int maxled = std::min(packlen , ledlen); for (int ledIndex=0; ledIndex < maxled; ledIndex++) { @@ -73,9 +88,7 @@ void UDPListener::processTheDatagram(const QByteArray * datagram) rgb.red = datagram->at(ledIndex*3+0); rgb.green = datagram->at(ledIndex*3+1); rgb.blue = datagram->at(ledIndex*3+2); -// printf("%02x%02x%02x%02x ", ledIndex, rgb.red, rgb.green, rgb.blue); } -// printf ("\n"); _hyperion->setColors(_priority, _ledColors, _timeout, -1); diff --git a/src/hyperiond/hyperiond.cpp b/src/hyperiond/hyperiond.cpp index 03f88456..35f0a163 100644 --- a/src/hyperiond/hyperiond.cpp +++ b/src/hyperiond/hyperiond.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -249,6 +250,7 @@ void HyperionDaemon::startNetworkServices() _udpListener = new UDPListener( udpListenerConfig.get("priority",700).asInt(), udpListenerConfig.get("timeout",10000).asInt(), + udpListenerConfig.get("address", "").asString(), udpListenerConfig.get("port", 2801).asUInt() ); Info(_log, "UDP listener created and started on port %d", _udpListener->getPort());