1
0
tinklu-paslaugos/Lab4/ModeratorSimpleRPC/Moderator.cs
2024-12-07 17:21:09 +02:00

82 lines
2.2 KiB
C#

using ChatRoomContract;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using SimpleRpc.Transports.Http.Client;
using SimpleRpc.Serialization.Hyperion;
using SimpleRpc.Transports;
using System.Diagnostics;
using Moderator;
namespace ModeratorSimpleRPC;
internal class Moderator
{
/// <summary>
/// Configures logging subsystem.
/// </summary>
private static 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;
}
static void Main(string[] args)
{
Logger log = LogManager.GetCurrentClassLogger();
//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:5001/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);
var moderator = new ModeratorLogic(chatRoom);
try
{
moderator.Run();
}
catch (Exception e)
{
//log whatever exception to console
log.Warn(e, "Unhandled exception caught. Will restart main loop.");
//prevent console spamming
Thread.Sleep(2000);
}
}
}
}