hyperion.ng/libsrc/udplistener/UDPListener.cpp

162 lines
4.1 KiB
C++
Raw Normal View History

// project includes
#include <udplistener/UDPListener.h>
// bonjour includes
2018-12-27 23:11:32 +01:00
#include <bonjour/bonjourserviceregister.h>
// hyperion includes
#include <hyperion/Hyperion.h>
#include <hyperion/HyperionIManager.h>
#include "HyperionConfig.h"
// utils includes
#include <utils/NetOrigin.h>
2018-12-27 23:11:32 +01:00
// qt includes
#include <QUdpSocket>
#include <QJsonObject>
2018-12-27 23:11:32 +01:00
using namespace hyperion;
UDPListener::UDPListener(const QJsonDocument& config)
: QObject()
, _server(new QUdpSocket(this))
, _priority(0)
, _timeout(0)
, _log(Logger::getInstance("UDPLISTENER"))
, _isActive(false)
, _listenPort(0)
, _netOrigin(NetOrigin::getInstance())
{
2018-12-27 23:11:32 +01:00
// init
handleSettingsUpdate(settings::UDPLISTENER, config);
}
UDPListener::~UDPListener()
{
// clear the current channel
stop();
delete _server;
}
void UDPListener::start()
{
if ( active() )
return;
QHostAddress mcastGroup;
if (_listenAddress.isInSubnet(QHostAddress::parseSubnet("224.0.0.0/4")))
{
mcastGroup = _listenAddress;
}
if (!_server->bind(_listenAddress, _listenPort, _bondage))
{
2018-12-27 23:11:32 +01:00
Error(_log, "Could not bind to %s:%d", _listenAddress.toString().toStdString().c_str(), _listenPort);
}
else
{
Info(_log, "Started, listening on %s:%d", _listenAddress.toString().toStdString().c_str(), _listenPort);
if (!mcastGroup.isNull())
{
bool joinGroupOK = _server->joinMulticastGroup(_listenAddress);
InfoIf ( joinGroupOK, _log, "Multicast enabled");
WarningIf( ! joinGroupOK, _log, "Multicast failed");
}
_isActive = true;
2018-12-27 23:11:32 +01:00
if(_serviceRegister == nullptr)
{
_serviceRegister = new BonjourServiceRegister(this);
_serviceRegister->registerService("_hyperiond-udp._udp", _listenPort);
}
else if( _serviceRegister->getPort() != _listenPort)
2018-12-27 23:11:32 +01:00
{
delete _serviceRegister;
_serviceRegister = new BonjourServiceRegister(this);
_serviceRegister->registerService("_hyperiond-udp._udp", _listenPort);
2018-12-27 23:11:32 +01:00
}
}
}
void UDPListener::stop()
{
if ( ! active() )
return;
_server->close();
_isActive = false;
2018-12-27 23:11:32 +01:00
Info(_log, "Stopped");
}
void UDPListener::updatedComponentState(const hyperion::Components component, bool enable)
{
if (component == COMP_UDPLISTENER)
{
if (_isActive != enable)
{
if (enable) start();
else stop();
}
}
}
uint16_t UDPListener::getPort() const
{
return _server->localPort();
}
void UDPListener::readPendingDatagrams()
{
while (_server->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(_server->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
_server->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
if(_netOrigin->accessAllowed(sender, _listenAddress))
processTheDatagram(&datagram, &sender);
}
}
void UDPListener::processTheDatagram(const QByteArray * datagram, const QHostAddress * sender)
{
int packetLedCount = datagram->size()/3;
//DebugIf( (packetLedCount != hyperionLedCount), _log, "packetLedCount (%d) != hyperionLedCount (%d)", packetLedCount, hyperionLedCount);
std::vector<ColorRgb> _ledColors(packetLedCount, ColorRgb::BLACK);
for (int ledIndex=0; ledIndex < packetLedCount; ledIndex++)
{
ColorRgb & rgb = _ledColors[ledIndex];
rgb.red = datagram->at(ledIndex*3+0);
rgb.green = datagram->at(ledIndex*3+1);
rgb.blue = datagram->at(ledIndex*3+2);
}
2018-12-27 23:11:32 +01:00
// TODO provide a setInput with origin arg to overwrite senders smarter
emit registerGlobalInput(_priority, hyperion::COMP_UDPLISTENER, QString("UDPListener@%1").arg(sender->toString()));
emit setGlobalInput(_priority, _ledColors, _timeout);
}
2018-12-27 23:11:32 +01:00
void UDPListener::handleSettingsUpdate(const settings::type& type, const QJsonDocument& config)
{
if(type == settings::UDPLISTENER)
{
QJsonObject obj = config.object();
// if we change the prio we need to make sure the old one is cleared before we apply the new one!
stop();
QString addr = obj["address"].toString("");
_priority = obj["priority"].toInt();
_listenPort = obj["port"].toInt();
_listenAddress = addr.isEmpty()? QHostAddress::AnyIPv4 : QHostAddress(addr);
_bondage = (obj["shared"].toBool(false)) ? QAbstractSocket::ShareAddress : QAbstractSocket::DefaultForPlatform;
_timeout = obj["timeout"].toInt(10000);
if(obj["enable"].toBool())
start();
}
}