RSE/Manager/Commands.cs
2024-11-25 19:13:37 +01:00

90 lines
3.5 KiB
C#

using MessagePack;
using Models.Model.External;
using NetMQ;
using NetMQ.Sockets;
namespace Manager;
public static class Commands
{
public static void GetProgress()
{
Console.WriteLine("Getting progress ...");
CommunicationObject communicationObject = new();
communicationObject.Command = CommunicationCommand.GetScanningProgress;
ScanningStatus temp = GetProgress(communicationObject);
Console.WriteLine($"Total filtered: {temp.TotalFiltered:n0}");
Console.WriteLine($"Total discarded: {temp.TotalDiscarded:n0}");
Console.WriteLine($"Total percentage scanned: {temp.PercentageOfIpv4Scanned}");
Console.WriteLine($"Total Ips left: {temp.AmountOfIpv4Left:n0}");
Console.WriteLine($"Filtered DB size: {temp.FilteredDbSize} Kb");
Console.WriteLine($"Discarded DB size: {temp.DiscardedDbSize} Kb");
Console.WriteLine($"Mydb DB size: {temp.MyDbSize} Kb");
}
public static void StopServer()
{
CommunicationObject communicationObject = new();
communicationObject.Command = CommunicationCommand.StopScanning;
Console.WriteLine(SendAndRecieveStringMessage(communicationObject));
}
public static void Vacuum()
{
CommunicationObject communicationObject = new();
communicationObject.Command = CommunicationCommand.DbVacuum;
Console.WriteLine(SendAndRecieveStringMessage(communicationObject));
}
public static void ReIndex()
{
CommunicationObject communicationObject = new();
communicationObject.Command = CommunicationCommand.DbReindex;
Console.WriteLine(SendAndRecieveStringMessage(communicationObject));
}
public static void GetHelp()
{
Console.WriteLine("Available commands:");
Console.WriteLine(" stop - stops the server");
Console.WriteLine(" clear - clears the console");
Console.WriteLine(" q - quits the program");
Console.WriteLine(" p - print the progress information of the scanner");
Console.WriteLine(" g - manual garbage collect on the server");
Console.WriteLine(" r - manual reindex the databases");
Console.WriteLine(" v - manual vacuum the databases");
Console.WriteLine(" help - shows this help");
Console.WriteLine();
}
private static string SendAndRecieveStringMessage(CommunicationObject communicationObject)
{
byte[] bytes = MessagePackSerializer.Serialize(communicationObject);
using RequestSocket client = new();
client.Connect("tcp://127.0.0.1:5556");
client.SendFrame(bytes);
byte[] msg = client.ReceiveFrameBytes();
client.Close();
return MessagePackSerializer.Deserialize<string>(msg, MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray));
}
private static ScanningStatus GetProgress(CommunicationObject communicationObject)
{
byte[] bytes = MessagePackSerializer.Serialize(communicationObject);
using RequestSocket client = new();
client.Connect("tcp://127.0.0.1:5556");
client.SendFrame(bytes);
byte[] msg = client.ReceiveFrameBytes();
client.Close();
return MessagePackSerializer.Deserialize<ScanningStatus>(msg, MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray));
}
}