From 6d60f3891dd7c5145e1759da118f7b1ef9125454 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Thu, 14 Dec 2023 10:05:53 +0100 Subject: [PATCH] Add `sync` command --- HyperbolicDownloader/InputHandler.cs | 1 + .../Commands/HostCommands.cs | 16 ++++++ .../Networking/HostsManager.cs | 50 +++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/HyperbolicDownloader/InputHandler.cs b/HyperbolicDownloader/InputHandler.cs index 7679148..ef72b58 100644 --- a/HyperbolicDownloader/InputHandler.cs +++ b/HyperbolicDownloader/InputHandler.cs @@ -26,6 +26,7 @@ internal class InputHandler _ = commander.Register(_ => exit = true, (HelpText)"Exits the application.", "exit", "quit"); _ = commander.Register(clientCommands.ShowInfo, (HelpText)"Displays the private and public IP address.", "info", "inf"); _ = commander.Register(hostCommands.Discover, (HelpText)"Tries to find other active hosts on the local network.", "discover", "disc"); + _ = commander.Register(hostCommands.Sync, (HelpText)"Syncs the host list", "sync"); _ = commander.Register(logCommands.Log, (HelpText)"Displays live log.", "log"); Command getCommand = commander.Register(downloadCommands.GetFile, (HelpText)"Attempts to retrieve a file from another host using a hash.", "get"); diff --git a/HyperbolicDownloaderApi/Commands/HostCommands.cs b/HyperbolicDownloaderApi/Commands/HostCommands.cs index 8bc86da..c3f83f9 100644 --- a/HyperbolicDownloaderApi/Commands/HostCommands.cs +++ b/HyperbolicDownloaderApi/Commands/HostCommands.cs @@ -202,4 +202,20 @@ public class HostCommands ApiManager.SendNotificationMessageNewLine($"Invalid host! Error message: {ex.Message}", NotificationMessageType.Error); } } + + public void Sync(string _) + { + int newHostsCount = hostsManager.Sync(); + + ApiManager.SendNotificationMessageNewLine(string.Empty, NotificationMessageType.Info); + + if (newHostsCount > 0) + { + ApiManager.SendNotificationMessage($"Added {newHostsCount} new hosts)."); + } + else + { + ApiManager.SendNotificationMessage($"No new hosts added.", NotificationMessageType.Warning); + } + } } \ No newline at end of file diff --git a/HyperbolicDownloaderApi/Networking/HostsManager.cs b/HyperbolicDownloaderApi/Networking/HostsManager.cs index 9029f61..f4b9082 100644 --- a/HyperbolicDownloaderApi/Networking/HostsManager.cs +++ b/HyperbolicDownloaderApi/Networking/HostsManager.cs @@ -1,5 +1,6 @@ using HyperbolicDownloaderApi.Managment; +using System.Net; using System.Net.Sockets; using System.Text.Json; @@ -93,6 +94,55 @@ public class HostsManager return activeHostsCount; } + public int Sync() + { + int newHostsCount = 0; + + foreach (NetworkSocket host in hosts) + { + ApiManager.SendNotificationMessage($"{host.IPAddress}:{host.Port} > ???", NotificationMessageType.Warning); + + try + { + NetworkSocket? localSocket = ApiManager.GetLocalSocket() ?? new NetworkSocket("0.0.0.0", 0, DateTime.MinValue); + + Task?> sendTask = NetworkClient.SendAsync>(IPAddress.Parse(host.IPAddress), host.Port, "GetHostsList", localSocket); + + _ = sendTask.Wait(1000); + + Console.CursorLeft = 0; + if (sendTask.IsCompletedSuccessfully) + { + int newHosts = AddRange(sendTask.Result ?? new()); + newHostsCount += newHosts; + + if (newHosts > 0) + { + ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Found {newHosts} new hosts", NotificationMessageType.Success); + } + else + { + ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > No new host found", NotificationMessageType.Warning); + } + + host.LastActive = DateTime.Now; + } + else + { + ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error); + } + } + catch + { + Console.CursorLeft = 0; + ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error); + } + } + + SaveHosts(); + return newHostsCount; + } + public List ToList() { return hosts.ToList();