Skip to content

Commit d1316e0

Browse files
committed
Discoveries event channels are now created when start sync is called
1 parent b3a0ccd commit d1316e0

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

arduino/discovery/discovery.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -390,44 +390,38 @@ func (disc *PluggableDiscovery) List() ([]*Port, error) {
390390
}
391391
}
392392

393-
// EventChannel creates a channel used to receive events from the pluggable discovery.
394-
// The event channel must be consumed as quickly as possible since it may block the
395-
// discovery if it becomes full. The channel size is configurable.
396-
func (disc *PluggableDiscovery) EventChannel(size int) <-chan *Event {
397-
disc.statusMutex.Lock()
398-
defer disc.statusMutex.Unlock()
399-
if disc.eventChan != nil {
400-
// In case there is already an existing event channel in use we close it
401-
// before creating a new one.
402-
close(disc.eventChan)
403-
}
404-
c := make(chan *Event, size)
405-
disc.eventChan = c
406-
return c
407-
}
408-
409393
// StartSync puts the discovery in "events" mode: the discovery will send "add"
410394
// and "remove" events each time a new port is detected or removed respectively.
411395
// After calling StartSync an initial burst of "add" events may be generated to
412396
// report all the ports available at the moment of the start.
413-
func (disc *PluggableDiscovery) StartSync() error {
397+
// It also creates a channel used to receive events from the pluggable discovery.
398+
// The event channel must be consumed as quickly as possible since it may block the
399+
// discovery if it becomes full. The channel size is configurable.
400+
func (disc *PluggableDiscovery) StartSync(size int) (<-chan *Event, error) {
414401
if err := disc.sendCommand("START_SYNC\n"); err != nil {
415-
return err
402+
return nil, err
416403
}
417404

418405
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
419-
return fmt.Errorf("calling START_SYNC: %w", err)
406+
return nil, fmt.Errorf("calling START_SYNC: %w", err)
420407
} else if msg.EventType != "start_sync" {
421-
return errors.Errorf(tr("communication out of sync, expected 'start_sync', received '%s'"), msg.EventType)
408+
return nil, errors.Errorf(tr("communication out of sync, expected 'start_sync', received '%s'"), msg.EventType)
422409
} else if msg.Message != "OK" || msg.Error {
423-
return errors.Errorf(tr("command failed: %s"), msg.Message)
410+
return nil, errors.Errorf(tr("command failed: %s"), msg.Message)
424411
}
425412

426413
disc.statusMutex.Lock()
427414
defer disc.statusMutex.Unlock()
428415
disc.state = Syncing
429416
disc.cachedPorts = map[string]*Port{}
430-
return nil
417+
if disc.eventChan != nil {
418+
// In case there is already an existing event channel in use we close it
419+
// before creating a new one.
420+
close(disc.eventChan)
421+
}
422+
c := make(chan *Event, size)
423+
disc.eventChan = c
424+
return c, nil
431425
}
432426

433427
// ListSync returns a list of the available ports. The list is a cache of all the

arduino/discovery/discoverymanager/discoverymanager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ func (dm *DiscoveryManager) StartSyncAll() (<-chan *discovery.Event, []error) {
141141
return nil
142142
}
143143

144-
eventCh := d.EventChannel(5)
145-
if err := d.StartSync(); err != nil {
144+
eventCh, err := d.StartSync(5)
145+
if err != nil {
146146
return fmt.Errorf("start syncing discovery %s: %w", d.GetID(), err)
147147
}
148148
go func() {

0 commit comments

Comments
 (0)