-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOPackHandler.cs
More file actions
124 lines (107 loc) · 4.3 KB
/
IOPackHandler.cs
File metadata and controls
124 lines (107 loc) · 4.3 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System.Collections.Generic;
namespace NETLIB
{
/// <summary>
/// Better manage the incoming and outgoing <typeparamref name="TPack"/> using a <see cref="Protocol{TPack}"/>
/// to redistribute the packs. It has an internal dictionary of <see cref="Protocol{TPack}"/>
/// that can be exchanged for the currently used.
/// </summary>
/// <typeparam name="TPack">Pack class derived from <see cref="BasePack"/> that the IOPackHandler will manage.</typeparam>
public abstract class IOPackHandler<TPack> : Consumer<TPack> where TPack : BasePack
{
#region Variables
/// <summary>
/// Dictionary protocols that will store all protocols used by <see cref="IOPackHandler{TPack}"/>.
/// </summary>
/// <seealso cref="AddProtocol(Protocol{TPack}, bool)"/>
/// <seealso cref="ExchangeProtocol(string)"/>
Dictionary<string, Protocol<TPack>> protocols;
/// <summary>
/// Protocol currently used.
/// </summary>
/// <seealso cref="ExchangeProtocol(string)"/>
Protocol<TPack> currentProtocol;
#endregion
#region Constructor
/// <summary>
///
/// </summary>
/// <param name="publisher"></param>
public IOPackHandler(Publisher publisher)
: base(publisher)
{
this.protocols = new Dictionary<string, Protocol<TPack>>();
}
/// <summary>
/// Initializes the handler with a publisher who will publish the
/// packages and a protocol that will be used initially by this connection.
/// </summary>
/// <param name="publisher">Publisher who will publish the packages to be managed.</param>
/// <param name="initialProtocol">Initial <see cref="Protocol{TPack}"/> to be used by this connection.</param>
public IOPackHandler(Publisher publisher, Protocol<TPack> initialProtocol)
: base(publisher)
{
this.protocols = new Dictionary<string, Protocol<TPack>>();
this.protocols.Add(initialProtocol.Name, initialProtocol);
this.currentProtocol = initialProtocol;
}
#endregion
#region Attributes
/// <summary>
/// Name of the current protocol.
/// </summary>
public string CurrentProtocolName
{
get { return currentProtocol?.Name; }
}
#endregion
#region Methods
/// <summary>
/// Add a new protocol in the protocols dictionary.
/// </summary>
/// <param name="newProtocol">Protocol to be added to the dictionary.</param>
/// <param name="exchage">Exchange current protocol.</param>
/// <seealso cref="protocols"/>
public void AddProtocol(Protocol<TPack> newProtocol, bool exchage = false)
{
protocols.Add(newProtocol.Name, newProtocol);
if (exchage)
{
currentProtocol = newProtocol;
}
}
/// <summary>
/// Change the current protocol for one stored in the dictionary.
/// </summary>
/// <param name="protocolName">Protocol name to be used.</param>
/// <exception cref="KeyNotFoundException">
/// Throws when the required protocol does not exist in the dictionary.
/// </exception>
public void ExchangeProtocol(string protocolName)
{
currentProtocol = protocols[protocolName];
}
/// <summary>
/// Removes a protocol from the protocol dictionary.
/// </summary>
/// <param name="protocolName">Name of the protocol to be removed.</param>
public void RemoveProtocol(string protocolName)
{
if (currentProtocol.Name == protocolName)
{
currentProtocol = null;
}
protocols.Remove(protocolName);
}
/// <summary>
///
/// </summary>
/// <param name="pack"></param>
protected override void OnReceivedPackCall(TPack pack)
{
currentProtocol?.OnReceivedPackCall(this, pack);
base.OnReceivedPackCall(pack);
}
#endregion
}
}