Skip to content

Commit 246e9e6

Browse files
committed
Use github.com/brutella/dnssd
So we have "Remove" callback for free Something like "reachability filter" in Java IDE could be advisable for boards declaring "fake" TTL
1 parent 568eb88 commit 246e9e6

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

main.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import (
2222
"encoding/json"
2323
"fmt"
2424
"os"
25+
"strconv"
2526
"strings"
2627
"sync"
2728

2829
properties "github.com/arduino/go-properties-orderedmap"
29-
"github.com/oleksandr/bonjour"
30+
"github.com/brutella/dnssd"
3031
)
3132

3233
func main() {
@@ -108,19 +109,30 @@ func outputList() {
108109
*/
109110
}
110111

111-
func newBoardPortJSON(port *bonjour.ServiceEntry) *boardPortJSON {
112+
func newBoardPortJSON(port *dnssd.Service) *boardPortJSON {
112113
prefs := properties.NewMap()
113114
identificationPrefs := properties.NewMap()
115+
116+
ip := "127.0.0.1"
117+
118+
if len(port.IPs) > 0 {
119+
ip = port.IPs[0].String()
120+
}
121+
114122
portJSON := &boardPortJSON{
115-
Address: port.AddrIPv4.String(),
116-
Label: port.AddrIPv4.String(),
123+
Address: ip,
124+
Label: ip,
117125
Protocol: "network",
118126
ProtocolLabel: "Network Port",
119127
Prefs: prefs,
120128
IdentificationPrefs: identificationPrefs,
121129
}
122-
portJSON.Prefs.Set("hostname", port.HostName)
123-
portJSON.Prefs.Set("text", port.Text[0])
130+
portJSON.Prefs.Set("ttl", port.Ttl.String())
131+
portJSON.Prefs.Set("hostname", port.Hostname())
132+
portJSON.Prefs.Set("port", strconv.Itoa(port.Port))
133+
for key, value := range port.Text {
134+
portJSON.Prefs.Set(key, value)
135+
}
124136
return portJSON
125137
}
126138

sync.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
package main
1919

2020
import (
21+
"context"
2122
"encoding/json"
22-
"fmt"
23-
"log"
2423

25-
"github.com/oleksandr/bonjour"
24+
"github.com/brutella/dnssd"
2625
)
2726

27+
var service = "_arduino._tcp.local."
28+
2829
type syncOutputJSON struct {
2930
EventType string `json:"eventType"`
3031
Port *boardPortJSON `json:"port"`
@@ -43,32 +44,24 @@ func startSync() (chan<- bool, error) {
4344

4445
closeChan := make(chan bool)
4546

46-
resolver, err := bonjour.NewResolver(nil)
47-
if err != nil {
48-
log.Println("Failed to initialize resolver:", err.Error())
49-
return nil, err
47+
addFn := func(srv dnssd.Service) {
48+
outputSyncMessage(&syncOutputJSON{
49+
EventType: "add",
50+
Port: newBoardPortJSON(&srv),
51+
})
5052
}
5153

52-
results := make(chan *bonjour.ServiceEntry)
53-
54-
go func(results chan *bonjour.ServiceEntry, exitCh chan<- bool) {
55-
for {
56-
for e := range results {
57-
log.Printf("%+v", e)
58-
if e.AddrIPv4 != nil {
59-
fmt.Println(e)
60-
}
61-
}
62-
}
63-
}(results, resolver.Exit)
54+
remFn := func(srv dnssd.Service) {
55+
outputSyncMessage(&syncOutputJSON{
56+
EventType: "remove",
57+
Port: newBoardPortJSON(&srv),
58+
})
59+
}
6460

65-
// Sample output
66-
//2018/12/12 18:05:14 &{ServiceRecord:{Instance:Arduino Service:_arduino._tcp Domain:local serviceName: serviceInstanceName: serviceTypeName:} HostName:Arduino.local. Port:65280 Text:[ssh_upload=no tcp_check=no auth_upload=yes board=uno2018] TTL:120 AddrIPv4:10.130.22.247 AddrIPv6:<nil>}
67-
//&{{Arduino _arduino._tcp local } Arduino.local. 65280 [ssh_upload=no tcp_check=no auth_upload=yes board=uno2018] 120 10.130.22.247 <nil>}
61+
ctx, cancel := context.WithCancel(context.Background())
62+
defer cancel()
6863

69-
err = resolver.Browse("_arduino._tcp", "", results)
70-
if err != nil {
71-
log.Println("Failed to browse:", err.Error())
64+
if err := dnssd.LookupType(ctx, service, addFn, remFn); err != nil {
7265
return nil, err
7366
}
7467

0 commit comments

Comments
 (0)