15
15
// a commercial license, send an email to [email protected] .
16
16
//
17
17
18
- // discovery is a library for handling the Arduino Pluggable-Discovery protocol
18
+ // Package discovery is a library for handling the Arduino Pluggable-Discovery protocol
19
19
// (https://github.com/arduino/tooling-rfcs/blob/main/RFCs/0002-pluggable-discovery.md#pluggable-discovery-api-via-stdinstdout)
20
20
//
21
21
// The library implements the state machine and the parsing logic to communicate with a pluggable-discovery client.
@@ -81,9 +81,9 @@ type EventCallback func(event string, port *Port)
81
81
// performs a STOP+START_SYNC cycle.
82
82
type ErrorCallback func (err string )
83
83
84
- // A DiscoveryServer is a pluggable discovery protocol handler,
85
- // it must be created using the NewDiscoveryServer function.
86
- type DiscoveryServer struct {
84
+ // A Server is a pluggable discovery protocol handler,
85
+ // it must be created using the NewServer function.
86
+ type Server struct {
87
87
impl Discovery
88
88
outputChan chan * message
89
89
userAgent string
@@ -95,11 +95,11 @@ type DiscoveryServer struct {
95
95
cachedErr string
96
96
}
97
97
98
- // NewDiscoveryServer creates a new discovery server backed by the
98
+ // NewServer creates a new discovery server backed by the
99
99
// provided pluggable discovery implementation. To start the server
100
100
// use the Run method.
101
- func NewDiscoveryServer (impl Discovery ) * DiscoveryServer {
102
- return & DiscoveryServer {
101
+ func NewServer (impl Discovery ) * Server {
102
+ return & Server {
103
103
impl : impl ,
104
104
outputChan : make (chan * message ),
105
105
}
@@ -110,7 +110,7 @@ func NewDiscoveryServer(impl Discovery) *DiscoveryServer {
110
110
// The function blocks until the `QUIT` command is received or
111
111
// the input stream is closed. In case of IO error the error is
112
112
// returned.
113
- func (d * DiscoveryServer ) Run (in io.Reader , out io.Writer ) error {
113
+ func (d * Server ) Run (in io.Reader , out io.Writer ) error {
114
114
go d .outputProcessor (out )
115
115
defer close (d .outputChan )
116
116
reader := bufio .NewReader (in )
@@ -150,7 +150,7 @@ func (d *DiscoveryServer) Run(in io.Reader, out io.Writer) error {
150
150
}
151
151
}
152
152
153
- func (d * DiscoveryServer ) hello (cmd string ) {
153
+ func (d * Server ) hello (cmd string ) {
154
154
if d .initialized {
155
155
d .outputChan <- messageError ("hello" , "HELLO already called" )
156
156
return
@@ -162,12 +162,12 @@ func (d *DiscoveryServer) hello(cmd string) {
162
162
return
163
163
}
164
164
d .userAgent = matches [2 ]
165
- if v , err := strconv .ParseInt (matches [1 ], 10 , 64 ); err != nil {
165
+ v , err := strconv .ParseInt (matches [1 ], 10 , 64 )
166
+ if err != nil {
166
167
d .outputChan <- messageError ("hello" , "Invalid protocol version: " + matches [2 ])
167
168
return
168
- } else {
169
- d .reqProtocolVersion = int (v )
170
169
}
170
+ d .reqProtocolVersion = int (v )
171
171
if err := d .impl .Hello (d .userAgent , 1 ); err != nil {
172
172
d .outputChan <- messageError ("hello" , err .Error ())
173
173
return
@@ -180,7 +180,7 @@ func (d *DiscoveryServer) hello(cmd string) {
180
180
d .initialized = true
181
181
}
182
182
183
- func (d * DiscoveryServer ) start () {
183
+ func (d * Server ) start () {
184
184
if d .started {
185
185
d .outputChan <- messageError ("start" , "Discovery already STARTed" )
186
186
return
@@ -199,7 +199,7 @@ func (d *DiscoveryServer) start() {
199
199
d .outputChan <- messageOk ("start" )
200
200
}
201
201
202
- func (d * DiscoveryServer ) eventCallback (event string , port * Port ) {
202
+ func (d * Server ) eventCallback (event string , port * Port ) {
203
203
id := port .Address + "|" + port .Protocol
204
204
if event == "add" {
205
205
d .cachedPorts [id ] = port
@@ -209,11 +209,11 @@ func (d *DiscoveryServer) eventCallback(event string, port *Port) {
209
209
}
210
210
}
211
211
212
- func (d * DiscoveryServer ) errorCallback (msg string ) {
212
+ func (d * Server ) errorCallback (msg string ) {
213
213
d .cachedErr = msg
214
214
}
215
215
216
- func (d * DiscoveryServer ) list () {
216
+ func (d * Server ) list () {
217
217
if ! d .started {
218
218
d .outputChan <- messageError ("list" , "Discovery not STARTed" )
219
219
return
@@ -236,7 +236,7 @@ func (d *DiscoveryServer) list() {
236
236
}
237
237
}
238
238
239
- func (d * DiscoveryServer ) startSync () {
239
+ func (d * Server ) startSync () {
240
240
if d .syncStarted {
241
241
d .outputChan <- messageError ("start_sync" , "Discovery already START_SYNCed" )
242
242
return
@@ -253,7 +253,7 @@ func (d *DiscoveryServer) startSync() {
253
253
d .outputChan <- messageOk ("start_sync" )
254
254
}
255
255
256
- func (d * DiscoveryServer ) stop () {
256
+ func (d * Server ) stop () {
257
257
if ! d .syncStarted && ! d .started {
258
258
d .outputChan <- messageError ("stop" , "Discovery already STOPped" )
259
259
return
@@ -269,18 +269,18 @@ func (d *DiscoveryServer) stop() {
269
269
d .outputChan <- messageOk ("stop" )
270
270
}
271
271
272
- func (d * DiscoveryServer ) syncEvent (event string , port * Port ) {
272
+ func (d * Server ) syncEvent (event string , port * Port ) {
273
273
d .outputChan <- & message {
274
274
EventType : event ,
275
275
Port : port ,
276
276
}
277
277
}
278
278
279
- func (d * DiscoveryServer ) errorEvent (msg string ) {
279
+ func (d * Server ) errorEvent (msg string ) {
280
280
d .outputChan <- messageError ("start_sync" , msg )
281
281
}
282
282
283
- func (d * DiscoveryServer ) outputProcessor (outWriter io.Writer ) {
283
+ func (d * Server ) outputProcessor (outWriter io.Writer ) {
284
284
// Start go routine to serialize messages printing
285
285
go func () {
286
286
for msg := range d .outputChan {
0 commit comments