Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dc42 committed Sep 18, 2024
1 parent a3f558f commit 2eca53a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 75 deletions.
108 changes: 37 additions & 71 deletions src/Networking/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,44 @@ void Network::CreateAdditionalInterface() noexcept
#if HAS_NETWORKING

// Terminate all responders that handle a specified protocol (unless AnyProtocol is passed) on a specified interface
void Network::TerminateResponders(const NetworkInterface *iface, NetworkProtocol protocol) noexcept
void Network::TerminateResponders(const NetworkInterface *iface, NetworkProtocol protocol, bool client) noexcept
{
for (NetworkResponder *r = responders; r != nullptr; r = r->GetNext())
# if HAS_RESPONDERS
if (!client)
{
for (NetworkResponder *r = responders; r != nullptr; r = r->GetNext())
{
r->Terminate(protocol, iface);
}
}

# if SUPPORT_HTTP
if (protocol == HttpProtocol || protocol == AnyProtocol)
{
r->Terminate(protocol, iface);
HttpResponder::DisableInterface(iface); // remove HTTP sessions that use this interface
}
# endif
# if SUPPORT_FTP
if (protocol == FtpProtocol || protocol == AnyProtocol)
{
FtpResponder::Disable(); // TODO leave any FTP session using a different interface alone
}
# endif
# if SUPPORT_TELNET
if (protocol == TelnetProtocol || protocol == AnyProtocol)
{
TelnetResponder::Disable(); // TODO leave any Telnet session using a different interface alone
}
# endif
# if SUPPORT_MQTT
if (protocol == MqttProtocol || protocol == AnyProtocol)
{
MqttClient::Disable(); // TODO leave any Mqtt clients using a different interface alone
}
# endif

// Nothing needed here for Multicast Discovery protocol
# endif
}

#endif
Expand Down Expand Up @@ -294,54 +326,9 @@ GCodeResult Network::DisableProtocol(unsigned int interface, NetworkProtocol pro

NetworkInterface * const iface = interfaces[interface];
const GCodeResult ret = iface->DisableProtocol(protocol, reply, !client);

if (ret == GCodeResult::ok)
{
#if HAS_RESPONDERS
if (!client)
{
TerminateResponders(iface, protocol);
}

switch (protocol)
{
#if SUPPORT_HTTP
case HttpProtocol:
HttpResponder::DisableInterface(iface); // free up output buffers etc.
break;
#endif

#if SUPPORT_FTP
case FtpProtocol:
// TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol.
FtpResponder::Disable();
break;
#endif

#if SUPPORT_TELNET
case TelnetProtocol:
// TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol.
TelnetResponder::Disable();
break;
#endif

#if SUPPORT_MULTICAST_DISCOVERY
// TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol.
case MulticastDiscoveryProtocol:
break;
#endif

#if SUPPORT_MQTT
case MqttProtocol:
// TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol.
MqttClient::Disable();
break;
#endif

default:
break;
}
#endif // HAS_RESPONDERS
TerminateResponders(iface, protocol, client);
}
return ret;
}
Expand Down Expand Up @@ -382,22 +369,7 @@ GCodeResult Network::EnableInterface(unsigned int interface, int mode, const Str
const GCodeResult ret = iface->EnableInterface(mode, ssid, reply);
if (ret == GCodeResult::ok && mode < 1) // if disabling the interface
{
#if HAS_RESPONDERS
TerminateResponders(iface, AnyProtocol);

# if SUPPORT_HTTP
HttpResponder::DisableInterface(iface); // remove sessions that use this interface
# endif
# if SUPPORT_FTP
FtpResponder::Disable(); // TODO leave any Telnet session using a different interface alone
# endif
# if SUPPORT_TELNET
TelnetResponder::Disable(); // TODO leave any Telnet session using a different interface alone
# endif
# if SUPPORT_MQTT
MqttClient::Disable();
# endif
#endif // HAS_RESPONDERS
TerminateResponders(iface, AnyProtocol, false);
}
return ret;
}
Expand Down Expand Up @@ -740,11 +712,8 @@ bool Network::UsingDhcp(unsigned int interface) const noexcept
return interface < GetNumNetworkInterfaces() && interfaces[interface]->UsingDhcp();
}

#endif

void Network::SetHostname(const char *name) noexcept
{
#if HAS_NETWORKING
size_t i = 0;
while (*name && i < ARRAY_UPB(hostname))
{
Expand Down Expand Up @@ -776,11 +745,8 @@ void Network::SetHostname(const char *name) noexcept
iface->UpdateHostname(hostname);
}
}
#endif
}

#if HAS_NETWORKING

// Net the MAC address. Pass -1 as the interface number to set the default MAC address for interfaces that don't have one.
GCodeResult Network::SetMacAddress(unsigned int interface, const MacAddress& mac, const StringRef& reply) noexcept
{
Expand Down
7 changes: 3 additions & 4 deletions src/Networking/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,12 @@ class Network INHERIT_OBJECT_MODEL
bool UsingDhcp(unsigned int interface) const noexcept;
GCodeResult SetMacAddress(unsigned int interface, const MacAddress& mac, const StringRef& reply) noexcept;
const MacAddress& GetMacAddress(unsigned int interface) const noexcept;

void TerminateResponders(const NetworkInterface *iface, NetworkProtocol protocol) noexcept;
#endif

const char *GetHostname() const noexcept { return hostname; }
void SetHostname(const char *name) noexcept;

void TerminateResponders(const NetworkInterface *iface, NetworkProtocol protocol, bool client) noexcept;
#endif

#if SUPPORT_HTTP
const char *GetCorsSite() const noexcept { return corsSite.IsEmpty() ? nullptr : corsSite.c_str(); }
void SetCorsSite(const char *site) noexcept { corsSite.copy(site); }
Expand Down
2 changes: 2 additions & 0 deletions src/Platform/RepRap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2461,9 +2461,11 @@ void RepRap::SetName(const char* nm) noexcept
{
myName.copy(nm);

#if HAS_NETWORKING
// Set new DHCP hostname
network->SetHostname(myName.c_str());
NetworkUpdated();
#endif
}

// Firmware update operations
Expand Down

0 comments on commit 2eca53a

Please sign in to comment.