mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
mDNS Support (#1452)
* Allow build, if no grabbers are enabled * Align available functions to right Qt version * Update to next development version * Align available functions to right Qt version * fix workflows (apt/nightly) * Disable QNetworkConfigurationManager deprecation warnings * Initial go on Smart Pointers * Add Deallocation * Correct QT_WARNING_DISABLE_DEPRECATED (available since 5.9) * Cluster Build Variables * Hyperion Light * Address build warnings * Hyperion Light - UI * Update Protobuf to latest master * Removed compiler warnings * Added restart ability to systray * Correct Protobuf * Ignore 'no-return' warning on protobuf build * hyperion-remote: Fix auto discovery of hyperion server * Fix Qt version override * Update changelog * Remove Grabber Components, if no Grabber exists * Standalone Grabber - Fix fps default * Remote Control - Have Source Selction accrosswhole screen * Enable Blackborder detection only, if relevant input sources available * Enable Blackborder detection only, if relevant input sources available * Remote UI - rearrange containers * Checkout * Fix compilation on windows * Re-added qmdnsengine template cmake * chrono added for linux * Removed existing AVAHI/Bonjour, allow to enable/disable mDNS * hyperiond macos typo fix * Fix macOS Bundle build * Fix macOS bundle info details * Correct CMake files * Removed existing AVAHI/Bonjour (2) * Share hyperion's services via mDNS * Add mDNS Browser and mDNS for LED-Devices * Support mDNS discovery for standalone grabbers * Remove ZLib Dependency & Cleanup * mDNS - hanle 2.local2 an ".local." domains equally * Hue - Link discovery to bridge class, workaround port 443 for mDNS discovery * Fix save button state when switching between devices * Removed sessions (of other hyperions) * mDNS Publisher - Simplify service naming * mDNS refactoring & Forwarder discovery * mDNS Updates to use device service name * Consistency of standalone grabbers with mDNS Service Registry * Merge branch 'hyperion-project:master' into mDNS * Start JSON and WebServers only after Instance 0 is available * Remove bespoke qDebug Output again * MDNS updates and refactor Forwarder * Minor updates * Upgrade to CMake 3.1 * typo * macOS fix * Correct merge * - Remove dynamic linker flag from standalone dispmanX Grabber - Added ability to use system qmdns libs * Cec handler library will load at runtime * typo fix * protobuf changes * mDNS changes for Windows/macOS * test window build qmdnsengine * absolute path to protobuf cmake dir * Rework Hue Wizard supporting mDNS * LED-Devices - Retry support + Refactoring (excl. Hue) * LED-Devices - Refactoring/Retry support Hue + additional alignments * Address LGTM findings * Fix CI-Build, revert test changes * Build Windows in Release mode to avoid python problem * Correct that WebServerObject is available earlier * Ensure that instance name in logs for one instance are presented * Update content LEDs * Rework mDNS Address lookup * Fix LED UI * Fix for non mDNS Services (ignore default port) * Disbale device when now input is available * Revert back some updates, ensure last color is updated when switched on * Handle reopening case and changed IP, port for API-calls * Add UPD-DDP Device * WLED support for DDP * Fix printout * LEDDevice - Allow more retries, udapte defaults * LED-Net Devices - Select Custom device, if configured Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com> Co-authored-by: Paulchen Panther <Paulchen-Panter@protonmail.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// STL includes
|
||||
#include <stdexcept>
|
||||
#include <chrono>
|
||||
|
||||
// project includes
|
||||
#include <forwarder/MessageForwarder.h>
|
||||
@@ -12,23 +12,46 @@
|
||||
#include <utils/NetUtils.h>
|
||||
|
||||
// qt includes
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
#include <QHostInfo>
|
||||
#include <QNetworkInterface>
|
||||
#include <QThread>
|
||||
|
||||
#include <flatbufserver/FlatBufferConnection.h>
|
||||
|
||||
// mDNS discover
|
||||
#ifdef ENABLE_MDNS
|
||||
#include <mdns/MdnsBrowser.h>
|
||||
#include <mdns/MdnsServiceRegister.h>
|
||||
#endif
|
||||
|
||||
// Constants
|
||||
namespace {
|
||||
|
||||
const int DEFAULT_FORWARDER_FLATBUFFFER_PRIORITY = 140;
|
||||
|
||||
constexpr std::chrono::milliseconds CONNECT_TIMEOUT{500}; // JSON-socket connect timeout in ms
|
||||
|
||||
} //End of constants
|
||||
|
||||
MessageForwarder::MessageForwarder(Hyperion* hyperion)
|
||||
: _hyperion(hyperion)
|
||||
, _log(nullptr)
|
||||
, _muxer(_hyperion->getMuxerInstance())
|
||||
, _forwarder_enabled(true)
|
||||
, _priority(140)
|
||||
, _log(nullptr)
|
||||
, _muxer(_hyperion->getMuxerInstance())
|
||||
, _forwarder_enabled(false)
|
||||
, _priority(DEFAULT_FORWARDER_FLATBUFFFER_PRIORITY)
|
||||
, _messageForwarderFlatBufHelper(nullptr)
|
||||
{
|
||||
QString subComponent = hyperion->property("instance").toString();
|
||||
_log= Logger::getInstance("NETFORWARDER", subComponent);
|
||||
|
||||
qRegisterMetaType<TargetHost>("TargetHost");
|
||||
|
||||
#ifdef ENABLE_MDNS
|
||||
QMetaObject::invokeMethod(&MdnsBrowser::getInstance(), "browseForServiceType",
|
||||
Qt::QueuedConnection, Q_ARG(QByteArray, MdnsServiceRegister::getServiceType("jsonapi")));
|
||||
#endif
|
||||
|
||||
// get settings updates
|
||||
connect(_hyperion, &Hyperion::settingsChanged, this, &MessageForwarder::handleSettingsUpdate);
|
||||
|
||||
@@ -37,82 +60,23 @@ MessageForwarder::MessageForwarder(Hyperion* hyperion)
|
||||
|
||||
// connect with Muxer visible priority changes
|
||||
connect(_muxer, &PriorityMuxer::visiblePriorityChanged, this, &MessageForwarder::handlePriorityChanges);
|
||||
|
||||
// init
|
||||
handleSettingsUpdate(settings::NETFORWARD, _hyperion->getSetting(settings::NETFORWARD));
|
||||
}
|
||||
|
||||
MessageForwarder::~MessageForwarder()
|
||||
{
|
||||
while (!_forwardClients.isEmpty())
|
||||
{
|
||||
delete _forwardClients.takeFirst();
|
||||
}
|
||||
stopJsonTargets();
|
||||
stopFlatbufferTargets();
|
||||
}
|
||||
|
||||
|
||||
void MessageForwarder::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
|
||||
{
|
||||
if (type == settings::NETFORWARD)
|
||||
{
|
||||
// clear the current targets
|
||||
_jsonTargets.clear();
|
||||
_flatbufferTargets.clear();
|
||||
while (!_forwardClients.isEmpty())
|
||||
{
|
||||
delete _forwardClients.takeFirst();
|
||||
}
|
||||
|
||||
// build new one
|
||||
const QJsonObject& obj = config.object();
|
||||
if (!obj["json"].isNull())
|
||||
{
|
||||
const QJsonArray& addr = obj["json"].toArray();
|
||||
for (const auto& entry : addr)
|
||||
{
|
||||
addJsonTarget(entry.toObject());
|
||||
}
|
||||
}
|
||||
|
||||
if (!obj["flat"].isNull())
|
||||
{
|
||||
const QJsonArray& addr = obj["flat"].toArray();
|
||||
for (const auto& entry : addr)
|
||||
{
|
||||
addFlatbufferTarget(entry.toObject());
|
||||
}
|
||||
}
|
||||
|
||||
bool isForwarderEnabledinSettings = obj["enable"].toBool(false);
|
||||
|
||||
if (!_jsonTargets.isEmpty() && isForwarderEnabledinSettings && _forwarder_enabled)
|
||||
{
|
||||
for (const auto& targetHost : qAsConst(_jsonTargets))
|
||||
{
|
||||
InfoIf(isForwarderEnabledinSettings, _log, "Forwarding now to JSON-target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
}
|
||||
|
||||
connect(_hyperion, &Hyperion::forwardJsonMessage, this, &MessageForwarder::forwardJsonMessage, Qt::UniqueConnection);
|
||||
}
|
||||
else if (_jsonTargets.isEmpty() || !isForwarderEnabledinSettings || !_forwarder_enabled)
|
||||
{
|
||||
disconnect(_hyperion, &Hyperion::forwardJsonMessage, nullptr, nullptr);
|
||||
}
|
||||
|
||||
if (!_flatbufferTargets.isEmpty() && isForwarderEnabledinSettings && _forwarder_enabled)
|
||||
{
|
||||
for (const auto& targetHost : qAsConst(_flatbufferTargets))
|
||||
{
|
||||
InfoIf(isForwarderEnabledinSettings, _log, "Forwarding now to Flatbuffer-target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
}
|
||||
}
|
||||
else if (_flatbufferTargets.isEmpty() || !isForwarderEnabledinSettings || !_forwarder_enabled)
|
||||
{
|
||||
disconnect(_hyperion, &Hyperion::forwardSystemProtoMessage, nullptr, nullptr);
|
||||
disconnect(_hyperion, &Hyperion::forwardV4lProtoMessage, nullptr, nullptr);
|
||||
}
|
||||
|
||||
// update comp state
|
||||
_hyperion->setNewComponentState(hyperion::COMP_FORWARDER, isForwarderEnabledinSettings);
|
||||
enableTargets(isForwarderEnabledinSettings, obj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,30 +84,59 @@ void MessageForwarder::handleCompStateChangeRequest(hyperion::Components compone
|
||||
{
|
||||
if (component == hyperion::COMP_FORWARDER && _forwarder_enabled != enable)
|
||||
{
|
||||
_forwarder_enabled = enable;
|
||||
handleSettingsUpdate(settings::NETFORWARD, _hyperion->getSetting(settings::NETFORWARD));
|
||||
Info(_log, "Forwarder change state to %s", (_forwarder_enabled ? "enabled" : "disabled"));
|
||||
_hyperion->setNewComponentState(component, _forwarder_enabled);
|
||||
Info(_log, "Forwarder is %s", (enable ? "enabled" : "disabled"));
|
||||
QJsonDocument config {_hyperion->getSetting(settings::type::NETFORWARD)};
|
||||
enableTargets(enable, config.object());
|
||||
}
|
||||
}
|
||||
|
||||
void MessageForwarder::enableTargets(bool enable, const QJsonObject& config)
|
||||
{
|
||||
if (!enable)
|
||||
{
|
||||
_forwarder_enabled = false;
|
||||
stopJsonTargets();
|
||||
stopFlatbufferTargets();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
int jsonTargetNum = startJsonTargets(config);
|
||||
int flatbufTargetNum = startFlatbufferTargets(config);
|
||||
|
||||
if (flatbufTargetNum > 0)
|
||||
{
|
||||
hyperion::Components activeCompId = _hyperion->getPriorityInfo(_hyperion->getCurrentPriority()).componentId;
|
||||
if (activeCompId == hyperion::COMP_GRABBER)
|
||||
{
|
||||
connect(_hyperion, &Hyperion::forwardSystemProtoMessage, this, &MessageForwarder::forwardFlatbufferMessage, Qt::UniqueConnection);
|
||||
}
|
||||
else if (activeCompId == hyperion::COMP_V4L)
|
||||
{
|
||||
connect(_hyperion, &Hyperion::forwardV4lProtoMessage, this, &MessageForwarder::forwardFlatbufferMessage, Qt::UniqueConnection);
|
||||
}
|
||||
}
|
||||
|
||||
if (jsonTargetNum > 0 || flatbufTargetNum > 0)
|
||||
{
|
||||
_forwarder_enabled = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_forwarder_enabled = false;
|
||||
Warning(_log,"No JSON- nor Flatbuffer-Forwarder configured -> Forwarding disabled", _forwarder_enabled);
|
||||
}
|
||||
}
|
||||
_hyperion->setNewComponentState(hyperion::COMP_FORWARDER, _forwarder_enabled);
|
||||
}
|
||||
|
||||
void MessageForwarder::handlePriorityChanges(int priority)
|
||||
{
|
||||
const QJsonObject obj = _hyperion->getSetting(settings::NETFORWARD).object();
|
||||
if (priority != 0 && _forwarder_enabled && obj["enable"].toBool())
|
||||
if (priority != 0 && _forwarder_enabled)
|
||||
{
|
||||
hyperion::Components activeCompId = _hyperion->getPriorityInfo(priority).componentId;
|
||||
if (activeCompId == hyperion::COMP_GRABBER || activeCompId == hyperion::COMP_V4L)
|
||||
{
|
||||
if (!obj["flat"].isNull())
|
||||
{
|
||||
const QJsonArray& addr = obj["flat"].toArray();
|
||||
for (const auto& entry : addr)
|
||||
{
|
||||
addFlatbufferTarget(entry.toObject());
|
||||
}
|
||||
}
|
||||
|
||||
switch (activeCompId)
|
||||
{
|
||||
case hyperion::COMP_GRABBER:
|
||||
@@ -177,69 +170,131 @@ void MessageForwarder::addJsonTarget(const QJsonObject& targetConfig)
|
||||
{
|
||||
TargetHost targetHost;
|
||||
|
||||
QString config_host = targetConfig["host"].toString();
|
||||
if (NetUtils::resolveHostAddress(_log, config_host, targetHost.host))
|
||||
{
|
||||
int config_port = targetConfig["port"].toInt();
|
||||
if (NetUtils::isValidPort(_log, config_port, config_host))
|
||||
{
|
||||
targetHost.port = static_cast<quint16>(config_port);
|
||||
QString hostName = targetConfig["host"].toString();
|
||||
int port = targetConfig["port"].toInt();
|
||||
|
||||
// verify loop with JSON-server
|
||||
const QJsonObject& obj = _hyperion->getSetting(settings::JSONSERVER).object();
|
||||
if ((QNetworkInterface::allAddresses().indexOf(targetHost.host) != -1) && targetHost.port == static_cast<quint16>(obj["port"].toInt()))
|
||||
if (!hostName.isEmpty())
|
||||
{
|
||||
if (NetUtils::resolveHostToAddress(_log, hostName, targetHost.host, port))
|
||||
{
|
||||
QString address = targetHost.host.toString();
|
||||
if (hostName != address)
|
||||
{
|
||||
Error(_log, "Loop between JSON-Server and Forwarder! Configuration for host: %s, port: %d is ignored.", QSTRING_CSTR(config_host), config_port);
|
||||
Info(_log, "Resolved hostname [%s] to address [%s]", QSTRING_CSTR(hostName), QSTRING_CSTR(address));
|
||||
}
|
||||
else
|
||||
|
||||
if (NetUtils::isValidPort(_log, port, targetHost.host.toString()))
|
||||
{
|
||||
if (_forwarder_enabled)
|
||||
targetHost.port = static_cast<quint16>(port);
|
||||
|
||||
// verify loop with JSON-server
|
||||
const QJsonObject& obj = _hyperion->getSetting(settings::JSONSERVER).object();
|
||||
if ((QNetworkInterface::allAddresses().indexOf(targetHost.host) != -1) && targetHost.port == static_cast<quint16>(obj["port"].toInt()))
|
||||
{
|
||||
Error(_log, "Loop between JSON-Server and Forwarder! Configuration for host: %s, port: %d is ignored.", QSTRING_CSTR(targetHost.host.toString()), port);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_jsonTargets.indexOf(targetHost) == -1)
|
||||
{
|
||||
Info(_log, "JSON-Forwarder settings: Adding target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
Debug(_log, "JSON-Forwarder settings: Adding target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
_jsonTargets << targetHost;
|
||||
}
|
||||
else
|
||||
{
|
||||
Warning(_log, "JSON Forwarder settings: Duplicate target host configuration! Configuration for host: %s, port: %d is ignored.", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
Warning(_log, "JSON-Forwarder settings: Duplicate target host configuration! Configuration for host: %s, port: %d is ignored.", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int MessageForwarder::startJsonTargets(const QJsonObject& config)
|
||||
{
|
||||
if (!config["jsonapi"].isNull())
|
||||
{
|
||||
_jsonTargets.clear();
|
||||
const QJsonArray& addr = config["jsonapi"].toArray();
|
||||
|
||||
#ifdef ENABLE_MDNS
|
||||
if (!addr.isEmpty())
|
||||
{
|
||||
QMetaObject::invokeMethod(&MdnsBrowser::getInstance(), "browseForServiceType",
|
||||
Qt::QueuedConnection, Q_ARG(QByteArray, MdnsServiceRegister::getServiceType("jsonapi")));
|
||||
}
|
||||
#endif
|
||||
|
||||
for (const auto& entry : addr)
|
||||
{
|
||||
addJsonTarget(entry.toObject());
|
||||
}
|
||||
|
||||
if (!_jsonTargets.isEmpty())
|
||||
{
|
||||
for (const auto& targetHost : qAsConst(_jsonTargets))
|
||||
{
|
||||
Info(_log, "Forwarding now to JSON-target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
}
|
||||
|
||||
connect(_hyperion, &Hyperion::forwardJsonMessage, this, &MessageForwarder::forwardJsonMessage, Qt::UniqueConnection);
|
||||
}
|
||||
}
|
||||
return _jsonTargets.size();
|
||||
}
|
||||
|
||||
|
||||
void MessageForwarder::stopJsonTargets()
|
||||
{
|
||||
if (!_jsonTargets.isEmpty())
|
||||
{
|
||||
disconnect(_hyperion, &Hyperion::forwardJsonMessage, nullptr, nullptr);
|
||||
for (const auto& targetHost : qAsConst(_jsonTargets))
|
||||
{
|
||||
Info(_log, "Stopped forwarding to JSON-target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
}
|
||||
_jsonTargets.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void MessageForwarder::addFlatbufferTarget(const QJsonObject& targetConfig)
|
||||
{
|
||||
TargetHost targetHost;
|
||||
|
||||
QString config_host = targetConfig["host"].toString();
|
||||
if (NetUtils::resolveHostAddress(_log, config_host, targetHost.host))
|
||||
{
|
||||
int config_port = targetConfig["port"].toInt();
|
||||
if (NetUtils::isValidPort(_log, config_port, config_host))
|
||||
{
|
||||
targetHost.port = static_cast<quint16>(config_port);
|
||||
QString hostName = targetConfig["host"].toString();
|
||||
int port = targetConfig["port"].toInt();
|
||||
|
||||
// verify loop with Flatbuffer-server
|
||||
const QJsonObject& obj = _hyperion->getSetting(settings::FLATBUFSERVER).object();
|
||||
if ((QNetworkInterface::allAddresses().indexOf(targetHost.host) != -1) && targetHost.port == static_cast<quint16>(obj["port"].toInt()))
|
||||
if (!hostName.isEmpty())
|
||||
{
|
||||
if (NetUtils::resolveHostToAddress(_log, hostName, targetHost.host, port))
|
||||
{
|
||||
QString address = targetHost.host.toString();
|
||||
if (hostName != address)
|
||||
{
|
||||
Error(_log, "Loop between Flatbuffer-Server and Forwarder! Configuration for host: %s, port: %d is ignored.", QSTRING_CSTR(config_host), config_port);
|
||||
Info(_log, "Resolved hostname [%s] to address [%s]", QSTRING_CSTR(hostName), QSTRING_CSTR(address));
|
||||
}
|
||||
else
|
||||
|
||||
if (NetUtils::isValidPort(_log, port, targetHost.host.toString()))
|
||||
{
|
||||
if (_forwarder_enabled)
|
||||
targetHost.port = static_cast<quint16>(port);
|
||||
|
||||
// verify loop with Flatbuffer-server
|
||||
const QJsonObject& obj = _hyperion->getSetting(settings::FLATBUFSERVER).object();
|
||||
if ((QNetworkInterface::allAddresses().indexOf(targetHost.host) != -1) && targetHost.port == static_cast<quint16>(obj["port"].toInt()))
|
||||
{
|
||||
Error(_log, "Loop between Flatbuffer-Server and Forwarder! Configuration for host: %s, port: %d is ignored.", QSTRING_CSTR(targetHost.host.toString()), port);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_flatbufferTargets.indexOf(targetHost) == -1)
|
||||
{
|
||||
Info(_log, "Flatbuffer-Forwarder settings: Adding target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
Debug(_log, "Flatbuffer-Forwarder settings: Adding target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
_flatbufferTargets << targetHost;
|
||||
|
||||
FlatBufferConnection* flatbuf = new FlatBufferConnection("Forwarder", targetHost.host.toString(), _priority, false, targetHost.port);
|
||||
_forwardClients << flatbuf;
|
||||
if (_messageForwarderFlatBufHelper != nullptr)
|
||||
{
|
||||
emit _messageForwarderFlatBufHelper->addClient("Forwarder", targetHost, _priority, false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -251,6 +306,66 @@ void MessageForwarder::addFlatbufferTarget(const QJsonObject& targetConfig)
|
||||
}
|
||||
}
|
||||
|
||||
int MessageForwarder::startFlatbufferTargets(const QJsonObject& config)
|
||||
{
|
||||
if (!config["flatbuffer"].isNull())
|
||||
{
|
||||
if (_messageForwarderFlatBufHelper == nullptr)
|
||||
{
|
||||
_messageForwarderFlatBufHelper = new MessageForwarderFlatbufferClientsHelper();
|
||||
}
|
||||
else
|
||||
{
|
||||
emit _messageForwarderFlatBufHelper->clearClients();
|
||||
}
|
||||
_flatbufferTargets.clear();
|
||||
|
||||
const QJsonArray& addr = config["flatbuffer"].toArray();
|
||||
|
||||
#ifdef ENABLE_MDNS
|
||||
if (!addr.isEmpty())
|
||||
{
|
||||
QMetaObject::invokeMethod(&MdnsBrowser::getInstance(), "browseForServiceType",
|
||||
Qt::QueuedConnection, Q_ARG(QByteArray, MdnsServiceRegister::getServiceType("flatbuffer")));
|
||||
}
|
||||
#endif
|
||||
for (const auto& entry : addr)
|
||||
{
|
||||
addFlatbufferTarget(entry.toObject());
|
||||
}
|
||||
|
||||
if (!_flatbufferTargets.isEmpty())
|
||||
{
|
||||
for (const auto& targetHost : qAsConst(_flatbufferTargets))
|
||||
{
|
||||
Info(_log, "Forwarding now to Flatbuffer-target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
}
|
||||
}
|
||||
}
|
||||
return _flatbufferTargets.size();
|
||||
}
|
||||
|
||||
void MessageForwarder::stopFlatbufferTargets()
|
||||
{
|
||||
if (!_flatbufferTargets.isEmpty())
|
||||
{
|
||||
disconnect(_hyperion, &Hyperion::forwardSystemProtoMessage, nullptr, nullptr);
|
||||
disconnect(_hyperion, &Hyperion::forwardV4lProtoMessage, nullptr, nullptr);
|
||||
|
||||
if (_messageForwarderFlatBufHelper != nullptr)
|
||||
{
|
||||
delete _messageForwarderFlatBufHelper;
|
||||
_messageForwarderFlatBufHelper = nullptr;
|
||||
}
|
||||
|
||||
for (const auto& targetHost : qAsConst(_flatbufferTargets))
|
||||
{
|
||||
Info(_log, "Stopped forwarding to Flatbuffer-target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
||||
}
|
||||
_flatbufferTargets.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void MessageForwarder::forwardJsonMessage(const QJsonObject& message)
|
||||
{
|
||||
if (_forwarder_enabled)
|
||||
@@ -259,7 +374,7 @@ void MessageForwarder::forwardJsonMessage(const QJsonObject& message)
|
||||
for (const auto& targetHost : qAsConst(_jsonTargets))
|
||||
{
|
||||
client.connectToHost(targetHost.host, targetHost.port);
|
||||
if (client.waitForConnected(500))
|
||||
if (client.waitForConnected(CONNECT_TIMEOUT.count()))
|
||||
{
|
||||
sendJsonMessage(message, &client);
|
||||
client.close();
|
||||
@@ -270,11 +385,13 @@ void MessageForwarder::forwardJsonMessage(const QJsonObject& message)
|
||||
|
||||
void MessageForwarder::forwardFlatbufferMessage(const QString& /*name*/, const Image<ColorRgb>& image)
|
||||
{
|
||||
if (_forwarder_enabled)
|
||||
if (_messageForwarderFlatBufHelper != nullptr)
|
||||
{
|
||||
for (int i = 0; i < _forwardClients.size(); i++)
|
||||
bool isfree = _messageForwarderFlatBufHelper->isFree();
|
||||
|
||||
if (isfree && _forwarder_enabled)
|
||||
{
|
||||
_forwardClients.at(i)->setImage(image);
|
||||
QMetaObject::invokeMethod(_messageForwarderFlatBufHelper, "forwardImage", Qt::QueuedConnection, Q_ARG(Image<ColorRgb>, image));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -316,12 +433,72 @@ void MessageForwarder::sendJsonMessage(const QJsonObject& message, QTcpSocket* s
|
||||
|
||||
// parse reply data
|
||||
QJsonParseError error;
|
||||
QJsonDocument::fromJson(serializedReply, &error);
|
||||
/* QJsonDocument reply = */ QJsonDocument::fromJson(serializedReply, &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
Error(_log, "Error while parsing reply: invalid json");
|
||||
Error(_log, "Error while parsing reply: invalid JSON");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
MessageForwarderFlatbufferClientsHelper::MessageForwarderFlatbufferClientsHelper()
|
||||
{
|
||||
QThread* mainThread = new QThread();
|
||||
mainThread->setObjectName("ForwarderHelperThread");
|
||||
this->moveToThread(mainThread);
|
||||
mainThread->start();
|
||||
|
||||
_free = true;
|
||||
connect(this, &MessageForwarderFlatbufferClientsHelper::addClient, this, &MessageForwarderFlatbufferClientsHelper::addClientHandler);
|
||||
connect(this, &MessageForwarderFlatbufferClientsHelper::clearClients, this, &MessageForwarderFlatbufferClientsHelper::clearClientsHandler);
|
||||
}
|
||||
|
||||
MessageForwarderFlatbufferClientsHelper::~MessageForwarderFlatbufferClientsHelper()
|
||||
{
|
||||
_free=false;
|
||||
while (!_forwardClients.isEmpty())
|
||||
{
|
||||
_forwardClients.takeFirst()->deleteLater();
|
||||
}
|
||||
|
||||
|
||||
QThread* oldThread = this->thread();
|
||||
disconnect(oldThread, nullptr, nullptr, nullptr);
|
||||
oldThread->quit();
|
||||
oldThread->wait();
|
||||
delete oldThread;
|
||||
}
|
||||
|
||||
void MessageForwarderFlatbufferClientsHelper::addClientHandler(const QString& origin, const TargetHost& targetHost, int priority, bool skipReply)
|
||||
{
|
||||
FlatBufferConnection* flatbuf = new FlatBufferConnection(origin, targetHost.host.toString(), priority, skipReply, targetHost.port);
|
||||
_forwardClients << flatbuf;
|
||||
_free = true;
|
||||
}
|
||||
|
||||
void MessageForwarderFlatbufferClientsHelper::clearClientsHandler()
|
||||
{
|
||||
while (!_forwardClients.isEmpty())
|
||||
{
|
||||
delete _forwardClients.takeFirst();
|
||||
}
|
||||
_free = false;
|
||||
}
|
||||
|
||||
bool MessageForwarderFlatbufferClientsHelper::isFree() const
|
||||
{
|
||||
return _free;
|
||||
}
|
||||
|
||||
void MessageForwarderFlatbufferClientsHelper::forwardImage(const Image<ColorRgb>& image)
|
||||
{
|
||||
_free = false;
|
||||
|
||||
for (int i = 0; i < _forwardClients.size(); i++)
|
||||
{
|
||||
_forwardClients.at(i)->setImage(image);
|
||||
}
|
||||
|
||||
_free = true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user