Commit 65f1043b authored by Cristian Maglie's avatar Cristian Maglie

pluggable-discovery handling in grpc Instances (WIP)

serial-discovery must be moved from commands/board to commands
parent 2c1b10d8
......@@ -55,20 +55,15 @@ func BoardList(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp,
}
}
serialDiscovery, err := discovery.NewFromCommandLine(serialDiscoveryTool.InstallDir.Join("serial-discovery").String())
// TODO: move to 'commands' modules
_, err := discovery.NewFromCommandLine(serialDiscoveryTool.InstallDir.Join("serial-discovery").String())
if err != nil {
formatter.PrintError(err, "Error setting up serial-discovery tool.")
os.Exit(cli.ErrCoreConfig)
}
// Find all installed discoveries
discoveries := append(discovery.ExtractDiscoveriesFromPlatforms(pm), serialDiscovery)
resp := &rpc.BoardListResp{Ports: []*rpc.DetectedPort{}}
for _, disc := range discoveries {
disc.Start()
defer disc.Close()
for _, disc := range commands.GetDiscoveries(req) {
ports, err := disc.List()
if err != nil {
fmt.Printf("Error getting port list from discovery %s: %s\n", disc.ID, err)
......
//
// This file is part of arduino-cli.
//
// Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to modify or
// otherwise use the software for commercial activities involving the Arduino
// software without disclosing the source code of your own applications. To purchase
// a commercial license, send an email to license@arduino.cc.
//
package commands
import (
"sync"
"github.com/arduino/arduino-cli/arduino/discovery"
)
type sharedDiscovery struct {
discovery *discovery.Discovery
referenceCount int
}
// this map contains all the running pluggable-discoveries instances
var sharedDiscoveries = map[string]*sharedDiscovery{}
var sharedDiscoveriesMutex sync.Mutex
// StartSharedDiscovery starts a discovery or returns the instance of an already
// started shared discovery.
func StartSharedDiscovery(disc *discovery.Discovery) (*discovery.Discovery, error) {
sharedDiscoveriesMutex.Lock()
defer sharedDiscoveriesMutex.Unlock()
instance, started := sharedDiscoveries[disc.ID]
if started {
instance.referenceCount++
return instance.discovery, nil
}
sharedDiscoveries[disc.ID] = &sharedDiscovery{
discovery: disc,
referenceCount: 1,
}
err := disc.Start()
return disc, err
}
// StopSharedDiscovery will dispose an instance of a shared discovery if it is
// no more needed.
func StopSharedDiscovery(disc *discovery.Discovery) error {
sharedDiscoveriesMutex.Lock()
defer sharedDiscoveriesMutex.Unlock()
instance, started := sharedDiscoveries[disc.ID]
if started {
instance.referenceCount--
if instance.referenceCount == 0 {
delete(sharedDiscoveries, disc.ID)
return instance.discovery.Close()
}
}
return nil
}
......@@ -26,8 +26,8 @@ import (
"time"
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/arduino/discovery"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/configs"
......@@ -46,10 +46,11 @@ var instancesCount int32 = 1
// instantiate as many as needed by providing a different configuration
// for each one.
type CoreInstance struct {
config *configs.Configuration
pm *packagemanager.PackageManager
lm *librariesmanager.LibrariesManager
getLibOnly bool
config *configs.Configuration
pm *packagemanager.PackageManager
lm *librariesmanager.LibrariesManager
getLibOnly bool
discoveries []*discovery.Discovery
}
type InstanceContainer interface {
......@@ -72,6 +73,32 @@ func GetLibraryManager(req InstanceContainer) *librariesmanager.LibrariesManager
return i.lm
}
func GetDiscoveries(req InstanceContainer) []*discovery.Discovery {
i, ok := instances[req.GetInstance().GetId()]
if !ok {
return nil
}
return i.discoveries
}
func (instance *CoreInstance) startDiscoveries(downloadCB DownloadProgressCB, taskCB TaskProgressCB) error {
discoveriesToStop := instance.discoveries
discoveriesToStart := discovery.ExtractDiscoveriesFromPlatforms(instance.pm)
instance.discoveries = []*discovery.Discovery{}
for _, disc := range discoveriesToStart {
sharedDisc, err := StartSharedDiscovery(disc)
if err != nil {
return fmt.Errorf("starting discovery: %s", err)
}
instance.discoveries = append(instance.discoveries, sharedDisc)
}
for _, disc := range discoveriesToStop {
StopSharedDiscovery(disc)
}
return nil
}
func Init(ctx context.Context, req *rpc.InitReq, downloadCB DownloadProgressCB, taskCB TaskProgressCB) (*rpc.InitResp, error) {
inConfig := req.GetConfiguration()
if inConfig == nil {
......@@ -108,6 +135,10 @@ func Init(ctx context.Context, req *rpc.InitReq, downloadCB DownloadProgressCB,
instancesCount++
instances[handle] = instance
if err := instance.startDiscoveries(downloadCB, taskCB); err != nil {
// TODO: handle discovery errors
fmt.Println(err)
}
return &rpc.InitResp{
Instance: &rpc.Instance{Id: handle},
PlatformsIndexErrors: reqPltIndex,
......@@ -120,6 +151,11 @@ func Destroy(ctx context.Context, req *rpc.DestroyReq) (*rpc.DestroyResp, error)
if _, ok := instances[id]; !ok {
return nil, fmt.Errorf("invalid handle")
}
for _, disc := range GetDiscoveries(req) {
StopSharedDiscovery(disc)
}
delete(instances, id)
return &rpc.DestroyResp{}, nil
}
......@@ -207,6 +243,9 @@ func Rescan(ctx context.Context, req *rpc.RescanReq) (*rpc.RescanResp, error) {
}
coreInstance.pm = pm
coreInstance.lm = lm
coreInstance.startDiscoveries(nil, nil)
return &rpc.RescanResp{
PlatformsIndexErrors: reqPltIndex,
LibrariesIndexError: reqLibIndex,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment