From 6c523af453095b12fbcecfbb5d91b1ea81e6a8da Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sun, 15 Sep 2024 12:11:42 +0300 Subject: [PATCH] added sending of messages --- Lab1/ChatRoom/ChatRoomLogic.cs | 66 +++++++++++++++++++++-- Lab1/ChatRoom/ChatRoomService.cs | 10 ++-- Lab1/ChatRoom/ChatRoomState.cs | 20 +++++++ Lab1/ChatRoom/Server.cs | 2 + Lab1/ChatRoomContract/IChatRoomService.cs | 2 +- Lab1/Moderator/Moderator.csproj | 1 + Lab1/Participant/Participant.cs | 27 ++++------ Lab1/Participant/Participant.csproj | 1 + 8 files changed, 101 insertions(+), 28 deletions(-) diff --git a/Lab1/ChatRoom/ChatRoomLogic.cs b/Lab1/ChatRoom/ChatRoomLogic.cs index 2c6b4bd..170fc33 100644 --- a/Lab1/ChatRoom/ChatRoomLogic.cs +++ b/Lab1/ChatRoom/ChatRoomLogic.cs @@ -5,22 +5,30 @@ namespace ChatRoom; public class ChatRoomLogic { private Thread thread; - private ChatRoomState state = new ChatRoomState(); + private ChatRoomState state = new ChatRoomState(); private Logger log = LogManager.GetCurrentClassLogger(); - public ChatRoomLogic() - { + public ChatRoomLogic() + { thread = new Thread(BackgroundTask); thread.Start(); } + int NextId() + { + int id = state.lastUniqueId; + state.lastUniqueId++; + return id; + } + public int RegisterClient(string name) { lock (state.accessLock) { - int clientId = state.lastUniqueId; - state.clients.Add(new Client{ + int clientId = NextId(); + state.clients.Add(new Client + { id = clientId, name = name }); @@ -29,6 +37,54 @@ public class ChatRoomLogic } } + Client? FindClientById(int clientId) + { + foreach (var client in state.clients) + { + if (client.id == clientId) + { + return client; + } + } + return null; + } + + public bool SendMessage(int clientId, string contents, bool needsToBeCensored) + { + lock (state.accessLock) + { + var client = FindClientById(clientId); + if (client == null) + { + return false; + } + + if (client.blockedUntil != null) + { + if (DateTime.UtcNow < client.blockedUntil) + { + return false; + } + else + { + client.blockedUntil = null; + } + } + + state.messages.Add(new Message + { + id = NextId(), + clientId = clientId, + contents = contents, + needsToBeCensored = needsToBeCensored, + status = MessageStatus.WaitingApproval + }); + log.Info($"Client '{client.name}' ({client.id}) sent message '{contents}'. Needs to censored: {needsToBeCensored}"); + } + + return true; + } + public void BackgroundTask() { while (true) diff --git a/Lab1/ChatRoom/ChatRoomService.cs b/Lab1/ChatRoom/ChatRoomService.cs index 7997fb2..113c799 100644 --- a/Lab1/ChatRoom/ChatRoomService.cs +++ b/Lab1/ChatRoom/ChatRoomService.cs @@ -1,8 +1,6 @@ namespace ChatRoom; -using ChatRoomContract; - -public class ChatRoomService : IChatRoomService +public class ChatRoomService : ChatRoomContract.IChatRoomService { //NOTE: instance-per-request service would need logic to be static or injected from a singleton instance private readonly ChatRoomLogic logic = new ChatRoomLogic(); @@ -17,7 +15,7 @@ public class ChatRoomService : IChatRoomService throw new NotImplementedException(); } - public Message? GetNewMessage() + public ChatRoomContract.Message? GetNewMessage() { throw new NotImplementedException(); } @@ -32,8 +30,8 @@ public class ChatRoomService : IChatRoomService throw new NotImplementedException(); } - public bool SendMessage(int clientId, string contents) + public bool SendMessage(int clientId, string contents, bool needsToBeCensored) { - throw new NotImplementedException(); + return logic.SendMessage(clientId, contents, needsToBeCensored); } } diff --git a/Lab1/ChatRoom/ChatRoomState.cs b/Lab1/ChatRoom/ChatRoomState.cs index f175d8a..6f34280 100644 --- a/Lab1/ChatRoom/ChatRoomState.cs +++ b/Lab1/ChatRoom/ChatRoomState.cs @@ -5,6 +5,24 @@ public class Client public int id; public string name; public int strikes = 0; + + public DateTime? blockedUntil = null; +} + +public enum MessageStatus +{ + WaitingApproval, + Approved, + Rejected +} + +public class Message +{ + public int id; + public int clientId; + public string contents; + public bool needsToBeCensored; + public MessageStatus status = MessageStatus.WaitingApproval; } public class ChatRoomState @@ -20,4 +38,6 @@ public class ChatRoomState public int lastUniqueId; public List clients = new List(); + + public List messages = new List(); } diff --git a/Lab1/ChatRoom/Server.cs b/Lab1/ChatRoom/Server.cs index 4a22302..bc3ffe4 100644 --- a/Lab1/ChatRoom/Server.cs +++ b/Lab1/ChatRoom/Server.cs @@ -66,6 +66,8 @@ public class Server /// Command line arguments. private void StartServer(string[] args) { + Console.Title = "Chat Room"; + ///create web app builder var builder = WebApplication.CreateBuilder(args); diff --git a/Lab1/ChatRoomContract/IChatRoomService.cs b/Lab1/ChatRoomContract/IChatRoomService.cs index 8c1f03f..0a716f1 100644 --- a/Lab1/ChatRoomContract/IChatRoomService.cs +++ b/Lab1/ChatRoomContract/IChatRoomService.cs @@ -13,7 +13,7 @@ public interface IChatRoomService int GetStrikes(int clientId); - bool SendMessage(int clientId, string contents); + bool SendMessage(int clientId, string contents, bool needsToBeCensored); Message? GetNewMessage(); diff --git a/Lab1/Moderator/Moderator.csproj b/Lab1/Moderator/Moderator.csproj index ff4f6e0..e095a2a 100644 --- a/Lab1/Moderator/Moderator.csproj +++ b/Lab1/Moderator/Moderator.csproj @@ -8,6 +8,7 @@ + diff --git a/Lab1/Participant/Participant.cs b/Lab1/Participant/Participant.cs index 917312a..2c8e60b 100644 --- a/Lab1/Participant/Participant.cs +++ b/Lab1/Participant/Participant.cs @@ -5,25 +5,12 @@ using SimpleRpc.Transports.Http.Client; using SimpleRpc.Serialization.Hyperion; using SimpleRpc.Transports; using System.Diagnostics; +using Bogus; namespace Participant; internal class Participant { - private readonly List FIRSTNAMES = - new List { - "John", "Peter", "Jack", "Steve" - }; - - /// - /// A set of surnames to choose from. - /// - private readonly List LASTNAMES = - new List { - "Johnson", "Peterson", "Jackson", "Steveson" - }; - - /// /// Logger for this class. /// @@ -49,17 +36,25 @@ internal class Participant private void RunConnection(IChatRoomService chatRoom) { + var faker = new Faker("en"); var rnd = new Random(); - var name = FIRSTNAMES[rnd.Next(FIRSTNAMES.Count)] + " " + LASTNAMES[rnd.Next(LASTNAMES.Count)]; - + var name = faker.Name.FullName(); int clientId = chatRoom.RegisterClient(name); log.Info($"Registered with client id {clientId}"); Console.Title = $"Participant | {name} | {clientId}"; while (true) { + var message = string.Join(" ", faker.Lorem.Words(5)); + bool needsToBeCensored = rnd.Next(0, 100) > 50; + if (chatRoom.SendMessage(clientId, message, needsToBeCensored)) { + log.Info("Sent message"); + } else { + log.Info("Failed to send message, blocked"); + } + Thread.Sleep(2 * 1000); } } diff --git a/Lab1/Participant/Participant.csproj b/Lab1/Participant/Participant.csproj index ff4f6e0..e095a2a 100644 --- a/Lab1/Participant/Participant.csproj +++ b/Lab1/Participant/Participant.csproj @@ -8,6 +8,7 @@ +