Snowflake/RPG.Services.Core/ServiceManager.cs

44 lines
1.4 KiB
C#

using Microsoft.Extensions.Options;
using RPG.Services.Core.Network;
using RPG.Services.Core.Network.Command;
using RPG.Services.Core.Options;
namespace RPG.Services.Core;
public class ServiceManager
{
private readonly IOptions<ServiceNodeOptions> _nodeOptions;
private readonly IOptions<RPGServiceOptions> _serviceOptions;
private readonly ServiceCommandHandler _handler;
private readonly ServiceBox _serviceBox;
private ServiceEndPoint? _serviceEndPoint;
public ServiceManager(IOptions<ServiceNodeOptions> options, IOptions<RPGServiceOptions> serviceOptions, ServiceCommandHandler commandHandler, ServiceBox serviceBox)
{
_nodeOptions = options;
_serviceOptions = serviceOptions;
_handler = commandHandler;
_serviceBox = serviceBox;
}
public void Start()
{
ServiceNodeOptions nodeOptions = _nodeOptions.Value;
RPGServiceOptions serviceOptions = _serviceOptions.Value;
_serviceBox.Construct();
_serviceEndPoint = new(nodeOptions.GetEntry(serviceOptions.ServiceType));
_serviceEndPoint.OnCommand += _handler.HandleAsync;
_serviceEndPoint.Start();
}
public async Task ShutdownAsync()
{
if (_serviceEndPoint != null)
{
await _serviceEndPoint.StopAsync();
_serviceEndPoint.OnCommand -= _handler.HandleAsync;
}
}
}