native UDP listener enhancements (#48)

- uses new logger
- can specifiy which ip address to listen on
- if its a multicast address, multicast is enabled
This commit is contained in:
penfold42 2016-06-25 23:15:23 +10:00 committed by brindosch
parent 33f1ef457b
commit 0b59614ab3
3 changed files with 30 additions and 11 deletions

View File

@ -9,6 +9,7 @@
// Hyperion includes // Hyperion includes
#include <hyperion/Hyperion.h> #include <hyperion/Hyperion.h>
#include <utils/Logger.h>
class UDPClientConnection; class UDPClientConnection;
@ -25,7 +26,7 @@ public:
/// @param hyperion Hyperion instance /// @param hyperion Hyperion instance
/// @param port port number on which to start listening for connections /// @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(); ~UDPListener();
/// ///
@ -59,4 +60,7 @@ private:
/// The latest led color data /// The latest led color data
std::vector<ColorRgb> _ledColors; std::vector<ColorRgb> _ledColors;
/// Logger instance
Logger * _log;
}; };

View File

@ -10,19 +10,38 @@
#include "utils/ColorRgb.h" #include "utils/ColorRgb.h"
#include "HyperionConfig.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(), QObject(),
_hyperion(Hyperion::getInstance()), _hyperion(Hyperion::getInstance()),
_server(), _server(),
_openConnections(), _openConnections(),
_priority(priority), _priority(priority),
_timeout(timeout), _timeout(timeout),
_ledColors(Hyperion::getInstance()->getLedCount(), ColorRgb::BLACK) _ledColors(Hyperion::getInstance()->getLedCount(), ColorRgb::BLACK),
_log(Logger::getInstance("UDPLISTENER"))
{ {
_server = new QUdpSocket(this); _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 // Set trigger for incoming connections
@ -52,7 +71,6 @@ void UDPListener::readPendingDatagrams()
_server->readDatagram(datagram.data(), datagram.size(), _server->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort); &sender, &senderPort);
// std::cout << "UDPLISTENER INFO: new packet from " << std::endl;
processTheDatagram(&datagram); processTheDatagram(&datagram);
} }
@ -61,11 +79,8 @@ void UDPListener::readPendingDatagrams()
void UDPListener::processTheDatagram(const QByteArray * datagram) void UDPListener::processTheDatagram(const QByteArray * datagram)
{ {
// std::cout << "udp message: " << datagram->data() << std::endl;
int packlen = datagram->size()/3; int packlen = datagram->size()/3;
int ledlen = _ledColors.size(); int ledlen = _ledColors.size();
// int maxled = std::min(datagram->size()/3, _ledColors.size());
int maxled = std::min(packlen , ledlen); int maxled = std::min(packlen , ledlen);
for (int ledIndex=0; ledIndex < maxled; ledIndex++) { 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.red = datagram->at(ledIndex*3+0);
rgb.green = datagram->at(ledIndex*3+1); rgb.green = datagram->at(ledIndex*3+1);
rgb.blue = datagram->at(ledIndex*3+2); 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); _hyperion->setColors(_priority, _ledColors, _timeout, -1);

View File

@ -5,6 +5,7 @@
#include <QLocale> #include <QLocale>
#include <QFile> #include <QFile>
#include <QHostInfo> #include <QHostInfo>
#include <QHostAddress>
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
@ -249,6 +250,7 @@ void HyperionDaemon::startNetworkServices()
_udpListener = new UDPListener( _udpListener = new UDPListener(
udpListenerConfig.get("priority",700).asInt(), udpListenerConfig.get("priority",700).asInt(),
udpListenerConfig.get("timeout",10000).asInt(), udpListenerConfig.get("timeout",10000).asInt(),
udpListenerConfig.get("address", "").asString(),
udpListenerConfig.get("port", 2801).asUInt() udpListenerConfig.get("port", 2801).asUInt()
); );
Info(_log, "UDP listener created and started on port %d", _udpListener->getPort()); Info(_log, "UDP listener created and started on port %d", _udpListener->getPort());