Skip to content

Commit 0bbb45b

Browse files
committed
🧹(cleanup) and 🛠️(refactoring) of bufferflow stuff
1 parent 8f9ff20 commit 0bbb45b

6 files changed

+100
-273
lines changed

bufferflow.go

+4-34
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package main
22

3-
import (
4-
//"log"
5-
//"time"
6-
)
7-
8-
var availableBufferAlgorithms = []string{"default", "timed", "timedraw", "timedbinary"}
3+
// availableBufferAlgorithms = {"default", "timed", "timedraw", "timedbinary"}
94

105
type BufferMsg struct {
116
Cmd string
@@ -18,32 +13,7 @@ type BufferMsg struct {
1813
type Bufferflow interface {
1914
Init()
2015
BlockUntilReady(cmd string, id string) (bool, bool) // implement this method
21-
//JustQueue(cmd string, id string) bool // implement this method
22-
OnIncomingData(data string) // implement this method
23-
ClearOutSemaphore() // implement this method
24-
BreakApartCommands(cmd string) []string // implement this method
25-
Pause() // implement this method
26-
Unpause() // implement this method
27-
SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool // implement this method
28-
SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool // implement this method
29-
SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool // implement this method
30-
SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool // implement this method
31-
SeeIfSpecificCommandsReturnNoResponse(cmd string) bool // implement this method
32-
ReleaseLock() // implement this method
33-
IsBufferGloballySendingBackIncomingData() bool // implement this method
34-
Close() // implement this method
35-
}
36-
37-
/*data packets returned to client*/
38-
type DataCmdComplete struct {
39-
Cmd string
40-
Id string
41-
P string
42-
BufSize int `json:"-"`
43-
D string `json:"-"`
44-
}
45-
46-
type DataPerLine struct {
47-
P string
48-
D string
16+
OnIncomingData(data string) // implement this method
17+
IsBufferGloballySendingBackIncomingData() bool // implement this method
18+
Close() // implement this method
4919
}

bufferflow_default.go

+6-42
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import (
55
)
66

77
type BufferflowDefault struct {
8-
Name string
9-
Port string
8+
port string
109
}
1110

12-
var ()
11+
func NewBufferflowDefault(port string) *BufferflowDefault {
12+
return &BufferflowDefault{
13+
port: port,
14+
}
15+
}
1316

1417
func (b *BufferflowDefault) Init() {
1518
log.Println("Initting default buffer flow (which means no buffering)")
@@ -24,45 +27,6 @@ func (b *BufferflowDefault) OnIncomingData(data string) {
2427
//log.Printf("OnIncomingData() start. data:%v\n", data)
2528
}
2629

27-
// Clean out b.sem so it can truly block
28-
func (b *BufferflowDefault) ClearOutSemaphore() {
29-
}
30-
31-
func (b *BufferflowDefault) BreakApartCommands(cmd string) []string {
32-
return []string{cmd}
33-
}
34-
35-
func (b *BufferflowDefault) Pause() {
36-
return
37-
}
38-
39-
func (b *BufferflowDefault) Unpause() {
40-
return
41-
}
42-
43-
func (b *BufferflowDefault) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool {
44-
return false
45-
}
46-
47-
func (b *BufferflowDefault) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool {
48-
return false
49-
}
50-
51-
func (b *BufferflowDefault) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool {
52-
return false
53-
}
54-
55-
func (b *BufferflowDefault) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool {
56-
return false
57-
}
58-
59-
func (b *BufferflowDefault) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool {
60-
return false
61-
}
62-
63-
func (b *BufferflowDefault) ReleaseLock() {
64-
}
65-
6630
func (b *BufferflowDefault) IsBufferGloballySendingBackIncomingData() bool {
6731
return false
6832
}

bufferflow_timed.go

+28-63
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,53 @@ import (
88
)
99

1010
type BufferflowTimed struct {
11-
Name string
12-
Port string
13-
Output chan []byte
14-
Input chan string
15-
done chan bool
16-
ticker *time.Ticker
11+
port string
12+
output chan []byte
13+
input chan string
14+
done chan bool
15+
ticker *time.Ticker
16+
sPort string
17+
bufferedOutput string
1718
}
1819

19-
var (
20-
bufferedOutput string
21-
sPort string
22-
)
20+
func NewBufferflowTimed(port string, output chan []byte) *BufferflowTimed {
21+
return &BufferflowTimed{
22+
port: port,
23+
output: output,
24+
input: make(chan string),
25+
done: make(chan bool),
26+
ticker: time.NewTicker(16 * time.Millisecond),
27+
sPort: "",
28+
bufferedOutput: "",
29+
}
30+
}
2331

2432
func (b *BufferflowTimed) Init() {
2533
log.Println("Initting timed buffer flow (output once every 16ms)")
26-
bufferedOutput = ""
27-
sPort = ""
2834

2935
go func() {
30-
b.ticker = time.NewTicker(16 * time.Millisecond)
31-
b.done = make(chan bool)
3236
Loop:
3337
for {
3438
select {
35-
case data := <-b.Input:
36-
bufferedOutput = bufferedOutput + data
37-
sPort = b.Port
39+
case data := <-b.input:
40+
b.bufferedOutput = b.bufferedOutput + data
41+
b.sPort = b.port
3842
case <-b.ticker.C:
39-
if bufferedOutput != "" {
40-
m := SpPortMessage{sPort, bufferedOutput}
43+
if b.bufferedOutput != "" {
44+
m := SpPortMessage{b.sPort, b.bufferedOutput}
4145
buf, _ := json.Marshal(m)
4246
// data is now encoded in base64 format
4347
// need a decoder on the other side
44-
b.Output <- []byte(buf)
45-
bufferedOutput = ""
46-
sPort = ""
48+
b.output <- []byte(buf)
49+
b.bufferedOutput = ""
50+
b.sPort = ""
4751
}
4852
case <-b.done:
4953
break Loop
5054
}
5155
}
5256

53-
close(b.Input)
57+
close(b.input)
5458

5559
}()
5660

@@ -62,46 +66,7 @@ func (b *BufferflowTimed) BlockUntilReady(cmd string, id string) (bool, bool) {
6266
}
6367

6468
func (b *BufferflowTimed) OnIncomingData(data string) {
65-
b.Input <- data
66-
}
67-
68-
// Clean out b.sem so it can truly block
69-
func (b *BufferflowTimed) ClearOutSemaphore() {
70-
}
71-
72-
func (b *BufferflowTimed) BreakApartCommands(cmd string) []string {
73-
return []string{cmd}
74-
}
75-
76-
func (b *BufferflowTimed) Pause() {
77-
return
78-
}
79-
80-
func (b *BufferflowTimed) Unpause() {
81-
return
82-
}
83-
84-
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool {
85-
return false
86-
}
87-
88-
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool {
89-
return false
90-
}
91-
92-
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool {
93-
return false
94-
}
95-
96-
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool {
97-
return false
98-
}
99-
100-
func (b *BufferflowTimed) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool {
101-
return false
102-
}
103-
104-
func (b *BufferflowTimed) ReleaseLock() {
69+
b.input <- data
10570
}
10671

10772
func (b *BufferflowTimed) IsBufferGloballySendingBackIncomingData() bool {

bufferflow_timedbinary.go

+29-66
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,50 @@ import (
88
)
99

1010
type BufferflowTimedBinary struct {
11-
Name string
12-
Port string
13-
Output chan []byte
14-
Input chan []byte
15-
done chan bool
16-
ticker *time.Ticker
17-
}
18-
19-
var (
11+
port string
12+
output chan []byte
13+
input chan []byte
14+
done chan bool
15+
ticker *time.Ticker
2016
bufferedOutputBinary []byte
2117
sPortBinary string
22-
)
18+
}
19+
20+
func NewBufferflowTimedBinary(port string, output chan []byte) *BufferflowTimedBinary {
21+
return &BufferflowTimedBinary{
22+
port: port,
23+
output: output,
24+
input: make(chan []byte),
25+
done: make(chan bool),
26+
ticker: time.NewTicker(16 * time.Millisecond),
27+
bufferedOutputBinary: nil,
28+
sPortBinary: "",
29+
}
30+
}
2331

2432
func (b *BufferflowTimedBinary) Init() {
2533
log.Println("Initting timed buffer binary flow (output once every 16ms)")
26-
bufferedOutputBinary = nil
27-
sPortBinary = ""
28-
2934
go func() {
30-
b.ticker = time.NewTicker(16 * time.Millisecond)
31-
b.done = make(chan bool)
3235
Loop:
3336
for {
3437
select {
35-
case data := <-b.Input:
36-
bufferedOutputBinary = append(bufferedOutputBinary, data...)
37-
sPortBinary = b.Port
38+
case data := <-b.input:
39+
b.bufferedOutputBinary = append(b.bufferedOutputBinary, data...)
40+
b.sPortBinary = b.port
3841
case <-b.ticker.C:
39-
if bufferedOutputBinary != nil {
40-
m := SpPortMessageRaw{sPortBinary, bufferedOutputBinary}
42+
if b.bufferedOutputBinary != nil {
43+
m := SpPortMessageRaw{b.sPortBinary, b.bufferedOutputBinary}
4144
buf, _ := json.Marshal(m)
42-
b.Output <- buf
43-
bufferedOutputBinary = nil
44-
sPortBinary = ""
45+
b.output <- buf
46+
b.bufferedOutputBinary = nil
47+
b.sPortBinary = ""
4548
}
4649
case <-b.done:
4750
break Loop
4851
}
4952
}
5053

51-
close(b.Input)
54+
close(b.input)
5255
}()
5356
}
5457

@@ -57,48 +60,8 @@ func (b *BufferflowTimedBinary) BlockUntilReady(cmd string, id string) (bool, bo
5760
return true, false
5861
}
5962

60-
// not implemented, we are gonna use OnIncomingDataBinary
6163
func (b *BufferflowTimedBinary) OnIncomingData(data string) {
62-
b.Input <- []byte(data)
63-
}
64-
65-
// Clean out b.sem so it can truly block
66-
func (b *BufferflowTimedBinary) ClearOutSemaphore() {
67-
}
68-
69-
func (b *BufferflowTimedBinary) BreakApartCommands(cmd string) []string {
70-
return []string{cmd}
71-
}
72-
73-
func (b *BufferflowTimedBinary) Pause() {
74-
return
75-
}
76-
77-
func (b *BufferflowTimedBinary) Unpause() {
78-
return
79-
}
80-
81-
func (b *BufferflowTimedBinary) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool {
82-
return false
83-
}
84-
85-
func (b *BufferflowTimedBinary) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool {
86-
return false
87-
}
88-
89-
func (b *BufferflowTimedBinary) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool {
90-
return false
91-
}
92-
93-
func (b *BufferflowTimedBinary) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool {
94-
return false
95-
}
96-
97-
func (b *BufferflowTimedBinary) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool {
98-
return false
99-
}
100-
101-
func (b *BufferflowTimedBinary) ReleaseLock() {
64+
b.input <- []byte(data)
10265
}
10366

10467
func (b *BufferflowTimedBinary) IsBufferGloballySendingBackIncomingData() bool {
@@ -107,5 +70,5 @@ func (b *BufferflowTimedBinary) IsBufferGloballySendingBackIncomingData() bool {
10770

10871
func (b *BufferflowTimedBinary) Close() {
10972
b.ticker.Stop()
110-
close(b.Input)
73+
close(b.input)
11174
}

0 commit comments

Comments
 (0)