1
0

add registering of clients

This commit is contained in:
Rokas Puzonas 2024-09-15 11:49:18 +03:00
parent f65126bc26
commit dee39d988d
10 changed files with 269 additions and 33 deletions

38
Lab1/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,38 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "ChatRoom",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-ChatRoom",
"program": "${workspaceFolder}/ChatRoom/bin/Debug/net6.0/ChatRoom.dll",
"args": [],
"cwd": "${workspaceFolder}/ChatRoom",
"console": "externalTerminal",
"stopAtEntry": false
},
{
"name": "Moderator",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Moderator",
"program": "${workspaceFolder}/Moderator/bin/Debug/net6.0/Moderator.dll",
"args": [],
"cwd": "${workspaceFolder}/Moderator",
"console": "externalTerminal",
"stopAtEntry": false
},
{
"name": "Participant",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Participant",
"program": "${workspaceFolder}/Participant/bin/Debug/net6.0/Participant.dll",
"args": [],
"cwd": "${workspaceFolder}/Participant",
"console": "externalTerminal",
"stopAtEntry": false
}
]
}

41
Lab1/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build-ChatRoom",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/ChatRoom/ChatRoom.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "build-Moderator",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Moderator/Moderator.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "build-Participant",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Participant/Participant.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}

View File

@ -1,16 +1,13 @@
namespace ChatRoom; using NLog;
namespace ChatRoom;
public class ChatRoomLogic public class ChatRoomLogic
{ {
/// <summary>
/// Background task thread.
/// </summary>
private Thread thread; private Thread thread;
/// <summary>
/// State descriptor.
/// </summary>
private ChatRoomState state = new ChatRoomState(); private ChatRoomState state = new ChatRoomState();
private Logger log = LogManager.GetCurrentClassLogger();
public ChatRoomLogic() public ChatRoomLogic()
{ {
@ -18,6 +15,20 @@ public class ChatRoomLogic
thread.Start(); thread.Start();
} }
public int RegisterClient(string name)
{
lock (state.accessLock)
{
int clientId = state.lastUniqueId;
state.clients.Add(new Client{
id = clientId,
name = name
});
log.Info($"Registered with client '{name}' with id {clientId}");
return clientId;
}
}
public void BackgroundTask() public void BackgroundTask()
{ {
while (true) while (true)

View File

@ -7,12 +7,12 @@ public class ChatRoomService : IChatRoomService
//NOTE: instance-per-request service would need logic to be static or injected from a singleton instance //NOTE: instance-per-request service would need logic to be static or injected from a singleton instance
private readonly ChatRoomLogic logic = new ChatRoomLogic(); private readonly ChatRoomLogic logic = new ChatRoomLogic();
public void ApproveMessage(int messageId) public int RegisterClient(string name)
{ {
throw new NotImplementedException(); return logic.RegisterClient(name);
} }
public int GetClientId() public void ApproveMessage(int messageId)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -22,7 +22,7 @@ public class ChatRoomService : IChatRoomService
throw new NotImplementedException(); throw new NotImplementedException();
} }
public int GetStrikes() public int GetStrikes(int clientId)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -32,12 +32,7 @@ public class ChatRoomService : IChatRoomService
throw new NotImplementedException(); throw new NotImplementedException();
} }
public bool SendMessage(string contents) public bool SendMessage(int clientId, string contents)
{
throw new NotImplementedException();
}
public void SetClientName(string name)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -1,14 +1,23 @@
namespace ChatRoom; namespace ChatRoom;
public class Client
{
public int id;
public string name;
public int strikes = 0;
}
public class ChatRoomState public class ChatRoomState
{ {
/// <summary> /// <summary>
/// Access lock. /// Access lock.
/// </summary> /// </summary>
public readonly object AccessLock = new object(); public readonly object accessLock = new object();
/// <summary> /// <summary>
/// Last unique ID value generated. /// Last unique ID value generated.
/// </summary> /// </summary>
public int LastUniqueId; public int lastUniqueId;
public List<Client> clients = new List<Client>();
} }

View File

