From 3d9cdf94668d45c9e1b5a08ec3623786d173fb2e Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Fri, 6 Aug 2021 15:57:21 +0200 Subject: [PATCH] Fix failing go checks --- discovery_server.go => discovery.go | 42 ++++++++++++++--------------- dummy-discovery/args/args.go | 3 ++- dummy-discovery/main.go | 39 ++++++++++++++++++--------- 3 files changed, 50 insertions(+), 34 deletions(-) rename discovery_server.go => discovery.go (88%) diff --git a/discovery_server.go b/discovery.go similarity index 88% rename from discovery_server.go rename to discovery.go index d145773..e48894a 100644 --- a/discovery_server.go +++ b/discovery.go @@ -15,7 +15,7 @@ // a commercial license, send an email to license@arduino.cc. // -// discovery is a library for handling the Arduino Pluggable-Discovery protocol +// Package discovery is a library for handling the Arduino Pluggable-Discovery protocol // (https://github.com/arduino/tooling-rfcs/blob/main/RFCs/0002-pluggable-discovery.md#pluggable-discovery-api-via-stdinstdout) // // 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) // performs a STOP+START_SYNC cycle. type ErrorCallback func(err string) -// A DiscoveryServer is a pluggable discovery protocol handler, -// it must be created using the NewDiscoveryServer function. -type DiscoveryServer struct { +// A Server is a pluggable discovery protocol handler, +// it must be created using the NewServer function. +type Server struct { impl Discovery outputChan chan *message userAgent string @@ -95,11 +95,11 @@ type DiscoveryServer struct { cachedErr string } -// NewDiscoveryServer creates a new discovery server backed by the +// NewServer creates a new discovery server backed by the // provided pluggable discovery implementation. To start the server // use the Run method. -func NewDiscoveryServer(impl Discovery) *DiscoveryServer { - return &DiscoveryServer{ +func NewServer(impl Discovery) *Server { + return &Server{ impl: impl, outputChan: make(chan *message), } @@ -110,7 +110,7 @@ func NewDiscoveryServer(impl Discovery) *DiscoveryServer { // The function blocks until the `QUIT` command is received or // the input stream is closed. In case of IO error the error is // returned. -func (d *DiscoveryServer) Run(in io.Reader, out io.Writer) error { +func (d *Server) Run(in io.Reader, out io.Writer) error { go d.outputProcessor(out) defer close(d.outputChan) reader := bufio.NewReader(in) @@ -150,7 +150,7 @@ func (d *DiscoveryServer) Run(in io.Reader, out io.Writer) error { } } -func (d *DiscoveryServer) hello(cmd string) { +func (d *Server) hello(cmd string) { if d.initialized { d.outputChan <- messageError("hello", "HELLO already called") return @@ -162,12 +162,12 @@ func (d *DiscoveryServer) hello(cmd string) { return } d.userAgent = matches[2] - if v, err := strconv.ParseInt(matches[1], 10, 64); err != nil { + v, err := strconv.ParseInt(matches[1], 10, 64) + if err != nil { d.outputChan <- messageError("hello", "Invalid protocol version: "+matches[2]) return - } else { - d.reqProtocolVersion = int(v) } + d.reqProtocolVersion = int(v) if err := d.impl.Hello(d.userAgent, 1); err != nil { d.outputChan <- messageError("hello", err.Error()) return @@ -180,7 +180,7 @@ func (d *DiscoveryServer) hello(cmd string) { d.initialized = true } -func (d *DiscoveryServer) start() { +func (d *Server) start() { if d.started { d.outputChan <- messageError("start", "Discovery already STARTed") return @@ -199,7 +199,7 @@ func (d *DiscoveryServer) start() { d.outputChan <- messageOk("start") } -func (d *DiscoveryServer) eventCallback(event string, port *Port) { +func (d *Server) eventCallback(event string, port *Port) { id := port.Address + "|" + port.Protocol if event == "add" { d.cachedPorts[id] = port @@ -209,11 +209,11 @@ func (d *DiscoveryServer) eventCallback(event string, port *Port) { } } -func (d *DiscoveryServer) errorCallback(msg string) { +func (d *Server) errorCallback(msg string) { d.cachedErr = msg } -func (d *DiscoveryServer) list() { +func (d *Server) list() { if !d.started { d.outputChan <- messageError("list", "Discovery not STARTed") return @@ -236,7 +236,7 @@ func (d *DiscoveryServer) list() { } } -func (d *DiscoveryServer) startSync() { +func (d *Server) startSync() { if d.syncStarted { d.outputChan <- messageError("start_sync", "Discovery already START_SYNCed") return @@ -253,7 +253,7 @@ func (d *DiscoveryServer) startSync() { d.outputChan <- messageOk("start_sync") } -func (d *DiscoveryServer) stop() { +func (d *Server) stop() { if !d.syncStarted && !d.started { d.outputChan <- messageError("stop", "Discovery already STOPped") return @@ -269,18 +269,18 @@ func (d *DiscoveryServer) stop() { d.outputChan <- messageOk("stop") } -func (d *DiscoveryServer) syncEvent(event string, port *Port) { +func (d *Server) syncEvent(event string, port *Port) { d.outputChan <- &message{ EventType: event, Port: port, } } -func (d *DiscoveryServer) errorEvent(msg string) { +func (d *Server) errorEvent(msg string) { d.outputChan <- messageError("start_sync", msg) } -func (d *DiscoveryServer) outputProcessor(outWriter io.Writer) { +func (d *Server) outputProcessor(outWriter io.Writer) { // Start go routine to serialize messages printing go func() { for msg := range d.outputChan { diff --git a/dummy-discovery/args/args.go b/dummy-discovery/args/args.go index c547fa3..8b1423e 100644 --- a/dummy-discovery/args/args.go +++ b/dummy-discovery/args/args.go @@ -28,7 +28,8 @@ var Tag = "snapshot" // Timestamp is the current timestamp var Timestamp = "unknown" -func ParseArgs() { +// Parse arguments passed by the user +func Parse() { for _, arg := range os.Args[1:] { if arg == "" { continue diff --git a/dummy-discovery/main.go b/dummy-discovery/main.go index b475817..0ecc627 100644 --- a/dummy-discovery/main.go +++ b/dummy-discovery/main.go @@ -28,35 +28,49 @@ import ( "github.com/arduino/pluggable-discovery-protocol-handler/dummy-discovery/args" ) -type DummyDiscovery struct { +// dummyDiscovery is an example implementation of a Discovery. +// It simulates a real implementation of a Discovery by generating +// connected ports deterministically, it can also be used for testing +// purposes. +type dummyDiscovery struct { startSyncCount int closeChan chan<- bool } func main() { - args.ParseArgs() - dummyDiscovery := &DummyDiscovery{} - server := discovery.NewDiscoveryServer(dummyDiscovery) + args.Parse() + dummy := &dummyDiscovery{} + server := discovery.NewServer(dummy) if err := server.Run(os.Stdin, os.Stdout); err != nil { os.Exit(1) } } -func (d *DummyDiscovery) Hello(userAgent string, protocol int) error { +// Hello does nothing. +// In a real implementation it could setup background processes +// or other kind of resources necessary to discover Ports. +func (d *dummyDiscovery) Hello(userAgent string, protocol int) error { return nil } -func (d *DummyDiscovery) Quit() {} +// Quit does nothing. +// In a real implementation it can be used to tear down resources +// used to discovery Ports. +func (d *dummyDiscovery) Quit() {} -func (d *DummyDiscovery) Stop() error { +// Stop is used to stop the goroutine started by StartSync +// used to discover ports. +func (d *dummyDiscovery) Stop() error { if d.closeChan != nil { d.closeChan <- true + close(d.closeChan) d.closeChan = nil } return nil } -func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error { +// StartSync starts the goroutine that generates fake Ports. +func (d *dummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error { d.startSyncCount++ if d.startSyncCount%5 == 0 { return errors.New("could not start_sync every 5 times") @@ -70,8 +84,8 @@ func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB disc var closeChan <-chan bool = c // Output initial port state - eventCB("add", CreateDummyPort()) - eventCB("add", CreateDummyPort()) + eventCB("add", createDummyPort()) + eventCB("add", createDummyPort()) // Start sending events count := 0 @@ -84,7 +98,7 @@ func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB disc case <-time.After(2 * time.Second): } - port := CreateDummyPort() + port := createDummyPort() eventCB("add", port) select { @@ -108,7 +122,8 @@ func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB disc var dummyCounter = 0 -func CreateDummyPort() *discovery.Port { +// createDummyPort creates a Port with fake data +func createDummyPort() *discovery.Port { dummyCounter++ return &discovery.Port{ Address: fmt.Sprintf("%d", dummyCounter),