Skip to content

Commit

Permalink
Upgrade 'GPS Tracker' sample (#8130)
Browse files Browse the repository at this point in the history
  • Loading branch information
IEvangelist committed Nov 18, 2022
1 parent 3000c57 commit 32bae99
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 70 deletions.
4 changes: 1 addition & 3 deletions samples/GPSTracker/GPSTracker.Common/DeviceMessage.cs
@@ -1,8 +1,6 @@
using Orleans.Concurrency;

namespace GPSTracker.Common;

[Immutable, Serializable]
[Immutable, GenerateSerializer]
public record class DeviceMessage(
double Latitude,
double Longitude,
Expand Down
8 changes: 2 additions & 6 deletions samples/GPSTracker/GPSTracker.Common/GPSTracker.Common.csproj
@@ -1,15 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.6.0" />
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0" />
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion samples/GPSTracker/GPSTracker.Common/IDeviceGrain.cs
@@ -1,5 +1,4 @@
using GPSTracker.Common;
using Orleans;

namespace GPSTracker.GrainInterface;

Expand Down
1 change: 0 additions & 1 deletion samples/GPSTracker/GPSTracker.Common/IPushNotifierGrain.cs
@@ -1,5 +1,4 @@
using GPSTracker.Common;
using Orleans;

namespace GPSTracker.GrainInterface;

Expand Down
6 changes: 2 additions & 4 deletions samples/GPSTracker/GPSTracker.Common/VelocityMessage.cs
@@ -1,8 +1,6 @@
using Orleans.Concurrency;

namespace GPSTracker.Common;

[Immutable, Serializable]
[Immutable, GenerateSerializer]
public record class VelocityMessage(
DeviceMessage DeviceMessage,
double Velocity) :
Expand All @@ -13,5 +11,5 @@ public record class VelocityMessage(
DeviceMessage.DeviceId,
DeviceMessage.Timestamp);

[Immutable, Serializable]
[Immutable, GenerateSerializer]
public record class VelocityBatch(VelocityMessage[] Messages);
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
Expand All @@ -9,14 +9,9 @@
<ProjectReference Include="..\GPSTracker.Common\GPSTracker.Common.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Orleans.Client" Version="3.6.0" />
<PackageReference Include="Microsoft.Orleans.Core" Version="3.6.0" />
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.OrleansProviders" Version="3.6.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Client" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.OrleansProviders" Version="3.6.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>
15 changes: 9 additions & 6 deletions samples/GPSTracker/GPSTracker.FakeDeviceGateway/Program.cs
@@ -1,6 +1,7 @@
using GPSTracker.Common;
using GPSTracker.GrainInterface;
using Orleans;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Diagnostics;

namespace GPSTracker.FakeDeviceGateway;
Expand All @@ -19,11 +20,11 @@ internal class Program

private static async Task Main(string[] args)
{
var client = new ClientBuilder()
.UseLocalhostClustering()
var host = new HostBuilder()
.UseOrleansClient(builder => builder.UseLocalhostClustering())
.Build();

await client.Connect();
await host.StartAsync();

// Simulate 20 devices
var devices = new List<Model>();
Expand Down Expand Up @@ -52,13 +53,15 @@ private static async Task Main(string[] args)
var cancellation = new CancellationTokenSource();
Console.CancelKeyPress += (_, _) => cancellation.Cancel();

IGrainFactory factory = host.Services.GetRequiredService<IGrainFactory>();

// Update each device in a loop.
var tasks = new List<Task>();
while (!cancellation.IsCancellationRequested)
{
foreach (var model in devices)
{
tasks.Add(SendMessage(client, model));
tasks.Add(SendMessage(factory, model));
}

await Task.WhenAll(tasks);
Expand Down Expand Up @@ -126,7 +129,7 @@ private static void UpdateDevicePosition(Model model, double delta)

public static double NextDouble(double min, double max) => Random.NextDouble() * (max - min) + min;

private class Model
private sealed class Model
{
public Stopwatch TimeSinceLastUpdate { get; } = Stopwatch.StartNew();
public int DeviceId { get; set; }
Expand Down
20 changes: 7 additions & 13 deletions samples/GPSTracker/GPSTracker.Service/GPSTracker.Service.csproj
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
Expand All @@ -11,18 +11,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Orleans.Core" Version="3.6.0" />
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.OrleansProviders" Version="3.6.0" />
<PackageReference Include="Microsoft.Orleans.Server" Version="3.6.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.OrleansProviders" Version="3.6.5" />
<PackageReference Include="Microsoft.Orleans.Server" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

Expand Down
@@ -1,6 +1,5 @@
using GPSTracker.Common;
using GPSTracker.GrainInterface;
using Orleans;
using Orleans.Concurrency;

namespace GPSTracker.GrainImplementation;
Expand Down
@@ -1,4 +1,3 @@
using Orleans;
using Orleans.Runtime;

namespace GPSTracker.GrainImplementation;
Expand Down
@@ -1,4 +1,3 @@
using Orleans;
using Orleans.Runtime;

namespace GPSTracker.GrainImplementation;
Expand Down
@@ -1,5 +1,4 @@
using GPSTracker.Common;
using Orleans;

namespace GPSTracker;

Expand Down
@@ -1,6 +1,5 @@
using GPSTracker.Common;
using GPSTracker.GrainInterface;
using Orleans;
using Orleans.Concurrency;
using Orleans.Runtime;

Expand All @@ -16,7 +15,7 @@ public class PushNotifierGrain : Grain, IPushNotifierGrain
public PushNotifierGrain(ILogger<PushNotifierGrain> logger) => _logger = logger;
private Task _flushTask = Task.CompletedTask;

public override async Task OnActivateAsync()
public override async Task OnActivateAsync(CancellationToken cancellationToken)
{
// Set up a timer to regularly flush the message queue
RegisterTimer(
Expand All @@ -37,15 +36,15 @@ public override async Task OnActivateAsync()
dueTime: TimeSpan.FromSeconds(60),
period: TimeSpan.FromSeconds(60));

await base.OnActivateAsync();
await base.OnActivateAsync(cancellationToken);
}

public override async Task OnDeactivateAsync()
public override async Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
{
Flush();
await _flushTask;

await base.OnDeactivateAsync();
await base.OnDeactivateAsync(reason, cancellationToken);
}

private async Task RefreshHubs()
Expand Down
Expand Up @@ -6,7 +6,7 @@ namespace GPSTracker;
/// <summary>
/// Broadcasts location messages to clients which are connected to the local SignalR hub.
/// </summary>
internal class RemoteLocationHub : IRemoteLocationHub
internal sealed class RemoteLocationHub : IRemoteLocationHub
{
private readonly IHubContext<LocationHub> _hub;

Expand Down
5 changes: 2 additions & 3 deletions samples/GPSTracker/GPSTracker.Service/HubListUpdater.cs
@@ -1,14 +1,13 @@
using GPSTracker.GrainImplementation;
using Microsoft.AspNetCore.SignalR;
using Orleans;
using Orleans.Runtime;

namespace GPSTracker;

/// <summary>
/// Periodically updates the <see cref="IHubListGrain"/> implementation with a reference to the local <see cref="RemoteLocationHub"/>.
/// </summary>
internal class HubListUpdater : BackgroundService
internal sealed class HubListUpdater : BackgroundService
{
private readonly IGrainFactory _grainFactory;
private readonly ILogger<HubListUpdater> _logger;
Expand All @@ -31,7 +30,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var hubListGrain = _grainFactory.GetGrain<IHubListGrain>(Guid.Empty);
var localSiloAddress = _localSiloDetails.SiloAddress;
var selfReference = await _grainFactory.CreateObjectReference<IRemoteLocationHub>(_locationBroadcaster);
var selfReference = _grainFactory.CreateObjectReference<IRemoteLocationHub>(_locationBroadcaster);

// This runs in a loop because the HubListGrain does not use any form of persistence, so if the
// host which it is activated on stops, then it will lose any internal state.
Expand Down
2 changes: 1 addition & 1 deletion samples/GPSTracker/GPSTracker.Service/Hubs/LocationHub.cs
Expand Up @@ -5,6 +5,6 @@ namespace GPSTracker;
/// <summary>
/// The hub which Web clients connect to to receive location updates. Messages are broadcast by <see cref="RemoteLocationHub"/> using <see cref="IHubContext{LocationHub}"/>.
/// </summary>
public class LocationHub : Hub
public sealed class LocationHub : Hub
{
}
7 changes: 1 addition & 6 deletions samples/GPSTracker/GPSTracker.Service/Program.cs
@@ -1,4 +1,3 @@
using Orleans.Hosting;
using GPSTracker;
using System.Net;
var builder = WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -33,9 +32,5 @@
app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapHub<LocationHub>("/locationHub");
});
app.MapHub<LocationHub>("/locationHub");
app.Run();
Expand Up @@ -8,20 +8,20 @@
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"GPSTracker.Service": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

0 comments on commit 32bae99

Please sign in to comment.