Add new log command and some general command improvments

This commit is contained in:
Stone_Red
2023-12-13 21:34:57 +01:00
parent b009d1ac64
commit 8a587ee627
13 changed files with 319 additions and 118 deletions
@@ -47,7 +47,7 @@ public class DownloadCommands
return;
}
hostsManager.AddRange(publicHyperFileInfo.Hosts);
_ = hostsManager.AddRange(publicHyperFileInfo.Hosts);
GetFile(publicHyperFileInfo.Hash);
}
@@ -109,7 +109,7 @@ public class DownloadCommands
byte[] bytesToSend = Encoding.ASCII.GetBytes($"Download {hash}");
nwStream.Write(bytesToSend);
nwStream.ReadTimeout = 30000;
nwStream.ReadTimeout = 5000;
int bytesRead;
try
@@ -157,9 +157,8 @@ public class DownloadCommands
using FileStream? fileStream = new FileStream(filePath, FileMode.Create);
int bytesInOneSecond = 0;
int unitsPerSecond = 0;
string unit = "Kb";
int bytesPerSecond = 0;
int transferRate = 0;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
@@ -182,26 +181,16 @@ public class DownloadCommands
fileStream.Write(reciveBuffer, 0, bytesRead);
totalBytesRead += bytesRead;
bytesInOneSecond += bytesRead;
bytesPerSecond += bytesRead;
if (stopWatch.Elapsed.TotalSeconds >= 1)
{
unitsPerSecond = (int)(bytesInOneSecond * stopWatch.Elapsed.TotalSeconds);
if (unitsPerSecond > 125000)
{
unitsPerSecond /= 125000;
unit = "Mb";
}
else
{
unitsPerSecond /= 125;
unit = "Kb";
}
bytesInOneSecond = 0;
transferRate = bytesPerSecond;
bytesPerSecond = 0;
stopWatch.Restart();
}
ApiManager.SendNotificationMessage($"\rDownloading: {Math.Clamp(Math.Ceiling(100d / fileSize * totalBytesRead), 0, 100)}% {totalBytesRead / 1000}/{fileSize / 1000}KB [{unitsPerSecond}{unit}/s] ");
ApiManager.SendNotificationMessage($"\rDownloading: {Math.Clamp(Math.Ceiling(100d / fileSize * totalBytesRead), 0, 100)}% {DisplayFileSize(totalBytesRead)}/{DisplayFileSize(fileSize)} [{DisplayTransferRate(transferRate)}] ");
}
fileStream.Close();
@@ -233,4 +222,38 @@ public class DownloadCommands
ApiManager.SendNotificationMessageNewLine("None of the available hosts have the requested file!", NotificationMessageType.Error);
hostsManager.SaveHosts();
}
private string DisplayTransferRate(long bytesPerSecond)
{
string[] ordinals = new[] { "", "K", "M", "G", "T", "P", "E" };
decimal rate = bytesPerSecond * 8;
int ordinal = 0;
while (rate > 1000)
{
rate /= 1000;
ordinal++;
}
return $"{Math.Round(rate, 0, MidpointRounding.AwayFromZero)}{ordinals[ordinal]}bps";
}
private string DisplayFileSize(long bytes)
{
string[] ordinals = new[] { "", "K", "M", "G", "T", "P", "E" };
decimal rate = bytes;
int ordinal = 0;
while (rate > 1000)
{
rate /= 1000;
ordinal++;
}
return $"{Math.Round(rate, 0, MidpointRounding.AwayFromZero)}{ordinals[ordinal]}B";
}
}
@@ -32,11 +32,24 @@ public class FileCommands
}
}
public void RemoveFile(string hash)
public void RemoveFile(string args)
{
hash = hash.Trim().ToLower();
args = args.Trim().ToLower();
if (filesManager.TryRemove(hash))
if (int.TryParse(args, out int index))
{
List<PrivateHyperFileInfo> fileInfos = filesManager.ToList();
if (index < 1 || index > fileInfos.Count)
{
ApiManager.SendNotificationMessageNewLine("Invalid index!", NotificationMessageType.Error);
return;
}
args = fileInfos[index - 1].Hash;
}
if (filesManager.TryRemove(args))
{
ApiManager.SendNotificationMessageNewLine($"Successfully removed file!", NotificationMessageType.Success);
}
@@ -46,7 +59,7 @@ public class FileCommands
}
}
public void ListFiles(string _)
public void ListFiles(string searchString)
{
int index = 0;
List<PrivateHyperFileInfo> fileInfos = filesManager.ToList();
@@ -60,6 +73,12 @@ public class FileCommands
foreach (PrivateHyperFileInfo fileInfo in fileInfos)
{
index++;
if (!string.IsNullOrWhiteSpace(searchString) && !fileInfo.FilePath.Contains(searchString, StringComparison.OrdinalIgnoreCase))
{
continue;
}
ApiManager.SendNotificationMessageNewLine($"{index}) {fileInfo.FilePath}");
ApiManager.SendNotificationMessageNewLine($"Hash: {fileInfo.Hash}");
ApiManager.SendNotificationMessageNewLine(string.Empty);
@@ -67,7 +86,7 @@ public class FileCommands
Console.CursorTop--;
}
public void GenerateFileSingle(string hash)
public void GenerateFileSingle(string args)
{
string directoryPath = Path.Combine(ApiConfiguration.BasePath, "GeneratedFiles");
if (!Directory.Exists(directoryPath))
@@ -75,9 +94,23 @@ public class FileCommands
_ = Directory.CreateDirectory(directoryPath);
}
hash = hash.Trim().ToLower();
args = args.Trim().ToLower();
if (!filesManager.TryGet(hash, out PrivateHyperFileInfo? localHyperFileInfo))
PrivateHyperFileInfo? localHyperFileInfo;
if (int.TryParse(args, out int index))
{
List<PrivateHyperFileInfo> fileInfos = filesManager.ToList();
if (index < 1 || index > fileInfos.Count)
{
ApiManager.SendNotificationMessageNewLine("Invalid index!", NotificationMessageType.Error);
return;
}
localHyperFileInfo = fileInfos[index - 1];
}
else if (!filesManager.TryGet(args, out localHyperFileInfo))
{
ApiManager.SendNotificationMessageNewLine("The file is not being tracked!", NotificationMessageType.Error);
return;
@@ -86,7 +119,7 @@ public class FileCommands
string fileName = Path.GetFileName(localHyperFileInfo!.FilePath);
string filePath = Path.Combine(directoryPath, $"{fileName}.hyper");
PublicHyperFileInfo publicHyperFileInfo = new PublicHyperFileInfo(hash);
PublicHyperFileInfo publicHyperFileInfo = new PublicHyperFileInfo(args);
NetworkSocket? localHost = ApiManager.GetLocalSocket();
if (localHost is null)
@@ -106,7 +139,7 @@ public class FileCommands
ApiManager.SendNotificationMessageNewLine($"File saved at: {Path.GetFullPath(filePath)}");
}
public void GenerateFileFull(string hash)
public void GenerateFileFull(string args)
{
string directoryPath = Path.Combine(ApiConfiguration.BasePath, "GeneratedFiles");
if (!Directory.Exists(directoryPath))
@@ -114,9 +147,23 @@ public class FileCommands
_ = Directory.CreateDirectory(directoryPath);
}
hash = hash.Trim().ToLower();
args = args.Trim().ToLower();
if (!filesManager.TryGet(hash, out PrivateHyperFileInfo? localHyperFileInfo))
PrivateHyperFileInfo? localHyperFileInfo;
if (int.TryParse(args, out int index))
{
List<PrivateHyperFileInfo> fileInfos = filesManager.ToList();
if (index < 1 || index > fileInfos.Count)
{
ApiManager.SendNotificationMessageNewLine("Invalid index!", NotificationMessageType.Error);
return;
}
localHyperFileInfo = fileInfos[index - 1];
}
else if (!filesManager.TryGet(args, out localHyperFileInfo))
{
ApiManager.SendNotificationMessageNewLine("The file is not being tracked!", NotificationMessageType.Error);
return;
@@ -125,7 +172,7 @@ public class FileCommands
string fileName = Path.GetFileName(localHyperFileInfo!.FilePath);
string filePath = Path.Combine(directoryPath, $"{fileName}.hyper");
PublicHyperFileInfo publicHyperFileInfo = new PublicHyperFileInfo(hash);
PublicHyperFileInfo publicHyperFileInfo = new PublicHyperFileInfo(args);
NetworkSocket? localHost = ApiManager.GetLocalSocket();
if (localHost is null)
@@ -148,7 +195,7 @@ public class FileCommands
Console.CursorLeft = 0;
Task<bool> sendTask = NetworkClient.SendAsync<bool>(ipAddress!, host.Port, "HasFile", hash);
Task<bool> sendTask = NetworkClient.SendAsync<bool>(ipAddress!, host.Port, "HasFile", args);
_ = sendTask.Wait(1000);
@@ -67,6 +67,20 @@ public class HostCommands
{
string[] parts = args.Split(":");
if (int.TryParse(args, out int index))
{
if (hostsManager.Count < index || index < 1)
{
ApiManager.SendNotificationMessageNewLine("Invalid index!", NotificationMessageType.Error);
return;
}
hostsManager.Remove(hostsManager.ToList()[index - 1], true);
ApiManager.SendNotificationMessageNewLine($"Successfully Removed host!", NotificationMessageType.Success);
return;
}
if (parts.Length != 2)
{
ApiManager.SendNotificationMessageNewLine("Invalid format! Use this format: (xxx.xxx.xxx.xxx:yyyy)", NotificationMessageType.Error);
@@ -144,7 +158,17 @@ public class HostCommands
{
ApiManager.SendNotificationMessageNewLine("Waiting for response...", NotificationMessageType.Info);
NetworkSocket? localSocket = ApiManager.GetLocalSocket() ?? new NetworkSocket("0.0.0.0", 0, DateTime.MinValue);
List<NetworkSocket>? recivedHosts = NetworkClient.Send<List<NetworkSocket>>(ipAddress, port, "GetHostsList", localSocket);
Task<List<NetworkSocket>?> sendTask = NetworkClient.SendAsync<List<NetworkSocket>>(ipAddress, port, "GetHostsList", localSocket);
_ = sendTask.Wait(5000);
if (!sendTask.IsCompletedSuccessfully)
{
ApiManager.SendNotificationMessageNewLine($"No response from {ipAddress}:{port}", NotificationMessageType.Error);
return;
}
List<NetworkSocket>? recivedHosts = sendTask.Result;
if (recivedHosts is not null)
{