-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerList.cs
52 lines (44 loc) · 1.63 KB
/
ServerList.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
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
/// <summary>
/// Manages all informations for registered servers.
/// </summary>
public class ServerList {
int _serverTimeoutMinutes;
Dictionary<int, ServerDetails> _server;
public Dictionary<int, ServerDetails> server {
get { return _server; }
}
/// <param name="serverTimeoutMinutes">Servers will be removed N minutes after last update.</param>
/// <see cref="ServerController.Put(Unosquare.Labs.EmbedIO.WebServer, Unosquare.Net.HttpListenerContext)"/>
public ServerList(int serverTimeoutMinutes) {
_serverTimeoutMinutes = serverTimeoutMinutes;
_server = new Dictionary<int, ServerDetails>();
}
/// <summary>
/// Add a new server or update existing informations
/// </summary>
/// <param name="info">Details about server to add/update</param>
public void Add(ServerDetails info) {
_server[info.id] = info;
}
public ServerDetails Get(int serverId) {
ServerDetails info;
if (!_server.TryGetValue(serverId, out info))
return null;
return info;
}
/// <summary>
/// Removes old servers.
/// </summary>
/// <remarks>Minutes since server</remarks>
/// <see cref="ServerList(int)"/>
public void RemoveOldServers() {
var itemsToRemove = _server.Where(entry => (DateTime.Now - entry.Value.lastUpdated).TotalMinutes >= _serverTimeoutMinutes).ToArray();
foreach (var item in itemsToRemove) {
_server.Remove(item.Key);
}
}
}