Add sync command

This commit is contained in:
Stone_Red
2023-12-14 10:05:53 +01:00
parent 7913140dcd
commit 6d60f3891d
3 changed files with 67 additions and 0 deletions
@@ -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);
}
}
}
@@ -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<List<NetworkSocket>?> sendTask = NetworkClient.SendAsync<List<NetworkSocket>>(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<NetworkSocket> ToList()
{
return hosts.ToList();