Skip to content

Commit 03776a2

Browse files
committed
Code review by gemini3
1 parent dde12d4 commit 03776a2

14 files changed

Lines changed: 252 additions & 135 deletions

File tree

Source/Common/Constants.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace Common
2+
{
3+
public static class Constants
4+
{
5+
public static class Config
6+
{
7+
public const string AppSettingsFile = "appSettings.json";
8+
public const string PathKey = "Path";
9+
public const string IndexFile = "index.md";
10+
}
11+
12+
public static class Colors
13+
{
14+
public const string White = "<white>";
15+
public const string Red = "<red>";
16+
public const string Green = "<green>";
17+
public const string Blue = "<blue>";
18+
public const string Orange = "<orange>";
19+
public const string Black = "<black>";
20+
public const string Brown = "<brown>";
21+
public const string LightRed = "<lightred>";
22+
public const string Pink = "<pink>";
23+
public const string DarkGray = "<darkgray>";
24+
public const string DarkGrey = "<darkgrey>";
25+
public const string Gray = "<gray>";
26+
public const string Grey = "<grey>";
27+
public const string LightGreen = "<lightgreen>";
28+
public const string LightBlue = "<lightblue>";
29+
public const string LightGray = "<lightgray>";
30+
public const string LightGrey = "<lightgrey>";
31+
public const string Purple = "<purple>";
32+
public const string Yellow = "<yellow>";
33+
public const string Cyan = "<cyan>";
34+
public const string RevOn = "<revon>";
35+
public const string RevOff = "<revoff>";
36+
}
37+
38+
public static class Cursor
39+
{
40+
public const string Home = "<home>";
41+
public const string Down = "<crsrdown>";
42+
public const string Right = "<crsrright>";
43+
public const string Up = "<crsrup>";
44+
public const string Left = "<crsrleft>";
45+
}
46+
}
47+
}

Source/Encoder/Encoder.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<ProjectReference Include="..\PetsciiRawConverter\PetsciiRawConverter.csproj" />
11+
<ProjectReference Include="..\Common\Common.csproj" />
1112
</ItemGroup>
1213

1314
</Project>

Source/Encoder/Petscii.cs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text;
22
using System.Text.RegularExpressions;
3+
using Common;
34

