-
-
Notifications
You must be signed in to change notification settings - Fork 151
Remove global clients #1014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Remove global clients #1014
Changes from all commits
90cce2c
c96490b
7b4859a
279de2a
4f3ac5a
c41b815
55ca9e5
f07103e
ef94d97
d2d3aa6
640e644
99f8235
5e2161e
910bac2
17ed6b3
96f0f51
36af1ed
f939e32
e22281f
3bbf371
0773f99
9c2ca54
7e73b6c
fc51194
8106c84
4dba1a8
7ea21c0
a152b02
fac0b10
f04d5cc
47213d1
14ddd43
1f9b999
4585769
283064c
14d72e8
9e13dcb
a69c0dc
7e34071
815a791
9535905
0699e93
062bca0
2bcc06d
4da1624
effe50d
d68fc7a
fd3b2d7
0370591
a50aa86
65218a6
9cbe49e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -20,11 +20,11 @@ package main | |||||||
import ( | ||||||||
"encoding/json" | ||||||||
"slices" | ||||||||
"strconv" | ||||||||
"strings" | ||||||||
"sync" | ||||||||
"time" | ||||||||
|
||||||||
"github.com/arduino/arduino-create-agent/tools" | ||||||||
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2" | ||||||||
"github.com/sirupsen/logrus" | ||||||||
) | ||||||||
|
@@ -34,12 +34,27 @@ type serialhub struct { | |||||||
ports map[*serport]bool | ||||||||
|
||||||||
mu sync.Mutex | ||||||||
|
||||||||
onRegister func(port *serport) | ||||||||
onUnregister func(port *serport) | ||||||||
} | ||||||||
|
||||||||
func newSerialHub(onRegister func(port *serport), onUnregister func(port *serport)) *serialhub { | ||||||||
return &serialhub{ | ||||||||
ports: make(map[*serport]bool), | ||||||||
onRegister: onRegister, | ||||||||
onUnregister: onUnregister, | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// SerialPortList is the serial port list | ||||||||
type SerialPortList struct { | ||||||||
Ports []*SpPortItem | ||||||||
portsLock sync.Mutex | ||||||||
|
||||||||
tools *tools.Tools `json:"-"` | ||||||||
OnList func([]byte) `json:"-"` | ||||||||
OnErr func(string) `json:"-"` | ||||||||
Comment on lines
+56
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
|
||||||||
// SpPortItem is the serial port item | ||||||||
|
@@ -56,27 +71,18 @@ type SpPortItem struct { | |||||||
ProductID string | ||||||||
} | ||||||||
|
||||||||
// serialPorts contains the ports attached to the machine | ||||||||
var serialPorts SerialPortList | ||||||||
|
||||||||
var sh = serialhub{ | ||||||||
ports: make(map[*serport]bool), | ||||||||
} | ||||||||
|
||||||||
// Register serial ports from the connections. | ||||||||
func (sh *serialhub) Register(port *serport) { | ||||||||
sh.mu.Lock() | ||||||||
//log.Print("Registering a port: ", p.portConf.Name) | ||||||||
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + ",\"BufferType\":\"" + port.BufferType + "\"}") | ||||||||
sh.onRegister(port) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
sh.ports[port] = true | ||||||||
sh.mu.Unlock() | ||||||||
lucarin91 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
} | ||||||||
|
||||||||
// Unregister requests from connections. | ||||||||
func (sh *serialhub) Unregister(port *serport) { | ||||||||
sh.mu.Lock() | ||||||||
//log.Print("Unregistering a port: ", p.portConf.Name) | ||||||||
h.broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "}") | ||||||||
sh.onUnregister(port) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
delete(sh.ports, port) | ||||||||
close(port.sendBuffered) | ||||||||
close(port.sendNoBuf) | ||||||||
|
@@ -90,25 +96,30 @@ func (sh *serialhub) FindPortByName(portname string) (*serport, bool) { | |||||||
for port := range sh.ports { | ||||||||
if strings.EqualFold(port.portConf.Name, portname) { | ||||||||
// we found our port | ||||||||
//spHandlerClose(port) | ||||||||
return port, true | ||||||||
} | ||||||||
} | ||||||||
return nil, false | ||||||||
} | ||||||||
|
||||||||
func newSerialPortList(tools *tools.Tools, onList func(data []byte), onErr func(err string)) *SerialPortList { | ||||||||
return &SerialPortList{ | ||||||||
tools: tools, | ||||||||
OnList: onList, | ||||||||
OnErr: onErr, | ||||||||
Comment on lines
+108
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
} | ||||||||
|
||||||||
// List broadcasts a Json representation of the ports found | ||||||||
func (sp *SerialPortList) List() { | ||||||||
sp.portsLock.Lock() | ||||||||
ls, err := json.MarshalIndent(sp, "", "\t") | ||||||||
sp.portsLock.Unlock() | ||||||||
|
||||||||
if err != nil { | ||||||||
//log.Println(err) | ||||||||
h.broadcastSys <- []byte("Error creating json on port list " + | ||||||||
err.Error()) | ||||||||
sp.OnErr("Error creating json on port list " + err.Error()) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} else { | ||||||||
h.broadcastSys <- ls | ||||||||
sp.OnList(ls) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
} | ||||||||
|
||||||||
|
@@ -125,11 +136,12 @@ func (sp *SerialPortList) Run() { | |||||||
|
||||||||
func (sp *SerialPortList) runSerialDiscovery() { | ||||||||
// First ensure that all the discoveries are available | ||||||||
if err := Tools.Download("builtin", "serial-discovery", "latest", "keep"); err != nil { | ||||||||
noOpProgress := func(msg string) {} | ||||||||
if err := sp.tools.Download("builtin", "serial-discovery", "latest", "keep", noOpProgress); err != nil { | ||||||||
logrus.Errorf("Error downloading serial-discovery: %s", err) | ||||||||
panic(err) | ||||||||
} | ||||||||
sd, err := Tools.GetLocation("serial-discovery") | ||||||||
sd, err := sp.tools.GetLocation("serial-discovery") | ||||||||
if err != nil { | ||||||||
logrus.Errorf("Error downloading serial-discovery: %s", err) | ||||||||
panic(err) | ||||||||
|
@@ -255,22 +267,21 @@ func (sp *SerialPortList) getPortByName(portname string) *SpPortItem { | |||||||
return nil | ||||||||
} | ||||||||
|
||||||||
func spErr(err string) { | ||||||||
//log.Println("Sending err back: ", err) | ||||||||
//h.broadcastSys <- []byte(err) | ||||||||
// FIXME: the spErr, spWrite, spClose should be moved to the 'hub.go' file | ||||||||
func (h *hub) spErr(err string) { | ||||||||
dido18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
h.broadcastSys <- []byte("{\"Error\" : \"" + err + "\"}") | ||||||||
} | ||||||||
|
||||||||
func spClose(portname string) { | ||||||||
if myport, ok := sh.FindPortByName(portname); ok { | ||||||||
func (h *hub) spClose(portname string) { | ||||||||
if myport, ok := h.serialHub.FindPortByName(portname); ok { | ||||||||
h.broadcastSys <- []byte("Closing serial port " + portname) | ||||||||
myport.Close() | ||||||||
} else { | ||||||||
spErr("We could not find the serial port " + portname + " that you were trying to close.") | ||||||||
h.spErr("We could not find the serial port " + portname + " that you were trying to close.") | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
func spWrite(arg string) { | ||||||||
func (h *hub) spWrite(arg string) { | ||||||||
// we will get a string of comXX asdf asdf asdf | ||||||||
//log.Println("Inside spWrite arg: " + arg) | ||||||||
arg = strings.TrimPrefix(arg, " ") | ||||||||
|
@@ -279,7 +290,7 @@ func spWrite(arg string) { | |||||||
if len(args) != 3 { | ||||||||
errstr := "Could not parse send command: " + arg | ||||||||
//log.Println(errstr) | ||||||||
spErr(errstr) | ||||||||
h.spErr(errstr) | ||||||||
return | ||||||||
} | ||||||||
bufferingMode := args[0] | ||||||||
|
@@ -290,10 +301,10 @@ func spWrite(arg string) { | |||||||
//log.Println("The data is:" + data + "---") | ||||||||
|
||||||||
// See if we have this port open | ||||||||
port, ok := sh.FindPortByName(portname) | ||||||||
port, ok := h.serialHub.FindPortByName(portname) | ||||||||
if !ok { | ||||||||
// we couldn't find the port, so send err | ||||||||
spErr("We could not find the serial port " + portname + " that you were trying to write to.") | ||||||||
h.spErr("We could not find the serial port " + portname + " that you were trying to write to.") | ||||||||
return | ||||||||
} | ||||||||
|
||||||||
|
@@ -302,7 +313,7 @@ func spWrite(arg string) { | |||||||
case "send", "sendnobuf", "sendraw": | ||||||||
// valid buffering mode, go ahead | ||||||||
default: | ||||||||
spErr("Unsupported send command:" + args[0] + ". Please specify a valid one") | ||||||||
h.spErr("Unsupported send command:" + args[0] + ". Please specify a valid one") | ||||||||
return | ||||||||
} | ||||||||
|
||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would simplify both of this two with the
ChanWriter