122 lines
3.2 KiB
C#
Raw Normal View History

2024-04-01 20:40:03 +02:00
using ArgsUniform;
using AutoClient;
using CodexOpenApi;
2024-04-01 20:40:03 +02:00
using Core;
using Logging;
2024-09-12 12:05:42 +02:00
using Nethereum.Model;
using Utils;
2024-04-01 20:40:03 +02:00
2024-09-12 12:05:42 +02:00
public class Program
2024-04-01 20:40:03 +02:00
{
2024-09-12 12:05:42 +02:00
private readonly CancellationTokenSource cts;
private readonly Configuration config;
private readonly LogSplitter log;
private readonly IFileGenerator generator;
public Program(CancellationTokenSource cts, Configuration config, LogSplitter log, IFileGenerator generator)
{
this.cts = cts;
this.config = config;
this.log = log;
this.generator = generator;
}
public static async Task Main(string[] args)
2024-04-01 20:40:03 +02:00
{
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (sender, args) => cts.Cancel();
var uniformArgs = new ArgsUniform<Configuration>(PrintHelp, args);
var config = uniformArgs.Parse(true);
2024-06-28 08:47:09 +02:00
if (config.NumConcurrentPurchases < 1)
{
throw new Exception("Number of concurrent purchases must be > 0");
}
2024-04-01 20:40:03 +02:00
var log = new LogSplitter(
new FileLog(Path.Combine(config.LogPath, "autoclient")),
new ConsoleLog()
);
2024-09-12 12:05:42 +02:00
var generator = CreateGenerator(config, log);
2024-04-01 20:40:03 +02:00
2024-09-12 12:05:42 +02:00
var p = new Program(cts, config, log, generator);
await p.Run(args);
cts.Token.WaitHandle.WaitOne();
log.Log("Done.");
}
2024-04-01 20:40:03 +02:00
2024-09-12 12:05:42 +02:00
public async Task Run(string[] args)
{
var codexUsers = CreateUsers();
2024-04-01 20:40:03 +02:00
2024-09-12 12:05:42 +02:00
}
2024-04-01 20:47:56 +02:00
2024-09-12 12:05:42 +02:00
private async Task<CodexUser[]> CreateUsers()
{
var endpointStrs = config.CodexEndpoints.Split(";", StringSplitOptions.RemoveEmptyEntries);
var result = new List<CodexUser>();
2024-06-28 08:47:09 +02:00
2024-09-12 12:05:42 +02:00
foreach (var e in endpointStrs)
2024-06-28 08:47:09 +02:00
{
2024-09-12 12:05:42 +02:00
result.Add(await CreateUser(e));
2024-06-28 08:47:09 +02:00
}
2024-04-01 20:40:03 +02:00
2024-09-12 12:05:42 +02:00
return result.ToArray();
2024-04-01 20:40:03 +02:00
}
2024-09-12 12:05:42 +02:00
private async Task<CodexUser> CreateUser(string endpoint)
{
2024-09-12 12:05:42 +02:00
var splitIndex = endpoint.LastIndexOf(':');
var host = endpoint.Substring(0, splitIndex);
var port = Convert.ToInt32(endpoint.Substring(splitIndex + 1));
var address = new Address(
host: host,
port: port
);
log.Log($"Start. Address: {address}");
var client = new HttpClient();
var codex = new CodexApi(client);
codex.BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1";
await CheckCodex(codex);
return new CodexUser();
}
2024-09-12 12:05:42 +02:00
private async Task CheckCodex(CodexApi codex)
2024-04-01 20:47:56 +02:00
{
log.Log("Checking Codex...");
try
{
var info = await codex.GetDebugInfoAsync();
2024-04-01 20:47:56 +02:00
if (string.IsNullOrEmpty(info.Id)) throw new Exception("Failed to fetch Codex node id");
}
catch (Exception ex)
{
log.Log($"Codex not OK: {ex}");
throw;
}
}
2024-09-12 12:05:42 +02:00
private static IFileGenerator CreateGenerator(Configuration config, LogSplitter log)
{
if (config.FileSizeMb > 0)
{
return new RandomFileGenerator(config, log);
}
return new ImageGenerator(log);
}
2024-04-01 20:40:03 +02:00
private static void PrintHelp()
{
Console.WriteLine("Generates fake data and creates Codex storage contracts for it.");
}
}