using Fleet.Core;
using Fleet.Rpc;
using Fleet.Server;
using Microsoft.Extensions.DependencyInjection;
using MoShiro.Backend.Application;
using MoShiro.Protocol;
namespace MoShiro.Backend.Gateway;
public static class GatewayServiceCollectionExtensions
{
///
/// 在 Fleet.Server 之上接入 MoShiro:期望状态(TaskSet)、RawChunk 收件箱、moshiro.* 方法、注册挂钩和节点事件。
/// 需要事先注册 和 ,并在 AddMoShiroApplication 之后调用。
///
public static IServiceCollection AddMoShiroGateway(this IServiceCollection services)
{
services.AddSingleton();
services.AddHostedService();
services.AddFleetEnrollment();
services.AddFleetServer(options => options.NodeIdPrefix = AgentIds.Prefix)
.AddState()
.AddState()
.AddInbox()
.MapMethods(MapMoShiroMethods);
return services;
}
private static void MapMoShiroMethods(RpcRouter router, IServiceProvider services)
{
router
.OnNotify(
MoShiroMethods.Event,
MoShiroJsonContext.Default.AgentEventParams,
(context, parameters, ct) => services.GetRequiredService().OnEventAsync(context.Node.Id, parameters, ct))
.OnNotify(
MoShiroMethods.Host,
MoShiroJsonContext.Default.HostSnapshot,
(context, snapshot, ct) => services.GetRequiredService().OnHostAsync(context.Node.Id, snapshot, ct))
.OnNotify(
MoShiroMethods.TestStatus,
MoShiroJsonContext.Default.TestStatusParams,
(context, parameters, ct) => services.GetRequiredService().OnStatusAsync(context.Node.Id, parameters, ct));
}
}