-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdns.go
49 lines (38 loc) · 1.33 KB
/
mdns.go
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
package ship
import (
"context"
"log"
"strconv"
"time"
"github.com/LMF-DHBW/go-eebus/resources"
"github.com/grandcat/zeroconf"
)
func (shipNode *ShipNode) BrowseDns() {
log.Println("Browsing for entries")
// Discover all ship services on the network
resolver, err := zeroconf.NewResolver(nil)
resources.CheckError(err)
entries := make(chan *zeroconf.ServiceEntry)
go func(results <-chan *zeroconf.ServiceEntry) {
for entry := range results {
go shipNode.handleFoundService(entry)
}
}(entries)
ctx, _ := context.WithCancel(context.Background())
err = resolver.Browse(ctx, "_ship._tcp", "local.", entries)
resources.CheckError(err)
<-ctx.Done()
}
func (shipNode *ShipNode) RegisterDns() {
// Define values for DNS entry
port := strconv.Itoa(shipNode.serverPort)
id := "DEVICE-EEB01-" + shipNode.devId + ".local."
txtRecord := []string{"txtvers=1", "id=" + id, "path=wss://" + shipNode.hostname + ":" + port, "SKI=" + shipNode.getSki(), "register=true", "brand=" + shipNode.brand, "type=" + shipNode.devType}
log.Println("Registering: ", txtRecord)
server, err := zeroconf.Register("Device "+port, "_ship._tcp", "local.", shipNode.serverPort, txtRecord, nil)
resources.CheckError(err)
defer server.Shutdown()
defer log.Println("Registering stopped")
// Shutdown server after 2 minutes
<-time.After(time.Second * 120)
}