Skip to content

Commit 20b9ba1

Browse files
authoredFeb 15, 2024
[skip-changelog] Moved discovery client implementation into his own library (#2531)
* Moved discovery client implementation into his own library * Updated to latest release of pluggable-discovery-protocol-handler lib
1 parent 31e4478 commit 20b9ba1

File tree

14 files changed

+777
-604
lines changed

14 files changed

+777
-604
lines changed
 

‎.licenses/go/github.com/arduino/pluggable-discovery-protocol-handler/v2.dep.yml

+709
Large diffs are not rendered by default.

‎commands/board/list.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ import (
3131
"github.com/arduino/arduino-cli/commands/internal/instances"
3232
"github.com/arduino/arduino-cli/internal/arduino/cores"
3333
"github.com/arduino/arduino-cli/internal/arduino/cores/packagemanager"
34-
"github.com/arduino/arduino-cli/internal/arduino/discovery"
3534
"github.com/arduino/arduino-cli/internal/arduino/httpclient"
3635
"github.com/arduino/arduino-cli/internal/inventory"
3736
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3837
"github.com/arduino/go-properties-orderedmap"
38+
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
3939
"github.com/sirupsen/logrus"
4040
)
4141

@@ -234,7 +234,7 @@ func List(req *rpc.BoardListRequest) (r []*rpc.DetectedPort, discoveryStartError
234234
// boards slice can be empty at this point if neither the cores nor the
235235
// API managed to recognize the connected board
236236
b := &rpc.DetectedPort{
237-
Port: port.ToRPC(),
237+
Port: rpc.DiscoveryPortToRPC(port),
238238
MatchingBoards: boards,
239239
}
240240

@@ -283,7 +283,7 @@ func Watch(ctx context.Context, req *rpc.BoardListWatchRequest) (<-chan *rpc.Boa
283283
defer close(outChan)
284284
for event := range watcher.Feed() {
285285
port := &rpc.DetectedPort{
286-
Port: event.Port.ToRPC(),
286+
Port: rpc.DiscoveryPortToRPC(event.Port),
287287
}
288288

289289
boardsError := ""

‎commands/board/list_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222
"testing"
2323

2424
"github.com/arduino/arduino-cli/internal/arduino/cores/packagemanager"
25-
"github.com/arduino/arduino-cli/internal/arduino/discovery"
2625
"github.com/arduino/arduino-cli/internal/cli/configuration"
2726
"github.com/arduino/go-paths-helper"
2827
"github.com/arduino/go-properties-orderedmap"
28+
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
2929
"github.com/stretchr/testify/require"
3030
semver "go.bug.st/relaxed-semver"
3131
)

‎commands/upload/upload.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ import (
2929
f "github.com/arduino/arduino-cli/internal/algorithms"
3030
"github.com/arduino/arduino-cli/internal/arduino/cores"
3131
"github.com/arduino/arduino-cli/internal/arduino/cores/packagemanager"
32-
"github.com/arduino/arduino-cli/internal/arduino/discovery"
3332
"github.com/arduino/arduino-cli/internal/arduino/globals"
3433
"github.com/arduino/arduino-cli/internal/arduino/sketch"
3534
"github.com/arduino/arduino-cli/internal/i18n"
3635
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3736
paths "github.com/arduino/go-paths-helper"
3837
properties "github.com/arduino/go-properties-orderedmap"
3938
serialutils "github.com/arduino/go-serial-utils"
39+
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
4040
"github.com/sirupsen/logrus"
4141
)
4242

@@ -209,7 +209,7 @@ func runProgramAction(pme *packagemanager.Explorer,
209209
outStream, errStream io.Writer,
210210
dryRun bool, userFields map[string]string,
211211
) (*rpc.Port, error) {
212-
port := discovery.PortFromRPCPort(userPort)
212+
port := rpc.DiscoveryPortFromRPCPort(userPort)
213213
if port == nil || (port.Address == "" && port.Protocol == "") {
214214
// For no-port uploads use "default" protocol
215215
port = &discovery.Port{Protocol: "default"}
@@ -528,7 +528,7 @@ func runProgramAction(pme *packagemanager.Explorer,
528528
// If the algorithms can not detect the new port, fallback to the user-provided port.
529529
return userPort, nil
530530
}
531-
return updatedPort.ToRPC(), nil
531+
return rpc.DiscoveryPortToRPC(updatedPort), nil
532532
}
533533

534534
func detectUploadPort(

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/arduino/go-serial-utils v0.1.2
1313
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b
1414
github.com/arduino/go-win32-utils v1.0.0
15-
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.1.2
15+
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.2.0
1616
github.com/arduino/pluggable-monitor-protocol-handler v0.9.2
1717
github.com/cmaglie/pb v1.0.27
1818
github.com/codeclysm/extract/v3 v3.1.1

‎go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b h1:9hDi4F2st6
2121
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b/go.mod h1:uwGy5PpN4lqW97FiLnbcx+xx8jly5YuPMJWfVwwjJiQ=
2222
github.com/arduino/go-win32-utils v1.0.0 h1:/cXB86sOJxOsCHP7sQmXGLkdValwJt56mIwOHYxgQjQ=
2323
github.com/arduino/go-win32-utils v1.0.0/go.mod h1:0jqM7doGEAs6DaJCxxhLBUDS5OawrqF48HqXkcEie/Q=
24-
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.1.2 h1:Yrljx84UtFilOCpe+xE6+OFDyWyhghYpP/4A7vzQneM=
25-
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.1.2/go.mod h1:1dgblsmK2iBx3L5iNTyRIokeaxbTLUrYiUbHBK6yC3Y=
24+
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.2.0 h1:v7og6LpskewFabmaShKVzWXl5MXbmsxaRP3yo4dJta8=
25+
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.2.0/go.mod h1:1dgblsmK2iBx3L5iNTyRIokeaxbTLUrYiUbHBK6yC3Y=
2626
github.com/arduino/pluggable-monitor-protocol-handler v0.9.2 h1:vb5AmE3bT9we5Ej4AdBxcC9dJLXasRimVqaComf9L3M=
2727
github.com/arduino/pluggable-monitor-protocol-handler v0.9.2/go.mod h1:vMG8tgHyE+hli26oT0JB/M7NxUMzzWoU5wd6cgJQRK4=
2828
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=

‎internal/arduino/cores/packagemanager/loader.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/arduino/arduino-cli/commands/cmderrors"
2626
"github.com/arduino/arduino-cli/internal/arduino/cores"
27-
"github.com/arduino/arduino-cli/internal/arduino/discovery"
2827
"github.com/arduino/arduino-cli/internal/cli/configuration"
2928
"github.com/arduino/go-paths-helper"
3029
properties "github.com/arduino/go-properties-orderedmap"
@@ -636,8 +635,7 @@ func (pme *Explorer) loadDiscovery(id string) error {
636635
return errors.New(tr("discovery %s not installed", id))
637636
}
638637
discoveryPath := toolRelease.InstallDir.Join(tool.Name).String()
639-
d := discovery.New(id, discoveryPath)
640-
pme.discoveryManager.Add(d)
638+
pme.discoveryManager.Add(id, discoveryPath)
641639
return nil
642640
}
643641

@@ -713,8 +711,7 @@ func (pme *Explorer) loadDiscoveries(release *cores.PlatformRelease) []error {
713711
if cmdArgs, err := properties.SplitQuotedString(cmd, `"'`, true); err != nil {
714712
merr = append(merr, err)
715713
} else {
716-
d := discovery.New(discoveryID, cmdArgs...)
717-
pme.discoveryManager.Add(d)
714+
pme.discoveryManager.Add(discoveryID, cmdArgs...)
718715
}
719716
}
720717

‎internal/arduino/discovery/discovery.go

-472
This file was deleted.

‎internal/arduino/discovery/discovery_client/main.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"os"
2323
"sort"
2424

25-
"github.com/arduino/arduino-cli/internal/arduino/discovery"
2625
"github.com/arduino/arduino-cli/internal/arduino/discovery/discoverymanager"
2726
"github.com/sirupsen/logrus"
2827
)
@@ -35,8 +34,7 @@ func main() {
3534
logrus.SetLevel(logrus.ErrorLevel)
3635
dm := discoverymanager.New()
3736
for _, discCmd := range os.Args[1:] {
38-
disc := discovery.New(discCmd, discCmd)
39-
dm.Add(disc)
37+
dm.Add(discCmd, discCmd)
4038
}
4139
dm.Start()
4240

‎internal/arduino/discovery/discovery_test.go

-73
This file was deleted.

‎internal/arduino/discovery/discoverymanager/discoverymanager.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import (
2121
"sync"
2222
"time"
2323

24-
"github.com/arduino/arduino-cli/internal/arduino/discovery"
24+
"github.com/arduino/arduino-cli/internal/cli/configuration"
2525
"github.com/arduino/arduino-cli/internal/i18n"
26+
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
2627
"github.com/sirupsen/logrus"
2728
)
2829

@@ -34,9 +35,9 @@ import (
3435
// is called.
3536
type DiscoveryManager struct {
3637
discoveriesMutex sync.Mutex
37-
discoveries map[string]*discovery.PluggableDiscovery // all registered PluggableDiscovery
38-
discoveriesRunning bool // set to true once discoveries are started
39-
feed chan *discovery.Event // all events will pass through this channel
38+
discoveries map[string]*discovery.Client // all registered PluggableDiscovery
39+
discoveriesRunning bool // set to true once discoveries are started
40+
feed chan *discovery.Event // all events will pass through this channel
4041
watchersMutex sync.Mutex
4142
watchers map[*PortWatcher]bool // all registered Watcher
4243
watchersCache map[string]map[string]*discovery.Event // this is a cache of all active ports
@@ -47,7 +48,7 @@ var tr = i18n.Tr
4748
// New creates a new DiscoveryManager
4849
func New() *DiscoveryManager {
4950
return &DiscoveryManager{
50-
discoveries: map[string]*discovery.PluggableDiscovery{},
51+
discoveries: map[string]*discovery.Client{},
5152
watchers: map[*PortWatcher]bool{},
5253
feed: make(chan *discovery.Event, 50),
5354
watchersCache: map[string]map[string]*discovery.Event{},
@@ -65,7 +66,7 @@ func (dm *DiscoveryManager) Clear() {
6566
logrus.Infof("Closed and removed discovery %s", d.GetID())
6667
}
6768
}
68-
dm.discoveries = map[string]*discovery.PluggableDiscovery{}
69+
dm.discoveries = map[string]*discovery.Client{}
6970
}
7071

7172
// IDs returns the list of discoveries' ids in this DiscoveryManager
@@ -101,7 +102,7 @@ func (dm *DiscoveryManager) Start() []error {
101102
var wg sync.WaitGroup
102103
for _, d := range dm.discoveries {
103104
wg.Add(1)
104-
go func(d *discovery.PluggableDiscovery) {
105+
go func(d *discovery.Client) {
105106
if err := dm.startDiscovery(d); err != nil {
106107
errsLock.Lock()
107108
errs = append(errs, err)
@@ -117,7 +118,14 @@ func (dm *DiscoveryManager) Start() []error {
117118
}
118119

119120
// Add adds a discovery to the list of managed discoveries
120-
func (dm *DiscoveryManager) Add(d *discovery.PluggableDiscovery) error {
121+
func (dm *DiscoveryManager) Add(id string, args ...string) error {
122+
d := discovery.NewClient(id, args...)
123+
d.SetLogger(logrus.WithField("discovery", id))
124+
d.SetUserAgent(configuration.UserAgent(configuration.Settings))
125+
return dm.add(d)
126+
}
127+
128+
func (dm *DiscoveryManager) add(d *discovery.Client) error {
121129
dm.discoveriesMutex.Lock()
122130
defer dm.discoveriesMutex.Unlock()
123131

@@ -178,7 +186,7 @@ func (dm *DiscoveryManager) Watch() (*PortWatcher, error) {
178186
return watcher, nil
179187
}
180188

181-
func (dm *DiscoveryManager) startDiscovery(d *discovery.PluggableDiscovery) (discErr error) {
189+
func (dm *DiscoveryManager) startDiscovery(d *discovery.Client) (discErr error) {
182190
defer func() {
183191
// If this function returns an error log it
184192
if discErr != nil {
@@ -194,7 +202,7 @@ func (dm *DiscoveryManager) startDiscovery(d *discovery.PluggableDiscovery) (dis
194202
return fmt.Errorf("%s: %s", tr("starting discovery %s", d.GetID()), err)
195203
}
196204

197-
go func(d *discovery.PluggableDiscovery) {
205+
go func(d *discovery.Client) {
198206
// Transfer all incoming events from this discovery to the feed channel
199207
for ev := range eventCh {
200208
dm.feed <- ev
@@ -281,6 +289,6 @@ func (dm *DiscoveryManager) List() []*discovery.Port {
281289
// AddAllDiscoveriesFrom transfers discoveries from src to the receiver
282290
func (dm *DiscoveryManager) AddAllDiscoveriesFrom(src *DiscoveryManager) {
283291
for _, d := range src.discoveries {
284-
dm.Add(d)
292+
dm.add(d)
285293
}
286294
}

‎internal/arduino/discovery/testdata/cat/.gitignore

-2
This file was deleted.

‎internal/arduino/discovery/testdata/cat/main.go

-28
This file was deleted.

‎rpc/cc/arduino/cli/commands/v1/common.go

+36
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package commands
1818
import (
1919
"sort"
2020

21+
"github.com/arduino/go-properties-orderedmap"
22+
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
2123
semver "go.bug.st/relaxed-semver"
2224
)
2325

@@ -98,3 +100,37 @@ func (s *PlatformSummary) GetSortedReleases() []*PlatformRelease {
98100
})
99101
return res
100102
}
103+
104+
// DiscoveryPortToRPC converts a *discovery.Port into an *rpc.Port
105+
func DiscoveryPortToRPC(p *discovery.Port) *Port {
106+
props := p.Properties
107+
if props == nil {
108+
props = properties.NewMap()
109+
}
110+
return &Port{
111+
Address: p.Address,
112+
Label: p.AddressLabel,
113+
Protocol: p.Protocol,
114+
ProtocolLabel: p.ProtocolLabel,
115+
HardwareId: p.HardwareID,
116+
Properties: props.AsMap(),
117+
}
118+
}
119+
120+
// DiscoveryPortFromRPCPort converts an *rpc.Port into a *discovery.Port
121+
func DiscoveryPortFromRPCPort(o *Port) (p *discovery.Port) {
122+
if o == nil {
123+
return nil
124+
}
125+
res := &discovery.Port{
126+
Address: o.GetAddress(),
127+
AddressLabel: o.GetLabel(),
128+
Protocol: o.GetProtocol(),
129+
ProtocolLabel: o.GetProtocolLabel(),
130+
HardwareID: o.GetHardwareId(),
131+
}
132+
if o.GetProperties() != nil {
133+
res.Properties = properties.NewFromHashmap(o.GetProperties())
134+
}
135+
return res
136+
}

0 commit comments

Comments
 (0)
Please sign in to comment.