-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamiAdapter.go
269 lines (215 loc) · 4.79 KB
/
amiAdapter.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package amiConnect
import (
"bufio"
"bytes"
"errors"
"log"
"net"
"strconv"
"time"
)
type AMIAdapter struct {
ip string
username string
password string
chanActions chan map[string]string
chanResponses chan map[string]string
chanEvents chan map[string]string
}
func NewAMIAdapter(ip string) (*AMIAdapter, error) {
var a = new(AMIAdapter)
a.ip = ip
conn, err := openConnection(ip)
if err != nil {
return nil, err
}
chanOutStreamReader := make(chan byte)
a.chanActions = make(chan map[string]string)
chanQuitActionWriter := actionWriter(conn, a.chanActions)
chanErrStreamReader := streamReader(conn, chanOutStreamReader)
chanOutStreamParser := streamParser(chanOutStreamReader)
a.chanResponses, a.chanEvents = classifier(chanOutStreamParser)
go func() {
for {
err := <-chanErrStreamReader
chanQuitActionWriter <- true
log.Println("TCP ERROR")
for i := 100; i >= 0; i-- {
if i == 0 {
log.Fatalln("Reconnect failed 100 times. Give up!")
}
log.Println("Try reconnect in 10 seconds")
time.Sleep(time.Second * 10)
conn, err = openConnection(ip)
if err != nil {
log.Println("Reconnect failed! Retries remaining: " + strconv.Itoa(i))
} else {
chanErrStreamReader = streamReader(conn, chanOutStreamReader)
chanQuitActionWriter = actionWriter(conn, a.chanActions)
_, err = a.Login(a.username, a.password)
if err != nil {
log.Fatalln("Login failed!")
}
break
}
}
}
}()
return a, nil
}
func (a *AMIAdapter) Login(username string, password string) (chan map[string]string, error) {
a.username = username
a.password = password
var action = map[string]string{
"Action": "Login",
"Username": a.username,
"Secret": a.password,
}
var result = a.Exec(action)
if result["Response"] == "Error" {
return nil, errors.New("Login failed: " + result["Message"])
}
return a.chanEvents, nil
}
func (a *AMIAdapter) Exec(action map[string]string) map[string]string {
a.chanActions <- action
var response = <-a.chanResponses
return response
}
func streamReader(conn *net.TCPConn, chanOut chan byte) (chanErr chan error) {
chanErr = make(chan error)
reader := bufio.NewReader(conn)
go func() {
for {
b, err := reader.ReadByte()
if err != nil {
chanErr <- err
return
}
chanOut <- b
}
}()
return chanErr
}
func actionWriter(conn *net.TCPConn, in chan map[string]string) (chanQuit chan bool) {
chanQuit = make(chan bool)
go func() {
for {
select {
case action := <-in:
{
var data = serialize(action)
_, err := conn.Write(data)
if err != nil {
return
}
}
case <-chanQuit:
{
return
}
}
}
}()
return chanQuit
}
func streamParser(in chan byte) (chanOut chan map[string]string) {
chanOut = make(chan map[string]string)
var data = make(map[string]string)
var wordBuf bytes.Buffer
var key string
var value string
var lastByte byte
var curByte byte
var state = 0 // 0: key state, 1: value state
go func() {
for {
lastByte = curByte
curByte = <-in
if curByte == ':' || curByte == '\n' {
continue
}
switch state {
case 0:
{
if curByte == ' ' {
if lastByte == ':' {
key = wordBuf.String()
wordBuf.Reset()
state = 1
}
} else if curByte == '\r' {
if len(value) > 0 {
chanOut <- data
data = make(map[string]string)
}
wordBuf.Reset()
key = ""
value = ""
lastByte = 0
curByte = 0
state = 0
} else {
wordBuf.WriteByte(curByte)
}
}
case 1:
{
if curByte == '\r' {
value = wordBuf.String()
wordBuf.Reset()
state = 0
data[key] = value
} else {
wordBuf.WriteByte(curByte)
}
}
}
}
}()
return chanOut
}
func classifier(in chan map[string]string) (chanOutResponses chan map[string]string, chanOutEvents chan map[string]string) {
chanOutResponses = make(chan map[string]string)
chanOutEvents = make(chan map[string]string)
go func() {
for {
data := <-in
for d := range data {
switch d {
case "Response":
chanOutResponses <- data
break
case "Event":
chanOutEvents <- data
break
}
}
}
}()
return chanOutResponses, chanOutEvents
}
func openConnection(ip string) (*net.TCPConn, error) {
socket := ip + ":5038"
raddr, err := net.ResolveTCPAddr("tcp", socket)
if err != nil {
return nil, err
}
conn, err := net.DialTCP("tcp", nil, raddr)
if err != nil {
return nil, err
}
return conn, err
}
func serialize(data map[string]string) []byte {
var outBuf bytes.Buffer
for key := range data {
value := data[key]
outBuf.WriteString(key)
outBuf.WriteString(": ")
outBuf.WriteString(value)
outBuf.WriteString("\n")
}
outBuf.WriteString("\n")
return outBuf.Bytes()
}