Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MessageControl] Open a socket in UDPOutput only when Network is active #320

Merged
merged 7 commits into from
Nov 8, 2024
2 changes: 1 addition & 1 deletion MessageControl/MessageControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ namespace Thunder {
Announce(new Publishers::FileOutput(abbreviate, _config.FileName.Value()));
}
if ((_config.Remote.Binding.Value().empty() == false) && (_config.Remote.Port.Value() != 0)) {
Announce(new Publishers::UDPOutput(abbreviate, Core::NodeId(_config.Remote.NodeId())));
Announce(new Publishers::UDPOutput(abbreviate, Core::NodeId(_config.Remote.NodeId()), _service));
}

_webSocketExporter.Initialize(service, _config.MaxExportConnections.Value());
Expand Down
31 changes: 27 additions & 4 deletions MessageControl/MessageOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ namespace Publishers {
{
::memset(_sendBuffer, 0, sizeof(_sendBuffer));
}
UDPOutput::Channel::~Channel() {
UDPOutput::Channel::~Channel()
{
Close(Core::infinite);
}

Expand Down Expand Up @@ -160,16 +161,38 @@ namespace Publishers {
Trigger();
}

UDPOutput::UDPOutput(const Core::Messaging::MessageInfo::abbreviate abbreviate, const Core::NodeId& nodeId)
UDPOutput::UDPOutput(const Core::Messaging::MessageInfo::abbreviate abbreviate, const Core::NodeId& nodeId, PluginHost::IShell* service)
: _convertor(abbreviate)
, _output(nodeId)
, _notification(*this)
, _subSystem(service->SubSystems())
{
ASSERT(_subSystem != nullptr);

if (_subSystem != nullptr) {
_subSystem->AddRef();
_subSystem->Register(&_notification);
}
}

void UDPOutput::UpdateChannel()
{
_output.Open(0);
if (_subSystem->IsActive(PluginHost::ISubSystem::NETWORK)) {
if (_output.IsOpen() == false) {
_output.Open(0);
}
ASSERT(_output.IsOpen() == true);
}
else {
_output.Close(Core::infinite);
}
}

void UDPOutput::Message(const Core::Messaging::MessageInfo& metadata, const string& text) /* override */
{
_output.Output(_convertor.Convert(metadata, text));
if (_output.IsOpen() == true) {
_output.Output(_convertor.Convert(metadata, text));
}
}

} // namespace Publishers
Expand Down
42 changes: 40 additions & 2 deletions MessageControl/MessageOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,19 +333,57 @@ namespace Publishers {
Core::CriticalSection _adminLock;
};

class Notification : public PluginHost::ISubSystem::INotification {
public:
Notification() = delete;
Notification(const Notification&) = delete;
Notification(Notification&&) = delete;
Notification& operator=(const Notification&) = delete;
Notification& operator=(Notification&&) = delete;

explicit Notification(UDPOutput& parent)
: _parent(parent) {
}
~Notification() = default;

public:
void Updated() override
{
_parent.UpdateChannel();
}

BEGIN_INTERFACE_MAP(Notification)
INTERFACE_ENTRY(PluginHost::ISubSystem::INotification)
END_INTERFACE_MAP

private:
UDPOutput& _parent;
};

public:
UDPOutput() = delete;
UDPOutput(const UDPOutput&) = delete;
UDPOutput& operator=(const UDPOutput&) = delete;

explicit UDPOutput(const Core::Messaging::MessageInfo::abbreviate abbreviate, const Core::NodeId& nodeId);
~UDPOutput() = default;
explicit UDPOutput(const Core::Messaging::MessageInfo::abbreviate abbreviate, const Core::NodeId& nodeId, PluginHost::IShell* service);

~UDPOutput() override
{
if (_subSystem != nullptr) {
_subSystem->Unregister(&_notification);
_subSystem->Release();
_subSystem = nullptr;
}
}

void UpdateChannel();
void Message(const Core::Messaging::MessageInfo& metadata, const string& text);

private:
Text _convertor;
Channel _output;
Core::SinkType<Notification> _notification;
PluginHost::ISubSystem* _subSystem;
};

class WebSocketOutput : public IPublish {
Expand Down