/
inandouts.go
144 lines (122 loc) · 2.57 KB
/
inandouts.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package inout
import (
"sync/atomic"
"time"
"github.com/ocdogan/fluentgo/config"
"github.com/ocdogan/fluentgo/lib"
"github.com/ocdogan/fluentgo/log"
)
type InAndOuts struct {
state uint32
iman *InManager
oman *OutManager
mode lib.ServiceMode
logger log.Logger
quitSignal <-chan bool
}
func NewInOutManager(mode lib.ServiceMode, config *config.FluentConfig, logger log.Logger, quitSignal <-chan bool) *InAndOuts {
var (
iman *InManager
oman *OutManager
)
if mode == lib.SmIn || mode == lib.SmInOut {
iman = NewInManager(config, logger)
}
if mode == lib.SmOut || mode == lib.SmInOut {
oman = NewOutManager(config, logger)
}
return &InAndOuts{
iman: iman,
oman: oman,
mode: mode,
logger: logger,
quitSignal: quitSignal,
}
}
func (iao *InAndOuts) Process() {
if !atomic.CompareAndSwapUint32(&iao.state, 0, 1) {
return
}
defer func() {
atomic.StoreUint32(&iao.state, 0)
iao.logger.Println("* Stopping service...")
}()
iao.logger.Println("* Starting service...")
imActive := iao.iman != nil
omActive := iao.oman != nil
if !(imActive || omActive) {
return
}
// Start processes
imCompleted := make(chan bool)
omCompleted := make(chan bool)
if imActive {
iao.iman.Process(imCompleted)
}
if omActive {
iao.oman.Process(omCompleted)
}
defer func() {
if iao.iman != nil {
iao.iman.Close()
}
if iao.oman != nil {
iao.oman.Close()
}
}()
for imActive || omActive {
select {
case <-iao.quitSignal:
imActive = false
omActive = false
return
case <-imCompleted:
imActive = false
if !omActive {
return
}
case <-omCompleted:
omActive = false
if !imActive {
return
}
}
time.Sleep(time.Millisecond)
}
}
func (iao *InAndOuts) GetInputs() []InOutInfo {
if iao != nil && iao.iman != nil {
return iao.iman.GetInputs()
}
return nil
}
func (iao *InAndOuts) GetInputsWithType(typ string) []InOutInfo {
if iao != nil && iao.iman != nil {
return iao.iman.GetInputs()
}
return nil
}
func (iao *InAndOuts) GetOutputs() []InOutInfo {
if iao != nil && iao.oman != nil {
return iao.oman.GetOutputs()
}
return nil
}
func (iao *InAndOuts) GetOutputsWithType(typ string) []InOutInfo {
if iao != nil && iao.oman != nil {
return iao.oman.GetOutputsWithType(typ)
}
return nil
}
func (iao *InAndOuts) FindInput(id string) IOClient {
if iao != nil && iao.iman != nil {
return iao.iman.FindInput(id)
}
return nil
}
func (iao *InAndOuts) FindOutput(id string) IOClient {
if iao != nil && iao.oman != nil {
return iao.oman.FindOutput(id)
}
return nil
}