mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 12:26:30 +00:00
add azaion loader
This commit is contained in:
@@ -7,15 +7,17 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
||||
<PackageReference Include="LazyCache.AspNetCore" Version="2.4.0" />
|
||||
<PackageReference Include="MediatR" Version="12.4.1" />
|
||||
<PackageReference Include="MediatR" Version="12.5.0" />
|
||||
<PackageReference Include="MessagePack" Version="3.1.0" />
|
||||
<PackageReference Include="MessagePack.Annotations" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.5" />
|
||||
<PackageReference Include="NetMQ" Version="4.0.1.13" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Serilog" Version="4.2.0" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
using MessagePack;
|
||||
using CommandLine;
|
||||
using MessagePack;
|
||||
|
||||
namespace Azaion.CommonSecurity.DTO;
|
||||
|
||||
[MessagePackObject]
|
||||
public class ApiCredentials(string email, string password) : EventArgs
|
||||
public class ApiCredentials : EventArgs
|
||||
{
|
||||
[Key(nameof(Email))]
|
||||
public string Email { get; set; } = email;
|
||||
[Option('e', "email", Required = true, HelpText = "User Email")]
|
||||
public string Email { get; set; } = null!;
|
||||
|
||||
[Key(nameof(Password))]
|
||||
public string Password { get; set; } = password;
|
||||
[Option('p', "pass", Required = true, HelpText = "User Password")]
|
||||
public string Password { get; set; } = null!;
|
||||
}
|
||||
@@ -4,8 +4,11 @@ public abstract class ExternalClientConfig
|
||||
{
|
||||
public string ZeroMqHost { get; set; } = "";
|
||||
public int ZeroMqPort { get; set; }
|
||||
public double OneTryTimeoutSeconds { get; set; }
|
||||
public int RetryCount {get;set;}
|
||||
}
|
||||
|
||||
public class LoaderClientConfig : ExternalClientConfig
|
||||
{
|
||||
public string ApiUrl { get; set; } = null!;
|
||||
}
|
||||
|
||||
public class InferenceClientConfig : ExternalClientConfig
|
||||
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
namespace Azaion.CommonSecurity.DTO;
|
||||
|
||||
public class SecureAppConfig
|
||||
public class InitConfig
|
||||
{
|
||||
public LoaderClientConfig LoaderClientConfig { get; set; } = null!;
|
||||
public InferenceClientConfig InferenceClientConfig { get; set; } = null!;
|
||||
public GpsDeniedClientConfig GpsDeniedClientConfig { get; set; } = null!;
|
||||
public DirectoriesConfig DirectoriesConfig { get; set; } = null!;
|
||||
@@ -8,8 +8,14 @@ public class SecurityConstants
|
||||
|
||||
public const string DUMMY_DIR = "dummy";
|
||||
|
||||
private const string DEFAULT_API_URL = "https://api.azaion.com";
|
||||
|
||||
#region ExternalClientsConfig
|
||||
|
||||
private const string DEFAULT_ZMQ_LOADER_HOST = "127.0.0.1";
|
||||
private const int DEFAULT_ZMQ_LOADER_PORT = 5025;
|
||||
|
||||
public const string EXTERNAL_LOADER_PATH = "azaion-loader.exe";
|
||||
public const string EXTERNAL_INFERENCE_PATH = "azaion-inference.exe";
|
||||
public const string EXTERNAL_GPS_DENIED_FOLDER = "gps-denied";
|
||||
public static readonly string ExternalGpsDeniedPath = Path.Combine(EXTERNAL_GPS_DENIED_FOLDER, "image-matcher.exe");
|
||||
@@ -20,9 +26,6 @@ public class SecurityConstants
|
||||
public const string DEFAULT_ZMQ_GPS_DENIED_HOST = "127.0.0.1";
|
||||
public const int DEFAULT_ZMQ_GPS_DENIED_PORT = 5227;
|
||||
|
||||
public const int DEFAULT_RETRY_COUNT = 25;
|
||||
public const int DEFAULT_TIMEOUT_SECONDS = 5;
|
||||
|
||||
# region Cache keys
|
||||
|
||||
public const string CURRENT_USER_CACHE_KEY = "CurrentUser";
|
||||
@@ -30,21 +33,24 @@ public class SecurityConstants
|
||||
|
||||
# endregion
|
||||
|
||||
public static readonly SecureAppConfig DefaultSecureAppConfig = new()
|
||||
public static readonly InitConfig DefaultInitConfig = new()
|
||||
{
|
||||
LoaderClientConfig = new LoaderClientConfig
|
||||
{
|
||||
ZeroMqHost = DEFAULT_ZMQ_LOADER_HOST,
|
||||
ZeroMqPort = DEFAULT_ZMQ_LOADER_PORT,
|
||||
ApiUrl = DEFAULT_API_URL
|
||||
},
|
||||
InferenceClientConfig = new InferenceClientConfig
|
||||
{
|
||||
ZeroMqHost = DEFAULT_ZMQ_INFERENCE_HOST,
|
||||
ZeroMqPort = DEFAULT_ZMQ_INFERENCE_PORT,
|
||||
OneTryTimeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
|
||||
RetryCount = DEFAULT_RETRY_COUNT
|
||||
ApiUrl = DEFAULT_API_URL
|
||||
},
|
||||
GpsDeniedClientConfig = new GpsDeniedClientConfig
|
||||
{
|
||||
ZeroMqHost = DEFAULT_ZMQ_GPS_DENIED_HOST,
|
||||
ZeroMqPort = DEFAULT_ZMQ_GPS_DENIED_PORT,
|
||||
OneTryTimeoutSeconds = DEFAULT_TIMEOUT_SECONDS,
|
||||
RetryCount = DEFAULT_RETRY_COUNT,
|
||||
ZeroMqPort = DEFAULT_ZMQ_GPS_DENIED_PORT
|
||||
},
|
||||
DirectoriesConfig = new DirectoriesConfig
|
||||
{
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using Azaion.CommonSecurity.DTO;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Azaion.CommonSecurity.DTO.Commands;
|
||||
using MessagePack;
|
||||
using NetMQ;
|
||||
using NetMQ.Sockets;
|
||||
using Serilog;
|
||||
using Exception = System.Exception;
|
||||
|
||||
namespace Azaion.CommonSecurity.Services;
|
||||
|
||||
public class LoaderClient
|
||||
public class LoaderClient(LoaderClientConfig config, ILogger logger, CancellationToken ct = default)
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
private readonly ILogger<LoaderClient> _logger;
|
||||
private readonly DealerSocket _dealer = new();
|
||||
private readonly Guid _clientId = Guid.NewGuid();
|
||||
|
||||
public LoaderClient(IMediator mediator, IOptions<LoaderClientConfig> config, ILogger<LoaderClient> logger)
|
||||
public void StartClient()
|
||||
{
|
||||
_mediator = mediator;
|
||||
_logger = logger;
|
||||
try
|
||||
{
|
||||
using var process = new Process();
|
||||
process.StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = SecurityConstants.LoaderPath,
|
||||
Arguments = $"--port {config.Value.ZeroMqPort} --api {config.Value.ApiUrl}",
|
||||
//RedirectStandardOutput = true,
|
||||
//RedirectStandardError = true,
|
||||
//CreateNoWindow = true
|
||||
FileName = SecurityConstants.EXTERNAL_LOADER_PATH,
|
||||
Arguments = $"--port {config.ZeroMqPort} --api {config.ApiUrl}",
|
||||
CreateNoWindow = true
|
||||
};
|
||||
|
||||
process.OutputDataReceived += (_, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
|
||||
@@ -33,19 +33,44 @@ public class LoaderClient
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
//throw;
|
||||
logger.Error(e.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
_requestAddress = $"tcp://{gpsConfig.Value.ZeroMqHost}:{gpsConfig.Value.ZeroMqPort}";
|
||||
_requestSocket.Connect(_requestAddress);
|
||||
public void Connect()
|
||||
{
|
||||
_dealer.Options.Identity = Encoding.UTF8.GetBytes(_clientId.ToString("N"));
|
||||
_dealer.Connect($"tcp://{config.ZeroMqHost}:{config.ZeroMqPort}");
|
||||
}
|
||||
|
||||
_subscriberAddress = $"tcp://{gpsConfig.Value.ZeroMqHost}:{gpsConfig.Value.ZeroMqReceiverPort}";
|
||||
_subscriberSocket.Connect(_subscriberAddress);
|
||||
_subscriberSocket.Subscribe("");
|
||||
_subscriberSocket.ReceiveReady += async (sender, e) => await ProcessClientCommand(sender, e);
|
||||
public void Send(RemoteCommand command) =>
|
||||
_dealer.SendFrame(MessagePackSerializer.Serialize(command));
|
||||
|
||||
_poller.Add(_subscriberSocket);
|
||||
_poller.RunAsync();
|
||||
public MemoryStream LoadFile(string filename, string folder)
|
||||
{
|
||||
try
|
||||
{
|
||||
Send(RemoteCommand.Create(CommandType.Load, new LoadFileData(filename, folder)));
|
||||
|
||||
var retries = 10;
|
||||
var tryNum = 0;
|
||||
while (!ct.IsCancellationRequested && tryNum++ < retries)
|
||||
{
|
||||
if (!_dealer.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(150), out var bytes))
|
||||
continue;
|
||||
var resultCommand = MessagePackSerializer.Deserialize<RemoteCommand>(bytes, cancellationToken: ct);
|
||||
if (resultCommand.Data?.Length == 0)
|
||||
throw new Exception($"Can't load {filename}. Returns 0 bytes");
|
||||
return new MemoryStream(resultCommand.Data!);
|
||||
}
|
||||
|
||||
throw new Exception($"Can't load file {filename} after {retries} retries");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.Error(e, e.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user