Skip to content

Start() and List() are no more required #3

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
Jul 22, 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
37 changes: 25 additions & 12 deletions discovery_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ type Discovery interface {
// and the protocolVersion negotiated with the client.
Hello(userAgent string, protocolVersion int) error

// Start is called to start the discovery internal subroutines.
Start() error

// List returns the list of the currently available ports. It works
// only after a Start.
List() (portList []*Port, err error)

// StartSync is called to put the discovery in event mode. When the
// function returns the discovery must send port events ("add" or "remove")
// using the eventCB function.
Expand Down Expand Up @@ -101,6 +94,8 @@ type DiscoveryServer struct {
started bool
syncStarted bool
syncChannel chan interface{}
cachedPorts map[string]*Port
cachedErr string
}

// NewDiscoveryServer creates a new discovery server backed by the
Expand Down Expand Up @@ -195,14 +190,30 @@ func (d *DiscoveryServer) start() {
d.outputError("start", "Discovery already START_SYNCed, cannot START")
return
}
if err := d.impl.Start(); err != nil {
d.cachedPorts = map[string]*Port{}
d.cachedErr = ""
if err := d.impl.StartSync(d.eventCallback, d.errorCallback); err != nil {
d.outputError("start", "Cannot START: "+err.Error())
return
}
d.started = true
d.outputOk("start")
}

func (d *DiscoveryServer) eventCallback(event string, port *Port) {
id := port.Address + "|" + port.Protocol
if event == "add" {
d.cachedPorts[id] = port
}
if event == "remove" {
delete(d.cachedPorts, id)
}
}

func (d *DiscoveryServer) errorCallback(msg string) {
d.cachedErr = msg
}

func (d *DiscoveryServer) list() {
if !d.started {
d.outputError("list", "Discovery not STARTed")
Expand All @@ -212,12 +223,14 @@ func (d *DiscoveryServer) list() {
d.outputError("list", "discovery already START_SYNCed, LIST not allowed")
return
}
ports, err := d.impl.List()
if err != nil {
d.outputError("list", "LIST error: "+err.Error())
if d.cachedErr != "" {
d.outputError("list", d.cachedErr)
return
}

ports := []*Port{}
for _, port := range d.cachedPorts {
ports = append(ports, port)
}
type listOutputJSON struct {
EventType string `json:"eventType"`
Ports []*Port `json:"ports"`
Expand Down
15 changes: 0 additions & 15 deletions dummy-discovery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (

type DummyDiscovery struct {
startSyncCount int
listCount int
closeChan chan<- bool
}

Expand All @@ -49,20 +48,6 @@ func (d *DummyDiscovery) Hello(userAgent string, protocol int) error {

func (d *DummyDiscovery) Quit() {}

func (d *DummyDiscovery) List() ([]*discovery.Port, error) {
d.listCount++
if d.listCount%5 == 0 {
return nil, errors.New("could not list every 5 times")
}
return []*discovery.Port{
CreateDummyPort(),
}, nil
}

func (d *DummyDiscovery) Start() error {
return nil
}

func (d *DummyDiscovery) Stop() error {
if d.closeChan != nil {
d.closeChan <- true
Expand Down