Skip to content

Commit

Permalink
SharpTerminal: unregistered devices are show and removed
Browse files Browse the repository at this point in the history
Broker: unregistered devices when go offline are deleted
  • Loading branch information
bcsanches committed Aug 1, 2024
1 parent 0fb035a commit 7acd292
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
11 changes: 11 additions & 0 deletions src/BrokerLib/dcc/DccLiteService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,15 @@ namespace dcclite::broker
this->NotifyItemDestroyed(*session);
}

void DccLiteService::Device_DestroyUnregistered(NetworkDevice &dev)
{
if (dev.IsConnectionStable())
throw std::runtime_error(fmt::format("[DccLiteService::Device_DestroyUnregistered] Cannot destroy connected device {}", dev.GetName()));

auto item = this->m_pDevices->RemoveChild(dev.GetName());
this->NotifyItemDestroyed(*item.get());
}

Decoder* DccLiteService::TryFindDecoder(const DccAddress address) const
{
//Name is registered?
Expand Down Expand Up @@ -494,6 +503,8 @@ namespace dcclite::broker
m_rclProject
)
));

this->NotifyItemCreated(*netDevice);
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions src/BrokerLib/dcc/DccLiteService.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ namespace dcclite::broker
void Device_RegisterSession(NetworkDevice& dev, const dcclite::Guid& configToken) override;
void Device_UnregisterSession(NetworkDevice& dev, const dcclite::Guid& sessionToken) override;

void Device_DestroyUnregistered(NetworkDevice &dev);

Decoder& Device_CreateDecoder(
IDevice_DecoderServices &dev,
const std::string &className,
Expand Down
3 changes: 2 additions & 1 deletion src/BrokerLib/dcc/IDccLiteService.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ namespace dcclite::broker
virtual void Device_SendPacket(const dcclite::NetworkAddress destination, const dcclite::Packet& packet) = 0;

virtual void Device_RegisterSession(NetworkDevice &dev, const dcclite::Guid &configToken) = 0;
virtual void Device_UnregisterSession(NetworkDevice &dev, const dcclite::Guid &sessionToken) = 0;
virtual void Device_UnregisterSession(NetworkDevice &dev, const dcclite::Guid &sessionToken) = 0;
virtual void Device_DestroyUnregistered(NetworkDevice &dev) = 0;

virtual void Device_NotifyInternalItemCreated(dcclite::IObject &item) const = 0;
virtual void Device_NotifyInternalItemDestroyed(dcclite::IObject &item) const = 0;
Expand Down
35 changes: 19 additions & 16 deletions src/BrokerLib/dcc/NetworkDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,16 @@ namespace dcclite::broker
//During unload we go to a inconsistent state, so we force a disconnect so we ignore all remote device data
//We will need to go throught all the reconnect process

this->Disconnect();
if (m_kStatus == Status::OFFLINE)
return;

DevicePacket pkt{ dcclite::MsgTypes::DISCONNECT, m_SessionToken, m_ConfigToken };

m_clDccService.Device_SendPacket(m_RemoteAddress, pkt);

dcclite::Log::Warn("[Device::{}] [Disconnect] Sent disconnect packet", this->GetName());

this->GoOffline();
}

//
Expand Down Expand Up @@ -710,21 +719,7 @@ namespace dcclite::broker
m_clPinManager.Serialize(stream);
}

void NetworkDevice::Disconnect()
{
if (m_kStatus == Status::OFFLINE)
return;

DevicePacket pkt{ dcclite::MsgTypes::DISCONNECT, m_SessionToken, m_ConfigToken };

m_clDccService.Device_SendPacket(m_RemoteAddress, pkt);

dcclite::Log::Warn("[Device::{}] [Disconnect] Sent disconnect packet", this->GetName());

this->GoOffline();
}

void NetworkDevice::GoOffline()
bool NetworkDevice::GoOffline()
{
m_kStatus = Status::OFFLINE;

Expand All @@ -738,6 +733,14 @@ namespace dcclite::broker

dcclite::Log::Warn("[Device::{}] [GoOffline] Is OFFLINE", this->GetName());
m_clDccService.Device_NotifyStateChange(*this);

if (m_fRegistered)
return true;

//kill ourselves...
m_clDccService.Device_DestroyUnregistered(*this);

return false;
}


Expand Down
16 changes: 7 additions & 9 deletions src/BrokerLib/dcc/NetworkDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,15 @@ namespace dcclite::broker

[[nodiscard]] bool CheckSession(const dcclite::NetworkAddress remoteAddress);

/*
/**
* This methods sets the state as offline, such as in cases when a connection is dropped
*
*/
void GoOffline();

/*
* Disconnect does a graceful disconnect telling the device that we are disconnecting, after that, it goes to OFFLINE state
*
* Also, if device is unregistered, the method maybe suicidal, device will be destroyed after going offline
*
* Returns true if still alive
*
*/
void Disconnect();
bool GoOffline();

template <typename T, class... Args>
void SetState(Args&&...args);
Expand Down Expand Up @@ -331,7 +329,7 @@ namespace dcclite::broker
Devices that contact the Broker, but are not in the config files, are marked as unregistered
*/
bool m_fRegistered;
bool m_fRegistered;
};

}
5 changes: 3 additions & 2 deletions src/SharpTools/SharpTerminal/ObjectsTreeViewUserControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ private void DeleteTreeNodes_r(TreeNode node)
DeleteTreeNodes_r(subNode);
}

var remoteObject = (RemoteObject)node.Tag;
if (node.Tag is not RemoteObject remoteObject)
return;

var nodes = mObjectsNodes[remoteObject.InternalId];
var nodes = mObjectsNodes[remoteObject.InternalId];

nodes.Remove(node);

Expand Down
2 changes: 0 additions & 2 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
DCCLite
-------
- update route section to know next section and avoid re-ocupying the block
- ThrottleServiceImpl another candidate for using custom folder

- SharpTerminal: tree view should reflect update when a new device is added (test with unnamed device)
- SharpDude: add support to configure device name
- Auto config device name
- SharpTerminal will show unconfigured devices and allow to configure its name
Expand Down

0 comments on commit 7acd292

Please sign in to comment.