-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunc_dst.go
55 lines (47 loc) · 907 Bytes
/
func_dst.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
package fgbase
import (
"bufio"
"fmt"
"io"
)
type irw struct {
Initialized bool
RW *bufio.ReadWriter
}
func dstFire(n *Node) error {
a := n.Srcs[0]
s := n.Aux.(*irw)
rw := s.RW
var err error
// read ack
a.Flow = true
if s.Initialized {
_, err = rw.ReadString('\n')
if err != nil {
n.LogError("%v", err)
close(a.Ack)
a.Ack = nil
return nil
}
} else {
s.Initialized = true
}
// write data
_, err = rw.WriteString(fmt.Sprintf("%v\n", a.SrcGet()))
if err != nil {
n.LogError("%v", err)
close(a.Ack)
a.Ack = nil
return nil
}
rw.Flush()
return nil
}
// FuncDst writes data and waits for an acknowledging '\n'.
func FuncDst(a Edge, rw io.ReadWriter) Node {
node := MakeNode("dst", []*Edge{&a}, nil, nil, dstFire)
reader := bufio.NewReader(rw)
writer := bufio.NewWriter(rw)
node.Aux = &irw{RW: bufio.NewReadWriter(reader, writer)}
return node
}