Skip to content

Commit 621bb90

Browse files
committed
add sync mechanism for discovered ports and optimize a bit the code
1 parent 714ffce commit 621bb90

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

serial.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"encoding/json"
77
"strconv"
88
"strings"
9+
"sync"
910
"time"
1011

1112
"github.com/arduino/arduino-create-agent/upload"
13+
log "github.com/sirupsen/logrus"
1214
)
1315

1416
type writeRequest struct {
@@ -34,6 +36,7 @@ type serialhub struct {
3436
type SpPortList struct {
3537
Ports []SpPortItem
3638
Network bool
39+
mu sync.Mutex `json:"-"`
3740
}
3841

3942
type SpPortItem struct {
@@ -102,13 +105,17 @@ func write(wr writeRequest) {
102105

103106
// spList broadcasts a Json representation of the ports found
104107
func spList(network bool) {
105-
var list SpPortList
108+
var ls []byte
109+
var err error
106110
if network {
107-
list = NetworkPorts
111+
NetworkPorts.mu.Lock()
112+
ls, err = json.MarshalIndent(NetworkPorts, "", "\t")
113+
NetworkPorts.mu.Unlock()
108114
} else {
109-
list = SerialPorts
115+
SerialPorts.mu.Lock()
116+
ls, err = json.MarshalIndent(SerialPorts, "", "\t")
117+
SerialPorts.mu.Unlock()
110118
}
111-
ls, err := json.MarshalIndent(list, "", "\t")
112119
if err != nil {
113120
//log.Println(err)
114121
h.broadcastSys <- []byte("Error creating json on port list " +
@@ -120,10 +127,14 @@ func spList(network bool) {
120127

121128
// discoverLoop periodically update the list of ports found
122129
func discoverLoop() {
130+
SerialPorts.mu.Lock()
123131
SerialPorts.Network = false
124132
SerialPorts.Ports = make([]SpPortItem, 0)
133+
SerialPorts.mu.Unlock()
134+
NetworkPorts.mu.Lock()
125135
NetworkPorts.Network = true
126136
NetworkPorts.Ports = make([]SpPortItem, 0)
137+
NetworkPorts.mu.Unlock()
127138

128139
go func() {
129140
for {
@@ -184,13 +195,13 @@ func spListDual(network bool) {
184195
// to append the open/close state, baud rates, etc to make
185196
// a super clean nice list to send back to browser
186197
n := len(list)
187-
spl := SpPortList{make([]SpPortItem, n, n), network}
198+
spl := make([]SpPortItem, n, n)
188199

189200
ctr := 0
190201

191202
for _, item := range list {
192203

193-
spl.Ports[ctr] = SpPortItem{
204+
spl[ctr] = SpPortItem{
194205
Name: item.Name,
195206
SerialNumber: item.ISerial,
196207
DeviceClass: item.DeviceClass,
@@ -209,17 +220,21 @@ func spListDual(network bool) {
209220

210221
if isFound {
211222
// we found our port
212-
spl.Ports[ctr].IsOpen = true
213-
spl.Ports[ctr].Baud = myport.portConf.Baud
214-
spl.Ports[ctr].BufferAlgorithm = myport.BufferType
223+
spl[ctr].IsOpen = true
224+
spl[ctr].Baud = myport.portConf.Baud
225+
spl[ctr].BufferAlgorithm = myport.BufferType
215226
}
216227
ctr++
217228
}
218229

219230
if network {
220-
NetworkPorts = spl
231+
NetworkPorts.mu.Lock()
232+
NetworkPorts.Ports = spl
233+
NetworkPorts.mu.Unlock()
221234
} else {
222-
SerialPorts = spl
235+
SerialPorts.mu.Lock()
236+
SerialPorts.Ports = spl
237+
SerialPorts.mu.Unlock()
223238
}
224239
}
225240

0 commit comments

Comments
 (0)