|
1 | 1 | //
|
2 |
| -// This file is part of serial-discovery. |
| 2 | +// This file is part of mdns-discovery. |
3 | 3 | //
|
4 |
| -// Copyright 2018 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// Copyright 2018-2021 ARDUINO SA (http://www.arduino.cc/) |
5 | 5 | //
|
6 | 6 | // This software is released under the GNU General Public License version 3,
|
7 | 7 | // which covers the main part of arduino-cli.
|
|
18 | 18 | package main
|
19 | 19 |
|
20 | 20 | import (
|
21 |
| - "bufio" |
22 |
| - "encoding/json" |
| 21 | + "context" |
23 | 22 | "fmt"
|
24 | 23 | "os"
|
25 | 24 | "strconv"
|
26 |
| - "strings" |
27 |
| - "sync" |
28 | 25 |
|
29 | 26 | properties "github.com/arduino/go-properties-orderedmap"
|
| 27 | + discovery "github.com/arduino/pluggable-discovery-protocol-handler" |
30 | 28 | "github.com/brutella/dnssd"
|
31 | 29 | )
|
32 | 30 |
|
33 | 31 | func main() {
|
34 |
| - syncStarted := false |
35 |
| - var syncCloseChan chan<- bool |
36 |
| - |
37 |
| - reader := bufio.NewReader(os.Stdin) |
38 |
| - for { |
39 |
| - cmd, err := reader.ReadString('\n') |
40 |
| - if err != nil { |
41 |
| - outputError(err) |
42 |
| - os.Exit(1) |
43 |
| - } |
44 |
| - cmd = strings.ToUpper(strings.TrimSpace(cmd)) |
45 |
| - switch cmd { |
46 |
| - case "START": |
47 |
| - outputMessage("start", "OK") |
48 |
| - case "STOP": |
49 |
| - if syncStarted { |
50 |
| - syncCloseChan <- true |
51 |
| - syncStarted = false |
52 |
| - } |
53 |
| - outputMessage("stop", "OK") |
54 |
| - case "LIST": |
55 |
| - outputList() |
56 |
| - case "QUIT": |
57 |
| - outputMessage("quit", "OK") |
58 |
| - os.Exit(0) |
59 |
| - case "START_SYNC": |
60 |
| - if syncStarted { |
61 |
| - outputMessage("startSync", "OK") |
62 |
| - } else if close, err := startSync(); err != nil { |
63 |
| - outputError(err) |
64 |
| - } else { |
65 |
| - syncCloseChan = close |
66 |
| - syncStarted = true |
67 |
| - } |
68 |
| - default: |
69 |
| - outputError(fmt.Errorf("Command %s not supported", cmd)) |
70 |
| - } |
| 32 | + parseArgs() |
| 33 | + mdnsDiscovery := &MDNSDiscovery{} |
| 34 | + disc := discovery.NewDiscoveryServer(mdnsDiscovery) |
| 35 | + if err := disc.Run(os.Stdin, os.Stdout); err != nil { |
| 36 | + fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) |
| 37 | + os.Exit(1) |
71 | 38 | }
|
| 39 | + |
72 | 40 | }
|
73 | 41 |
|
74 |
| -type boardPortJSON struct { |
75 |
| - Address string `json:"address"` |
76 |
| - Label string `json:"label,omitempty"` |
77 |
| - Prefs *properties.Map `json:"prefs,omitempty"` |
78 |
| - IdentificationPrefs *properties.Map `json:"identificationPrefs,omitempty"` |
79 |
| - Protocol string `json:"protocol,omitempty"` |
80 |
| - ProtocolLabel string `json:"protocolLabel,omitempty"` |
| 42 | +const mdnsServiceName = "_arduino._tcp.local." |
| 43 | + |
| 44 | +type MDNSDiscovery struct { |
| 45 | + cancelFunc func() |
81 | 46 | }
|
82 | 47 |
|
83 |
| -type listOutputJSON struct { |
84 |
| - EventType string `json:"eventType"` |
85 |
| - Ports []*boardPortJSON `json:"ports"` |
| 48 | +// Hello handles the pluggable-discovery HELLO command |
| 49 | +func (d *MDNSDiscovery) Hello(userAgent string, protocolVersion int) error { |
| 50 | + return nil |
86 | 51 | }
|
87 | 52 |
|
88 |
| -func outputList() { |
89 |
| - /* |
90 |
| - list, err := enumerator.GetDetailedPortsList() |
91 |
| - if err != nil { |
92 |
| - outputError(err) |
93 |
| - return |
94 |
| - } |
95 |
| - portsJSON := []*boardPortJSON{} |
96 |
| - for _, port := range list { |
97 |
| - portJSON := newBoardPortJSON(port) |
98 |
| - portsJSON = append(portsJSON, portJSON) |
99 |
| - } |
100 |
| - d, err := json.MarshalIndent(&listOutputJSON{ |
101 |
| - EventType: "list", |
102 |
| - Ports: portsJSON, |
103 |
| - }, "", " ") |
104 |
| - if err != nil { |
105 |
| - outputError(err) |
106 |
| - return |
107 |
| - } |
108 |
| - syncronizedPrintLn(string(d)) |
109 |
| - */ |
| 53 | +// Start handles the pluggable-discovery START command |
| 54 | +func (d *MDNSDiscovery) Start() error { |
| 55 | + return nil |
110 | 56 | }
|
111 | 57 |
|
112 |
| -func newBoardPortJSON(port *dnssd.Service) *boardPortJSON { |
113 |
| - prefs := properties.NewMap() |
114 |
| - identificationPrefs := properties.NewMap() |
| 58 | +// Stop handles the pluggable-discovery STOP command |
| 59 | +func (d *MDNSDiscovery) Stop() error { |
| 60 | + if d.cancelFunc != nil { |
| 61 | + d.cancelFunc() |
| 62 | + d.cancelFunc = nil |
| 63 | + } |
| 64 | + return nil |
| 65 | +} |
115 | 66 |
|
116 |
| - ip := "127.0.0.1" |
| 67 | +// Quit handles the pluggable-discovery QUIT command |
| 68 | +func (d *MDNSDiscovery) Quit() { |
| 69 | +} |
| 70 | + |
| 71 | +// List handles the pluggable-discovery LIST command |
| 72 | +func (d *MDNSDiscovery) List() ([]*discovery.Port, error) { |
| 73 | + return []*discovery.Port{}, nil |
| 74 | +} |
| 75 | + |
| 76 | +// StartSync handles the pluggable-discovery START_SYNC command |
| 77 | +func (d *MDNSDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error { |
| 78 | + addFn := func(srv dnssd.Service) { |
| 79 | + eventCB("add", newBoardPortJSON(&srv)) |
| 80 | + } |
| 81 | + remFn := func(srv dnssd.Service) { |
| 82 | + eventCB("remove", newBoardPortJSON(&srv)) |
| 83 | + } |
| 84 | + ctx, cancel := context.WithCancel(context.Background()) |
| 85 | + |
| 86 | + go func() { |
| 87 | + if err := dnssd.LookupType(ctx, mdnsServiceName, addFn, remFn); err != nil { |
| 88 | + errorCB("mdns lookup error: " + err.Error()) |
| 89 | + } |
| 90 | + }() |
| 91 | + d.cancelFunc = cancel |
| 92 | + return nil |
| 93 | +} |
117 | 94 |
|
| 95 | +func newBoardPortJSON(port *dnssd.Service) *discovery.Port { |
| 96 | + ip := "127.0.0.1" |
118 | 97 | if len(port.IPs) > 0 {
|
119 | 98 | ip = port.IPs[0].String()
|
120 | 99 | }
|
121 | 100 |
|
122 |
| - portJSON := &boardPortJSON{ |
123 |
| - Address: ip, |
124 |
| - Label: port.Name + " at " + ip, |
125 |
| - Protocol: "network", |
126 |
| - ProtocolLabel: "Network Port", |
127 |
| - Prefs: prefs, |
128 |
| - IdentificationPrefs: identificationPrefs, |
129 |
| - } |
130 |
| - portJSON.Prefs.Set("ttl", port.Ttl.String()) |
131 |
| - portJSON.Prefs.Set("hostname", port.Hostname()) |
132 |
| - portJSON.Prefs.Set("port", strconv.Itoa(port.Port)) |
| 101 | + props := properties.NewMap() |
| 102 | + props.Set("ttl", strconv.Itoa(int(port.TTL.Seconds()))) |
| 103 | + props.Set("hostname", port.Hostname()) |
| 104 | + props.Set("port", strconv.Itoa(port.Port)) |
133 | 105 | for key, value := range port.Text {
|
134 |
| - portJSON.Prefs.Set(key, value) |
| 106 | + props.Set(key, value) |
135 | 107 | if key == "board" {
|
136 | 108 | // duplicate for backwards compatibility
|
137 |
| - identificationPrefs.Set(".", value) |
| 109 | + props.Set(".", value) |
138 | 110 | }
|
139 | 111 | }
|
140 |
| - return portJSON |
141 |
| -} |
142 |
| - |
143 |
| -type messageOutputJSON struct { |
144 |
| - EventType string `json:"eventType"` |
145 |
| - Message string `json:"message"` |
146 |
| -} |
147 |
| - |
148 |
| -func outputMessage(eventType, message string) { |
149 |
| - d, err := json.MarshalIndent(&messageOutputJSON{ |
150 |
| - EventType: eventType, |
151 |
| - Message: message, |
152 |
| - }, "", " ") |
153 |
| - if err != nil { |
154 |
| - outputError(err) |
155 |
| - } else { |
156 |
| - syncronizedPrintLn(string(d)) |
| 112 | + return &discovery.Port{ |
| 113 | + Address: ip, |
| 114 | + AddressLabel: port.Name + " at " + ip, |
| 115 | + Protocol: "network", |
| 116 | + ProtocolLabel: "Network Port", |
| 117 | + Properties: props, |
157 | 118 | }
|
158 | 119 | }
|
159 |
| - |
160 |
| -func outputError(err error) { |
161 |
| - outputMessage("error", err.Error()) |
162 |
| -} |
163 |
| - |
164 |
| -var stdoutMutext sync.Mutex |
165 |
| - |
166 |
| -func syncronizedPrintLn(a ...interface{}) { |
167 |
| - stdoutMutext.Lock() |
168 |
| - fmt.Println(a...) |
169 |
| - stdoutMutext.Unlock() |
170 |
| -} |
0 commit comments