Skip to content

Fix failing go checks #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions discovery_server.go → discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// a commercial license, send an email to [email protected].
//

// 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.
Expand Down Expand Up @@ -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
Expand All @@ -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),
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion dummy-discovery/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 27 additions & 12 deletions dummy-discovery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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),
Expand Down