@@ -56,13 +56,6 @@ type Discovery interface {
56
56
// and the protocolVersion negotiated with the client.
57
57
Hello (userAgent string , protocolVersion int ) error
58
58
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
-
66
59
// StartSync is called to put the discovery in event mode. When the
67
60
// function returns the discovery must send port events ("add" or "remove")
68
61
// using the eventCB function.
@@ -101,6 +94,8 @@ type DiscoveryServer struct {
101
94
started bool
102
95
syncStarted bool
103
96
syncChannel chan interface {}
97
+ cachedPorts map [string ]* Port
98
+ cachedErr string
104
99
}
105
100
106
101
// NewDiscoveryServer creates a new discovery server backed by the
@@ -195,14 +190,30 @@ func (d *DiscoveryServer) start() {
195
190
d .outputError ("start" , "Discovery already START_SYNCed, cannot START" )
196
191
return
197
192
}
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 {
199
196
d .outputError ("start" , "Cannot START: " + err .Error ())
200
197
return
201
198
}
202
199
d .started = true
203
200
d .outputOk ("start" )
204
201
}
205
202
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
+
206
217
func (d * DiscoveryServer ) list () {
207
218
if ! d .started {
208
219
d .outputError ("list" , "Discovery not STARTed" )
@@ -212,12 +223,14 @@ func (d *DiscoveryServer) list() {
212
223
d .outputError ("list" , "discovery already START_SYNCed, LIST not allowed" )
213
224
return
214
225
}
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 )
218
228
return
219
229
}
220
-
230
+ ports := []* Port {}
231
+ for _ , port := range d .cachedPorts {
232
+ ports = append (ports , port )
233
+ }
221
234
type listOutputJSON struct {
222
235
EventType string `json:"eventType"`
223
236
Ports []* Port `json:"ports"`
0 commit comments