-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathbufferflow_timedbinary.go
79 lines (69 loc) · 2.05 KB
/
bufferflow_timedbinary.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
package main
import (
"encoding/json"
"time"
log "github.com/sirupsen/logrus"
)
type BufferflowTimedBinary struct {
port string
output chan []byte
input chan []byte
done chan bool
ticker *time.Ticker
bufferedOutputBinary []byte
sPortBinary string
}
func NewBufferflowTimedBinary(port string, output chan []byte) *BufferflowTimedBinary {
return &BufferflowTimedBinary{
port: port,
output: output,
input: make(chan []byte),
done: make(chan bool),
ticker: time.NewTicker(16 * time.Millisecond),
bufferedOutputBinary: nil,
sPortBinary: "",
}
}
func (b *BufferflowTimedBinary) Init() {
log.Println("Initting timed buffer binary flow (output once every 16ms)")
go b.consumeInput()
}
func (b *BufferflowTimedBinary) consumeInput() {
Loop:
for {
select {
case data := <-b.input: // use the buffer and append data to it
b.bufferedOutputBinary = append(b.bufferedOutputBinary, data...)
b.sPortBinary = b.port
case <-b.ticker.C: // after 16ms send the buffered output message
if b.bufferedOutputBinary != nil {
m := SpPortMessageRaw{b.sPortBinary, b.bufferedOutputBinary}
buf, _ := json.Marshal(m)
// data is now encoded in base64 format
// need a decoder on the other side
b.output <- buf
// reset the buffer and the port
b.bufferedOutputBinary = nil
b.sPortBinary = ""
}
case <-b.done:
break Loop //this is required, a simple break statement would only exit the innermost switch statement
}
}
close(b.input)
}
func (b *BufferflowTimedBinary) BlockUntilReady(cmd string, id string) (bool, bool) {
//log.Printf("BlockUntilReady() start\n")
return true, false
}
func (b *BufferflowTimedBinary) OnIncomingData(data string) {
b.input <- []byte(data)
}
func (b *BufferflowTimedBinary) IsBufferGloballySendingBackIncomingData() bool {
return true
}
func (b *BufferflowTimedBinary) Close() {
b.ticker.Stop()
b.done <- true
close(b.done)
}