-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathbufferflow_default.go
52 lines (44 loc) · 1.04 KB
/
bufferflow_default.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
package main
import (
"encoding/json"
log "github.com/sirupsen/logrus"
)
type BufferflowDefault struct {
port string
output chan<- []byte
input chan string
done chan bool
}
func NewBufferflowDefault(port string, output chan<- []byte) *BufferflowDefault {
return &BufferflowDefault{
port: port,
output: output,
input: make(chan string),
done: make(chan bool),
}
}
func (b *BufferflowDefault) Init() {
log.Println("Initting default buffer flow (which means no buffering)")
go b.consumeInput()
}
func (b *BufferflowDefault) consumeInput() {
Loop:
for {
select {
case data := <-b.input:
m := SpPortMessage{b.port, data}
message, _ := json.Marshal(m)
b.output <- message
case <-b.done:
break Loop //this is required, a simple break statement would only exit the innermost switch statement
}
}
close(b.input) // close the input channel at the end of the computation
}
func (b *BufferflowDefault) OnIncomingData(data string) {
b.input <- data
}
func (b *BufferflowDefault) Close() {
b.done <- true
close(b.done)
}