45
namespace Encoder
56
{
@@ -18,35 +19,35 @@ public class Petscii : IEncoder
1819
/// </summary>
1920
private static Dictionary<string, string> ConversionMap = new Dictionary<string, string>() {
2021
// Convert color tags
21-
{ "<white>", new String((char)5, 1) },
22-
{ "<red>", new String((char)28, 1) },
23-
{ "<green>", new String((char)30, 1) },
24-
{ "<blue>", new String((char)31, 1) },
25-
{ "<orange>", new String((char)129, 1) },
26-
{ "<black>", new String((char)144, 1) },
27-
{ "<brown>", new String((char)149, 1) },
28-
{ "<lightred>", new String((char)150, 1) },
29-
{ "<pink>", new String((char)150, 1) },
30-
{ "<darkgray>", new String((char)151, 1) },
31-
{ "<darkgrey>", new String((char)151, 1) },
32-
{ "<gray>", new String((char)152, 1) },
33-
{ "<grey>", new String((char)152, 1) },
34-
{ "<lightgreen>", new String((char)153, 1) },
35-
{ "<lightblue>", new String((char)154, 1) },
36-
{ "<lightgray>", new String((char)155, 1) },
37-
{ "<lightgrey>", new String((char)155, 1) },
38-
{ "<purple>", new String((char)156, 1) },
39-
{ "<yellow>", new String((char)158, 1) },
40-
{ "<cyan>", new String((char)159, 1) },
41-
{ "<revon>", new String((char)18, 1) },
42-
{ "<revoff>", new String((char)146, 1) },
22+
{ Constants.Colors.White, new String((char)5, 1) },
23+
{ Constants.Colors.Red, new String((char)28, 1) },
24+
{ Constants.Colors.Green, new String((char)30, 1) },
25+
{ Constants.Colors.Blue, new String((char)31, 1) },
26+
{ Constants.Colors.Orange, new String((char)129, 1) },
27+
{ Constants.Colors.Black, new String((char)144, 1) },
28+
{ Constants.Colors.Brown, new String((char)149, 1) },
29+
{ Constants.Colors.LightRed, new String((char)150, 1) },
30+
{ Constants.Colors.Pink, new String((char)150, 1) },
31+
{ Constants.Colors.DarkGray, new String((char)151, 1) },
32+
{ Constants.Colors.DarkGrey, new String((char)151, 1) },
33+
{ Constants.Colors.Gray, new String((char)152, 1) },
34+
{ Constants.Colors.Grey, new String((char)152, 1) },
35+
{ Constants.Colors.LightGreen, new String((char)153, 1) },
36+
{ Constants.Colors.LightBlue, new String((char)154, 1) },
37+
{ Constants.Colors.LightGray, new String((char)155, 1) },
38+
{ Constants.Colors.LightGrey, new String((char)155, 1) },
39+
{ Constants.Colors.Purple, new String((char)156, 1) },
40+
{ Constants.Colors.Yellow, new String((char)158, 1) },
41+
{ Constants.Colors.Cyan, new String((char)159, 1) },
42+
{ Constants.Colors.RevOn, new String((char)18, 1) },
43+
{ Constants.Colors.RevOff, new String((char)146, 1) },
4344

4445
// Convert position tags
45-
{ "<home>", new String((char)19, 1) },
46-
{ "<crsrdown>", new String((char)17, 1) },
47-
{ "<crsrright>", new String((char)29, 1) },
48-
{ "<crsrup>", new String((char)145, 1) },
49-
{ "<crsrleft>", new String((char)157, 1) },
46+
{ Constants.Cursor.Home, new String((char)19, 1) },
47+
{ Constants.Cursor.Down, new String((char)17, 1) },
48+
{ Constants.Cursor.Right, new String((char)29, 1) },
49+
{ Constants.Cursor.Up, new String((char)145, 1) },
50+
{ Constants.Cursor.Left, new String((char)157, 1) },
5051
};
5152

5253
/// <summary>

Source/Encoder/Telnet.cs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text;
22
using System.Text.RegularExpressions;
3+
using Common;
34

45
namespace Encoder
56
{
@@ -13,35 +14,35 @@ public class Telnet : IEncoder
1314
/// </summary>
1415
private static Dictionary<string, string> ConversionMap = new Dictionary<string, string>()
1516
{
16-
{ "<white>", "\x1B[1;37m" },
17-
{ "<red>", "\x1B[31m" },
18-
{ "<green>", "\x1B[32m" },
19-
{ "<blue>", "\x1B[34m" },
20-
{ "<orange>", "\x1B[33m" },
21-
{ "<black>", "\x1B[30m" },
22-
{ "<brown>", "\x1B[31m" },
23-
{ "<lightred>", "\x1B[1;31m" },
24-
{ "<pink>", "\x1B[1;35m" },
25-
{ "<darkgray>", "\x1B[1;30m" },
26-
{ "<darkgrey>", "\x1B[1;30m" },
27-
{ "<gray>", "\x1B[1;30m" },
28-
{ "<grey>", "\x1B[1;30m" },
29-
{ "<lightgreen>", "\x1B[1;32m" },
30-
{ "<lightblue>", "\x1B[1;34m" },
31-
{ "<lightgray>", "\x1B[37m" },
32-
{ "<lightgrey>", "\x1B[37m" },
33-
{ "<purple>", "\x1B[35m" },
34-
{ "<yellow>", "\x1B[1;33m" },
35-
{ "<cyan>", "\x1B[36m" },
36-
{ "<revon>", "\x1B[7m" },
37-
{ "<revoff>", "\x1B[27m" },
17+
{ Constants.Colors.White, "\x1B[1;37m" },
18+
{ Constants.Colors.Red, "\x1B[31m" },
19+
{ Constants.Colors.Green, "\x1B[32m" },
20+
{ Constants.Colors.Blue, "\x1B[34m" },
21+
{ Constants.Colors.Orange, "\x1B[33m" },
22+
{ Constants.Colors.Black, "\x1B[30m" },
23+
{ Constants.Colors.Brown, "\x1B[31m" },
24+
{ Constants.Colors.LightRed, "\x1B[1;31m" },
25+
{ Constants.Colors.Pink, "\x1B[1;35m" },
26+
{ Constants.Colors.DarkGray, "\x1B[1;30m" },
27+
{ Constants.Colors.DarkGrey, "\x1B[1;30m" },
28+
{ Constants.Colors.Gray, "\x1B[1;30m" },
29+
{ Constants.Colors.Grey, "\x1B[1;30m" },
30+
{ Constants.Colors.LightGreen, "\x1B[1;32m" },
31+
{ Constants.Colors.LightBlue, "\x1B[1;34m" },
32+
{ Constants.Colors.LightGray, "\x1B[37m" },
33+
{ Constants.Colors.LightGrey, "\x1B[37m" },
34+
{ Constants.Colors.Purple, "\x1B[35m" },
35+
{ Constants.Colors.Yellow, "\x1B[1;33m" },
36+
{ Constants.Colors.Cyan, "\x1B[36m" },
37+
{ Constants.Colors.RevOn, "\x1B[7m" },
38+
{ Constants.Colors.RevOff, "\x1B[27m" },
3839

3940
// Convert position tags
40-
{ "<home>", "\x1B[1;1H" },
41-
{ "<crsrdown>", "\x1B[B" },
42-
{ "<crsrright>", "\x1B[C"},
43-
{ "<crsrup>", "\x1B[A" },
44-
{ "<crsrleft>", "\x1B[D" },
41+
{ Constants.Cursor.Home, "\x1B[1;1H" },
42+
{ Constants.Cursor.Down, "\x1B[B" },
43+
{ Constants.Cursor.Right, "\x1B[C"},
44+
{ Constants.Cursor.Up, "\x1B[A" },
45+
{ Constants.Cursor.Left, "\x1B[D" },
4546
};
4647

4748
/// <summary>

Source/Parser/Parser.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<ItemGroup>
1010
<PackageReference Include="HtmlAgilityPack" Version="1.12.4" />
1111
<PackageReference Include="Markdig" Version="0.43.0" />
12-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.0" />
13-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0" />
12+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.0-*" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0-*" />
1414
</ItemGroup>
1515

1616
<ItemGroup>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Net.Sockets;
2+
using RetroNET_BBS.Server;
3+
4+
namespace RetroNET_BBS.Client
5+
{
6+
public interface IUserFactory
7+
{
8+
User Create(ConnectionType type, TcpClient client, int onlineUsers, User.OnUserDisconnectCallback callback);
9+
}
10+
11+
public class UserFactory : IUserFactory
12+
{
13+
public User Create(ConnectionType type, TcpClient client, int onlineUsers, User.OnUserDisconnectCallback callback)
14+
{
15+
switch (type)
16+
{
17+
case ConnectionType.Petscii:
18+
return new PetsciiUser(client, onlineUsers, callback);
19+
case ConnectionType.Telnet:
20+
return new TelnetUser(client, onlineUsers, callback);
21+
default:
22+
throw new NotSupportedException($"Connection type {type} not supported");
23+
}
24+
}
25+
}
26+
}

Source/RetroNET-BBS/Program.cs

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,77 @@
11
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
25
using Parser.Markdown;
36
using Parser.Raw;
47
using RetroNET_BBS.Client;
58
using RetroNET_BBS.ContentProvider;
69
using RetroNET_BBS.Server;
10+
using Common;
711

812
public class Prg
913
{
10-
/// <summary>
11-
/// Start a new instance of the Petscii server
12-
/// </summary>
13-
static async void StartPetsciiServer()
14+
public static async Task Main(string[] args)
1415
{
15-
var svr = new Server("0.0.0.0", 8502, ConnectionType.Petscii);
16-
await svr.Start();
17-
}
18-
19-
/// <summary>
20-
/// Start a new instance of the Telnet server
21-
/// </summary>
22-
static async void StartTelnetServer()
23-
{
24-
var svr = new Server("0.0.0.0", 23, ConnectionType.Telnet);
25-
await svr.Start();
26-
}
27-
28-
public static void Main()
29-
{
30-
31-
Console.WriteLine("Hello, World! This is RetroNET-BBS!");
16+
var host = Host.CreateDefaultBuilder(args)
17+
.ConfigureAppConfiguration((hostingContext, config) =>
18+
{
19+
config.AddJsonFile(Constants.Config.AppSettingsFile, optional: false, reloadOnChange: true);
20+
})
21+
.ConfigureServices((hostContext, services) =>
22+
{
23+
services.AddLogging(configure => configure.AddConsole());
24+
25+
// Register PageContainer as Singleton (for now, until further refactoring)
26+
// Note: PageContainer is static, so we don't strictly need to register it,
27+
// but we should initialize it here.
28+
29+
// Register Servers as Hosted Services
30+
services.AddSingleton<IUserFactory, UserFactory>();
31+
services.AddHostedService<BbsBackgroundService>();
32+
})
33+
.Build();
3234

33-
// Start document import
34-
var builder = new ConfigurationBuilder().AddJsonFile("appSettings.json");
35-
var config = builder.Build();
35+
var config = host.Services.GetRequiredService<IConfiguration>();
36+
var logger = host.Services.GetRequiredService<ILogger<Prg>>();
3637

37-
var folder = config["Path"];
38-
var homePath = Path.Combine(folder, "index.md");
38+
logger.LogInformation("Hello, World! This is RetroNET-BBS!");
3939

40-
Console.WriteLine("Parsing pages...");
40+
var folder = config[Constants.Config.PathKey];
41+
42+
logger.LogInformation("Parsing pages...");
4143
PageContainer.Pages = Markdown.ParseAllFiles(folder);
4244

43-
Console.WriteLine("Parsing imports...");
45+
logger.LogInformation("Parsing imports...");
4446
PageContainer.Imports = Seq.ParseAllFiles(folder);
4547

46-
Console.WriteLine("Starting servers...");
47-
Thread thread1 = new Thread(StartPetsciiServer);
48-
thread1.IsBackground = true;
49-
thread1.Start();
48+
await host.RunAsync();
5049

51-
Thread thread2 = new Thread(StartTelnetServer);
52-
thread2.IsBackground = true;
53-
thread2.Start();
50+
logger.LogInformation("Goodbye, World!");
51+
}
52+
}
53+
54+
public class BbsBackgroundService : BackgroundService
55+
{
56+
private readonly ILogger<BbsBackgroundService> logger;
57+
private readonly IUserFactory userFactory;
5458

55-
Thread.Sleep(1000);
59+
public BbsBackgroundService(ILogger<BbsBackgroundService> logger, IUserFactory userFactory)
60+
{
61+
this.logger = logger;
62+
this.userFactory = userFactory;
63+
}
64+
65+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
66+
{
67+
logger.LogInformation("Starting servers...");
5668

57-
while (true)
58-
{
59-
Thread.Sleep(1000);
60-
}
69+
var petsciiServer = new Server("0.0.0.0", 8502, ConnectionType.Petscii, userFactory);
70+
var telnetServer = new Server("0.0.0.0", 23, ConnectionType.Telnet, userFactory);
6171

62-
Console.WriteLine("Goodbye, World!");
72+
var t1 = petsciiServer.Start(stoppingToken);
73+
var t2 = telnetServer.Start(stoppingToken);
6374

75+
await Task.WhenAll(t1, t2);
6476
}
6577
}

Source/RetroNET-BBS/RetroNET-BBS.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.22.1" />
14+
15+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0-*" />
16+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0-*" />
17+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="10.0.0-*" />
1518
</ItemGroup>
1619

1720
<ItemGroup>

0 commit comments

Comments
 (0)