Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CommandStatus.cs
1using System;
2using System.Collections.Generic;
3using System.Net;
4using System.Threading.Tasks;
5using Waher.Events;
6
8{
12 internal class CommandStatus<ResponseType> : CommandStatusBase
13 {
14 public Dictionary<IPEndPoint, KeyValuePair<ResponseType, Exception>> Responses = new Dictionary<IPEndPoint, KeyValuePair<ResponseType, Exception>>();
15 public EventHandlerAsync<ClusterResponseEventArgs<ResponseType>> Callback;
16
22 public override bool IsComplete(EndpointStatus[] Statuses)
23 {
24 lock (this.Responses)
25 {
26 foreach (EndpointStatus Status in Statuses)
27 {
28 if (!this.Responses.ContainsKey(Status.Endpoint))
29 return false;
30 }
31 }
32
33 return true;
34 }
35
40 public EndpointResponse<ResponseType>[] GetResponses(EndpointStatus[] Statuses)
41 {
42 EndpointResponse<ResponseType>[] Result;
43 int i, c;
44
45 lock (this.Responses)
46 {
47 foreach (EndpointStatus Status in Statuses)
48 {
49 if (!this.Responses.ContainsKey(Status.Endpoint))
50 this.Responses[Status.Endpoint] = new KeyValuePair<ResponseType, Exception>(default, new TimeoutException("No response returned."));
51 }
52
53 Result = new EndpointResponse<ResponseType>[c = this.Responses.Count];
54
55 i = 0;
56 foreach (KeyValuePair<IPEndPoint, KeyValuePair<ResponseType, Exception>> P in this.Responses)
57 Result[i++] = new EndpointResponse<ResponseType>(P.Key, P.Value.Key, P.Value.Value);
58 }
59
60 return Result;
61 }
62
68 public override void AddResponse(IPEndPoint From, object Response)
69 {
70 lock (this.Responses)
71 {
72 if (Response is ResponseType Response2)
73 this.Responses[From] = new KeyValuePair<ResponseType, Exception>(Response2, null);
74 else
75 this.Responses[From] = new KeyValuePair<ResponseType, Exception>(default, new Exception("Unexpected response returned."));
76 }
77 }
78
84 public override void AddError(IPEndPoint From, Exception Error)
85 {
86 lock (this.Responses)
87 {
88 this.Responses[From] = new KeyValuePair<ResponseType, Exception>(default, Error);
89 }
90 }
91
96 public override Task RaiseResponseEvent(EndpointStatus[] CurrentStatus)
97 {
98 return this.Callback.Raise(this, new ClusterResponseEventArgs<ResponseType>(
99 this.Command, this.GetResponses(CurrentStatus), this.State));
100 }
101
102 }
103}