Skip to content

Commit 58ea5bd

Browse files
committed
enabled 1:n messaging scenarios where there are n workers and 1 "boss"
1 parent e8f73ff commit 58ea5bd

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

Source/LocalNetAppChat/LocalNetAppChat.Server.Domain.Tests/Messaging/ReceiverPatternMatchingTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using LocalNetAppChat.Server.Domain.Messaging;
2+
using NUnit.Framework;
23

34
namespace LocalNetAppChat.Server.Domain.Tests.Messaging;
45

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace LocalNetAppChat.Server.Domain.Messaging;
4+
5+
public static class PatternConverter
6+
{
7+
public static string ConvertToRegex(string pattern)
8+
{
9+
string regexPattern = "^" + Regex.Escape(pattern).Replace(@"\*", ".*") + "$";
10+
return regexPattern;
11+
}
12+
}

Source/LocalNetAppChat/LocalNetAppChat.Server.Domain/Messaging/ReceiverPatternMatcher.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
using System.Text.RegularExpressions;
22

3-
public class PatternConverter
4-
{
5-
public static string ConvertToRegex(string pattern)
6-
{
7-
string regexPattern = "^" + Regex.Escape(pattern).Replace(@"\*", ".*") + "$";
8-
return regexPattern;
9-
}
10-
}
3+
namespace LocalNetAppChat.Server.Domain.Messaging;
114

125
public static class ReceiverPatternMatcher
136
{

Source/LocalNetAppChat/LocalNetAppChat.Server.Domain/SynchronizedCollectionBasedMessageList.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Concurrent;
2-
using System.Text.RegularExpressions;
32
using LocalNetAppChat.Domain.Shared;
43
using LocalNetAppChat.Server.Domain.Messaging;
54

@@ -9,6 +8,7 @@ public class SynchronizedCollectionBasedMessageList : IMessageList
98
{
109
private readonly TimeSpan _messageLifetime;
1110
private readonly SynchronizedCollection<ReceivedMessage> _messages = new();
11+
private readonly ConcurrentDictionary<long, long> _redirectedPatternMessages = new();
1212
private readonly ConcurrentDictionary<string, long> _clientStates = new();
1313

1414
public SynchronizedCollectionBasedMessageList(TimeSpan messageLifetime)
@@ -26,6 +26,9 @@ private void Cleanup()
2626
if (message.Timestamp < currentEndOfLife)
2727
{
2828
_messages.Remove(message);
29+
30+
if (_redirectedPatternMessages.ContainsKey(message.Id))
31+
_redirectedPatternMessages.TryRemove(message.Id, out _);
2932
}
3033
}
3134
}
@@ -66,10 +69,33 @@ private ReceivedMessage[] OnlyMessagesAfterIndex(ReceivedMessage[] messages, lon
6669

6770
private ReceivedMessage[] FilterOutDirectMessagesToTheOtherClients(ReceivedMessage[] messages, string clientId)
6871
{
69-
return messages
70-
.Where(x => string.IsNullOrEmpty(x.Receiver) || x.Receiver == clientId || ReceiverPatternMatcher.DoesMatch(clientId, x.Receiver))
71-
.OrderBy(x => x.Id)
72-
.ToArray();
72+
var result = new List<ReceivedMessage>();
73+
74+
foreach (var message in messages)
75+
{
76+
if (string.IsNullOrEmpty(message.Receiver))
77+
{
78+
result.Add(message);
79+
continue;
80+
}
81+
82+
if (message.Receiver == clientId)
83+
{
84+
result.Add(message);
85+
continue;
86+
}
87+
88+
if (ReceiverPatternMatcher.DoesMatch(clientId, message.Receiver))
89+
{
90+
if (_redirectedPatternMessages.ContainsKey(message.Id))
91+
continue;
92+
93+
_redirectedPatternMessages.AddOrUpdate(message.Id, message.Id, (_, _) => message.Id);
94+
result.Add(message);
95+
}
96+
}
97+
98+
return result.ToArray();
7399
}
74100

75101
private ReceivedMessage[] GetMessagesThatAreYoungerThan(DateTime currentEndOfLife)

0 commit comments

Comments
 (0)