-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathCommandStatus.cs
More file actions
103 lines (91 loc) · 3.1 KB
/
CommandStatus.cs
File metadata and controls
103 lines (91 loc) · 3.1 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
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Waher.Events;
namespace Waher.Networking.Cluster
{
/// <summary>
/// Keeps track of an outgoing command.
/// </summary>
internal class CommandStatus<ResponseType> : CommandStatusBase
{
public Dictionary<IPEndPoint, KeyValuePair<ResponseType, Exception>> Responses = new Dictionary<IPEndPoint, KeyValuePair<ResponseType, Exception>>();
public EventHandlerAsync<ClusterResponseEventArgs<ResponseType>> Callback;
/// <summary>
/// If all responses have been returned.
/// </summary>
/// <param name="Statuses">Valid statuses.</param>
/// <returns>If all responses have been returned.</returns>
public override bool IsComplete(EndpointStatus[] Statuses)
{
lock (this.Responses)
{
foreach (EndpointStatus Status in Statuses)
{
if (!this.Responses.ContainsKey(Status.Endpoint))
return false;
}
}
return true;
}
/// <summary>
/// Compiles available responses.
/// </summary>
/// <returns>Set of responses</returns>
public EndpointResponse<ResponseType>[] GetResponses(EndpointStatus[] Statuses)
{
EndpointResponse<ResponseType>[] Result;
int i, c;
lock (this.Responses)
{
foreach (EndpointStatus Status in Statuses)
{
if (!this.Responses.ContainsKey(Status.Endpoint))
this.Responses[Status.Endpoint] = new KeyValuePair<ResponseType, Exception>(default, new TimeoutException("No response returned."));
}
Result = new EndpointResponse<ResponseType>[c = this.Responses.Count];
i = 0;
foreach (KeyValuePair<IPEndPoint, KeyValuePair<ResponseType, Exception>> P in this.Responses)
Result[i++] = new EndpointResponse<ResponseType>(P.Key, P.Value.Key, P.Value.Value);
}
return Result;
}
/// <summary>
/// Adds a response from an endpoint.
/// </summary>
/// <param name="From">Endpoint providing response</param>
/// <param name="Response">Response object</param>
public override void AddResponse(IPEndPoint From, object Response)
{
lock (this.Responses)
{
if (Response is ResponseType Response2)
this.Responses[From] = new KeyValuePair<ResponseType, Exception>(Response2, null);
else
this.Responses[From] = new KeyValuePair<ResponseType, Exception>(default, new Exception("Unexpected response returned."));
}
}
/// <summary>
/// Adds an error from an endpoint.
/// </summary>
/// <param name="From">Endpoint providing response</param>
/// <param name="Error">Exception object describing the error.</param>
public override void AddError(IPEndPoint From, Exception Error)
{
lock (this.Responses)
{
this.Responses[From] = new KeyValuePair<ResponseType, Exception>(default, Error);
}
}
/// <summary>
/// Raises the response event.
/// </summary>
/// <param name="CurrentStatus">Current status of endpoints in cluster.</param>
public override Task RaiseResponseEvent(EndpointStatus[] CurrentStatus)
{
return this.Callback.Raise(this, new ClusterResponseEventArgs<ResponseType>(
this.Command, this.GetResponses(CurrentStatus), this.State));
}
}
}