1
0

added sending of messages

This commit is contained in:
Rokas Puzonas 2024-09-15 12:11:42 +03:00
parent dee39d988d
commit 6c523af453
8 changed files with 101 additions and 28 deletions

View File

@ -5,22 +5,30 @@ namespace ChatRoom;
public class ChatRoomLogic public class ChatRoomLogic
{ {
private Thread thread; private Thread thread;
private ChatRoomState state = new ChatRoomState(); private ChatRoomState state = new ChatRoomState();
private Logger log = LogManager.GetCurrentClassLogger(); private Logger log = LogManager.GetCurrentClassLogger();
public ChatRoomLogic() public ChatRoomLogic()
{ {
thread = new Thread(BackgroundTask); thread = new Thread(BackgroundTask);
thread.Start(); thread.Start();
} }
int NextId()
{
int id = state.lastUniqueId;
state.lastUniqueId++;
return id;
}
public int RegisterClient(string name) public int RegisterClient(string name)
{ {
lock (state.accessLock) lock (state.accessLock)
{ {
int clientId = state.lastUniqueId; int clientId = NextId();
state.clients.Add(new Client{ state.clients.Add(new Client
{
id = clientId, id = clientId,
name = name 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() public void BackgroundTask()
{ {
while (true) while (true)

View File

@ -1,8 +1,6 @@
namespace ChatRoom; namespace ChatRoom;
using ChatRoomContract; public class ChatRoomService : ChatRoomContract.IChatRoomService
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();
@ -17,7 +15,7 @@ public class ChatRoomService : IChatRoomService
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Message? GetNewMessage() public ChatRoomContract.Message? GetNewMessage()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -32,8 +30,8 @@ public class ChatRoomService : IChatRoomService
throw new NotImplementedException(); 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);
} }
} }

View File

@ -5,6 +5,24 @@ public class Client
public int id; public int id;
public string name; public string name;
public int strikes = 0; 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 public class ChatRoomState
@ -20,4 +38,6 @@ public class ChatRoomState
public int lastUniqueId; public int lastUniqueId;
public List<Client> clients = new List<Client>(); public List<Client> clients = new List<Client>();
public List<Message> messages = new List<Message>();
} }

View File

@ -66,6 +66,8 @@ public class Server
/// <param name="args">Command line arguments.</param> /// <param name="args">Command line arguments.</param>
private void StartServer(string[] args) private void StartServer(string[] args)
{ {
Console.Title = "Chat Room";
///create web app builder ///create web app builder
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);

View File

@ -13,7 +13,7 @@ public interface IChatRoomService
int GetStrikes(int clientId); int GetStrikes(int clientId);
bool SendMessage(int clientId, string contents); bool SendMessage(int clientId, string contents, bool needsToBeCensored);
Message? GetNewMessage(); Message? GetNewMessage();

View File

@ -8,6 +8,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Bogus" Version="35.6.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="NLog" Version="5.3.4" /> <PackageReference Include="NLog" Version="5.3.4" />
<PackageReference Include="SimpleRpc" Version="1.0.0-beta1" /> <PackageReference Include="SimpleRpc" Version="1.0.0-beta1" />

View File

@ -5,25 +5,12 @@ 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 Bogus;
namespace Participant; namespace Participant;
internal class 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> /// <summary>
/// Logger for this class. /// Logger for this class.
/// </summary> /// </summary>
@ -49,17 +36,25 @@ internal class Participant
private void RunConnection(IChatRoomService chatRoom) private void RunConnection(IChatRoomService chatRoom)
{ {
var faker = new Faker("en");
var rnd = new Random(); 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); int clientId = chatRoom.RegisterClient(name);
log.Info($"Registered with client id {clientId}"); log.Info($"Registered with client id {clientId}");
Console.Title = $"Participant | {name} | {clientId}"; Console.Title = $"Participant | {name} | {clientId}";
while (true) 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);
} }
} }

View File

@ -8,6 +8,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Bogus" Version="35.6.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="NLog" Version="5.3.4" /> <PackageReference Include="NLog" Version="5.3.4" />
<PackageReference Include="SimpleRpc" Version="1.0.0-beta1" /> <PackageReference Include="SimpleRpc" Version="1.0.0-beta1" />