@ -9,13 +9,11 @@ public class Message
public interface IChatRoomService public interface IChatRoomService
{ {
int GetClientId(); int RegisterClient(string name);
void SetClientName(string name); int GetStrikes(int clientId);
int GetStrikes(); bool SendMessage(int clientId, string contents);
bool SendMessage(string contents);
Message? GetNewMessage(); Message? GetNewMessage();

View File

@ -3,11 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.11.35208.52 VisualStudioVersion = 17.11.35208.52
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatRoom", "ChatRoom\ChatRoom.csproj", "{BA24D5FC-9D95-4E0B-8A0C-47CDAD181ADF}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChatRoom", "ChatRoom\ChatRoom.csproj", "{BA24D5FC-9D95-4E0B-8A0C-47CDAD181ADF}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatRoomContract", "ChatRoomContract\ChatRoomContract.csproj", "{22CBCBA6-8B72-474F-95F5-D07F1A59114F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChatRoomContract", "ChatRoomContract\ChatRoomContract.csproj", "{22CBCBA6-8B72-474F-95F5-D07F1A59114F}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moderator", "Moderator\Moderator.csproj", "{5FD486F7-E980-4479-96B0-DCE2167A313D}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moderator", "Moderator\Moderator.csproj", "{5FD486F7-E980-4479-96B0-DCE2167A313D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Participant", "Participant\Participant.csproj", "{03C7B2D5-7EFE-416A-B551-31C456C87E63}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -27,6 +29,10 @@ Global
{5FD486F7-E980-4479-96B0-DCE2167A313D}.Debug|Any CPU.Build.0 = Debug|Any CPU {5FD486F7-E980-4479-96B0-DCE2167A313D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5FD486F7-E980-4479-96B0-DCE2167A313D}.Release|Any CPU.ActiveCfg = Release|Any CPU {5FD486F7-E980-4479-96B0-DCE2167A313D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5FD486F7-E980-4479-96B0-DCE2167A313D}.Release|Any CPU.Build.0 = Release|Any CPU {5FD486F7-E980-4479-96B0-DCE2167A313D}.Release|Any CPU.Build.0 = Release|Any CPU
{03C7B2D5-7EFE-416A-B551-31C456C87E63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{03C7B2D5-7EFE-416A-B551-31C456C87E63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03C7B2D5-7EFE-416A-B551-31C456C87E63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03C7B2D5-7EFE-416A-B551-31C456C87E63}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -5,7 +5,6 @@ using SimpleRpc.Transports.Http.Client;
using SimpleRpc.Serialization.Hyperion; using SimpleRpc.Serialization.Hyperion;
using SimpleRpc.Transports; using SimpleRpc.Transports;
using System.Diagnostics; using System.Diagnostics;
using System.Security.Cryptography;
namespace Moderator; namespace Moderator;
@ -28,7 +27,7 @@ internal class Moderator
/// <summary> /// <summary>
/// Logger for this class. /// Logger for this class.
/// </summary> /// </summary>
Logger mLog = LogManager.GetCurrentClassLogger(); Logger log = LogManager.GetCurrentClassLogger();
/// <summary> /// <summary>
/// Configures logging subsystem. /// Configures logging subsystem.
@ -53,9 +52,11 @@ internal class Moderator
var rnd = new Random(); var rnd = new Random();
var name = FIRSTNAMES[rnd.Next(FIRSTNAMES.Count)] + " " + LASTNAMES[rnd.Next(LASTNAMES.Count)]; var name = FIRSTNAMES[rnd.Next(FIRSTNAMES.Count)] + " " + LASTNAMES[rnd.Next(LASTNAMES.Count)];
Console.Title = $"Moderator | ${name}";
chatRoom.SetClientName(name); int clientId = chatRoom.RegisterClient(name);
log.Info($"Registered with client id {clientId}");
Console.Title = $"Moderator | {name} | {clientId}";
while (true) while (true)
{ {
@ -100,7 +101,7 @@ internal class Moderator
catch (Exception e) catch (Exception e)
{ {
//log whatever exception to console //log whatever exception to console
mLog.Warn(e, "Unhandled exception caught. Will restart main loop."); log.Warn(e, "Unhandled exception caught. Will restart main loop.");
//prevent console spamming //prevent console spamming
Thread.Sleep(2000); Thread.Sleep(2000);

View File

@ -0,0 +1,116 @@
using ChatRoomContract;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using SimpleRpc.Transports.Http.Client;
using SimpleRpc.Serialization.Hyperion;
using SimpleRpc.Transports;
using System.Diagnostics;
namespace Participant;
internal class Participant
{
private readonly List<string> FIRSTNAMES =
new List<string> {
"John", "Peter", "Jack", "Steve"
};
/// <summary>
/// A set of surnames to choose from.
/// </summary>
private readonly List<string> LASTNAMES =
new List<String> {
"Johnson", "Peterson", "Jackson", "Steveson"
};
/// <summary>
/// Logger for this class.
/// </summary>
Logger log = LogManager.GetCurrentClassLogger();
/// <summary>
/// Configures logging subsystem.
/// </summary>
private void ConfigureLogging()
{
var config = new NLog.Config.LoggingConfiguration();
var console =
new NLog.Targets.ConsoleTarget("console")
{
Layout = @"${date:format=HH\:mm\:ss}|${level}| ${message} ${exception}"
};
config.AddTarget(console);
config.AddRuleForAllLevels(console);
LogManager.Configuration = config;
}
private void RunConnection(IChatRoomService chatRoom)
{
var rnd = new Random();
var name = FIRSTNAMES[rnd.Next(FIRSTNAMES.Count)] + " " + LASTNAMES[rnd.Next(LASTNAMES.Count)];
int clientId = chatRoom.RegisterClient(name);
log.Info($"Registered with client id {clientId}");
Console.Title = $"Participant | {name} | {clientId}";
while (true)
{
}
}
private void Run()
{
//configure logging
ConfigureLogging();
//initialize random number generator
var rnd = new Random();
while (true)
{
//connect to the server, get service client proxy
var sc = new ServiceCollection();
sc
.AddSimpleRpcClient(
"chatRoomService", //must be same as on line 86
new HttpClientTransportOptions
{
Url = "http://127.0.0.1:5000/simplerpc",
Serializer = "HyperionMessageSerializer"
}
)
.AddSimpleRpcHyperionSerializer();
sc.AddSimpleRpcProxy<IChatRoomService>("chatRoomService"); //must be same as on line 77
var sp = sc.BuildServiceProvider();
var chatRoom = sp.GetService<IChatRoomService>();
Debug.Assert(chatRoom != null);
try
{
RunConnection(chatRoom);
}
catch (Exception e)
{
//log whatever exception to console
log.Warn(e, "Unhandled exception caught. Will restart main loop.");
//prevent console spamming
Thread.Sleep(2000);
}
}
}
static void Main(string[] args)
{
var self = new Participant();
self.Run();
}
}

View File

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="NLog" Version="5.3.4" />
<PackageReference Include="SimpleRpc" Version="1.0.0-beta1" />
<PackageReference Include="SimpleRpc.Serialization.Hyperion" Version="1.0.0-beta1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ChatRoomContract\ChatRoomContract.csproj" />
</ItemGroup>
</Project>