-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatroom.cs
88 lines (65 loc) · 2.38 KB
/
Chatroom.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoFactory.GangOfFour.Mediator.RealWorld
{
/// <summary>
/// The 'ConcreteMediator' class
/// </summary>
class Chatroom : AbstractChatroom
{
private Dictionary<string, Participant> participants = new Dictionary<string, Participant>();
public override void Register(Participant participant)
{
if (!participants.ContainsValue(participant))
{
participants[participant.Name] = participant;
}
//add a participant to the chatroom
participant.Chatroom = this;
}
public override void Send(string from, string to, string message)
{
Participant participant = participants[to];
if (participant != null)
{
participant.Receive(from, message);
}
}
public override void SendAll(string from, string message)
{
//loop the chatroom and send the same messgae to all registered users
foreach (KeyValuePair<string, Participant> entry in participants)
{
//to replace the [to] argument with participants[entry.Key]
Participant participant = participants[entry.Key];
if (participant != null && participants[from] != participants[entry.Key])
{
participant.Receive(from, message);
}
else
{
Console.WriteLine("{0} you can't send yourself a message!", from);
}
}
}
public override void SendMany(string from, List<Participant> to, string message)
{
foreach (Participant entry in to)
{
//to replace the [to] argument with participants[entry.Name]
Participant participant = participants[entry.Name];
if (participant != null && participants[from] != participants[entry.Name])
{
participant.Receive(from, message);
}
else
{
Console.WriteLine("{0} you can't send yourself a message!", from);
}
}
}
}
}