Skip to content

Commit

Permalink
Start() and List() are no more required
Browse files Browse the repository at this point in the history
Those are emulated using StartSync method, ports are cached internally
and returned altogether when the client calls List on the server.
  • Loading branch information
cmaglie committed Jul 22, 2021
1 parent f46021c commit 5ada13a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
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

0 comments on commit 5ada13a

Please sign in to comment.