rewrite to zmq push and pull patterns.

file load works, suite can start up
This commit is contained in:
Alex Bezdieniezhnykh
2025-01-23 14:37:13 +02:00
parent ce25ef38b0
commit 82b3b526a7
20 changed files with 243 additions and 208 deletions
@@ -7,6 +7,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MessagePack" Version="3.1.0" />
<PackageReference Include="MessagePack.Annotations" Version="3.1.0" />
<PackageReference Include="NetMQ" Version="4.0.1.13" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.3.0" />
</ItemGroup>
@@ -0,0 +1,24 @@
using MessagePack;
namespace Azaion.CommonSecurity.DTO.Commands;
[MessagePackObject]
public class FileCommand
{
[Key("CommandType")]
public CommandType CommandType { get; set; }
[Key("Filename")]
public string Filename { get; set; }
[Key("Data")]
public byte[] Data { get; set; }
}
public enum CommandType
{
None = 0,
Inference = 1,
Load = 2
}
@@ -5,6 +5,7 @@ public class SecurityConstants
public const string CONFIG_PATH = "config.json";
public const string DUMMY_DIR = "dummy";
#region ApiConfig
public const string DEFAULT_API_URL = "https://api.azaion.com/";
@@ -16,4 +17,11 @@ public class SecurityConstants
public const string CLAIM_ROLE = "role";
#endregion ApiConfig
#region SocketClient
public const string SOCKET_HOST = "127.0.0.1";
public const int SOCKET_SEND_PORT = 5127;
public const int SOCKET_RECEIVE_PORT = 5128;
#endregion SocketClient
}
@@ -1,4 +1,8 @@
using Azaion.CommonSecurity.DTO;
using Azaion.CommonSecurity.DTO.Commands;
using MessagePack;
using NetMQ;
using NetMQ.Sockets;
namespace Azaion.CommonSecurity.Services;
@@ -7,6 +11,40 @@ public interface IResourceLoader
Task<MemoryStream> Load(string fileName, CancellationToken cancellationToken = default);
}
public class PythonResourceLoader : IResourceLoader
{
private readonly PushSocket _pushSocket = new();
private readonly PullSocket _pullSocket = new();
public PythonResourceLoader(ApiCredentials credentials)
{
//Run python by credentials
_pushSocket.Connect($"tcp://{SecurityConstants.SOCKET_HOST}:{SecurityConstants.SOCKET_SEND_PORT}");
_pullSocket.Connect($"tcp://{SecurityConstants.SOCKET_HOST}:{SecurityConstants.SOCKET_RECEIVE_PORT}");
}
public async Task<MemoryStream> Load(string fileName, CancellationToken cancellationToken = default)
{
try
{
var b = MessagePackSerializer.Serialize(new FileCommand
{
CommandType = CommandType.Load,
Filename = fileName
});
_pushSocket.SendFrame(b);
var bytes = _pullSocket.ReceiveFrameBytes(out bool more);
return new MemoryStream(bytes);
}
catch (Exception ex)
{
throw new Exception($"Failed to load fil0e '{fileName}': {ex.Message}", ex);
}
}
}
public class ResourceLoader(AzaionApiClient api, ApiCredentials credentials) : IResourceLoader
{
public async Task<MemoryStream> Load(string fileName, CancellationToken cancellationToken = default)