-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathbufferflow_timedraw.go
116 lines (94 loc) · 2.38 KB
/
bufferflow_timedraw.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
package main
import (
"encoding/json"
"time"
log "github.com/sirupsen/logrus"
)
type BufferflowTimedRaw struct {
Name string
Port string
Output chan []byte
Input chan string
done chan bool
ticker *time.Ticker
}
var (
bufferedOutputRaw []byte
sPortRaw string
)
func (b *BufferflowTimedRaw) Init() {
log.Println("Initting timed buffer raw flow (output once every 16ms)")
bufferedOutputRaw = nil
sPortRaw = ""
go func() {
b.ticker = time.NewTicker(16 * time.Millisecond)
b.done = make(chan bool)
Loop:
for {
select {
case data := <-b.Input:
bufferedOutputRaw = append(bufferedOutputRaw, []byte(data)...)
sPortRaw = b.Port
case <-b.ticker.C:
if bufferedOutputRaw != nil {
m := SpPortMessageRaw{sPortRaw, bufferedOutputRaw}
buf, _ := json.Marshal(m)
// data is now encoded in base64 format
// need a decoder on the other side
b.Output <- []byte(buf)
bufferedOutputRaw = nil
sPortRaw = ""
}
case <-b.done:
break Loop
}
}
close(b.Input)
}()
}
func (b *BufferflowTimedRaw) BlockUntilReady(cmd string, id string) (bool, bool) {
//log.Printf("BlockUntilReady() start\n")
return true, false
}
// not implemented, we are gonna use OnIncomingData
func (b *BufferflowTimedRaw) OnIncomingDataBinary(data []byte) {
}
func (b *BufferflowTimedRaw) OnIncomingData(data string) {
b.Input <- data
}
// Clean out b.sem so it can truly block
func (b *BufferflowTimedRaw) ClearOutSemaphore() {
}
func (b *BufferflowTimedRaw) BreakApartCommands(cmd string) []string {
return []string{cmd}
}
func (b *BufferflowTimedRaw) Pause() {
return
}
func (b *BufferflowTimedRaw) Unpause() {
return
}
func (b *BufferflowTimedRaw) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimedRaw) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimedRaw) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimedRaw) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool {
return false
}
func (b *BufferflowTimedRaw) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool {
return false
}
func (b *BufferflowTimedRaw) ReleaseLock() {
}
func (b *BufferflowTimedRaw) IsBufferGloballySendingBackIncomingData() bool {
return true
}
func (b *BufferflowTimedRaw) Close() {
b.ticker.Stop()
close(b.Input)
}