-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathbufferflow_timed.go
119 lines (95 loc) · 2.28 KB
/
bufferflow_timed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"encoding/json"
"time"
log "github.com/sirupsen/logrus"
)
type BufferflowTimed struct {
Name string
Port string
Output chan []byte
Input chan string
done chan bool
ticker *time.Ticker
}
var (
bufferedOutput string
sPort string
)
func (b *BufferflowTimed) Init() {
log.Println("Initting timed buffer flow (output once every 16ms)")
bufferedOutput = ""
sPort = ""
go func() {
b.ticker = time.NewTicker(16 * time.Millisecond)
b.done = make(chan bool)
Loop:
for {
select {
case data := <-b.Input:
bufferedOutput = bufferedOutput + data
sPort = b.Port
case <-b.ticker.C:
if bufferedOutput != "" {
m := SpPortMessage{sPort, bufferedOutput}
buf, _ := json.Marshal(m)
// data is now encoded in base64 format
// need a decoder on the other side
b.Output <- []byte(buf)
bufferedOutput = ""
sPort = ""
}
case <-b.done:
break Loop
}
}
close(b.Input)
}()
}
func (b *BufferflowTimed) BlockUntilReady(cmd string, id string) (bool, bool) {
//log.Printf("BlockUntilReady() start\n")
return true, false
}
// not implemented, we are gonna use OnIncomingData
func (b *BufferflowTimed) OnIncomingDataBinary(data []byte) {
}
func (b *BufferflowTimed) OnIncomingData(data string) {
b.Input <- data
}
// Clean out b.sem so it can truly block
func (b *BufferflowTimed) ClearOutSemaphore() {
}
func (b *BufferflowTimed) BreakApartCommands(cmd string) []string {
return []string{cmd}
}
func (b *BufferflowTimed) Pause() {
return
}
func (b *BufferflowTimed) Unpause() {
return
}
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimed) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimed) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool {
return false
}
func (b *BufferflowTimed) ReleaseLock() {
}
func (b *BufferflowTimed) IsBufferGloballySendingBackIncomingData() bool {
return true
}
func (b *BufferflowTimed) Close() {
b.ticker.Stop()
b.done <- true
close(b.done)
}