-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
81 lines (72 loc) · 1.39 KB
/
process.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
package firestorm
import (
_ "log"
"sync"
)
type StateIfc interface {
ProcessData(
msg Message,
outChan chan Message,
errChan chan error,
)
//(outChan chan Message, errChan chan error)
}
type chanBrancher struct {
branchOutChans []chan Message
}
type chanMerger struct {
mergeInChans []chan Message
mg sync.WaitGroup
}
type Process struct {
StateIfc
StdIn chan Message
StdOut chan Message
StdErr chan error
Output *Process
chanBrancher
chanMerger
}
func (p *Process) branchOut() {
go func() {
// log.Println("branch:channel: ", p.StdOut)
for d := range p.StdOut {
// log.Println("branch:msg: ", d, p)
for _, out := range p.branchOutChans {
dc := d
// copy(dc, d)
out <- dc
}
}
// Once all data is received, also close all the outputs
for _, out := range p.branchOutChans {
close(out)
}
}()
}
func (p *Process) mergeIn() {
// Start a merge goroutine for each input channel.
mergeData := func(c chan Message) {
for d := range c {
// log.Println("merge:msg: ", d)
p.StdIn <- d
}
p.mg.Done()
}
p.mg.Add(len(p.mergeInChans))
for _, in := range p.mergeInChans {
go mergeData(in)
}
go func() {
p.mg.Wait()
close(p.StdIn)
}()
}
func NewProcess(ifc StateIfc) *Process {
nc := new(Process)
nc.StateIfc = ifc
nc.StdIn = make(chan Message, 1)
nc.StdOut = make(chan Message, 1)
nc.StdErr = make(chan error, 1)
return nc
}