diff --git a/samples/ChatRoom/ChatRoom.Client/ChatRoom.Client.csproj b/samples/ChatRoom/ChatRoom.Client/ChatRoom.Client.csproj index dd02e51117..9277a4b58f 100644 --- a/samples/ChatRoom/ChatRoom.Client/ChatRoom.Client.csproj +++ b/samples/ChatRoom/ChatRoom.Client/ChatRoom.Client.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0 enable enable Exe @@ -17,10 +17,12 @@ - - - - + + + + + + diff --git a/samples/ChatRoom/ChatRoom.Client/ClientContext.cs b/samples/ChatRoom/ChatRoom.Client/ClientContext.cs index 391da1f842..dcf02957cb 100644 --- a/samples/ChatRoom/ChatRoom.Client/ClientContext.cs +++ b/samples/ChatRoom/ChatRoom.Client/ClientContext.cs @@ -1,5 +1,3 @@ -using Orleans; - internal readonly record struct ClientContext( IClusterClient Client, string? UserName = null, diff --git a/samples/ChatRoom/ChatRoom.Client/Program.cs b/samples/ChatRoom/ChatRoom.Client/Program.cs index 670c342c52..55db29322e 100644 --- a/samples/ChatRoom/ChatRoom.Client/Program.cs +++ b/samples/ChatRoom/ChatRoom.Client/Program.cs @@ -1,42 +1,38 @@ using System.Reflection; using ChatRoom; -using Orleans; -using Orleans.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Orleans.Runtime; using Spectre.Console; -var client = new ClientBuilder() - .UseLocalhostClustering() - .ConfigureApplicationParts( - parts => parts.AddApplicationPart(typeof(IChannelGrain).Assembly).WithReferences()) - .AddSimpleMessageStreamProvider("chat") +using var host = new HostBuilder() + .UseOrleansClient(clientBuilder => + { + clientBuilder.UseLocalhostClustering() + .AddMemoryStreams("chat"); + }) .Build(); PrintUsage(); +var client = host.Services.GetRequiredService(); + ClientContext context = new(client); -await StartAsync(context); +await StartAsync(host); context = context with { UserName = AnsiConsole.Ask("What is your [aqua]name[/]?") }; await ProcessLoopAsync(context); -await StopAsync(context); +await StopAsync(host); -static Task StartAsync(ClientContext context) => +static Task StartAsync(IHost host) => AnsiConsole.Status().StartAsync("Connecting to server", async ctx => { ctx.Spinner(Spinner.Known.Dots); ctx.Status = "Connecting..."; - await context.Client.Connect(async error => - { - AnsiConsole.MarkupLine("[bold red]Error:[/] error connecting to server!"); - AnsiConsole.WriteException(error); - ctx.Status = "Waiting to retry..."; - await Task.Delay(TimeSpan.FromSeconds(2)); - ctx.Status = "Retrying connection..."; - return true; - }); + await host.StartAsync(); ctx.Status = "Connected!"; }); @@ -68,22 +64,22 @@ static async Task ProcessLoopAsync(ClientContext context) } if (firstTwoCharacters switch - { - "/j" => JoinChannel(context, input.Replace("/j", "").Trim()), - "/l" => LeaveChannel(context), - _ => null - } is Task cxtTask) + { + "/j" => JoinChannel(context, input.Replace("/j", "").Trim()), + "/l" => LeaveChannel(context), + _ => null + } is Task cxtTask) { context = await cxtTask; continue; } if (firstTwoCharacters switch - { - "/h" => ShowCurrentChannelHistory(context), - "/m" => ShowChannelMembers(context), - _ => null - } is Task task) + { + "/h" => ShowCurrentChannelHistory(context), + "/m" => ShowChannelMembers(context), + _ => null + } is Task task) { await task; continue; @@ -93,11 +89,11 @@ static async Task ProcessLoopAsync(ClientContext context) } while (input is not "/exit"); } -static Task StopAsync(ClientContext context) => +static Task StopAsync(IHost host) => AnsiConsole.Status().StartAsync("Disconnecting...", async ctx => { ctx.Spinner(Spinner.Known.Dots); - await context.Client.Close(); + await host.StopAsync(); }); static void PrintUsage() @@ -225,13 +221,14 @@ static async Task JoinChannel( await AnsiConsole.Status().StartAsync("Joining channel...", async ctx => { var room = context.Client.GetGrain(context.CurrentChannel); - var streamId = await room.Join(context.UserName!); + await room.Join(context.UserName!); + var streamId = StreamId.Create("ChatRoom", context.CurrentChannel!); var stream = context.Client .GetStreamProvider("chat") - .GetStream(streamId, "default"); + .GetStream(streamId); - //subscribe to the stream to receive furthur messages sent to the chatroom + // Subscribe to the stream to receive furthur messages sent to the chatroom await stream.SubscribeAsync(new StreamObserver(channelName)); }); AnsiConsole.MarkupLine("[bold aqua]Joined channel [/]{0}", context.CurrentChannel!); @@ -247,14 +244,15 @@ static async Task LeaveChannel(ClientContext context) await AnsiConsole.Status().StartAsync("Leaving channel...", async ctx => { var room = context.Client.GetGrain(context.CurrentChannel); - var streamId = await room.Leave(context.UserName!); + await room.Leave(context.UserName!); + var streamId = StreamId.Create("ChatRoom", context.CurrentChannel!); var stream = context.Client .GetStreamProvider("chat") - .GetStream(streamId, "default"); + .GetStream(streamId); - //unsubscribe from the channel/stream since client left, so that client won't - //receive future messages from this channel/stream + // Unsubscribe from the channel/stream since client left, so that client won't + // receive future messages from this channel/stream. var subscriptionHandles = await stream.GetAllSubscriptionHandles(); foreach (var handle in subscriptionHandles) { @@ -263,5 +261,6 @@ await AnsiConsole.Status().StartAsync("Leaving channel...", async ctx => }); AnsiConsole.MarkupLine("[bold olive]Left channel [/]{0}", context.CurrentChannel!); + return context with { CurrentChannel = null }; } diff --git a/samples/ChatRoom/ChatRoom.Client/StreamObserver.cs b/samples/ChatRoom/ChatRoom.Client/StreamObserver.cs index 292aad1fe5..523731c139 100644 --- a/samples/ChatRoom/ChatRoom.Client/StreamObserver.cs +++ b/samples/ChatRoom/ChatRoom.Client/StreamObserver.cs @@ -3,7 +3,7 @@ namespace ChatRoom; -public class StreamObserver : IAsyncObserver +public sealed class StreamObserver : IAsyncObserver { private readonly string _roomName; diff --git a/samples/ChatRoom/ChatRoom.Common/ChatMsg.cs b/samples/ChatRoom/ChatRoom.Common/ChatMsg.cs index 89fece7a0c..fabb9b0937 100644 --- a/samples/ChatRoom/ChatRoom.Common/ChatMsg.cs +++ b/samples/ChatRoom/ChatRoom.Common/ChatMsg.cs @@ -1,9 +1,13 @@ namespace ChatRoom; -[Serializable] -public record class ChatMsg(string? Author, string Text) +[GenerateSerializer] +public record class ChatMsg( + string? Author, + string Text) { + [Id(0)] public string Author { get; init; } = Author ?? "Alexey"; - public DateTimeOffset Created { get; init; } = DateTimeOffset.Now; + [Id(1)] + public DateTimeOffset Created { get; init; } = DateTimeOffset.Now; } diff --git a/samples/ChatRoom/ChatRoom.Common/ChatRoom.Common.csproj b/samples/ChatRoom/ChatRoom.Common/ChatRoom.Common.csproj index 415175a64b..cd3a2fddfa 100644 --- a/samples/ChatRoom/ChatRoom.Common/ChatRoom.Common.csproj +++ b/samples/ChatRoom/ChatRoom.Common/ChatRoom.Common.csproj @@ -1,17 +1,14 @@ - + - net6.0 + net7.0 enable enable - - - all - runtime; build; native; contentfiles; analyzers - + + \ No newline at end of file diff --git a/samples/ChatRoom/ChatRoom.Common/IChannelGrain.cs b/samples/ChatRoom/ChatRoom.Common/IChannelGrain.cs index 0c26b92206..52505de003 100644 --- a/samples/ChatRoom/ChatRoom.Common/IChannelGrain.cs +++ b/samples/ChatRoom/ChatRoom.Common/IChannelGrain.cs @@ -1,11 +1,11 @@ -using Orleans; +using Orleans.Runtime; namespace ChatRoom; public interface IChannelGrain : IGrainWithStringKey { - Task Join(string nickname); - Task Leave(string nickname); + Task Join(string nickname); + Task Leave(string nickname); Task Message(ChatMsg msg); Task ReadHistory(int numberOfMessages); Task GetMembers(); diff --git a/samples/ChatRoom/ChatRoom.Service/ChannelGrain.cs b/samples/ChatRoom/ChatRoom.Service/ChannelGrain.cs index 8ae43acdf8..10250b7679 100644 --- a/samples/ChatRoom/ChatRoom.Service/ChannelGrain.cs +++ b/samples/ChatRoom/ChatRoom.Service/ChannelGrain.cs @@ -1,4 +1,4 @@ -using Orleans; +using Orleans.Runtime; using Orleans.Streams; namespace ChatRoom; @@ -10,17 +10,20 @@ public class ChannelGrain : Grain, IChannelGrain private IAsyncStream _stream = null!; - public override Task OnActivateAsync() + public override Task OnActivateAsync(CancellationToken cancellationToken) { - var streamProvider = GetStreamProvider("chat"); + var streamProvider = this.GetStreamProvider("chat"); + + var streamId = StreamId.Create( + "ChatRoom", this.GetPrimaryKeyString()); _stream = streamProvider.GetStream( - Guid.NewGuid(), "default"); + streamId); - return base.OnActivateAsync(); + return base.OnActivateAsync(cancellationToken); } - public async Task Join(string nickname) + public async Task Join(string nickname) { _onlineMembers.Add(nickname); @@ -29,10 +32,10 @@ await _stream.OnNextAsync( "System", $"{nickname} joins the chat '{this.GetPrimaryKeyString()}' ...")); - return _stream.Guid; + return _stream.StreamId; } - public async Task Leave(string nickname) + public async Task Leave(string nickname) { _onlineMembers.Remove(nickname); @@ -41,7 +44,7 @@ await _stream.OnNextAsync( "System", $"{nickname} leaves the chat...")); - return _stream.Guid; + return _stream.StreamId; } public async Task Message(ChatMsg msg) diff --git a/samples/ChatRoom/ChatRoom.Service/ChatRoom.Service.csproj b/samples/ChatRoom/ChatRoom.Service/ChatRoom.Service.csproj index 7408b61e05..d80f1deda1 100644 --- a/samples/ChatRoom/ChatRoom.Service/ChatRoom.Service.csproj +++ b/samples/ChatRoom/ChatRoom.Service/ChatRoom.Service.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0 enable enable Exe @@ -9,13 +9,10 @@ - - - - - all - runtime; build; native; contentfiles; analyzers - + + + + diff --git a/samples/ChatRoom/ChatRoom.Service/Program.cs b/samples/ChatRoom/ChatRoom.Service/Program.cs index cd2ebdaae7..a49de0be83 100644 --- a/samples/ChatRoom/ChatRoom.Service/Program.cs +++ b/samples/ChatRoom/ChatRoom.Service/Program.cs @@ -1,6 +1,4 @@ using Microsoft.Extensions.Hosting; -using Orleans; -using Orleans.Hosting; await Host.CreateDefaultBuilder(args) .UseOrleans(siloBuilder => @@ -8,9 +6,6 @@ await Host.CreateDefaultBuilder(args) siloBuilder .UseLocalhostClustering() .AddMemoryGrainStorage("PubSubStore") - .AddSimpleMessageStreamProvider("chat", options => - { - options.FireAndForgetDelivery = true; - }); + .AddMemoryStreams("chat"); }) .RunConsoleAsync();