-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
433 lines (357 loc) · 10 KB
/
main.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package goADS
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
"sync"
"time"
)
type Connection struct {
ip string
netid string
port uint16
connection net.Conn
target AMSAddress
source AMSAddress
sendChannel chan []byte
symbols map[string]ADSSymbol
datatypes map[string]ADSSymbolUploadDataType
shutdown chan bool
shutdownFinal chan bool
WaitGroup sync.WaitGroup
WaitGroupFinal sync.WaitGroup
// List of active requests that waits a response, invokeid is key and value is a channel to the request rutine
activeRequests map[uint32]chan []byte
activeNotifications map[uint32]chan []byte
invokeID uint32
invokeIDmutex *sync.Mutex
// Shutdown tools
}
type AMSAddress struct {
netid [6]byte
port uint16
}
var buf [1024000]byte
// Connection
func NewConnection(ip string, netid string, port uint16) (conn *Connection, err error) { /*{{{*/
defer logger.Flush()
conn = &Connection{ip: ip, netid: netid, port: port}
conn.activeRequests = map[uint32]chan []byte{}
conn.activeNotifications = map[uint32]chan []byte{}
conn.invokeID = 0
conn.invokeIDmutex = &sync.Mutex{}
conn.sendChannel = make(chan []byte)
return
}
func (conn *Connection) Connect() {
var err error
logger.Infof("Dailing ip: %s NetID: %s", conn.ip, conn.netid)
conn.connection, err = net.Dial("tcp", fmt.Sprintf("%s:48898", conn.ip))
//conn.connection, err = net.Dial("tcp", fmt.Sprintf("%s:6666",ip))
if err != nil {
return
}
logger.Trace("Connected")
conn.shutdown = make(chan bool)
conn.shutdownFinal = make(chan bool)
conn.target = stringToNetId(conn.netid)
conn.target.port = conn.port
localhost, _, _ := net.SplitHostPort(conn.connection.LocalAddr().String())
conn.source = stringToNetId(localhost)
conn.source.netid[4] = 1
conn.source.netid[5] = 1
conn.source.port = 800
go reciveWorker(conn)
go transmitWorker(conn)
return
} /*}}}*/
func (conn *Connection) Close() { /*{{{*/
logger.Critical("CLOSE is called")
if conn.shutdown != nil {
logger.Debug("Sending shutdown to workers")
close(conn.shutdown)
conn.shutdown = nil
logger.Debug("Waiting for workers to close")
conn.WaitGroup.Wait()
}
if conn.shutdownFinal != nil {
logger.Debug("Sending shutdown to connection")
close(conn.shutdownFinal)
conn.shutdownFinal = nil
logger.Debug("Waiting for connection to close")
conn.WaitGroupFinal.Wait()
}
logger.Critical("Close DONE")
} /*}}}*/
func (conn *Connection) Wait() { /*{{{*/
logger.Debug("Waiting for everything to close")
conn.WaitGroup.Wait()
conn.WaitGroupFinal.Wait()
logger.Info("All routines are closed")
} /*}}}*/
func (conn *Connection) Find(name string) (list []*ADSSymbol) { /*{{{*/
logger.Debug("Find: ", name)
if conn == nil {
logger.Error("Failed FIND, connection is nil pointer")
return
}
for i, _ := range conn.symbols {
symbol := conn.symbols[i]
if len(name) >= len(symbol.FullName) && name[:len(symbol.FullName)] == symbol.FullName {
found := symbol.Self.Find(name)
for i, _ := range found {
item := found[i]
list = append(list, item)
}
}
}
logger.Debug("Found ", len(list), " tags")
return
} /*}}}*/
func (conn *Connection) Value(name string) (value string) { /*{{{*/
logger.Debug("Value: ", name)
list := conn.Find(name)
for i, _ := range list {
symbol := list[i]
if len(symbol.FullName) >= len(name) && symbol.FullName == name {
logger.Debug("Found value ", symbol.Value)
return symbol.Value
break
} else {
logger.Debug("Not ", symbol.FullName)
}
}
return
} /*}}}*/
func (conn *Connection) Set(name, value string) { /*{{{*/
logger.Debug("Set: ", name, "=", value)
if conn == nil {
logger.Error("Failed SET, connection is nil pointer")
return
}
list := conn.Find(name)
for i, _ := range list {
symbol := list[i]
if len(symbol.FullName) >= len(name) && symbol.FullName == name {
if symbol.Self.conn == nil {
logger.Error("Failed SET, connection is nil pointer")
return
}
logger.Debug("Write tag")
symbol.Self.Write(value)
return
}
}
} /*}}}*/
func (conn *Connection) sendRequest(command uint16, data []byte) (response []byte, err error) { /*{{{*/
if conn == nil {
logger.Error("Failed to encode header, connection is nil pointer")
return
}
conn.WaitGroup.Add(1)
defer conn.WaitGroup.Done()
// First, request a new invoke id
id := conn.getNewInvokeId()
// Create a channel for the response
conn.activeRequests[id] = make(chan []byte)
pack := conn.encode(command, data, id)
select {
case conn.sendChannel <- pack:
// Sent successfully
case <-time.After(time.Second * 8):
return response, errors.New("Timeout, failed to send message")
case <-conn.shutdownFinal:
logger.Info("sendRequest aborted due to shutdown")
return response, errors.New("Request aborted, shutdown initiated")
}
select {
case response = <-conn.activeRequests[id]:
return
case <-time.After(time.Second * 8):
return response, errors.New("Timeout, got no answer in 4sec")
case <-conn.shutdownFinal:
logger.Info("sendRequest aborted due to shutdown")
return response, errors.New("Request aborted, shutdown initiated")
}
return
} /*}}}*/
func (conn *Connection) createNotificationWorker(data []byte, callback func([]byte)) (handle uint32, err error) { /*{{{*/
conn.WaitGroup.Add(1)
defer conn.WaitGroup.Done()
// First, request a new invoke id
id := conn.getNewInvokeId()
// Create a channel for the response
conn.activeRequests[id] = make(chan []byte)
pack := conn.encode(uint16(6), data, id)
select {
case conn.sendChannel <- pack:
// Sent successfully
case <-time.After(time.Second * 8):
return 0, errors.New("Timeout, failed to send message")
case <-conn.shutdown:
logger.Info("createNotificationWorker aborted due to shutdown")
return 0, errors.New("Request aborted, shutdown initiated")
}
select {
case response := <-conn.activeRequests[id]:
result := binary.LittleEndian.Uint32(response[0:4])
handle = binary.LittleEndian.Uint32(response[4:8])
if result > 0 {
err = errors.New("Got ADS error number: " + strconv.FormatUint(uint64(result), 10) + " when creating a notification handle")
return
}
go func() {
conn.WaitGroup.Add(1)
defer conn.WaitGroup.Done()
logger.Debug("Started notification reciver for ", handle)
conn.activeNotifications[handle] = make(chan []byte, 100)
Label:
for {
select {
case response = <-conn.activeNotifications[handle]:
//logger.Warn(hex.Dump(response))
go callback(response)
case <-conn.shutdown:
logger.Info("createNotificationWorker (2) aborted due to shutdown")
break Label
}
}
logger.Info("Trying to remove notifications again for ", handle)
conn.DeleteDeviceNotification(handle)
close(conn.activeNotifications[handle])
logger.Debug("Closed notification reciver for ", handle)
}()
return
case <-time.After(time.Second * 8):
return handle, errors.New("Timeout, got no answer in 4sec")
case <-conn.shutdown:
logger.Debug("Aborted createNotificationWorker")
return handle, errors.New("Request aborted, shutdown initiated")
}
return
} /*}}}*/
func listen(conn *Connection) <-chan []byte { /*{{{*/
c := make(chan []byte)
go func(conn *Connection) {
b := make([]byte, 1024)
for {
n, err := conn.connection.Read(b)
if n > 0 {
res := make([]byte, n)
copy(res, b[:n])
c <- res
}
if err == io.EOF {
//fmt.Println("client: Read EOF",n)
break
}
if err != nil {
logger.Errorf("Failed to read socket: %s", err)
c <- nil
return
}
}
}(conn)
return c
} /*}}}*/
// Helpers
func stringToNetId(source string) (result AMSAddress) { /*{{{*/
localhost_split := strings.Split(source, ".")
for i, a := range localhost_split {
value, _ := strconv.ParseUint(a, 10, 8)
result.netid[i] = byte(value)
}
return
} /*}}}*/
func (conn *Connection) getNewInvokeId() uint32 { /*{{{*/
conn.invokeIDmutex.Lock()
conn.invokeID++
id := conn.invokeID
conn.invokeIDmutex.Unlock()
return id
} /*}}}*/
// Workers
func reciveWorker(conn *Connection) { /*{{{*/
conn.WaitGroupFinal.Add(1)
defer conn.WaitGroupFinal.Done()
// Create a buffer so we can join halfdone messages
var buff bytes.Buffer
// Create a listner
read := listen(conn)
loop:
for {
select {
case data := <-read:
if data == nil {
logger.Error("Got an error from the socket reader")
break loop
}
logger.Tracef("Got data!: \r\n%s", hex.Dump(data))
// Add it to the buffer
buff.Write(data)
// Decode the AMS header
for buff.Len() >= 38 {
logger.Tracef("Buffer len: %d bytes", buff.Len())
// Read the header
header := make([]byte, 38)
buff.Read(header)
command, length, invoke, err := conn.decode(header)
if err != nil {
logger.Warnf("Failed to decode AMS header: %s", err)
continue
}
// Read the body
pack := make([]byte, length)
n, _ := buff.Read(pack)
if n != int(length) {
logger.Tracef("Did not get the whole message, only got %d bytes of %d, adding data back to buffer", n, length)
buff.Write(header)
buff.Write(pack[:n])
break // Wait for more data
}
switch command {
case 8:
conn.DeviceNotification(pack)
default:
// Check if the response channel exists and is open
_, test := conn.activeRequests[invoke]
if test {
// Try to send the response to the waiting request function
select {
case conn.activeRequests[invoke] <- pack:
logger.Tracef("Successfully deliverd answer to invoke %d - command %d", invoke, command)
default:
}
} else {
logger.Debug("Got broadcast, invoke: ", invoke)
logger.Debug(hex.Dump(pack))
}
}
}
case <-conn.shutdownFinal:
logger.Debug("Exit reciveWorker")
break loop
}
}
} /*}}}*/
func transmitWorker(conn *Connection) { /*{{{*/
conn.WaitGroupFinal.Add(1)
defer conn.WaitGroupFinal.Done()
loop:
for {
select {
case data := <-conn.sendChannel:
logger.Tracef("Sending %d bytes", len(data))
go conn.connection.Write(data)
case <-conn.shutdownFinal:
logger.Debug("Exit transmitWorker")
break loop
}
}
} /*}}}*/