|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to license@arduino.cc. |
| 15 | + |
| 16 | +package discoverymanager |
| 17 | + |
| 18 | +import ( |
| 19 | + "github.com/arduino/arduino-cli/arduino/discovery" |
| 20 | + "github.com/pkg/errors" |
| 21 | +) |
| 22 | + |
| 23 | +// DiscoveryManager is required to handle multiple pluggable-discovery that |
| 24 | +// may be shared across platforms |
| 25 | +type DiscoveryManager struct { |
| 26 | + discoveries map[string]*discovery.PluggableDiscovery |
| 27 | +} |
| 28 | + |
| 29 | +// New creates a new DiscoveriesManager |
| 30 | +func New() *DiscoveryManager { |
| 31 | + return &DiscoveryManager{ |
| 32 | + discoveries: map[string]*discovery.PluggableDiscovery{}, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Add adds a discovery to the list of managed discoveries |
| 37 | +func (dm *DiscoveryManager) Add(disc *discovery.PluggableDiscovery) error { |
| 38 | + id := disc.GetID() |
| 39 | + if _, has := dm.discoveries[id]; has { |
| 40 | + return errors.Errorf("pluggable discovery already added: %s", id) |
| 41 | + } |
| 42 | + dm.discoveries[id] = disc |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +// StartAll the discoveries for this DiscoveryManager, |
| 47 | +// returns the first error it meets or nil |
| 48 | +func (dm *DiscoveryManager) StartAll() error { |
| 49 | + for _, d := range dm.discoveries { |
| 50 | + err := d.Start() |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + err = d.StartSync() |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// StopAll the discoveries for this DiscoveryManager, |
| 63 | +// returns the first error it meets or nil |
| 64 | +func (dm *DiscoveryManager) StopAll() error { |
| 65 | + for _, d := range dm.discoveries { |
| 66 | + err := d.Stop() |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + } |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +// ListPorts return the current list of ports detected from all discoveries |
| 75 | +func (dm *DiscoveryManager) ListPorts() []*discovery.Port { |
| 76 | + // c := make(chan []*discovery.Port, len(dm.discoveries)) |
| 77 | + |
| 78 | + // var wg sync.WaitGroup |
| 79 | + // for _, d := range dm.discoveries { |
| 80 | + // wg.Add(1) |
| 81 | + // d := d |
| 82 | + // go func() { |
| 83 | + // c <- d.ListSync() |
| 84 | + // wg.Done() |
| 85 | + // }() |
| 86 | + // } |
| 87 | + // wg.Wait() |
| 88 | + // // Close the channel only after all the goroutines are finished |
| 89 | + // close(c) |
| 90 | + |
| 91 | + // ports := []*discovery.Port{} |
| 92 | + // for p := range c { |
| 93 | + // ports = append(ports, p...) |
| 94 | + // } |
| 95 | + |
| 96 | + // return ports |
| 97 | + res := []*discovery.Port{} |
| 98 | + for _, disc := range dm.discoveries { |
| 99 | + res = append(res, disc.ListSync()...) |
| 100 | + } |
| 101 | + return res |
| 102 | +} |
0 commit comments