Skip to content

Commit f3d5dca

Browse files
committed
uniform default bufferflow and 🧹
1 parent 5d0bd27 commit f3d5dca

5 files changed

+37
-50
lines changed

bufferflow.go

-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ type Bufferflow interface {
1414
Init()
1515
BlockUntilReady(cmd string, id string) (bool, bool) // implement this method
1616
OnIncomingData(data string) // implement this method
17-
IsBufferGloballySendingBackIncomingData() bool // implement this method
1817
Close() // implement this method
1918
}

bufferflow_default.go

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
11
package main
22

33
import (
4+
"encoding/json"
5+
46
log "github.com/sirupsen/logrus"
57
)
68

79
type BufferflowDefault struct {
8-
port string
10+
port string
11+
output chan []byte
12+
input chan string
13+
done chan bool
914
}
1015

11-
func NewBufferflowDefault(port string) *BufferflowDefault {
16+
func NewBufferflowDefault(port string, output chan []byte) *BufferflowDefault {
1217
return &BufferflowDefault{
13-
port: port,
18+
port: port,
19+
output: output,
20+
input: make(chan string),
21+
done: make(chan bool),
1422
}
1523
}
1624

1725
func (b *BufferflowDefault) Init() {
1826
log.Println("Initting default buffer flow (which means no buffering)")
27+
go b.consumeInput()
28+
}
29+
30+
func (b *BufferflowDefault) consumeInput() {
31+
Loop:
32+
for {
33+
select {
34+
case data := <-b.input:
35+
m := SpPortMessage{b.port, data}
36+
message, _ := json.Marshal(m)
37+
// data is now encoded in base64 format
38+
// need a decoder on the other side
39+
b.output <- message
40+
case <-b.done:
41+
break Loop //this is required, a simple break statement would only exit the innermost switch statement
42+
}
43+
}
44+
close(b.input) // close the input channel at the end of the computation
1945
}
2046

2147
func (b *BufferflowDefault) BlockUntilReady(cmd string, id string) (bool, bool) {
@@ -24,12 +50,10 @@ func (b *BufferflowDefault) BlockUntilReady(cmd string, id string) (bool, bool)
2450
}
2551

2652
func (b *BufferflowDefault) OnIncomingData(data string) {
27-
//log.Printf("OnIncomingData() start. data:%v\n", data)
28-
}
29-
30-
func (b *BufferflowDefault) IsBufferGloballySendingBackIncomingData() bool {
31-
return false
53+
b.input <- data
3254
}
3355

3456
func (b *BufferflowDefault) Close() {
57+
b.done <- true
58+
close(b.done)
3559
}

bufferflow_timed.go

-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ func (b *BufferflowTimed) OnIncomingData(data string) {
6868
b.input <- data
6969
}
7070

71-
func (b *BufferflowTimed) IsBufferGloballySendingBackIncomingData() bool {
72-
return true
73-
}
74-
7571
func (b *BufferflowTimed) Close() {
7672
b.ticker.Stop()
7773
b.done <- true

bufferflow_timedraw.go

-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ func (b *BufferflowTimedRaw) OnIncomingData(data string) {
6868
b.input <- data
6969
}
7070

71-
func (b *BufferflowTimedRaw) IsBufferGloballySendingBackIncomingData() bool {
72-
return true
73-
}
74-
7571
func (b *BufferflowTimedRaw) Close() {
7672
b.ticker.Stop()
7773
b.done <- true

serialport.go

+5-33
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"bytes"
5-
"encoding/json"
65
"io"
76
"strconv"
87
"time"
@@ -121,6 +120,10 @@ func (p *serport) reader(buftype string) {
121120
switch buftype {
122121
case "timedraw", "timed", "timedbinary":
123122
data = string(bufferPart[:n])
123+
// give the data to our bufferflow so it can do it's work
124+
// to read/translate the data to see if it wants to block
125+
// writes to the serialport. each bufferflow type will decide
126+
// this on its own based on its logic
124127
p.bufferwatcher.OnIncomingData(data)
125128
case "default": // the bufferbuftype is actually called default 🤷‍♂️
126129
// save the left out bytes for the next iteration due to UTF-8 encoding
@@ -139,41 +142,10 @@ func (p *serport) reader(buftype string) {
139142
data += string(runeValue)
140143
w = width
141144
}
142-
// give the data to our bufferflow so it can do it's work
143-
// to read/translate the data to see if it wants to block
144-
// writes to the serialport. each bufferflow type will decide
145-
// this on its own based on its logic, i.e. tinyg vs grbl vs others
146145
p.bufferwatcher.OnIncomingData(data)
147146
default:
148147
log.Panicf("unknown buffer type %s", buftype)
149148
}
150-
151-
// see if the OnIncomingData handled the broadcast back
152-
// to the user. this option was added in case the OnIncomingData wanted
153-
// to do something fancier or implementation specific, i.e. TinyG Buffer
154-
// actually sends back data on a perline basis rather than our method
155-
// where we just send the moment we get it. the reason for this is that
156-
// the browser was sometimes getting back packets out of order which
157-
// of course would screw things up when parsing
158-
159-
if p.bufferwatcher.IsBufferGloballySendingBackIncomingData() == false {
160-
//m := SpPortMessage{"Alice", "Hello"}
161-
m := SpPortMessage{p.portConf.Name, data}
162-
//log.Print("The m obj struct is:")
163-
//log.Print(m)
164-
165-
//b, err := json.MarshalIndent(m, "", "\t")
166-
b, err := json.Marshal(m)
167-
if err != nil {
168-
log.Println(err)
169-
h.broadcastSys <- []byte("Error creating json on " + p.portConf.Name + " " +
170-
err.Error() + " The data we were trying to convert is: " + string(ch[:n]))
171-
break
172-
}
173-
//log.Print("Printing out json byte data...")
174-
//log.Print(string(b))
175-
h.broadcastSys <- b
176-
}
177149
}
178150

179151
// double check that we got characters in the buffer
@@ -332,7 +304,7 @@ func spHandlerOpen(portname string, baud int, buftype string) {
332304
case "timedbinary":
333305
bw = NewBufferflowTimedBinary(portname, h.broadcastSys)
334306
case "default":
335-
bw = NewBufferflowDefault(portname)
307+
bw = NewBufferflowDefault(portname, h.broadcastSys)
336308
default:
337309
log.Panicf("unknown buffer type: %s", buftype)
338310
}

0 commit comments

Comments
 (0)