Skip to content

Commit

Permalink
full reformatting (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb authored May 5, 2024
1 parent 01dff18 commit 40f9117
Show file tree
Hide file tree
Showing 42 changed files with 1,167 additions and 1,283 deletions.
32 changes: 16 additions & 16 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project>
<PropertyGroup>
<Copyright>Copyright © 2015-2024 Petabridge</Copyright>
<Authors>Petabridge</Authors>
<VersionPrefix>0.2.2</VersionPrefix>
<PackageReleaseNotes>Added footer that displays assembly version on Pricing UI</PackageReleaseNotes>
<PackageIconUrl>
</PackageIconUrl>
<PackageProjectUrl>
</PackageProjectUrl>
<PackageLicenseUrl>
</PackageLicenseUrl>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>
<PropertyGroup>
<NetVersion>net8.0</NetVersion>
</PropertyGroup>
<PropertyGroup>
<Copyright>Copyright © 2015-2024 Petabridge</Copyright>
<Authors>Petabridge</Authors>
<VersionPrefix>0.2.2</VersionPrefix>
<PackageReleaseNotes>Added footer that displays assembly version on Pricing UI</PackageReleaseNotes>
<PackageIconUrl>
</PackageIconUrl>
<PackageProjectUrl>
</PackageProjectUrl>
<PackageLicenseUrl>
</PackageLicenseUrl>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>
<PropertyGroup>
<NetVersion>net8.0</NetVersion>
</PropertyGroup>
</Project>
167 changes: 81 additions & 86 deletions src/DrawTogether.UI/Server/Actors/PaintInstanceActor.cs
Original file line number Diff line number Diff line change
@@ -1,115 +1,110 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Actor;
using Akka.Event;
using Akka.Streams;
using Akka.Streams.Dsl;
using Akka.Util.Internal;
using DrawTogether.UI.Server.Hubs;
using DrawTogether.UI.Server.Services;
using DrawTogether.UI.Shared;
using DrawTogether.UI.Shared.Connectivity;
using Microsoft.AspNetCore.SignalR;
using static DrawTogether.UI.Shared.Connectivity.PaintSessionProtocol;

namespace DrawTogether.UI.Server.Actors
namespace DrawTogether.UI.Server.Actors;

/// <summary>
/// Holds all stroke data in memory for a single drawing instance.
/// </summary>
public class PaintInstanceActor : UntypedActor
{
/// <summary>
/// Holds all stroke data in memory for a single drawing instance.
/// </summary>
public class PaintInstanceActor : UntypedActor
{
// Handle to the Akka.NET asynchronous logging system
private readonly ILoggingAdapter _log = Context.GetLogger();
private readonly List<ConnectedStroke> _connectedStrokes = new();
private readonly IDrawHubHandler _hubHandler;

private readonly List<ConnectedStroke> _connectedStrokes = new();
private readonly List<string> _users = new();
private readonly IDrawHubHandler _hubHandler;
private readonly string _sessionId;
private readonly TimeSpan _idleTimeout = TimeSpan.FromMinutes(20);
private IActorRef _streamsDebouncer;
private readonly TimeSpan _idleTimeout = TimeSpan.FromMinutes(20);

public PaintInstanceActor(string sessionId, IDrawHubHandler hubHandler)
{
_sessionId = sessionId;
_hubHandler = hubHandler;
}
// Handle to the Akka.NET asynchronous logging system
private readonly ILoggingAdapter _log = Context.GetLogger();
private readonly string _sessionId;
private readonly List<string> _users = new();
private IActorRef _streamsDebouncer;

protected override void OnReceive(object message)
public PaintInstanceActor(string sessionId, IDrawHubHandler hubHandler)

Check warning on line 28 in src/DrawTogether.UI/Server/Actors/PaintInstanceActor.cs

View workflow job for this annotation

GitHub Actions / Test-ubuntu-latest

Non-nullable field '_streamsDebouncer' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 28 in src/DrawTogether.UI/Server/Actors/PaintInstanceActor.cs

View workflow job for this annotation

GitHub Actions / Test-windows-latest

Non-nullable field '_streamsDebouncer' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_sessionId = sessionId;
_hubHandler = hubHandler;
}

protected override void OnReceive(object message)
{
switch (message)
{
switch (message)
case JoinPaintSession join:
{
case JoinPaintSession join:
{
_log.Debug("User [{0}] joined [{1}]", join.ConnectionId, join.InstanceId);
// need to make immutable copy of stroke data and pass it along
var strokeCopy = _connectedStrokes.ToArray();

// sync a single user.
_hubHandler.PushConnectedStrokes(join.ConnectionId, _sessionId, strokeCopy);
_hubHandler.AddUsers(join.ConnectionId, _users.ToArray());

// let all users know about the new user
_users.Add(join.UserId);
_hubHandler.AddUser(_sessionId, join.UserId);
break;
}
case AddPointToConnectedStroke or CreateConnectedStroke:
{
_streamsDebouncer.Tell(message);
break;
}
case ReceiveTimeout _:
{
_log.Info("Terminated Painting Session [{0}] after [{1}]", _sessionId, _idleTimeout);
Context.Stop(Self);
break;
}
default:
Unhandled(message);
break;
_log.Debug("User [{0}] joined [{1}]", join.ConnectionId, join.InstanceId);
// need to make immutable copy of stroke data and pass it along
var strokeCopy = _connectedStrokes.ToArray();

// sync a single user.
_hubHandler.PushConnectedStrokes(join.ConnectionId, _sessionId, strokeCopy);
_hubHandler.AddUsers(join.ConnectionId, _users.ToArray());

// let all users know about the new user
_users.Add(join.UserId);
_hubHandler.AddUser(_sessionId, join.UserId);
break;
}
case AddPointToConnectedStroke or CreateConnectedStroke:
{
_streamsDebouncer.Tell(message);
break;
}
case ReceiveTimeout _:
{
_log.Info("Terminated Painting Session [{0}] after [{1}]", _sessionId, _idleTimeout);
Context.Stop(Self);
break;
}
default:
Unhandled(message);
break;
}
}

protected override void PreStart()
{
_log.Info("Started drawing session [{0}]", _sessionId);
protected override void PreStart()
{
_log.Info("Started drawing session [{0}]", _sessionId);

var materializer = Context.Materializer();
var (sourceRef, source) = Source.ActorRef<IPaintSessionMessage>(1000, OverflowStrategy.DropHead)
.PreMaterialize(materializer);
var materializer = Context.Materializer();
var (sourceRef, source) = Source.ActorRef<IPaintSessionMessage>(1000, OverflowStrategy.DropHead)
.PreMaterialize(materializer);

_streamsDebouncer = sourceRef;
source.GroupedWithin(10, TimeSpan.FromMilliseconds(75)).RunForeach(TransmitActions, materializer);
_streamsDebouncer = sourceRef;
source.GroupedWithin(10, TimeSpan.FromMilliseconds(75)).RunForeach(TransmitActions, materializer);

// idle timeout all drawings after 20 minutes
Context.SetReceiveTimeout(_idleTimeout);
}
// idle timeout all drawings after 20 minutes
Context.SetReceiveTimeout(_idleTimeout);
}

private void TransmitActions(IEnumerable<IPaintSessionMessage> actions)
{
var createActions = actions.Where(a => a is CreateConnectedStroke).Select(a => ((CreateConnectedStroke)a).ConnectedStroke);
private void TransmitActions(IEnumerable<IPaintSessionMessage> actions)
{
var createActions = actions.Where(a => a is CreateConnectedStroke)
.Select(a => ((CreateConnectedStroke)a).ConnectedStroke);

var addActions = actions.Where(a => a is AddPointToConnectedStroke).Select(a => (AddPointToConnectedStroke)a);
var addActions = actions.Where(a => a is AddPointToConnectedStroke).Select(a => (AddPointToConnectedStroke)a);

_log.Info("BATCHED {0} create actions and {1} add actions", createActions.Count(), addActions.Count());
_log.Info("BATCHED {0} create actions and {1} add actions", createActions.Count(), addActions.Count());

createActions.ForEach(c => { c.Points = addActions.Where(a => a.Id == c.Id).Select(a => a.Point).ToList(); });
createActions.ForEach(c => { c.Points = addActions.Where(a => a.Id == c.Id).Select(a => a.Point).ToList(); });

addActions = addActions.Where(a => !createActions.Any(c => a.Id == c.Id));
addActions = addActions.Where(a => !createActions.Any(c => a.Id == c.Id));

_hubHandler.PushConnectedStrokes(_sessionId, createActions.ToArray());
_connectedStrokes.AddRange(createActions);
_hubHandler.PushConnectedStrokes(_sessionId, createActions.ToArray());
_connectedStrokes.AddRange(createActions);

addActions.GroupBy(a => a.Id).ForEach(add => {
_connectedStrokes.Where(c => c.Id == add.Key).First().Points.AddRange(add.Select(a => a.Point));
addActions.GroupBy(a => a.Id).ForEach(add =>
{
_connectedStrokes.Where(c => c.Id == add.Key).First().Points.AddRange(add.Select(a => a.Point));

// sync ALL users
// TODO: look into zero-copy for this
_hubHandler.AddPointsToConnectedStroke(_sessionId, add.Key, add.Select(a => a.Point).ToArray());
});
}
// sync ALL users
// TODO: look into zero-copy for this
_hubHandler.AddPointsToConnectedStroke(_sessionId, add.Key, add.Select(a => a.Point).ToArray());
});
}
}
}
35 changes: 17 additions & 18 deletions src/DrawTogether.UI/Server/Actors/PaintInstanceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,29 @@
using Akka.DependencyInjection;
using DrawTogether.UI.Shared.Connectivity;

