Skip to content

Commit 0769e5a

Browse files
authored
chore: modernize example programs (#2219)
1 parent a06b80c commit 0769e5a

File tree

3 files changed

+240
-318
lines changed

3 files changed

+240
-318
lines changed

examples/Chat/Client/Program.cs

Lines changed: 106 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,106 @@
1-
// -----------------------------------------------------------------------
2-
// <copyright file="Program.cs" company="Asynkron AB">
3-
// Copyright (C) 2015-2024 Asynkron AB All rights reserved
4-
// </copyright>
5-
// -----------------------------------------------------------------------
6-
7-
using System;
8-
using System.Threading.Tasks;
9-
using chat.messages;
10-
using Microsoft.Extensions.Logging;
11-
using Proto;
12-
using Proto.Remote;
13-
using Proto.Remote.GrpcNet;
14-
using static Proto.Remote.GrpcNet.GrpcNetRemoteConfig;
15-
16-
namespace Client;
17-
18-
internal static class Program
19-
{
20-
private static IRootContext context;
21-
22-
private static PID client;
23-
24-
private static PID server;
25-
26-
private static void Main()
27-
{
28-
Log.SetLoggerFactory(LoggerFactory.Create(c => c
29-
.SetMinimumLevel(LogLevel.Information)
30-
.AddConsole()
31-
));
32-
33-
InitializeActorSystem();
34-
SpawnClient();
35-
ObtainServerPid();
36-
ConnectToServer();
37-
EvaluateCommands();
38-
context.System.Remote().ShutdownAsync().GetAwaiter().GetResult();
39-
}
40-
41-
private static void InitializeActorSystem()
42-
{
43-
var config =
44-
BindToLocalhost()
45-
.WithProtoMessages(ChatReflection.Descriptor);
46-
47-
var system =
48-
new ActorSystem()
49-
.WithClientRemote(config);
50-
51-
system
52-
.Remote()
53-
.StartAsync();
54-
55-
context = system.Root;
56-
}
57-
58-
private static void SpawnClient() =>
59-
client = context.Spawn(
60-
Props.FromFunc(
61-
ctx =>
62-
{
63-
switch (ctx.Message)
64-
{
65-
case Connected connected:
66-
Console.WriteLine(connected.Message);
67-
68-
break;
69-
case SayResponse sayResponse:
70-
Console.WriteLine($"{sayResponse.UserName} {sayResponse.Message}");
71-
72-
break;
73-
case NickResponse nickResponse:
74-
Console.WriteLine($"{nickResponse.OldUserName} is now {nickResponse.NewUserName}");
75-
76-
break;
77-
}
78-
79-
return Task.CompletedTask;
80-
}
81-
)
82-
);
83-
84-
private static void ObtainServerPid() => server = PID.FromAddress("127.0.0.1:8000", "chatserver");
85-
86-
private static void ConnectToServer() =>
87-
context.Send(
88-
server,
89-
new Connect
90-
{
91-
Sender = client
92-
}
93-
);
94-
95-
private static void EvaluateCommands()
96-
{
97-
var nick = "Alex";
98-
99-
while (true)
100-
{
101-
var text = Console.ReadLine();
102-
103-
if (string.IsNullOrWhiteSpace(text))
104-
{
105-
continue;
106-
}
107-
108-
if (text.Equals("/exit"))
109-
{
110-
return;
111-
}
112-
113-
if (text.StartsWith("/nick "))
114-
{
115-
var t = text.Split(' ')[1];
116-
117-
context.Send(
118-
server,
119-
new NickRequest
120-
{
121-
OldUserName = nick,
122-
NewUserName = t
123-
}
124-
);
125-
126-
nick = t;
127-
128-
continue;
129-
}
130-
131-
context.Send(
132-
server,
133-
new SayRequest
134-
{
135-
UserName = nick,
136-
Message = text
137-
}
138-
);
139-
}
140-
}
141-
}
1+
// -----------------------------------------------------------------------
2+
// <copyright file="Program.cs" company="Asynkron AB">
3+
// Copyright (C) 2015-2024 Asynkron AB All rights reserved
4+
// </copyright>
5+
// -----------------------------------------------------------------------
6+
7+
using System;
8+
using System.Threading.Tasks;
9+
using chat.messages;
10+
using Microsoft.Extensions.Logging;
11+
using Proto;
12+
using Proto.Remote;
13+
using Proto.Remote.GrpcNet;
14+
using static Proto.Remote.GrpcNet.GrpcNetRemoteConfig;
15+
16+
Log.SetLoggerFactory(LoggerFactory.Create(c => c
17+
.SetMinimumLevel(LogLevel.Information)
18+
.AddConsole()
19+
));
20+
21+
var config =
22+
BindToLocalhost()
23+
.WithProtoMessages(ChatReflection.Descriptor);
24+
25+
var system = new ActorSystem().WithClientRemote(config);
26+
await system.Remote().StartAsync();
27+
28+
var context = system.Root;
29+
30+
var client = context.Spawn(
31+
Props.FromFunc(
32+
ctx =>
33+
{
34+
switch (ctx.Message)
35+
{
36+
case Connected connected:
37+
Console.WriteLine(connected.Message);
38+
break;
39+
case SayResponse sayResponse:
40+
Console.WriteLine($"{sayResponse.UserName} {sayResponse.Message}");
41+
break;
42+
case NickResponse nickResponse:
43+
Console.WriteLine($"{nickResponse.OldUserName} is now {nickResponse.NewUserName}");
44+
break;
45+
}
46+
47+
return Task.CompletedTask;
48+
}
49+
)
50+
);
51+
52+
var server = PID.FromAddress("127.0.0.1:8000", "chatserver");
53+
54+
context.Send(
55+
server,
56+
new Connect
57+
{
58+
Sender = client
59+
}
60+
);
61+
62+
var nick = "Alex";
63+
64+
while (true)
65+
{
66+
var text = Console.ReadLine();
67+
68+
if (string.IsNullOrWhiteSpace(text))
69+
{
70+
continue;
71+
}
72+
73+
if (text.Equals("/exit"))
74+
{
75+
break;
76+
}
77+
78+
if (text.StartsWith("/nick "))
79+
{
80+
var t = text.Split(' ')[1];
81+
82+
context.Send(
83+
server,
84+
new NickRequest
85+
{
86+
OldUserName = nick,
87+
NewUserName = t
88+
}
89+
);
90+
91+
nick = t;
92+
93+
continue;
94+
}
95+
96+
context.Send(
97+
server,
98+
new SayRequest
99+
{
100+
UserName = nick,
101+
Message = text
102+
}
103+
);
104+
}
105+
106+
await system.Remote().ShutdownAsync();

0 commit comments

Comments
 (0)