Реализована запись полученных кадров в файл

This commit is contained in:
Пытков Роман
2025-09-19 23:10:54 +03:00
parent c27e78cffe
commit af62ec6375
6 changed files with 183 additions and 6 deletions

View File

@@ -1,4 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
@@ -9,14 +11,32 @@ using Domain;
using Domain.Dto;
using MessagePack;
if (args.Length < 2)
if (args.Length == 0 || args.Contains("--help") || args.Contains("-h"))
{
System.Console.WriteLine("Pass two args: http/tcp and json/bin");
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),
@@ -24,6 +44,17 @@ IClient? client = protocol switch
_ => 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);
@@ -44,6 +75,44 @@ 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();