namespace DrawTogether.UI.Server.Actors
namespace DrawTogether.UI.Server.Actors;

public sealed class PaintInstanceManager : UntypedActor
{
public sealed class PaintInstanceManager : UntypedActor
protected override void OnReceive(object message)
{
protected override void OnReceive(object message)
switch (message)
{
switch (message)
case PaintSessionProtocol.IPaintSessionMessage m:
{
case PaintSessionProtocol.IPaintSessionMessage m:
{
// need to create or get child that corresponds to sessionId
var child = Context.Child(m.InstanceId)
.GetOrElse(() =>
Context.ActorOf(
DependencyResolver.For(Context.System).Props<PaintInstanceActor>(m.InstanceId),
m.InstanceId));
// need to create or get child that corresponds to sessionId
var child = Context.Child(m.InstanceId)
.GetOrElse(() =>
Context.ActorOf(
DependencyResolver.For(Context.System).Props<PaintInstanceActor>(m.InstanceId),
m.InstanceId));

child.Forward(m);
break;
}
default:
Unhandled(message);
break;
child.Forward(m);
break;
}
default:
Unhandled(message);
break;
}
}
}
26 changes: 10 additions & 16 deletions src/DrawTogether.UI/Server/Actors/UserInstanceActor.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Actor;

namespace DrawTogether.UI.Server.Actors
namespace DrawTogether.UI.Server.Actors;

