-
Notifications
You must be signed in to change notification settings - Fork 235
/
stdin_input.go
62 lines (51 loc) · 1.22 KB
/
stdin_input.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
package input
import (
"bufio"
"os"
"time"
"github.com/childe/gohangout/codec"
"github.com/childe/gohangout/topology"
"github.com/golang/glog"
)
type StdinInput struct {
config map[interface{}]interface{}
decoder codec.Decoder
scanner *bufio.Scanner
messages chan []byte
stop bool
}
func init() {
Register("Stdin", newStdinInput)
}
func newStdinInput(config map[interface{}]interface{}) topology.Input {
var codertype string = "plain"
if v, ok := config["codec"]; ok {
codertype = v.(string)
}
p := &StdinInput{
config: config,
decoder: codec.NewDecoder(codertype),
scanner: bufio.NewScanner(os.Stdin),
messages: make(chan []byte, 10),
}
return p
}
func (p *StdinInput) ReadOneEvent() map[string]interface{} {
if p.scanner.Scan() {
t := p.scanner.Bytes()
msg := make([]byte, len(t))
copy(msg, t)
return p.decoder.Decode(msg)
}
if err := p.scanner.Err(); err != nil {
glog.Errorf("stdin scan error: %v", err)
} else {
// EOF here. when stdin is closed by C-D, cpu will raise up to 100% if not sleep
time.Sleep(time.Millisecond * 1000)
}
return nil
}
func (p *StdinInput) Shutdown() {
// what we need is to stop emit new event; close messages or not is not important
p.stop = true
}