-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.go
201 lines (167 loc) · 3.84 KB
/
simulation.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"context"
"encoding/hex"
"errors"
"log"
"strings"
"time"
)
const (
CMD = "cmd"
PUTS = "puts"
PUT_HEX = "put_hex"
SLEEP = "sleep"
)
type Config struct {
Commands map[string][]map[string]string
Input []map[string]string
Default []string
Mutex [][]string
}
func (this *Config) GetAllCommandName() []string {
var commands []string
for command, _ := range this.Commands {
commands = append(commands, command)
}
return commands
}
func (this *Config) hasCommand(cmd string) bool {
for command, _ := range this.Commands {
if command == cmd {
return true
}
}
return false
}
func (this *Config) Verify() error {
var store []string
// input
for _, item := range this.Input {
for command, _ := range item {
if !this.hasCommand(command) {
store = append(store, "Input: No Command "+command)
}
}
}
// default
for _, command := range this.Default {
if !this.hasCommand(command) {
store = append(store, "Default: No Command "+command)
}
}
// mutex
for _, item := range this.Mutex {
for _, command := range item {
if !this.hasCommand(command) {
store = append(store, "Mutex: No Command "+command)
}
}
}
if len(store) == 0 {
return nil
}
return errors.New(strings.Join(store, "\n"))
}
func (this *Config) DetectMutex(command string) [][]string {
var results [][]string
for _, m := range this.Mutex {
for _, i := range m {
if command == i {
results = append(results, m)
}
}
}
return results
}
type Simulation struct {
running map[string]context.CancelFunc
config Config
logger *log.Logger
Input chan []byte
Output chan []byte
Command chan string
}
func (this *Simulation) RunDefault() {
for _, command := range this.config.Default {
this.Command <- command
}
}
func (this *Simulation) ipc() {
for raw := range this.Input {
match := false
for _, item := range this.config.Input {
for k, v := range item {
if v == string(raw) {
match = true
this.Command <- k
}
}
}
if !match {
this.logger.Printf("Ignore Input: %v\n", string(raw))
}
}
}
func (this *Simulation) Run() {
this.running = make(map[string]context.CancelFunc)
for command := range this.Command {
for _, line := range this.config.DetectMutex(command) {
for _, i := range line {
// Cancel All Mutex
if this.running[i] != nil {
this.running[i]()
delete(this.running, i)
}
}
}
// Cancel duplicate
if this.running[command] != nil {
this.running[command]()
}
// New Cancel() save to this.running
ctx, cancel := context.WithCancel(context.Background())
this.running[command] = cancel
go this.doCommand(command, ctx)
}
}
func (this *Simulation) doCommand(command string, ctx context.Context) {
this.logger.Printf("=== RUN %v RUN === \n", command)
defer func() {
this.logger.Printf("=== END %v END === \n", command)
}()
for _, value := range this.config.Commands[command] {
select {
case <-ctx.Done():
this.logger.Printf("Executed cancel() \n")
return
default:
//this.logger.Println("Exec: ", value)
switch {
case value[PUTS] != "":
this.logger.Printf("PUTS: %v\n", value[PUTS])
this.Output <- []byte(value[PUTS] + "\n")
case value[PUT_HEX] != "":
this.logger.Printf("PUT_HEX: %v\n", value[PUT_HEX])
str, err := hex.DecodeString(value[PUT_HEX])
if err != nil {
this.logger.Panicln(err)
}
this.Output <- []byte(str)
case value[SLEEP] != "":
this.logger.Printf("SLEEP: %v\n", value[SLEEP])
if second, err := time.ParseDuration(value[SLEEP]); err != nil {
this.logger.Println("ERROR: ", err)
} else {
time.Sleep(second)
}
case value[CMD] != "":
this.logger.Printf("CMD: %v\n", value[CMD])
//go this.doCommand(value[CMD], ctx)
this.Command <- value[CMD]
default:
this.logger.Println("Unknow: ", value)
}
}
}
}