-
Notifications
You must be signed in to change notification settings - Fork 2
/
MDNSClient.cs
132 lines (115 loc) · 5.39 KB
/
MDNSClient.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
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
125
126
127
128
129
130
131
132
using EEBUS.Models;
using Makaretu.Dns;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace EEBUS
{
public class MDNSClient
{
private ConcurrentDictionary<ServerNode, DateTime> _currentEEBUSNodes = new ConcurrentDictionary<ServerNode, DateTime>();
public void Run()
{
_ = Task.Run(async() =>
{
Thread.CurrentThread.IsBackground = true;
MulticastService mdns = new MulticastService();
ServiceDiscovery sd = new ServiceDiscovery(mdns);
sd.ServiceDiscovered += (s, serviceName) => { mdns.SendQuery(serviceName); };
sd.ServiceInstanceDiscovered += Sd_ServiceInstanceDiscovered;
try
{
mdns.Start();
while (true)
{
sd.QueryAllServices();
// purge all records that are more than 1 hour old
foreach(KeyValuePair<ServerNode, DateTime> t in _currentEEBUSNodes)
{
if (t.Value < DateTime.UtcNow.Add(new TimeSpan(-1,0,0)))
{
_currentEEBUSNodes.TryRemove(t);
}
}
await Task.Delay(5000).ConfigureAwait(false);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sd.Dispose();
mdns.Stop();
}
});
}
public ServerNode[] getEEBUSNodes()
{
return _currentEEBUSNodes.Keys.ToArray();
}
private void Sd_ServiceInstanceDiscovered(object sender, ServiceInstanceDiscoveryEventArgs e)
{
if (e.ServiceInstanceName.ToString().Contains("._ship."))
{
Console.WriteLine($"EEBUS service instance '{e.ServiceInstanceName}' discovered.");
IEnumerable<SRVRecord> servers = e.Message.AdditionalRecords.OfType<SRVRecord>();
IEnumerable<AddressRecord> addresses = e.Message.AdditionalRecords.OfType<AddressRecord>();
IEnumerable<string> txtRecords = e.Message.AdditionalRecords.OfType<TXTRecord>()?.SelectMany(s => s.Strings);
if (servers?.Count() > 0 && addresses?.Count() > 0 && txtRecords?.Count() > 0)
{
foreach (SRVRecord server in servers)
{
IEnumerable<AddressRecord> serverAddresses = addresses.Where(w => w.Name == server.Target);
if (serverAddresses?.Count() > 0)
{
foreach (AddressRecord serverAddress in serverAddresses)
{
// we only want IPv4 addresses
if (serverAddress.Address.AddressFamily == AddressFamily.InterNetwork)
{
string id = string.Empty;
string path = string.Empty;
foreach (string textRecord in txtRecords)
{
if (textRecord.StartsWith("path"))
{
path = textRecord.Substring(textRecord.IndexOf('=') + 1);
}
if (textRecord.StartsWith("id"))
{
id = textRecord.Substring(textRecord.IndexOf('=') + 1);
}
}
if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(path))
{
ServerNode newNode = new ServerNode
{
Name = e.ServiceInstanceName.ToString(),
Url = serverAddress.Address.ToString() + ":" + server.Port.ToString() + path,
Id = id
};
if (_currentEEBUSNodes.ContainsKey(newNode))
{
_currentEEBUSNodes[newNode] = DateTime.UtcNow;
}
else
{
_currentEEBUSNodes.TryAdd(newNode, DateTime.UtcNow);
}
}
}
}
}
}
}
}
}
}
}