Skip to content

Commit 3a4246b

Browse files
silvanocerzacmaglie
authored andcommitted
[skip changelog] Add DiscoveryManager to PackageManager
1 parent df2d30e commit 3a4246b

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

arduino/cores/packagemanager/loader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
"github.com/arduino/arduino-cli/arduino/cores"
26+
"github.com/arduino/arduino-cli/arduino/discovery"
2627
"github.com/arduino/arduino-cli/configuration"
2728
"github.com/arduino/go-paths-helper"
2829
properties "github.com/arduino/go-properties-orderedmap"

arduino/cores/packagemanager/package_manager.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/arduino/arduino-cli/arduino/cores"
2626
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
27+
"github.com/arduino/arduino-cli/arduino/discovery/discoverymanager"
2728
paths "github.com/arduino/go-paths-helper"
2829
properties "github.com/arduino/go-properties-orderedmap"
2930
"github.com/sirupsen/logrus"
@@ -43,6 +44,7 @@ type PackageManager struct {
4344
DownloadDir *paths.Path
4445
TempDir *paths.Path
4546
CustomGlobalProperties *properties.Map
47+
discoveryManager *discoverymanager.DiscoveryManager
4648
}
4749

4850
// NewPackageManager returns a new instance of the PackageManager
@@ -55,13 +57,21 @@ func NewPackageManager(indexDir, packagesDir, downloadDir, tempDir *paths.Path)
5557
DownloadDir: downloadDir,
5658
TempDir: tempDir,
5759
CustomGlobalProperties: properties.NewMap(),
60+
discoveryManager: discoverymanager.New(),
5861
}
5962
}
6063

6164
// Clear resets the PackageManager to its initial state
6265
func (pm *PackageManager) Clear() {
6366
pm.Packages = cores.NewPackages()
6467
pm.CustomGlobalProperties = properties.NewMap()
68+
pm.discoveryManager.StopAll()
69+
pm.discoveryManager = discoverymanager.New()
70+
}
71+
72+
// DiscoveryManager returns the DiscoveryManager in use by this PackageManager
73+
func (pm *PackageManager) DiscoveryManager() *discoverymanager.DiscoveryManager {
74+
return pm.discoveryManager
6575
}
6676

6777
// FindPlatformReleaseProvidingBoardsWithVidPid FIXMEDOC
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)