Skip to content

Commit ca285eb

Browse files
authored
Merge pull request #8 from arduino/scerza/fix-failing-checks
Fix failing go checks
2 parents 1d056da + 3d9cdf9 commit ca285eb

File tree

3 files changed

+50
-34
lines changed

3 files changed

+50
-34
lines changed

discovery_server.go renamed to discovery.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// a commercial license, send an email to [email protected].
1616
//
1717

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
1919
// (https://github.com/arduino/tooling-rfcs/blob/main/RFCs/0002-pluggable-discovery.md#pluggable-discovery-api-via-stdinstdout)
2020
//
2121
// 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)
8181
// performs a STOP+START_SYNC cycle.
8282
type ErrorCallback func(err string)
8383

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 {
8787
impl Discovery
8888
outputChan chan *message
8989
userAgent string
@@ -95,11 +95,11 @@ type DiscoveryServer struct {
9595
cachedErr string
9696
}
9797

98-
// NewDiscoveryServer creates a new discovery server backed by the
98+
// NewServer creates a new discovery server backed by the
9999
// provided pluggable discovery implementation. To start the server
100100
// use the Run method.
101-
func NewDiscoveryServer(impl Discovery) *DiscoveryServer {
102-
return &DiscoveryServer{
101+
func NewServer(impl Discovery) *Server {
102+
return &Server{
103103
impl: impl,
104104
outputChan: make(chan *message),
105105
}
@@ -110,7 +110,7 @@ func NewDiscoveryServer(impl Discovery) *DiscoveryServer {
110110
// The function blocks until the `QUIT` command is received or
111111
// the input stream is closed. In case of IO error the error is
112112
// 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 {
114114
go d.outputProcessor(out)
115115
defer close(d.outputChan)
116116
reader := bufio.NewReader(in)
@@ -150,7 +150,7 @@ func (d *DiscoveryServer) Run(in io.Reader, out io.Writer) error {
150150
}
151151
}
152152

153-
func (d *DiscoveryServer) hello(cmd string) {
153+
func (d *Server) hello(cmd string) {
154154
if d.initialized {
155155
d.outputChan <- messageError("hello", "HELLO already called")
156156
return
@@ -162,12 +162,12 @@ func (d *DiscoveryServer) hello(cmd string) {
162162
return
163163
}
164164
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 {
166167
d.outputChan <- messageError("hello", "Invalid protocol version: "+matches[2])
167168
return
168-
} else {
169-
d.reqProtocolVersion = int(v)
170169
}
170+
d.reqProtocolVersion = int(v)
171171
if err := d.impl.Hello(d.userAgent, 1); err != nil {
172172
d.outputChan <- messageError("hello", err.Error())
173173
return
@@ -180,7 +180,7 @@ func (d *DiscoveryServer) hello(cmd string) {
180180
d.initialized = true
181181
}
182182

183-
func (d *DiscoveryServer) start() {
183+
func (d *Server) start() {
184184
if d.started {
185185
d.outputChan <- messageError("start", "Discovery already STARTed")
186186
return
@@ -199,7 +199,7 @@ func (d *DiscoveryServer) start() {
199199
d.outputChan <- messageOk("start")
200200
}
201201

202-
func (d *DiscoveryServer) eventCallback(event string, port *Port) {
202+
func (d *Server) eventCallback(event string, port *Port) {
203203
id := port.Address + "|" + port.Protocol
204204
if event == "add" {
205205
d.cachedPorts[id] = port
@@ -209,11 +209,11 @@ func (d *DiscoveryServer) eventCallback(event string, port *Port) {
209209
}
210210
}
211211

212-
func (d *DiscoveryServer) errorCallback(msg string) {
212+
func (d *Server) errorCallback(msg string) {
213213
d.cachedErr = msg
214214
}
215215

216-
func (d *DiscoveryServer) list() {
216+
func (d *Server) list() {
217217
if !d.started {
218218
d.outputChan <- messageError("list", "Discovery not STARTed")
219219
return
@@ -236,7 +236,7 @@ func (d *DiscoveryServer) list() {
236236
}
237237
}
238238

239-
func (d *DiscoveryServer) startSync() {
239+
func (d *Server) startSync() {
240240
if d.syncStarted {
241241
d.outputChan <- messageError("start_sync", "Discovery already START_SYNCed")
242242
return
@@ -253,7 +253,7 @@ func (d *DiscoveryServer) startSync() {
253253
d.outputChan <- messageOk("start_sync")
254254
}
255255

256-
func (d *DiscoveryServer) stop() {
256+
func (d *Server) stop() {
257257
if !d.syncStarted && !d.started {
258258
d.outputChan <- messageError("stop", "Discovery already STOPped")
259259
return
@@ -269,18 +269,18 @@ func (d *DiscoveryServer) stop() {
269269
d.outputChan <- messageOk("stop")
270270
}
271271

272-
func (d *DiscoveryServer) syncEvent(event string, port *Port) {
272+
func (d *Server) syncEvent(event string, port *Port) {
273273
d.outputChan <- &message{
274274
EventType: event,
275275
Port: port,
276276
}
277277
}
278278

279-
func (d *DiscoveryServer) errorEvent(msg string) {
279+
func (d *Server) errorEvent(msg string) {
280280
d.outputChan <- messageError("start_sync", msg)
281281
}
282282

283-
func (d *DiscoveryServer) outputProcessor(outWriter io.Writer) {
283+
func (d *Server) outputProcessor(outWriter io.Writer) {
284284
// Start go routine to serialize messages printing
285285
go func() {
286286
for msg := range d.outputChan {

dummy-discovery/args/args.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var Tag = "snapshot"
2828
// Timestamp is the current timestamp
2929
var Timestamp = "unknown"
3030

31-
func ParseArgs() {
31+
// Parse arguments passed by the user
32+
func Parse() {
3233
for _, arg := range os.Args[1:] {
3334
if arg == "" {
3435
continue

dummy-discovery/main.go

+27-12
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,49 @@ import (
2828
"github.com/arduino/pluggable-discovery-protocol-handler/dummy-discovery/args"
2929
)
3030

31-
type DummyDiscovery struct {
31+
// dummyDiscovery is an example implementation of a Discovery.
32+
// It simulates a real implementation of a Discovery by generating
33+
// connected ports deterministically, it can also be used for testing
34+
// purposes.
35+
type dummyDiscovery struct {
3236
startSyncCount int
3337
closeChan chan<- bool
3438
}
3539

3640
func main() {
37-
args.ParseArgs()
38-
dummyDiscovery := &DummyDiscovery{}
39-
server := discovery.NewDiscoveryServer(dummyDiscovery)
41+
args.Parse()
42+
dummy := &dummyDiscovery{}
43+
server := discovery.NewServer(dummy)
4044
if err := server.Run(os.Stdin, os.Stdout); err != nil {
4145
os.Exit(1)
4246
}
4347
}
4448

45-
func (d *DummyDiscovery) Hello(userAgent string, protocol int) error {
49+
// Hello does nothing.
50+
// In a real implementation it could setup background processes
51+
// or other kind of resources necessary to discover Ports.
52+
func (d *dummyDiscovery) Hello(userAgent string, protocol int) error {
4653
return nil
4754
}
4855

49-
func (d *DummyDiscovery) Quit() {}
56+
// Quit does nothing.
57+
// In a real implementation it can be used to tear down resources
58+
// used to discovery Ports.
59+
func (d *dummyDiscovery) Quit() {}
5060

51-
func (d *DummyDiscovery) Stop() error {
61+
// Stop is used to stop the goroutine started by StartSync
62+
// used to discover ports.
63+
func (d *dummyDiscovery) Stop() error {
5264
if d.closeChan != nil {
5365
d.closeChan <- true
66+
close(d.closeChan)
5467
d.closeChan = nil
5568
}
5669
return nil
5770
}
5871

59-
func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error {
72+
// StartSync starts the goroutine that generates fake Ports.
73+
func (d *dummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error {
6074
d.startSyncCount++
6175
if d.startSyncCount%5 == 0 {
6276
return errors.New("could not start_sync every 5 times")
@@ -70,8 +84,8 @@ func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB disc
7084
var closeChan <-chan bool = c
7185

7286
// Output initial port state
73-
eventCB("add", CreateDummyPort())
74-
eventCB("add", CreateDummyPort())
87+
eventCB("add", createDummyPort())
88+
eventCB("add", createDummyPort())
7589

7690
// Start sending events
7791
count := 0
@@ -84,7 +98,7 @@ func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB disc
8498
case <-time.After(2 * time.Second):
8599
}
86100

87-
port := CreateDummyPort()
101+
port := createDummyPort()
88102
eventCB("add", port)
89103

90104
select {
@@ -108,7 +122,8 @@ func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB disc
108122

109123
var dummyCounter = 0
110124

111-
func CreateDummyPort() *discovery.Port {
125+
// createDummyPort creates a Port with fake data
126+
func createDummyPort() *discovery.Port {
112127
dummyCounter++
113128
return &discovery.Port{
114129
Address: fmt.Sprintf("%d", dummyCounter),

0 commit comments

Comments
 (0)