Skip to content

Commit a6b143e

Browse files
committed
Reworked sync process to query from time to time
1 parent 1eb4fb7 commit a6b143e

File tree

3 files changed

+79
-55
lines changed

3 files changed

+79
-55
lines changed

go.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,5 @@ require (
66
github.com/arduino/go-paths-helper v1.6.1 // indirect
77
github.com/arduino/go-properties-orderedmap v1.5.0
88
github.com/arduino/pluggable-discovery-protocol-handler v1.2.0
9-
github.com/brutella/dnssd v1.1.1
10-
github.com/miekg/dns v1.1.31 // indirect
11-
golang.org/x/crypto v0.0.0-20201002094018-c90954cbb977 // indirect
12-
golang.org/x/net v0.0.0-20200930145003-4acb6c075d10 // indirect
13-
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f // indirect
9+
github.com/hashicorp/mdns v1.0.4
1410
)

go.sum

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,29 @@ github.com/arduino/go-properties-orderedmap v1.5.0 h1:istmr13qQN3nneuU3lsqlMvI6j
66
github.com/arduino/go-properties-orderedmap v1.5.0/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
77
github.com/arduino/pluggable-discovery-protocol-handler v1.2.0 h1:gw6W8CtgGc+kh+DKfh+z6cUVPqaZh9Tu3XCt/uGgJUE=
88
github.com/arduino/pluggable-discovery-protocol-handler v1.2.0/go.mod h1:vQfYGJnunfcscLoUcZKqJBlEkZ/qiE28TQj+RV9UT74=
9-
github.com/brutella/dnssd v1.1.1 h1:Ar5ytE2Z9x5DTmuNnASlMTBpcQWQLm9ceHb326s0ykg=
10-
github.com/brutella/dnssd v1.1.1/go.mod h1:9gIcMKQSJvYlO2x+HR50cqqjghb9IWK9hvykmyveVVs=
119
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
1210
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13-
github.com/miekg/dns v1.1.1/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
14-
github.com/miekg/dns v1.1.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo=
15-
github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
11+
github.com/hashicorp/mdns v1.0.4 h1:sY0CMhFmjIPDMlTB+HfymFHCaYLhgifZ0QhjaYKD/UQ=
12+
github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/YAJqrc=
13+
github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY=
14+
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
1615
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1716
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1817
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1918
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2019
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2120
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
2221
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
23-
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
24-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
25-
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
26-
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
27-
golang.org/x/crypto v0.0.0-20201002094018-c90954cbb977 h1:yH6opeNE+0SY+7pXT4gclZUoKHogXeC2EvOSHGOMGPU=
28-
golang.org/x/crypto v0.0.0-20201002094018-c90954cbb977/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
29-
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
30-
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
31-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
32-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
33-
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
34-
golang.org/x/net v0.0.0-20200930145003-4acb6c075d10 h1:YfxMZzv3PjGonQYNUaeU2+DhAdqOxerQ30JFB6WgAXo=
35-
golang.org/x/net v0.0.0-20200930145003-4acb6c075d10/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
36-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
37-
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
38-
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
39-
golang.org/x/sys v0.0.0-20181206074257-70b957f3b65e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
40-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
41-
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
42-
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
43-
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
44-
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
45-
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
46-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
47-
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
48-
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
22+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
23+
golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1 h1:4qWs8cYYH6PoEFy4dfhDFgoMGkwAcETd+MmPdCPMzUc=
24+
golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8=
25+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
26+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
27+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
28+
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
29+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
30+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
31+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
32+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
33+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
34+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

main.go

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ package main
2020
import (
2121
"context"
2222
"fmt"
23+
"io/ioutil"
24+
"log"
2325
"os"
2426
"strconv"
27+
"strings"
28+
"time"
2529

2630
properties "github.com/arduino/go-properties-orderedmap"
2731
discovery "github.com/arduino/pluggable-discovery-protocol-handler"
28-
"github.com/brutella/dnssd"
32+
"github.com/hashicorp/mdns"
2933
)
3034

