-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
170 lines (139 loc) · 3.07 KB
/
client.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
package club
import (
"encoding/json"
"errors"
"net/url"
"os"
"os/signal"
"strings"
"time"
"github.com/gorilla/websocket"
"io/ioutil"
"fmt"
"github.com/uber-go/zap"
)
type message struct {
Type string `json:"type"`
Data interface{} `json:"data"`
}
type data struct {
States map[string]message `json:"states"`
Events map[string]string `json:"events"`
}
func StartClient(dataFile string) error {
if len(strings.TrimSpace(dataFile)) == 0 {
return errors.New("must specify path to data file")
}
bytes, err := ioutil.ReadFile(dataFile)
if err != nil {
return err
}
d := &data{}
if err := json.Unmarshal(bytes, d); err != nil {
return err
}
if err := connect(d); err != nil {
return err
}
return nil
}
func connect(d *data) error {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "ws", Host: "localhost:8282", Path: "/ws"}
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
return err
}
defer c.Close()
done := make(chan struct{})
in := make(chan message, 1)
go func() {
defer c.Close()
defer close(done)
for {
msg := message{}
err := c.ReadJSON(&msg)
if err != nil {
log.Fatal("recv error:", zap.Error(err))
}
log.Info(fmt.Sprintf("recv: %v\n", msg))
if nextState, ok := d.Events[msg.Type]; ok {
if nextCommand, ok := d.States[nextState]; ok {
in <- nextCommand
}
}
}
}()
in <- d.States["#start"]
for {
select {
case msg := <-in:
err := c.WriteJSON(&msg)
if err != nil {
log.Fatal("send error:", zap.Error(err))
}
log.Info(fmt.Sprintf("\n---------------\nsend: %v\n", msg))
case <-interrupt:
log.Info("interrupt")
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Info("close client:", zap.Error(err))
}
select {
case <-done:
case <-time.After(time.Second):
}
c.Close()
}
}
return nil
}
/*func initClient(name string, in chan message, out chan message) {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "ws", Host: "localhost:8282", Path: "/ws"}
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatalln("dial error:", err)
}
defer c.Close()
done := make(chan struct{})
go func() {
defer c.Close()
defer close(done)
for {
msg := message{}
err := c.ReadJSON(&msg)
if err != nil {
log.Fatalln("recv error:", err)
return
}
log.Printf("[%s] recv: %v\n", name, msg)
out <- msg
}
}()
for {
select {
case msg := <-in:
err := c.WriteJSON(&msg)
if err != nil {
log.Fatalln("send error:", err)
}
log.Printf("---------------\n[%s] send: %v\n", name, msg)
case <-interrupt:
log.Println("interrupt")
err := c.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("close client:", err)
}
select {
case <-done:
case <-time.After(time.Second):
}
c.Close()
}
}
}
*/