Skip to content

Commit

Permalink
fixed actor integration (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb authored May 2, 2024
1 parent a992af6 commit 01dff18
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 60 deletions.
63 changes: 8 additions & 55 deletions src/DrawTogether.UI/Server/Services/AkkaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,65 +22,17 @@ namespace DrawTogether.UI.Server.Services
/// </summary>
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<IDrawSessionHandler, AkkaService>();

// starts the IHostedService, which creates the ActorSystem and actors
services.AddHostedService<AkkaService>(sp => (AkkaService)sp.GetRequiredService<IDrawSessionHandler>());
}
}

/// <summary>
/// Runs in the background of Server process. Hosts <see cref="ActorSystem"/> responsible for powering
/// actors that track session state data.
/// </summary>
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<PaintInstanceManager>(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);
}
}
}
18 changes: 18 additions & 0 deletions src/DrawTogether.UI/Server/Services/IDrawSessionHandler.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = paintInstanceManager.GetAsync().Result;
}

public void Handle(IPaintSessionMessage msg)
{
_paintInstanceManager.Tell(msg);
}
}
}
5 changes: 3 additions & 2 deletions src/DrawTogether.UI/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IPaintSessionGenerator, GuidPaintSessionGenerator>();
services.AddSingleton<IUserIdProvider, RandomNameService>();
services.AddAkka();
services.AddDrawTogetherAkka();
services.AddTransient<IDrawHubHandler, DrawHubHandler>();
services.AddSignalR().AddJsonProtocol();
services.AddSingleton<IDrawSessionHandler, DrawSessionHandler>();
services.AddSignalR();
services.AddServerSideBlazor();
services.AddControllersWithViews();
services.AddRazorPages();
Expand Down
3 changes: 0 additions & 3 deletions src/DrawTogether.UI/Server/_ViewStart.cshtml

This file was deleted.

0 comments on commit 01dff18

Please sign in to comment.