Skip to content

Commit bae632a

Browse files
committed
refactor: remove unused Bufferflow implementations and simplify buffer handling
1 parent aff40ab commit bae632a

8 files changed

+15
-253
lines changed

Diff for: bufferflow.go

-23
This file was deleted.

Diff for: bufferflow_default.go

-72
This file was deleted.

Diff for: bufferflow_timed.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ type BufferflowTimed struct {
3333
bufferedOutput string
3434
}
3535

36-
// NewBufferflowTimed will create a new timed bufferflow
37-
func NewBufferflowTimed(port string, output chan<- []byte) *BufferflowTimed {
36+
// NewBufferFlowTimed will create a new timed bufferflow
37+
func NewBufferFlowTimed(port string, output chan<- []byte) *BufferflowTimed {
3838
return &BufferflowTimed{
3939
port: port,
4040
output: output,

Diff for: bufferflow_timedraw.go

-89
This file was deleted.

Diff for: conn.go

+1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ func wsHandler() *WsServer {
220220
c := &connection{send: make(chan []byte, 256*10), ws: so}
221221
h.register <- c
222222
so.On("command", func(message string) {
223+
fmt.Println("command:", message)
223224
h.broadcast <- []byte(message)
224225
})
225226

Diff for: hub.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,8 @@ func checkCmd(m []byte) {
146146
go spErr("Problem converting baud rate " + args[2])
147147
return
148148
}
149-
// pass in buffer type now as string. if user does not
150-
// ask for a buffer type pass in empty string
151-
bufferAlgorithm := "default" // use the default buffer if none is specified
152-
if len(args) > 3 {
153-
// cool. we got a buffer type request
154-
buftype := strings.Replace(args[3], "\n", "", -1)
155-
bufferAlgorithm = buftype
156-
}
157-
fmt.Println("bufferAlgorithm: ", bufferAlgorithm)
158-
go spHandlerOpen(args[1], baud, bufferAlgorithm)
149+
150+
go spHandlerOpen(args[1], baud)
159151

160152
} else if strings.HasPrefix(sl, "close") {
161153

Diff for: serial.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var sh = serialhub{
6767
func (sh *serialhub) Register(port *serport) {
6868
sh.mu.Lock()
6969
//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 + "\"}")
70+
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "\"}")
7171
sh.ports[port] = true
7272
sh.mu.Unlock()
7373
}

Diff for: serialport.go

+9-56
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"io"
2222
"strconv"
2323
"time"
24-
"unicode/utf8"
2524

2625
log "github.com/sirupsen/logrus"
2726
serial "go.bug.st/serial"
@@ -56,10 +55,7 @@ type serport struct {
5655
// channel containing raw base64 encoded binary data (outbound messages)
5756
sendRaw chan string
5857

59-
// Do we have an extra channel/thread to watch our buffer?
60-
BufferType string
61-
//bufferwatcher *BufferflowDummypause
62-
bufferwatcher Bufferflow
58+
bufferFlow *BufferflowTimed
6359
}
6460

6561
// SpPortMessage is the serial port message
@@ -74,10 +70,9 @@ type SpPortMessageRaw struct {
7470
D []byte // the data, i.e. G0 X0 Y0
7571
}
7672

77-
func (p *serport) reader(buftype string) {
73+
func (p *serport) reader() {
7874

7975
timeCheckOpen := time.Now()
80-
var bufferedCh bytes.Buffer
8176

8277
serialBuffer := make([]byte, 1024)
8378
for {
@@ -95,39 +90,8 @@ func (p *serport) reader(buftype string) {
9590
// read can return legitimate bytes as well as an error
9691
// so process the n bytes red, if n > 0
9792
if n > 0 && err == nil {
98-
9993
log.Print("Read " + strconv.Itoa(n) + " bytes ch: " + string(bufferPart[:n]))
100-
101-
data := ""
102-
switch buftype {
103-
case "timedraw", "timed":
104-
data = string(bufferPart[:n])
105-
// give the data to our bufferflow so it can do it's work
106-
// to read/translate the data to see if it wants to block
107-
// writes to the serialport. each bufferflow type will decide
108-
// this on its own based on its logic
109-
p.bufferwatcher.OnIncomingData(data)
110-
case "default": // the bufferbuftype is actually called default 🤷‍♂️
111-
// save the left out bytes for the next iteration due to UTF-8 encoding
112-
bufferPart = append(bufferedCh.Bytes(), bufferPart[:n]...)
113-
n += len(bufferedCh.Bytes())
114-
bufferedCh.Reset()
115-
for i, w := 0, 0; i < n; i += w {
116-
runeValue, width := utf8.DecodeRune(bufferPart[i:n]) // try to decode the first i bytes in the buffer (UTF8 runes do not have a fixed length)
117-
if runeValue == utf8.RuneError {
118-
bufferedCh.Write(bufferPart[i:n])
119-
break
120-
}
121-
if i == n {
122-
bufferedCh.Reset()
123-
}
124-
data += string(runeValue)
125-
w = width
126-
}
127-
p.bufferwatcher.OnIncomingData(data)
128-
default:
129-
log.Panicf("unknown buffer type %s", buftype)
130-
}
94+
p.bufferFlow.OnIncomingData(string(bufferPart[:n]))
13195
}
13296

13397
// double check that we got characters in the buffer
@@ -272,7 +236,7 @@ func (p *serport) writerRaw() {
272236
h.broadcastSys <- []byte(msgstr)
273237
}
274238

275-
func spHandlerOpen(portname string, baud int, buftype string) {
239+
func spHandlerOpen(portname string, baud int) {
276240

277241
log.Print("Inside spHandler")
278242

@@ -311,23 +275,12 @@ func spHandlerOpen(portname string, baud int, buftype string) {
311275
portConf: conf,
312276
portIo: sp,
313277
portName: portname,
314-
BufferType: buftype}
315-
316-
var bw Bufferflow
317-
318-
switch buftype {
319-
case "timed":
320-
bw = NewBufferflowTimed(portname, h.broadcastSys)
321-
case "timedraw":
322-
bw = NewBufferflowTimedRaw(portname, h.broadcastSys)
323-
case "default":
324-
bw = NewBufferflowDefault(portname, h.broadcastSys)
325-
default:
326-
log.Panicf("unknown buffer type: %s", buftype)
327278
}
328279

280+
bw := NewBufferFlowTimed(portname, h.broadcastSys)
329281
bw.Init()
330-
p.bufferwatcher = bw
282+
283+
p.bufferFlow = bw
331284

332285
sh.Register(p)
333286
defer sh.Unregister(p)
@@ -342,14 +295,14 @@ func spHandlerOpen(portname string, baud int, buftype string) {
342295
// this is thread to send to serial port but with base64 decoding
343296
go p.writerRaw()
344297

345-
p.reader(buftype)
298+
p.reader()
346299

347300
serialPorts.List()
348301
}
349302

350303
func (p *serport) Close() {
351304
p.isClosing = true
352-
p.bufferwatcher.Close()
305+
p.bufferFlow.Close()
353306
p.portIo.Close()
354307
serialPorts.MarkPortAsClosed(p.portName)
355308
serialPorts.List()

0 commit comments

Comments
 (0)