Skip to content

Commit 8e28a0c

Browse files
authored
Removed unnecessary use of channels (#938)
1 parent 51f4ee4 commit 8e28a0c

File tree

3 files changed

+44
-76
lines changed

3 files changed

+44
-76
lines changed

main.go

-2
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,6 @@ func loop() {
346346
go serialPorts.Run()
347347
// launch the hub routine which is the singleton for the websocket server
348348
go h.run()
349-
// launch our serial port routine
350-
go sh.run()
351349
// launch our dummy data routine
352350
//go d.run()
353351

serial.go

+42-72
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,10 @@ import (
2929
"github.com/sirupsen/logrus"
3030
)
3131

32-
type writeRequest struct {
33-
p *serport
34-
d string
35-
buffer string
36-
}
37-
3832
type serialhub struct {
3933
// Opened serial ports.
4034
ports map[*serport]bool
4135

42-
//write chan *serport, chan []byte
43-
write chan writeRequest
44-
45-
// Register requests from the connections.
46-
register chan *serport
47-
48-
// Unregister requests from connections.
49-
unregister chan *serport
50-
5136
mu sync.Mutex
5237
}
5338

@@ -75,46 +60,39 @@ type SpPortItem struct {
7560
var serialPorts SerialPortList
7661

7762
var sh = serialhub{
78-
//write: make(chan *serport, chan []byte),
79-
write: make(chan writeRequest),
80-
register: make(chan *serport),
81-
unregister: make(chan *serport),
82-
ports: make(map[*serport]bool),
63+
ports: make(map[*serport]bool),
8364
}
8465

85-
func (sh *serialhub) run() {
86-
87-
//log.Print("Inside run of serialhub")
88-
//cmdIdCtr := 0
89-
90-
for {
91-
select {
92-
case p := <-sh.register:
93-
sh.mu.Lock()
94-
//log.Print("Registering a port: ", p.portConf.Name)
95-
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + ",\"BufferType\":\"" + p.BufferType + "\"}")
96-
sh.ports[p] = true
97-
sh.mu.Unlock()
98-
case p := <-sh.unregister:
99-
sh.mu.Lock()
100-
//log.Print("Unregistering a port: ", p.portConf.Name)
101-
h.broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + "}")
102-
delete(sh.ports, p)
103-
close(p.sendBuffered)
104-
close(p.sendNoBuf)
105-
sh.mu.Unlock()
106-
case wr := <-sh.write:
107-
// if user sent in the commands as one text mode line
108-
switch wr.buffer {
109-
case "send":
110-
wr.p.sendBuffered <- wr.d
111-
case "sendnobuf":
112-
wr.p.sendNoBuf <- []byte(wr.d)
113-
case "sendraw":
114-
wr.p.sendRaw <- wr.d
115-
}
116-
// no default since we alredy verified in spWrite()
117-
}
66+
// Register serial ports from the connections.
67+
func (sh *serialhub) Register(port *serport) {
68+
sh.mu.Lock()
69+
//log.Print("Registering a port: ", p.portConf.Name)
70+
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + ",\"BufferType\":\"" + port.BufferType + "\"}")
71+
sh.ports[port] = true
72+
sh.mu.Unlock()
73+
}
74+
75+
// Unregister requests from connections.
76+
func (sh *serialhub) Unregister(port *serport) {
77+
sh.mu.Lock()
78+
//log.Print("Unregistering a port: ", p.portConf.Name)
79+
h.broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "}")
80+
delete(sh.ports, port)
81+
close(port.sendBuffered)
82+
close(port.sendNoBuf)
83+
sh.mu.Unlock()
84+
}
85+
86+
// Write data to the serial port.
87+
func (sh *serialhub) Write(port *serport, data string, sendMode string) {
88+
// if user sent in the commands as one text mode line
89+
switch sendMode {
90+
case "send":
91+
port.sendBuffered <- data
92+
case "sendnobuf":
93+
port.sendNoBuf <- []byte(data)
94+
case "sendraw":
95+
port.sendRaw <- data
11896
}
11997
}
12098

@@ -296,38 +274,30 @@ func spWrite(arg string) {
296274
spErr(errstr)
297275
return
298276
}
277+
bufferingMode := args[0]
299278
portname := strings.Trim(args[1], " ")
300-
//log.Println("The port to write to is:" + portname + "---")
301-
//log.Println("The data is:" + args[2] + "---")
279+
data := args[2]
302280

303-
// see if we have this port open
304-
myport, isFound := sh.FindPortByName(portname)
281+
//log.Println("The port to write to is:" + portname + "---")
282+
//log.Println("The data is:" + data + "---")
305283

306-
if !isFound {
284+
// See if we have this port open
285+
port, ok := sh.FindPortByName(portname)
286+
if !ok {
307287
// we couldn't find the port, so send err
308288
spErr("We could not find the serial port " + portname + " that you were trying to write to.")
309289
return
310290
}
311291

312-
// we found our port
313-
// create our write request
314-
var wr writeRequest
315-
wr.p = myport
316-
317-
// see if args[0] is send or sendnobuf or sendraw
318-
switch args[0] {
292+
// see if bufferingMode is valid
293+
switch bufferingMode {
319294
case "send", "sendnobuf", "sendraw":
320-
wr.buffer = args[0]
295+
// valid buffering mode, go ahead
321296
default:
322297
spErr("Unsupported send command:" + args[0] + ". Please specify a valid one")
323298
return
324299
}
325300

326-
// include newline or not in the write? that is the question.
327-
// for now lets skip the newline
328-
//wr.d = []byte(args[2] + "\n")
329-
wr.d = args[2] //[]byte(args[2])
330-
331301
// send it to the write channel
332-
sh.write <- wr
302+
sh.Write(port, data, bufferingMode)
333303
}

serialport.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ func spHandlerOpen(portname string, baud int, buftype string) {
323323
bw.Init()
324324
p.bufferwatcher = bw
325325

326-
sh.register <- p
327-
defer func() { sh.unregister <- p }()
326+
sh.Register(p)
327+
defer sh.Unregister(p)
328328

329329
serialPorts.List()
330330

0 commit comments

Comments
 (0)