Skip to content

Commit f46021c

Browse files
authored
Merge pull request #2 from arduino/error_events
Added error handling in sync mode
2 parents f60f4a9 + 372cba9 commit f46021c

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

discovery_server.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type Discovery interface {
6666
// StartSync is called to put the discovery in event mode. When the
6767
// function returns the discovery must send port events ("add" or "remove")
6868
// using the eventCB function.
69-
StartSync(eventCB EventCallback) error
69+
StartSync(eventCB EventCallback, errorCB ErrorCallback) error
7070

7171
// Stop stops the discovery internal subroutines. If the discovery is
7272
// in event mode it must stop sending events through the eventCB previously
@@ -83,6 +83,12 @@ type Discovery interface {
8383
// is detected.
8484
type EventCallback func(event string, port *Port)
8585

86+
// ErrorCallback is a callback function to signal unrecoverable errors to the
87+
// client while the discovery is in event mode. Once the discovery signal an
88+
// error it means that no more port-events will be delivered until the client
89+
// performs a STOP+START_SYNC cycle.
90+
type ErrorCallback func(err string)
91+
8692
// A DiscoveryServer is a pluggable discovery protocol handler,
8793
// it must be created using the NewDiscoveryServer function.
8894
type DiscoveryServer struct {
@@ -233,7 +239,7 @@ func (d *DiscoveryServer) startSync() {
233239
}
234240
c := make(chan interface{}, 10) // buffer up to 10 events
235241
d.syncChannel = c
236-
if err := d.impl.StartSync(d.syncEvent); err != nil {
242+
if err := d.impl.StartSync(d.syncEvent, d.errorEvent); err != nil {
237243
d.outputError("start_sync", "Cannot START_SYNC: "+err.Error())
238244
close(d.syncChannel) // do not leak channel...
239245
d.syncChannel = nil
@@ -278,6 +284,19 @@ func (d *DiscoveryServer) syncEvent(event string, port *Port) {
278284
}
279285
}
280286

287+
func (d *DiscoveryServer) errorEvent(msg string) {
288+
type syncOutputJSON struct {
289+
EventType string `json:"eventType"`
290+
Error bool `json:"error"`
291+
Message string `json:"message"`
292+
}
293+
d.syncChannel <- &syncOutputJSON{
294+
EventType: "start_sync",
295+
Error: true,
296+
Message: msg,
297+
}
298+
}
299+
281300
type genericMessageJSON struct {
282301
EventType string `json:"eventType"`
283302
Message string `json:"message"`

dummy-discovery/main.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (d *DummyDiscovery) Stop() error {
7171
return nil
7272
}
7373

74-
func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback) error {
74+
func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error {
7575
d.startSyncCount++
7676
if d.startSyncCount%5 == 0 {
7777
return errors.New("could not start_sync every 5 times")
@@ -89,7 +89,10 @@ func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback) error {
8989
eventCB("add", CreateDummyPort())
9090

9191
// Start sending events
92-
for {
92+
count := 0
93+
for count < 2 {
94+
count++
95+
9396
select {
9497
case <-closeChan:
9598
return
@@ -110,6 +113,9 @@ func (d *DummyDiscovery) StartSync(eventCB discovery.EventCallback) error {
110113
Protocol: port.Protocol,
111114
})
112115
}
116+
117+
errorCB("unrecoverable error, cannot send more events")
118+
<-closeChan
113119
}()
114120

115121
return nil

0 commit comments

Comments
 (0)