3135
func main() {
@@ -39,15 +43,18 @@ func main() {
3943

4044
}
4145

42-
const mdnsServiceName = "_arduino._tcp.local."
46+
const mdnsServiceName = "_arduino._tcp"
4347

4448
// MDNSDiscovery is the implementation of the network pluggable-discovery
4549
type MDNSDiscovery struct {
46-
cancelFunc func()
50+
cancelFunc func()
51+
entriesChan chan *mdns.ServiceEntry
4752
}
4853

4954
// Hello handles the pluggable-discovery HELLO command
5055
func (d *MDNSDiscovery) Hello(userAgent string, protocolVersion int) error {
56+
// The mdns library used has some logs statement that we must disable
57+
log.SetOutput(ioutil.Discard)
5158
return nil
5259
}
5360

@@ -57,52 +64,87 @@ func (d *MDNSDiscovery) Stop() error {
5764
d.cancelFunc()
5865
d.cancelFunc = nil
5966
}
67+
if d.entriesChan != nil {
68+
close(d.entriesChan)
69+
d.entriesChan = nil
70+
}
6071
return nil
6172
}
6273

6374
// Quit handles the pluggable-discovery QUIT command
6475
func (d *MDNSDiscovery) Quit() {
76+
close(d.entriesChan)
6577
}
6678

6779
// StartSync handles the pluggable-discovery START_SYNC command
6880
func (d *MDNSDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error {
69-
addFn := func(srv dnssd.Service) {
70-
eventCB("add", newBoardPortJSON(&srv))
81+
if d.entriesChan != nil {
82+
return fmt.Errorf("already syncing")
7183
}
72-
remFn := func(srv dnssd.Service) {
73-
eventCB("remove", newBoardPortJSON(&srv))
84+
85+
d.entriesChan = make(chan *mdns.ServiceEntry, 4)
86+
var receiver <-chan *mdns.ServiceEntry = d.entriesChan
87+
var sender chan<- *mdns.ServiceEntry = d.entriesChan
88+
89+
go func() {
90+
for entry := range receiver {
91+
eventCB("add", toDiscoveryPort(entry))
92+
}
93+
}()
94+
95+
params := &mdns.QueryParam{
96+
Service: mdnsServiceName,
97+
Domain: "local",
98+
Timeout: time.Second * 15,
99+
Entries: sender,
100+
WantUnicastResponse: false,
74101
}
75-
ctx, cancel := context.WithCancel(context.Background())
76102

103+
ctx, cancel := context.WithCancel(context.Background())
77104
go func() {
78-
if err := dnssd.LookupType(ctx, mdnsServiceName, addFn, remFn); err != nil {
79-
errorCB("mdns lookup error: " + err.Error())
105+
for {
106+
if err := mdns.Query(params); err != nil {
107+
errorCB("mdns lookup error: " + err.Error())
108+
}
109+
select {
110+
default:
111+
case <-ctx.Done():
112+
return
113+
}
80114
}
81115
}()
82116
d.cancelFunc = cancel
83117
return nil
84118
}
85119

86-
func newBoardPortJSON(port *dnssd.Service) *discovery.Port {
87-
ip := "127.0.0.1"
88-
if len(port.IPs) > 0 {
89-
ip = port.IPs[0].String()
120+
func toDiscoveryPort(entry *mdns.ServiceEntry) *discovery.Port {
121+
ip := ""
122+
if len(entry.AddrV4) > 0 {
123+
ip = entry.AddrV4.String()
124+
} else if len(entry.AddrV6) > 0 {
125+
ip = entry.AddrV6.String()
90126
}
91127

92128
props := properties.NewMap()
93-
props.Set("ttl", strconv.Itoa(int(port.TTL.Seconds())))
94-
props.Set("hostname", port.Hostname())
95-
props.Set("port", strconv.Itoa(port.Port))
96-
for key, value := range port.Text {
129+
props.Set("hostname", entry.Host)
130+
props.Set("port", strconv.Itoa(entry.Port))
131+
132+
for _, field := range entry.InfoFields {
133+
split := strings.Split(field, "=")
134+
if len(split) != 2 {
135+
continue
136+
}
137+
key, value := split[0], split[1]
97138
props.Set(key, value)
98139
if key == "board" {
99140
// duplicate for backwards compatibility
100141
props.Set(".", value)
101142
}
102143
}
144+
103145
return &discovery.Port{
104146
Address: ip,
105-
AddressLabel: port.Name + " at " + ip,
147+
AddressLabel: fmt.Sprintf("%s at %s", entry.Name, ip),
106148
Protocol: "network",
107149
ProtocolLabel: "Network Port",
108150
Properties: props,

0 commit comments

Comments
 (0)