Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EndpointStatistics.cs
1using System;
2using System.Collections.Generic;
3
5{
9 public class EndpointStatistics
10 {
11 private readonly List<IEndpoint> endpoints = new List<IEndpoint>();
12 private readonly object synchObj = new object();
13 private readonly string name;
14 private readonly string type;
15 private DateTime lastConnect = DateTime.MinValue;
16 private DateTime lastDisconnect = DateTime.MinValue;
17 private long sumConnectionTimeMilliseconds = 0;
18 private long nrConnectionTimes = 0;
19 private volatile int connectionCount = 0;
20
26 public EndpointStatistics(string Name, string Type)
27 {
28 this.name = Name;
29 this.type = Type;
30 }
31
37 {
38 lock (this.synchObj)
39 {
40 if (!this.endpoints.Contains(Endpoint))
41 {
42 if (this.connectionCount == 0)
43 this.lastConnect = DateTime.Now;
44
45 this.endpoints.Add(Endpoint);
46 this.connectionCount++;
47 }
48 }
49 }
50
56 public void EndpointDisconnected(IEndpoint Endpoint, long ConnectionTimeMilliseconds)
57 {
58 lock (this.synchObj)
59 {
60 if (this.endpoints.Remove(Endpoint))
61 {
62 this.sumConnectionTimeMilliseconds += ConnectionTimeMilliseconds;
63 this.nrConnectionTimes++;
64
65 if (this.connectionCount > 0)
66 {
67 this.connectionCount--;
68 if (this.connectionCount == 0)
69 this.lastDisconnect = DateTime.Now;
70 }
71 }
72 }
73 }
74
78 public string Name => this.name;
79
83 public string Type => this.type;
84
89 {
90 get
91 {
92 lock (this.synchObj)
93 {
94 return this.endpoints.ToArray();
95 }
96 }
97 }
98
103 {
104 get
105 {
106 lock (this.synchObj)
107 {
108 if (this.endpoints.Count == 0)
109 return null;
110 else
111 return this.endpoints[0];
112 }
113 }
114 }
115
119 public DateTime LastConnect => this.lastConnect;
120
124 public DateTime LastDisconnect => this.lastDisconnect;
125
129 public int ActiveConnectionCount => this.connectionCount;
130
134 public bool Connected => this.connectionCount > 0;
135
140 {
141 get
142 {
143 lock (this.synchObj)
144 {
145 return this.nrConnectionTimes + this.connectionCount;
146 }
147 }
148 }
149
154 {
155 get
156 {
157 lock (this.synchObj)
158 {
159 if (this.nrConnectionTimes == 0)
160 return null;
161
162 double x = this.sumConnectionTimeMilliseconds;
163 x /= this.nrConnectionTimes;
164 x /= 1000;
165
166 return x;
167 }
168 }
169 }
170
174 public string AvgConnectionTime
175 {
176 get
177 {
178 double? d = this.AvgConnectionTimeSeconds;
179 if (!d.HasValue)
180 return string.Empty;
181
182 return ToStr(d.Value);
183 }
184 }
185
186 private static string ToStr(double x)
187 {
188 if (x < 60)
189 return x.ToString("F0") + " s";
190 else
191 {
192 x /= 60;
193
194 if (x < 60)
195 return x.ToString("F1") + " min";
196 else
197 {
198 x /= 60;
199
200 if (x < 24)
201 return x.ToString("F1") + " h";
202 else
203 {
204 x /= 24;
205 return x.ToString("F1") + " days";
206 }
207 }
208 }
209 }
210
215 {
216 get
217 {
218 lock (this.synchObj)
219 {
220 if (this.connectionCount == 0)
221 return null;
222
223 return (DateTime.Now - this.lastConnect).TotalSeconds;
224 }
225 }
226 }
227
232 {
233 get
234 {
235 double? d = this.CurrentConnectionTimeSeconds;
236 if (!d.HasValue)
237 return string.Empty;
238
239 return ToStr(d.Value);
240 }
241 }
242
243 }
244}
Mainstains information about connectivity from a specific endpoint.
double? AvgConnectionTimeSeconds
Average connection time, in seconds, if available, null otherwise.
void EndpointConnected(IEndpoint Endpoint)
Call this method when the endpoint connects.
IEndpoint Endpoint
First registered active endpoint, if any, null otherwise.
string CurrentConnectionTime
Average connection time, as a string.
DateTime LastDisconnect
When endpoint last disconnected.
void EndpointDisconnected(IEndpoint Endpoint, long ConnectionTimeMilliseconds)
Call this method when the endpoint disconnects.
long TotalConnectionCount
Total number of connections made by endpoint.
EndpointStatistics(string Name, string Type)
Mainstains information about connectivity from a specific endpoint.
int ActiveConnectionCount
Current number of active connections
string AvgConnectionTime
Average connection time, as a string.
double? CurrentConnectionTimeSeconds
Current connection time, in seconds, if available, null otherwise.
IEndpoint[] Endpoints
Registered connection endpoints.
Interface for XMPP endpoints
Definition: IEndpoint.cs:10