-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoacp.go
356 lines (319 loc) · 7.51 KB
/
goacp.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
package goworld
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"os"
)
// Acp holds I/O buffer to communicate with Magik ACP
type Acp struct {
Name string
io *bufio.ReadWriter
}
// NewAcp creates and init new Acp with name
func NewAcp(name string) *Acp {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
inout := bufio.NewReadWriter(reader, writer)
return &Acp{Name: name, io: inout}
}
// Flush send buffer data
func (a *Acp) Flush() {
a.io.Flush()
}
// Write writes buffer to Acp output
func (a *Acp) Write(buf []byte) {
a.io.Write(buf)
a.Flush()
}
// PutBool sends boolean value to Acp output
func (a *Acp) PutBool(b bool) {
var ival byte
if b {
ival = 1
}
a.io.WriteByte(ival)
a.Flush()
}
// PutUbyte sends unsigned byte value to Acp output
func (a *Acp) PutUbyte(value uint8) {
binary.Write(a.io, binary.LittleEndian, value)
a.Flush()
}
// PutByte sends byte value to Acp output
func (a *Acp) PutByte(value int8) {
binary.Write(a.io, binary.LittleEndian, value)
a.Flush()
}
// PutUshort sends unsigned short value to Acp output
func (a *Acp) PutUshort(value uint16) {
binary.Write(a.io, binary.LittleEndian, value)
a.Flush()
}
// PutShort sends short value to Acp output
func (a *Acp) PutShort(value int16) {
binary.Write(a.io, binary.LittleEndian, value)
a.Flush()
}
// PutUint sends int value to Acp output
func (a *Acp) PutUint(value uint32) {
binary.Write(a.io, binary.LittleEndian, value)
a.Flush()
}
// PutInt sends int value to Acp output
func (a *Acp) PutInt(value int32) {
binary.Write(a.io, binary.LittleEndian, value)
a.Flush()
}
// PutUlong sends unsigned long value to Acp output
func (a *Acp) PutUlong(value uint64) {
binary.Write(a.io, binary.LittleEndian, value)
a.Flush()
}
// PutLong sends long value to Acp output
func (a *Acp) PutLong(value int64) {
binary.Write(a.io, binary.LittleEndian, value)
a.Flush()
}
// PutShortFloat sends short float value to Acp output
func (a *Acp) PutShortFloat(value float32) {
binary.Write(a.io, binary.LittleEndian, value)
a.Flush()
}
// PutFloat sends float value to Acp output
func (a *Acp) PutFloat(value float64) {
binary.Write(a.io, binary.LittleEndian, value)
a.Flush()
}
// PutString sends string value to Acp output
func (a *Acp) PutString(s string) {
bytes := []byte(s)
l := len(bytes)
a.PutUshort(uint16(l))
a.Write(bytes)
}
// read bytes from Acp input
func (a *Acp) readBytes(n int) (buf []byte, err error) {
buf = make([]byte, 0, n)
_, err = a.io.Read(buf[:cap(buf)])
if err != nil && err != io.EOF {
err = fmt.Errorf("Error reading short: %v\n", err)
return
}
buf = buf[:n]
return
}
// ReadNumber reads number from Acp input
func (a *Acp) ReadNumber(data interface{}) {
if err := binary.Read(a.io, binary.LittleEndian, data); err != nil {
log.Fatal(err)
}
}
// GetBool reads boolean value from Acp input
func (a *Acp) GetBool() bool {
b, err := a.io.ReadByte()
if err != nil {
log.Fatalf("Error reading boolean %v\n", err)
}
return b == 1
}
// GetUbyte reads unsigned byte from Acp input
func (a *Acp) GetUbyte() int {
var res uint8
a.ReadNumber(&res)
return int(res)
}
// GetByte reads byte from Acp input
func (a *Acp) GetByte() int {
var res int8
a.ReadNumber(&res)
return int(res)
}
// GetUshort reads unsigned short from Acp input
func (a *Acp) GetUshort() int {
var res uint16
a.ReadNumber(&res)
return int(res)
}
// GetShort reads short from Acp input
func (a *Acp) GetShort() int {
var res int16
a.ReadNumber(&res)
return int(res)
}
// GetUint reads unsigned int from Acp input
func (a *Acp) GetUint() int {
var res uint32
a.ReadNumber(&res)
return int(res)
}
// GetInt reads unsigned int from Acp input
func (a *Acp) GetInt() int {
var res int32
a.ReadNumber(&res)
return int(res)
}
// GetUlong reads unsigned long from Acp input
func (a *Acp) GetUlong() uint64 {
var res uint64
a.ReadNumber(&res)
return res
}
// GetLong reads long from Acp input
func (a *Acp) GetLong() int64 {
var res int64
a.ReadNumber(&res)
return res
}
// GetShortFloat read float32 from Acp input
func (a *Acp) GetShortFloat() float32 {
var res float32
a.ReadNumber(&res)
return res
}
// GetFloat read float64 from Acp input
func (a *Acp) GetFloat() float64 {
var res float64
a.ReadNumber(&res)
return res
}
// GetString reads string from Acp input
func (a *Acp) GetString() string {
b := a.GetUshort()
buf, err := a.readBytes(b)
if err != nil {
log.Fatal(err)
}
return string(buf)
}
// VerifyConnection verify Acp process name
func (a *Acp) VerifyConnection(name string) bool {
acpName := a.GetString()
res := acpName == name
log.Printf("Name: %s; From SW: %s; Res: %v\n", name, acpName, res)
a.PutBool(!res)
return res
}
// EstablishProtocol checks Acp protocol
func (a *Acp) EstablishProtocol(minProtocol, maxProtocol int) bool {
min, max := minProtocol, maxProtocol
for {
in := a.GetUshort()
log.Printf("Protocol from SW: %d\n", in)
if in < min || in > max {
a.PutBool(true)
a.PutUshort(uint16(min))
a.PutUshort(uint16(max))
return false
}
break
}
a.PutBool(false)
return true
}
// Connect verify connection and protocol to Acp
func (a *Acp) Connect(processName string, protocolMin, protocolMax int) (err error) {
log.Printf("ACP started name: %s\n", processName)
if res := a.VerifyConnection(processName); !res {
err = errors.New("Error verify connection")
return
}
log.Println("Connection verified")
if res := a.EstablishProtocol(protocolMin, protocolMax); !res {
err = errors.New("Error establish protocol")
return
}
log.Println("Protocol established")
log.Println("Connected")
return
}
// Put convert value to dataType and send this value to ACP
func (a *Acp) Put(dataType string, value interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
return
}
}()
switch dataType {
case "boolean":
ival := value.(bool)
a.PutBool(ival)
case "unsigned_byte":
ival := value.(uint8)
a.PutUbyte(ival)
case "signed_byte":
ival := value.(int8)
a.PutByte(ival)
case "unsigned_short":
ival := value.(uint16)
a.PutUshort(ival)
case "signed_short":
ival := value.(int16)
a.PutShort(ival)
case "unsigned_int":
ival := value.(uint32)
a.PutUint(ival)
case "signed_int":
ival := value.(int32)
a.PutInt(ival)
case "unsigned_long":
ival := value.(uint64)
a.PutUlong(ival)
case "signed_long":
ival := value.(int64)
a.PutLong(ival)
case "short_float":
ival := value.(float32)
a.PutShortFloat(ival)
case "float":
ival := value.(float64)
a.PutFloat(ival)
case "chars":
ival := value.(string)
a.PutString(ival)
default:
return fmt.Errorf("Unsuported data type '%s' in Put method", dataType)
}
return nil
}
// Get method reads dataType value from ACP
func (a *Acp) Get(dataType string) (value interface{}, err *AcpErr) {
defer func() {
if r := recover(); r != nil {
err = NewAcpErr(fmt.Sprint(r.(error)))
return
}
}()
switch dataType {
case "boolean":
return a.GetBool(), nil
case "unsigned_byte":
return a.GetUbyte(), nil
case "signed_byte":
return a.GetByte(), nil
case "unsigned_short":
return a.GetUshort(), nil
case "signed_short":
return a.GetShort(), nil
case "unsigned_int":
return a.GetUint(), nil
case "signed_int":
return a.GetInt(), nil
case "unsigned_long":
return a.GetUlong(), nil
case "signed_long":
return a.GetLong(), nil
case "short_float":
return a.GetShortFloat(), nil
case "float":
return a.GetFloat(), nil
case "chars":
return a.GetString(), nil
default:
return nil, NewAcpErr(fmt.Sprintf("Unsuported data type '%s' in Get method", dataType))
}
}