diff --git a/src/DrawTogether.UI/Server/Services/AkkaService.cs b/src/DrawTogether.UI/Server/Services/AkkaService.cs index 0ed3d7d..46bc5eb 100644 --- a/src/DrawTogether.UI/Server/Services/AkkaService.cs +++ b/src/DrawTogether.UI/Server/Services/AkkaService.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using Akka.Actor; using Akka.DependencyInjection; +using Akka.Hosting; using DrawTogether.UI.Server.Actors; using DrawTogether.UI.Shared.Connectivity; using Microsoft.Extensions.DependencyInjection; @@ -21,65 +22,17 @@ namespace DrawTogether.UI.Server.Services /// public static class AkkaExtensions { - public static void AddAkka(this IServiceCollection services) + public static void AddDrawTogetherAkka(this IServiceCollection services) { // creates an instance of the ISignalRProcessor that can be handled by SignalR - services.AddSingleton(); - - // starts the IHostedService, which creates the ActorSystem and actors - services.AddHostedService(sp => (AkkaService)sp.GetRequiredService()); - } - } - - /// - /// Runs in the background of Server process. Hosts responsible for powering - /// actors that track session state data. - /// - public sealed class AkkaService : IHostedService, IDrawSessionHandler - { - private readonly IServiceProvider _serviceProvider; - private readonly IHostApplicationLifetime _applicationLifetime; - - private ActorSystem _system; - private IActorRef _paintManager; - - public AkkaService(IServiceProvider serviceProvider, IHostApplicationLifetime applicationLifetime) - { - _serviceProvider = serviceProvider; - _applicationLifetime = applicationLifetime; - } - - public Task StartAsync(CancellationToken cancellationToken) - { - // need this for Akka.NET DI - var spSetup = ServiceProviderSetup.Create(_serviceProvider); - - // need this for HOCON (when we actually need it) - var bootstrapSetup = BootstrapSetup.Create(); - - var actorSystemSetup = spSetup.And(bootstrapSetup); - - _system = ActorSystem.Create("PaintSys", actorSystemSetup); - _paintManager = _system.ActorOf(Props.Create(() => new PaintInstanceManager()), "paint"); - - // mutually assured destruction - _system.WhenTerminated.ContinueWith(tr => + services.AddAkka("DrawTogether", (builder, sp) => { - _applicationLifetime.StopApplication(); + builder.WithActors((system, registry, resolver) => + { + var paintActor = system.ActorOf(Props.Create(() => new PaintInstanceManager()), "paint"); + registry.Register(paintActor); + }); }); - - return Task.CompletedTask; - } - - public async Task StopAsync(CancellationToken cancellationToken) - { - // force ActorSystem to shut down - await _system.Terminate(); - } - - public void Handle(PaintSessionProtocol.IPaintSessionMessage msg) - { - _paintManager.Tell(msg); } } } \ No newline at end of file diff --git a/src/DrawTogether.UI/Server/Services/IDrawSessionHandler.cs b/src/DrawTogether.UI/Server/Services/IDrawSessionHandler.cs index 6c03dbe..b0c949d 100644 --- a/src/DrawTogether.UI/Server/Services/IDrawSessionHandler.cs +++ b/src/DrawTogether.UI/Server/Services/IDrawSessionHandler.cs @@ -1,5 +1,8 @@ using System.Collections.Generic; using System.Linq; +using Akka.Actor; +using Akka.Hosting; +using DrawTogether.UI.Server.Actors; using static DrawTogether.UI.Shared.Connectivity.PaintSessionProtocol; namespace DrawTogether.UI.Server.Services @@ -11,4 +14,19 @@ public interface IDrawSessionHandler { void Handle(IPaintSessionMessage msg); } + + public sealed class DrawSessionHandler : IDrawSessionHandler + { + private readonly IActorRef _paintInstanceManager; + + public DrawSessionHandler(IRequiredActor paintInstanceManager) + { + _paintInstanceManager = paintInstanceManager.GetAsync().Result; + } + + public void Handle(IPaintSessionMessage msg) + { + _paintInstanceManager.Tell(msg); + } + } } diff --git a/src/DrawTogether.UI/Server/Startup.cs b/src/DrawTogether.UI/Server/Startup.cs index 96746ac..e0ff4f2 100644 --- a/src/DrawTogether.UI/Server/Startup.cs +++ b/src/DrawTogether.UI/Server/Startup.cs @@ -29,9 +29,10 @@ public void ConfigureServices(IServiceCollection services) { services.AddSingleton(); services.AddSingleton(); - services.AddAkka(); + services.AddDrawTogetherAkka(); services.AddTransient(); - services.AddSignalR().AddJsonProtocol(); + services.AddSingleton(); + services.AddSignalR(); services.AddServerSideBlazor(); services.AddControllersWithViews(); services.AddRazorPages(); diff --git a/src/DrawTogether.UI/Server/_ViewStart.cshtml b/src/DrawTogether.UI/Server/_ViewStart.cshtml deleted file mode 100644 index 1304a0c..0000000 --- a/src/DrawTogether.UI/Server/_ViewStart.cshtml +++ /dev/null @@ -1,3 +0,0 @@ -@{ - Layout = "--generateLayout"; -}