-
Notifications
You must be signed in to change notification settings - Fork 0
/
emwin.go
207 lines (187 loc) · 4.7 KB
/
emwin.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
package main
import (
"bytes"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
)
const (
//servAddr = "140.90.128.146:1000"
servAddr = "2.pool.iemwin.net:2211"
mask = 0xFF
filepath = "/home/acb/emwin/"
)
var (
buffer []byte
header []byte
data []byte
files map[string][][]byte
last = make(map[string]string)
conn *net.TCPConn
)
var startFlag = []byte{47, 80, 70} // "/PF"
var q chan []byte
func main() {
c := make(chan []byte, 20)
go byteblaster(c)
for {
data = <-c
for _, value := range data {
buffer = append(buffer, value)
}
extractBlocks()
}
}
// pull out and process all blocks in current buffer
func extractBlocks() {
// find block start and remove what's before
for len(buffer) > 1116 {
blockStart := bytes.Index(buffer, startFlag)
if blockStart > -1 {
buffer = buffer[blockStart:]
blockEnd := bytes.Index(buffer[3:], startFlag)
if blockEnd > -1 {
block := buffer[:blockEnd+3]
buffer = buffer[blockEnd+3:]
fileIt(block)
} else {
break
}
}
}
}
// File ... Assemble received blocks into files and store
func fileIt(block []byte) {
header = block[0:80]
filename := string(header[3:15])
part := cleanint(header[18:20])
parts := cleanint(header[27:33])
filedate := strings.TrimRight(string(header[47:]), " \n")
data = block[80:1104]
// if the checksum matches, add to file collector
if checked(header, data) {
buildfile(filename, filedate, part, parts, data)
}
}
func buildfile(filename string, filedate string, part int, parts int, data []byte) {
// if we already have this version, ignore this block
if !(filedate > last[filename]) {
return
}
// in case of SAHOURLY.TXT, TAFALLUS.TXT (et al.?) do files need to be concatenated?
// if we aren't already assembling this one, create an accumulator for it
if len(files[filename]) == 0 {
newfile(filename, filedate, part, parts, data)
} else {
// if a known filename and the same date, add block to existing file
if string(files[filename][0]) == filedate {
files[filename][part] = data
} else { // but if date is different, create a new file structure for that filename
newfile(filename, filedate, part, parts, data)
}
}
// if file structure for current filename is complete, save it as file, clear from collector
if iscomplete(filename) {
filebytes := make([]byte, 1024)
i := 0
for i = 1; i < parts+1; i++ {
filebytes = append(filebytes, files[filename][i]...)
}
if filename != "FILLFILE.TXT" {
store(filename, filedate, filebytes)
last[filename] = filedate
}
delete(files, filename)
}
}
// create a new file structure ([][]byte), initialize with header and current block
func newfile(filename string, filedate string, part int, parts int, data []byte) {
file := make([][]byte, parts+1)
files = make(map[string][][]byte)
files[filename] = file
files[filename][0] = []byte(filedate)
files[filename][part] = data
}
// test whether stated checksum matches data payload
func checked(header []byte, data []byte) bool {
cs := cleanint(header[36:43])
sum := checkSum(data)
if cs == sum {
return true
}
return false
}
// return a QBT check sum of payload
func checkSum(data []byte) int {
sum := 0
for _, value := range data {
sum = sum + int(value)
}
return sum
}
// test whether all blocks of a file have been received
func iscomplete(filename string) bool {
if _, key := files[filename]; !key {
return false
}
for _, block := range files[filename] {
if len(block) == 0 {
return false
}
}
return true
}
// remove whitespace and return int value of string
func cleanint(value []byte) int {
integer, _ := strconv.Atoi(strings.TrimSpace(string(value)))
return integer
}
func store(filename string, filedate string, product []byte) {
l := strconv.Itoa(len(product))
log.Println("Saving", filename, "("+l+")", filedate)
filepath := filepath + filename
f, err := os.Create(filepath)
if err != nil {
log.Println("File Open Error", err)
}
defer f.Close()
_, err = f.Write(product)
if err != nil {
log.Println("File Write Error", err)
}
// if .ZIS, unzip
f.Sync()
}
// Run ... get data from EMWIN server
func byteblaster(ch chan []byte) {
conn = connect()
data := make([]byte, 1116)
d2 := make([]byte, 1116)
for {
_, err := conn.Read(data)
if err != nil {
conn = connect()
}
for _, octet := range data {
d2 = append(d2, octet^mask) // EMWIN-required XOR of received bytes
}
ch <- d2
}
}
func connect() *net.TCPConn {
fmt.Println("Starting ByteBlaster interface at", servAddr)
tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
if err != nil {
log.Panic("ResolveTCPAddr failed:", err.Error())
os.Exit(1)
}
conn, err = net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
log.Panic("Dial failed:", err.Error())
os.Exit(1)
}
return conn
}