-
Notifications
You must be signed in to change notification settings - Fork 0
/
gate.go
128 lines (103 loc) · 2.54 KB
/
gate.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
package cloud_computer
import (
"context"
"github.com/go-redis/redis/v9"
"reflect"
)
var _ Gater = (*Gate)(nil)
type Gate struct {
Name string
Inputs []Element
Outputs []Element
UseOptimization bool
IsVerbose bool
PreviousInputs []bool
InputChannels []<-chan bool
PreviousOutputs []bool
OutputChannels []chan<- bool
}
func (g *Gate) Init(ctx context.Context, client *redis.Client) {
g.PreviousInputs = make([]bool, len(g.Inputs))
g.PreviousOutputs = make([]bool, len(g.Outputs))
g.InputChannels = make([]<-chan bool, len(g.Inputs))
for i, element := range g.GetInputs() {
if element.IsStaticValue {
g.PreviousInputs[i] = element.StaticValue
continue
}
g.InputChannels[i] = ReadAsyncRedis(ctx, client, element.String())
v, err := ReadRedis(ctx, client, element.String()+".status")
if err != nil {
panic(err)
}
g.PreviousInputs[i] = v
}
for i, element := range g.Outputs {
element.GateName = g.GetName()
g.Outputs[i] = element
}
g.OutputChannels = make([]chan<- bool, len(g.Outputs))
for i, element := range g.GetOutputs() {
g.OutputChannels[i] = WriteAsyncRedis(ctx, client, element.String())
}
}
func (g *Gate) SelectCases() (cases []reflect.SelectCase) {
cases = make([]reflect.SelectCase, len(g.InputChannels))
for i, ch := range g.InputChannels {
cases[i] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ch),
}
}
return
}
func (g *Gate) Handler(_ int, _ bool) ([]bool, bool) {
panic("implement me")
}
func (g *Gate) GetName() string {
return g.Name
}
func (g *Gate) GetInputs() (inputs []Element) {
return g.Inputs
}
func (g *Gate) GetInputSize() int {
return len(g.Inputs)
}
func (g *Gate) GetOutputs() (outputs []Element) {
return g.Outputs
}
func (g *Gate) GetOutputSize() int {
return len(g.Outputs)
}
func (g *Gate) GetOutputChannels() []chan<- bool {
return g.OutputChannels
}
func (g *Gate) SetOptimization(v bool) {
g.UseOptimization = v
}
func (g *Gate) SetVerbose(v bool) {
g.IsVerbose = v
}
func (g *Gate) Changed(v1, v2 []bool) bool {
for i := range v1 {
if v1[i] != v2[i] {
return true
}
}
return false
}
func (g *Gate) GetType() string {
panic("not implements")
}
type Gater interface {
Init(ctx context.Context, client *redis.Client)
Handler(index int, input bool) (o []bool, changed bool)
GetName() string
GetInputs() (inputs []Element)
GetInputSize() int
SelectCases() (cases []reflect.SelectCase)
GetOutputs() (outputs []Element)
GetOutputSize() int
GetOutputChannels() []chan<- bool
GetType() string
}