142 lines
4.5 KiB
C#
142 lines
4.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Client;
|
|
using Domain;
|
|
using Domain.Dto;
|
|
using MessagePack;
|
|
|
|
if (args.Length == 0 || args.Contains("--help") || args.Contains("-h"))
|
|
{
|
|
PrintHelp();
|
|
return 0;
|
|
}
|
|
|
|
if (args.Length < 2 || (args.Length > 2 && !args[2].StartsWith("--save-dir=")))
|
|
{
|
|
Console.WriteLine("Error: Insufficient arguments provided.");
|
|
Console.WriteLine();
|
|
PrintHelp();
|
|
return -1;
|
|
}
|
|
|
|
var protocol = args[0];
|
|
var serialization = args[1];
|
|
string? saveDir = null;
|
|
if (args.Length >= 3)
|
|
{
|
|
saveDir = args[2].Substring("--save-dir=".Length);
|
|
if (Directory.Exists(saveDir) == false)
|
|
{
|
|
System.Console.WriteLine("Save dir does not exist. Ignore.");
|
|
}
|
|
}
|
|
|
|
IClient? client = protocol switch
|
|
{
|
|
"http" => new HttpClientWrapper(serialization == "json" ? ConvertJsonResponse : ConvertMessagePackResponse),
|
|
"tcp" => new TcpClientWrapper(serialization == "json" ? ConvertBytesJson : ConvertBytesMessagePack),
|
|
_ => null
|
|
};
|
|
|
|
if (saveDir != null)
|
|
{
|
|
// Create FrameAssembler
|
|
var frameAssembler = new FrameAssembler(frame =>
|
|
{
|
|
SaveFrame(frame, saveDir);
|
|
});
|
|
|
|
client?.RegisterCallback(frameAssembler.AddData);
|
|
}
|
|
|
|
client?.Start();
|
|
System.Console.WriteLine("Client started:");
|
|
System.Console.WriteLine(client);
|
|
|
|
// Обработка выхода по Ctrl+C
|
|
Console.CancelKeyPress += (sender, e) =>
|
|
{
|
|
e.Cancel = true; // Prevent immediate termination
|
|
Console.WriteLine("Shutdown signal received. Stopping client...");
|
|
client?.Stop();
|
|
Console.WriteLine("Goodbye!");
|
|
Environment.Exit(0);
|
|
};
|
|
|
|
// Бесконечный цикл ожидания
|
|
while (true)
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
|
|
static void PrintHelp()
|
|
{
|
|
Console.WriteLine("NetworkTest Client - A network testing client application");
|
|
Console.WriteLine();
|
|
Console.WriteLine("USAGE:");
|
|
Console.WriteLine(" Client <protocol> <serialization> [--save-dir=<path>]");
|
|
Console.WriteLine();
|
|
Console.WriteLine("ARGUMENTS:");
|
|
Console.WriteLine(" <protocol> The protocol to use:");
|
|
Console.WriteLine(" - http: Connect using HTTP");
|
|
Console.WriteLine(" - tcp: Connect using TCP");
|
|
Console.WriteLine();
|
|
Console.WriteLine(" <serialization> The serialization format:");
|
|
Console.WriteLine(" - json: Use JSON serialization");
|
|
Console.WriteLine(" - bin: Use MessagePack binary serialization");
|
|
Console.WriteLine();
|
|
Console.WriteLine("OPTIONS:");
|
|
Console.WriteLine(" --save-dir=<path> Directory to save received frames (optional)");
|
|
Console.WriteLine(" --help, -h Show this help message");
|
|
Console.WriteLine();
|
|
Console.WriteLine("EXAMPLES:");
|
|
Console.WriteLine(" Client http json");
|
|
Console.WriteLine(" Client tcp bin --save-dir=./frames");
|
|
Console.WriteLine(" Client --help");
|
|
Console.WriteLine();
|
|
Console.WriteLine("The client will connect to the server and start receiving data. Press Ctrl+C to stop.");
|
|
}
|
|
|
|
static void SaveFrame(List<Data> frame, string saveDir)
|
|
{
|
|
var fileName = $"frame_{frame[0].FrameIndex}";
|
|
fileName += ".json";
|
|
var jsonDatas = frame.Select(d => new JsonData(d)).ToList();
|
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
|
var json = JsonSerializer.Serialize(jsonDatas, options);
|
|
File.WriteAllText(Path.Combine(saveDir, fileName), json);
|
|
}
|
|
|
|
static async Task<Data?> ConvertJsonResponse(HttpResponseMessage response)
|
|
{
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
var jsonData = JsonSerializer.Deserialize<JsonData>(json);
|
|
return jsonData?.ToData();
|
|
}
|
|
|
|
static async Task<Data?> ConvertMessagePackResponse(HttpResponseMessage response)
|
|
{
|
|
var bytes = await response.Content.ReadAsByteArrayAsync();
|
|
var msgPackData = MessagePackSerializer.Deserialize<MessagePackData>(bytes);
|
|
return msgPackData?.ToData();
|
|
}
|
|
|
|
static Task<Data?> ConvertBytesJson(byte[] bytes)
|
|
{
|
|
var json = Encoding.UTF8.GetString(bytes);
|
|
var jsonData = JsonSerializer.Deserialize<JsonData>(json);
|
|
return Task.FromResult(jsonData?.ToData());
|
|
}
|
|
|
|
static Task<Data?> ConvertBytesMessagePack(byte[] bytes)
|
|
{
|
|
var msgPackData = MessagePackSerializer.Deserialize<MessagePackData>(bytes);
|
|
return Task.FromResult(msgPackData?.ToData());
|
|
}
|