Started implementing server service and daemon controllers
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using MoonlightServers.Daemon.Http.Hubs;
|
||||
using MoonlightServers.Daemon.ServerSystem.Interfaces;
|
||||
using MoonlightServers.Daemon.ServerSystem.Models;
|
||||
|
||||
namespace MoonlightServers.Daemon.ServerSystem.Implementations;
|
||||
|
||||
public class ConsoleSignalRComponent : IServerComponent
|
||||
{
|
||||
private readonly IHubContext<ServerWebSocketHub> Hub;
|
||||
private readonly ServerContext Context;
|
||||
|
||||
private IAsyncDisposable? StdOutSubscription;
|
||||
private string HubGroup;
|
||||
|
||||
public ConsoleSignalRComponent(IHubContext<ServerWebSocketHub> hub, ServerContext context)
|
||||
{
|
||||
Hub = hub;
|
||||
Context = context;
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
HubGroup = Context.Configuration.Id.ToString();
|
||||
|
||||
StdOutSubscription = await Context.Server.Console.SubscribeStdOutAsync(OnStdOut);
|
||||
}
|
||||
|
||||
private async ValueTask OnStdOut(string output)
|
||||
{
|
||||
await Hub.Clients.Group(HubGroup).SendAsync("ConsoleOutput", output);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (StdOutSubscription != null)
|
||||
await StdOutSubscription.DisposeAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections;
|
||||
using MoonlightServers.Daemon.ServerSystem.Enums;
|
||||
using MoonlightServers.Daemon.ServerSystem.Interfaces;
|
||||
using MoonlightServers.Daemon.ServerSystem.Models;
|
||||
@@ -39,7 +40,8 @@ public partial class Server : IAsyncDisposable
|
||||
IRestorer restorer,
|
||||
IRuntime runtime,
|
||||
IStatistics statistics,
|
||||
IServerStateHandler[] handlers
|
||||
IEnumerable<IServerStateHandler> handlers,
|
||||
IEnumerable<IServerComponent> additionalComponents
|
||||
)
|
||||
{
|
||||
Logger = logger;
|
||||
@@ -54,13 +56,15 @@ public partial class Server : IAsyncDisposable
|
||||
Runtime = runtime;
|
||||
Statistics = statistics;
|
||||
|
||||
AllComponents =
|
||||
IEnumerable<IServerComponent> defaultComponents =
|
||||
[
|
||||
Console, RuntimeFileSystem, InstallationFileSystem, Installation, OnlineDetector, Reporter, Restorer,
|
||||
Runtime, Statistics
|
||||
];
|
||||
|
||||
Handlers = handlers;
|
||||
AllComponents = defaultComponents.Concat(additionalComponents).ToArray();
|
||||
|
||||
Handlers = handlers.ToArray();
|
||||
}
|
||||
|
||||
private void ConfigureStateMachine(ServerState initialState)
|
||||
|
||||
@@ -63,6 +63,11 @@ public class ServerFactory
|
||||
handlers.Add(ActivatorUtilities.CreateInstance<ShutdownHandler>(scope.ServiceProvider));
|
||||
handlers.Add(ActivatorUtilities.CreateInstance<InstallationHandler>(scope.ServiceProvider));
|
||||
handlers.Add(ActivatorUtilities.CreateInstance<DebugHandler>(scope.ServiceProvider));
|
||||
|
||||
// Resolve additional components
|
||||
var components = new List<IServerComponent>();
|
||||
|
||||
components.Add(ActivatorUtilities.CreateInstance<ConsoleSignalRComponent>(scope.ServiceProvider));
|
||||
|
||||
// TODO: Add a plugin hook for dynamically resolving components and checking if any is unset
|
||||
|
||||
@@ -81,7 +86,8 @@ public class ServerFactory
|
||||
runtime,
|
||||
statistics,
|
||||
// And now all the handlers
|
||||
handlers.ToArray()
|
||||
handlers,
|
||||
components
|
||||
);
|
||||
|
||||
context.Server = server;
|
||||
|
||||
Reference in New Issue
Block a user