Skip to content

Commit 5ada13a

Browse files
committed
Start() and List() are no more required
Those are emulated using StartSync method, ports are cached internally and returned altogether when the client calls List on the server.
1 parent f46021c commit 5ada13a

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

discovery_server.go

+25-12
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ type Discovery interface {
5656
// and the protocolVersion negotiated with the client.
5757
Hello(userAgent string, protocolVersion int) error
5858

59-
// Start is called to start the discovery internal subroutines.
60-
Start() error
61-
62-
// List returns the list of the currently available ports. It works
63-
// only after a Start.
64-
List() (portList []*Port, err error)
65-
6659
// StartSync is called to put the discovery in event mode. When the
6760
// function returns the discovery must send port events ("add" or "remove")
6861
// using the eventCB function.
@@ -101,6 +94,8 @@ type DiscoveryServer struct {
10194
started bool
10295
syncStarted bool
10396
syncChannel chan interface{}
97+
cachedPorts map[string]*Port
98+
cachedErr string
10499
}
105100

106101
// NewDiscoveryServer creates a new discovery server backed by the
@@ -195,14 +190,30 @@ func (d *DiscoveryServer) start() {
195190
d.outputError("start", "Discovery already START_SYNCed, cannot START")
196191
return
197192
}
198-
if err := d.impl.Start(); err != nil {
193+
d.cachedPorts = map[string]*Port{}
194+
d.cachedErr = ""
195+
if err := d.impl.StartSync(d.eventCallback, d.errorCallback); err != nil {
199196
d.outputError("start", "Cannot START: "+err.Error())
200197
return
201198
}
202199
d.started = true
203200
d.outputOk("start")
204201
}
205202

203+
func (d *DiscoveryServer) eventCallback(event string, port *Port) {
204+
id := port.Address + "|" + port.Protocol
205+
if event == "add" {
206+
d.cachedPorts[id] = port
207+
}
208+
if event == "remove" {
209+
delete(d.cachedPorts, id)
210+
}
211+
}
212+
213+
func (d *DiscoveryServer) errorCallback(msg string) {
214+
d.cachedErr = msg
215+
}
216+
206217
func (d *DiscoveryServer) list() {
207218
if !d.started {
208219
d.outputError("list", "Discovery not STARTed")
@@ -212,12 +223,14 @@ func (d *DiscoveryServer) list() {
212223
d.outputError("list", "discovery already START_SYNCed, LIST not allowed")
213224
return
214225
}
215-
ports, err := d.impl.List()
216-
if err != nil {
217-
d.outputError("list", "LIST error: "+err.Error())
226+
if d.cachedErr != "" {
227+
d.outputError("list", d.cachedErr)
218228
return
219229
}
220-
230+
ports := []*Port{}
231+
for _, port := range d.cachedPorts {
232+
ports = append(ports, port)
233+
}
221234
type listOutputJSON struct {
222235
EventType string `json:"eventType"`
223236
Ports []*Port `json:"ports"`

dummy-discovery/main.go

-15
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030

3131
type DummyDiscovery struct {
3232
startSyncCount int
33-
listCount int
3433
closeChan chan<- bool
3534
}
3635

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

5049
func (d *DummyDiscovery) Quit() {}
5150

52-
func (d *DummyDiscovery) List() ([]*discovery.Port, error) {
53-
d.listCount++
54-
if d.listCount%5 == 0 {
55-
return nil, errors.New("could not list every 5 times")
56-
}
57-
return []*discovery.Port{
58-
CreateDummyPort(),
59-
}, nil
60-
}
61-
62-
func (d *DummyDiscovery) Start() error {
63-
return nil
64-
}
65-
6651
func (d *DummyDiscovery) Stop() error {
6752
if d.closeChan != nil {
6853
d.closeChan <- true

0 commit comments

Comments
 (0)