Skip to content

Commit

Permalink
added Akka.NET messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed May 6, 2024
1 parent 60ca19e commit ea9d45b
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 4 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!-- Akka.NET Package Versions -->
<ItemGroup>
<PackageVersion Include="Akka" Version="1.5.20" />
<PackageVersion Include="Akka.Cluster.Hosting" Version="1.5.20" />
<PackageVersion Include="Akka.Hosting" Version="1.5.20" />
<PackageVersion Include="Akka.Hosting.TestKit" Version="1.5.20" />
<PackageVersion Include="Akka.Streams" Version="1.5.20" />
Expand Down
14 changes: 14 additions & 0 deletions DrawTogether.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "infrastructure", "infrastru
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DrawTogether.Entities", "src\DrawTogether.Entities\DrawTogether.Entities.csproj", "{7F01390E-AF15-442C-831C-C574D6AAFC1F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DrawTogether.Actors", "src\DrawTogether.Actors\DrawTogether.Actors.csproj", "{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -95,6 +97,18 @@ Global
{7F01390E-AF15-442C-831C-C574D6AAFC1F}.Release|x64.Build.0 = Release|Any CPU
{7F01390E-AF15-442C-831C-C574D6AAFC1F}.Release|x86.ActiveCfg = Release|Any CPU
{7F01390E-AF15-442C-831C-C574D6AAFC1F}.Release|x86.Build.0 = Release|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Debug|x64.ActiveCfg = Debug|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Debug|x64.Build.0 = Debug|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Debug|x86.ActiveCfg = Debug|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Debug|x86.Build.0 = Debug|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Release|Any CPU.Build.0 = Release|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Release|x64.ActiveCfg = Release|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Release|x64.Build.0 = Release|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Release|x86.ActiveCfg = Release|Any CPU
{5863DD74-00DE-4DE9-9F0A-FCBC32195E78}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
43 changes: 43 additions & 0 deletions src/DrawTogether.Actors/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Akka.Actor;
using Akka.Cluster.Sharding;

namespace DrawTogether.Actors;

/// <summary>
/// A generic "child per entity" parent actor.
/// </summary>
/// <remarks>
/// Intended for simplifying unit tests where we don't want to use Akka.Cluster.Sharding.
/// </remarks>
public sealed class GenericChildPerEntityParent : UntypedActor
{
public static Props Props(IMessageExtractor extractor, Func<string, Props> propsFactory)
{
return Akka.Actor.Props.Create(() => new GenericChildPerEntityParent(extractor, propsFactory));
}

/*
* Re-use Akka.Cluster.Sharding's infrastructure here to keep things simple.
*/
private readonly IMessageExtractor _extractor;
private readonly Func<string, Props> _propsFactory;

public GenericChildPerEntityParent(IMessageExtractor extractor, Func<string, Props> propsFactory)
{
_extractor = extractor;
_propsFactory = propsFactory;
}

protected override void OnReceive(object message)
{
var result = _extractor.EntityId(message);
if (result is null)
{
Unhandled(message);
return;
}

Context.Child(result).GetOrElse(() => Context.ActorOf(_propsFactory(result), result))
.Forward(_extractor.EntityMessage(message));
}
}
17 changes: 17 additions & 0 deletions src/DrawTogether.Actors/DrawTogether.Actors.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Akka.Cluster.Hosting" />
</ItemGroup>

<ItemGroup>
<Folder Include="Drawings\" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions src/DrawTogether.Entities/CommandResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace DrawTogether.Entities;

public enum ResultCode
{
Ok,
NoOp,
BadRequest,
Unauthorized,
TimeOut
}

public sealed record CommandResult
{
public ResultCode Code { get; init; }

public string? Message { get; init; }

public static CommandResult Ok() => new() { Code = ResultCode.Ok };

public bool IsError => Code != ResultCode.Ok && Code != ResultCode.NoOp;
}
4 changes: 0 additions & 4 deletions src/DrawTogether.Entities/DrawTogether.Entities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Folder Include="Users\" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions src/DrawTogether.Entities/Drawings/DrawingSessionId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace DrawTogether.Entities.Drawings;

public sealed class DrawingSessionId(string SessionId);

Check warning on line 3 in src/DrawTogether.Entities/Drawings/DrawingSessionId.cs

View workflow job for this annotation

GitHub Actions / Test-ubuntu-latest

Parameter 'SessionId' is unread.

Check warning on line 3 in src/DrawTogether.Entities/Drawings/DrawingSessionId.cs

View workflow job for this annotation

GitHub Actions / Test-windows-latest

Parameter 'SessionId' is unread.
12 changes: 12 additions & 0 deletions src/DrawTogether.Entities/Drawings/DrawingSessionState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Immutable;
using DrawTogether.Entities.Drawings.Messages;
using DrawTogether.Entities.Users;

namespace DrawTogether.Entities.Drawings;

public sealed record DrawingSessionState(DrawingSessionId DrawingSessionId) : IWithDrawingSessionId
{
public ImmutableDictionary<StrokeId, ConnectedStroke> Strokes { get; init; } = ImmutableDictionary<StrokeId, ConnectedStroke>.Empty;

public ImmutableHashSet<UserId> ConnectedUsers { get; init; } = ImmutableHashSet<UserId>.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using DrawTogether.Entities.Users;

namespace DrawTogether.Entities.Drawings.Messages;

public interface IDrawingSessionCommand : IWithDrawingSessionId{ }

public static class DrawingSessionCommands
{
public sealed record AddStroke(DrawingSessionId DrawingSessionId, ConnectedStroke Stroke) : IDrawingSessionCommand;

public sealed record RemoveStroke(DrawingSessionId DrawingSessionId, StrokeId StrokeId) : IDrawingSessionCommand;

public sealed record ClearStrokes(DrawingSessionId DrawingSessionId) : IDrawingSessionCommand;

public sealed record AddUser(DrawingSessionId DrawingSessionId, UserId UserId) : IDrawingSessionCommand;

public sealed record RemoveUser(DrawingSessionId DrawingSessionId, UserId UserId) : IDrawingSessionCommand;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using DrawTogether.Entities.Users;

namespace DrawTogether.Entities.Drawings.Messages;

public interface IDrawingSessionEvent : IWithDrawingSessionId { }

public static class DrawingSessionEvents
{
public sealed record DrawingSessionCreated(DrawingSessionId DrawingSessionId) : IDrawingSessionEvent;

public sealed record StrokeAdded(DrawingSessionId DrawingSessionId, ConnectedStroke Stroke) : IDrawingSessionEvent;

public sealed record StrokeRemoved(DrawingSessionId DrawingSessionId, StrokeId StrokeId) : IDrawingSessionEvent;

public sealed record StrokesCleared(DrawingSessionId DrawingSessionId) : IDrawingSessionEvent;

public sealed record UserAdded(DrawingSessionId DrawingSessionId, UserId UserId) : IDrawingSessionEvent;

public sealed record UserRemoved(DrawingSessionId DrawingSessionId, UserId UserId) : IDrawingSessionEvent;

/// <summary>
/// Occurs when the last user leaves the session.
/// </summary>
public sealed record DrawingSessionClosed(DrawingSessionId DrawingSessionId) : IDrawingSessionEvent;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace DrawTogether.Entities.Drawings.Messages;

public interface IDrawingSessionQuery : IWithDrawingSessionId { }

public static class DrawingSessionQueries
{
public sealed record GetDrawingSessionState(DrawingSessionId DrawingSessionId) : IDrawingSessionQuery;

public sealed record GetDrawingSessionUsers(DrawingSessionId DrawingSessionId) : IDrawingSessionQuery;

public sealed record SubscribeToDrawingSession(DrawingSessionId DrawingSessionId) : IDrawingSessionQuery;

public sealed record UnsubscribeFromDrawingSession(DrawingSessionId DrawingSessionId) : IDrawingSessionQuery;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using DrawTogether.Entities.Users;

namespace DrawTogether.Entities.Drawings.Messages;

/// <summary>
/// Marker interface for messages that have a <see cref="DrawingSessionId"/>.
/// </summary>
public interface IWithDrawingSessionId
{
DrawingSessionId DrawingSessionId { get; }
}
3 changes: 3 additions & 0 deletions src/DrawTogether.Entities/Users/UserId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace DrawTogether.Entities.Users;

public sealed class UserId(string IdentityName);

Check warning on line 3 in src/DrawTogether.Entities/Users/UserId.cs

View workflow job for this annotation

GitHub Actions / Test-ubuntu-latest

Parameter 'IdentityName' is unread.

Check warning on line 3 in src/DrawTogether.Entities/Users/UserId.cs

View workflow job for this annotation

GitHub Actions / Test-windows-latest

Parameter 'IdentityName' is unread.

0 comments on commit ea9d45b

Please sign in to comment.