/// <summary>
/// Gives a user a randomly-assigned name and associates that
/// with their session on DrawTogether
/// </summary>
public class UserInstanceActor : UntypedActor
{
/// <summary>
/// Gives a user a randomly-assigned name and associates that
/// with their session on DrawTogether
/// </summary>
public class UserInstanceActor : UntypedActor
protected override void OnReceive(object message)
{
protected override void OnReceive(object message)
{

}
}
}
}
4 changes: 2 additions & 2 deletions src/DrawTogether.UI/Server/App.razor
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</Router>
26 changes: 10 additions & 16 deletions src/DrawTogether.UI/Server/Components/Circle.razor
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
<circle cx="@cX" cy="@cY" r="@(radius*0.8)"
stroke="@(borderColor)" stroke-width="@(radius *0.05)"
fill="@(fillColor)"
id="@(cursorId)"/>
<circle cx="@cX" cy="@cY" r="@(radius * 0.8)"
stroke="@(borderColor)" stroke-width="@(radius * 0.05)"
fill="@(fillColor)"
id="@(cursorId)"/>

@code {

[Parameter]
public int radius { get; set; } = 10;
[Parameter] public int radius { get; set; } = 10;

[Parameter]
public string borderColor { get; set; } = "#eee";
[Parameter] public string borderColor { get; set; } = "#eee";

[Parameter]
public string fillColor { get; set; } = "#eee";
[Parameter] public string fillColor { get; set; } = "#eee";

[Parameter]
public string cursorId { get; set; }
[Parameter] public string cursorId { get; set; }

Check warning on line 14 in src/DrawTogether.UI/Server/Components/Circle.razor

View workflow job for this annotation

GitHub Actions / Test-ubuntu-latest

Non-nullable property 'cursorId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 14 in src/DrawTogether.UI/Server/Components/Circle.razor

View workflow job for this annotation

GitHub Actions / Test-windows-latest

Non-nullable property 'cursorId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[Parameter]
public double cX { get; set; }
[Parameter] public double cX { get; set; }

[Parameter]
public double cY { get; set; }
[Parameter] public double cY { get; set; }

}
8 changes: 3 additions & 5 deletions src/DrawTogether.UI/Server/Components/ColorPicker.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
Color
<input @bind="CurrentColor"/>
</label>

<button class="btn btn-primary" @onclick="@(async e => await ColorValueChanged.InvokeAsync(CurrentColor))">Set Color</button>
</section>

@code {
[Parameter]
public string CurrentColor { get; set; } = "#ff0000";
[Parameter] public string CurrentColor { get; set; } = "#ff0000";

[Parameter]
public EventCallback<string> ColorValueChanged { get; set; }
[Parameter] public EventCallback<string> ColorValueChanged { get; set; }
}
Loading

0 comments on commit 40f9117

Please sign in to comment.