@@ -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+
206217func (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"`
0 commit comments