-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferflow_3devo.go
More file actions
379 lines (310 loc) · 10.4 KB
/
Copy pathbufferflow_3devo.go
File metadata and controls
379 lines (310 loc) · 10.4 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"regexp"
"strings"
"sync"
"time"
"github.com/3devo/dvconnector/models"
. "github.com/logrusorgru/aurora"
)
type Bufferflow3Devo struct {
Name string
Port string
//Output chan []byte
Input chan string
ticker *time.Ticker
IsOpen bool
bufferedOutput string
reNewLine *regexp.Regexp
reCmdDone *regexp.Regexp
// additional lock for BlockUntilReady vs OnIncomingData method
inOutLock *sync.Mutex
q *Queue
sem chan int // semaphore to wait on until given release
Paused bool
ManualPaused bool
lock *sync.Mutex
manualLock *sync.Mutex
BufferMax int
}
// generateRegexFromHeaders takes the incoming headers and matches them up with the one found in the configuration struct
// It then creates a regex based on the format of the headers using the validator value from the configuration
// If a header is unknown to the configuration it will throw an error
func generateRegexFromHeaders(headers []string, knownColumns *map[string]Column) string {
genericMatch := `[^\t]*`
generatedRegex := "^"
for index, headerName := range headers {
if column, key := (*knownColumns)[headerName]; !key {
log.Println("HEADER => unknown header '" + headerName + "', defaulting to generic match regex")
generatedRegex += genericMatch
} else {
generatedRegex += column.Validator
}
if index < len(headers)-1 {
generatedRegex += `\t`
} else {
generatedRegex += `$`
}
}
return generatedRegex
}
func (b *Bufferflow3Devo) Init() {
log.Println("Initting timed buffer flow (output once every 16ms)")
b.bufferedOutput = ""
b.IsOpen = true
b.reNewLine, _ = regexp.Compile("\\r{0,1}\\n")
b.inOutLock = &sync.Mutex{}
b.q = NewQueue()
// when we get a > response we know a line was processed
b.reCmdDone, _ = regexp.Compile("^(>|stdin:|=)")
b.sem = make(chan int, 1000)
b.Paused = false
b.ManualPaused = false
b.lock = &sync.Mutex{}
b.manualLock = &sync.Mutex{}
b.Input = make(chan string)
b.BufferMax = 2
var validateLogRegex *regexp.Regexp
initCompleted := false
lastTime := "0"
query := db.Select().Limit(1).OrderBy("Timestamp").Reverse()
var logFile = models.LogFile{}
err := query.First(&logFile)
if err != nil {
log.Println(Red("Can not find logfile"))
}
go func() {
for data := range b.Input {
//log.Printf("Got to b.Input chan loop. data:%v\n", data)
// Lock the packet ctr at start and then end
b.inOutLock.Lock()
b.bufferedOutput = b.bufferedOutput + data
arrLines := b.reNewLine.Split(b.bufferedOutput, -1)
if len(arrLines) > 1 {
// that means we found a newline and have 2 or greater array values
// so we need to analyze our arrLines[] lines but keep last line
// for next trip into OnIncomingData
//log.Printf("We have data lines to analyze. numLines:%v\n", len(arrLines))
} else {
// we don't have a newline yet, so just exit and move on
// we don't have to reset b.LatestData because we ended up
// without any newlines so maybe we will next time into this method
//log.Printf("Did not find newline yet, so nothing to analyze\n")
b.inOutLock.Unlock()
continue
}
// if we made it here we have lines to analyze
// so analyze all of them except the last line
for _, element := range arrLines[:len(arrLines)-1] {
//log.Printf("Working on element:%v, index:%v", element, index)
//log.Printf("Working on element:%v, index:%v", element)
// log.Printf("\t\tData:%v", element)
// handle communication back to client
// for base serial data (this is not the cmd:"Write" or cmd:"Complete")
// Check if incoming data contains corruption
splitLine := strings.Split(element, "\t")
// For now only check on data that starts with a digit
if initCompleted {
// Bruteforced regex to check if the line matches with our current (01-29-2019) log format
if match := validateLogRegex.MatchString(element); match == false {
log.Println(Red("Corrupt data found -> "), Blue(element))
h.broadcastSys <- []byte("{\"Cmd\":\"Error\",\"Desc\":\"Corrupt data occurred around time:" + lastTime + "\",\"Port\":\"" + b.Port + "\"}")
// Drop entire line
continue
} else {
lastTime = splitLine[0]
}
}
if !initCompleted && splitLine[0] == "Time" {
generatedRegex := generateRegexFromHeaders(splitLine, &KnownColumns)
validateLogRegex = regexp.MustCompile(generatedRegex)
initCompleted = true
}
m := DataPerLine{b.Port, element + "\n"}
bm, err := json.Marshal(m)
if err == nil {
err := logFile.AppendLog(m.D, env)
if err != nil {
log.Println(Red(fmt.Sprintf("Can't write to log file -> %v", logFile.GetFileName())))
}
//log.Println(Green("Sending data -> "), m.D)
h.broadcastSys <- bm
}
} // for loop
b.bufferedOutput = arrLines[len(arrLines)-1]
b.inOutLock.Unlock()
}
}()
/*
go func() {
b.ticker = time.NewTicker(16 * time.Millisecond)
for _ = range b.ticker.C {
if b.bufferedOutput != "" {
m := SpPortMessage{b.Port, b.bufferedOutput}
buf, _ := json.Marshal(m)
b.Output <- []byte(buf)
//log.Println(buf)
b.bufferedOutput = ""
}
}
}()
*/
}
func ByteArrayEquals(a []byte, b []byte) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func (b *Bufferflow3Devo) BlockUntilReady(cmd string, id string) (bool, bool, string) {
// Lock for this ENTIRE method
b.inOutLock.Lock()
log.Printf("BlockUntilReady() Start\n")
log.Printf("\tid:%v, txt:%v\n", id, strings.Replace(cmd, "\n", "\\n", -1))
// keep track of whether we need to unlock at end of method or not
// i.e. we unlock if we have to pause, thus we won't have to doubly unlock at end of method
isNeedToUnlock := true
b.q.Push(cmd, id)
if b.q.Len() >= b.BufferMax {
b.SetPaused(true, 0) // b.Paused = true
log.Printf("\tIt looks like the local queue at Len: %v is over the allowed size of BufferMax: %v, so we are going to pause. Then when some incoming responses come in a check will occur to see if there's room to send this command. Pausing...", b.q.Len(), b.BufferMax)
}
if b.GetPaused() {
//log.Println("It appears we are being asked to pause, so we will wait on b.sem")
// We are being asked to pause our sending of commands
// clear all b.sem signals so when we block below, we truly block
b.ClearOutSemaphore()
// since we need other code to run while we're blocking, we better release the packet ctr lock
b.inOutLock.Unlock()
// since we already unlocked this thread, note it so we don't doubly unlock
isNeedToUnlock = false
log.Println("\tBlocking on b.sem until told from OnIncomingData to go")
unblockType, ok := <-b.sem // will block until told from OnIncomingData to go
log.Printf("\tDone blocking cuz got b.sem semaphore release. ok:%v, unblockType:%v\n", ok, unblockType)
log.Printf("\tDone blocking cuz got b.sem semaphore release. ok:%v, unblockType:%v\n", ok, unblockType)
// we get an unblockType of 1 for normal unblocks
// we get an unblockType of 2 when we're being asked to wipe the buffer, i.e. from a % cmd
if unblockType == 2 {
log.Println("\tThis was an unblock of type 2, which means we're being asked to wipe internal buffer. so return false.")
// returning false asks the calling method to wipe the serial send once
// this function returns
return false, false, ""
}
}
log.Printf("BlockUntilReady() end\n")
time.Sleep(10 * time.Millisecond)
if isNeedToUnlock {
b.inOutLock.Unlock()
}
//return true, willHandleCompleteResponse, newCmd
return true, true, ""
}
func (b *Bufferflow3Devo) OnIncomingData(data string) {
b.Input <- data
}
// Clean out b.sem so it can truly block
func (b *Bufferflow3Devo) ClearOutSemaphore() {
keepLooping := true
for keepLooping {
select {
case _, ok := <-b.sem: // case d, ok :=
//log.Printf("Consuming b.sem queue to clear it before we block. ok:%v, d:%v\n", ok, string(d))
//ctr++
if ok == false {
keepLooping = false
}
default:
keepLooping = false
//log.Println("Hit default in select clause")
}
}
}
func (b *Bufferflow3Devo) BreakApartCommands(cmd string) []string {
return []string{cmd}
}
func (b *Bufferflow3Devo) Pause() {
return
}
func (b *Bufferflow3Devo) Unpause() {
return
}
func (b *Bufferflow3Devo) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool {
return false
}
func (b *Bufferflow3Devo) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool {
return false
}
func (b *Bufferflow3Devo) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool {
return false
}
func (b *Bufferflow3Devo) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool {
return false
}
func (b *Bufferflow3Devo) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool {
reWhiteSpace := regexp.MustCompile("^\\s*$")
if reWhiteSpace.MatchString(cmd) {
log.Println("Found a whitespace only command")
return true
} else {
return false
}
//return false
}
func (b *Bufferflow3Devo) ReleaseLock() {
log.Println("Wiping NodeMCU buffer")
b.q.Delete()
b.SetPaused(false, 2)
}
func (b *Bufferflow3Devo) Close() {
if b.IsOpen == false {
// we are being asked a 2nd time to close when we already have
// that will cause a panic
log.Println("We got called a 2nd time to close, but already closed")
return
}
b.IsOpen = false
//b.ticker.Stop()
close(b.Input)
}
func (b *Bufferflow3Devo) RewriteSerialData(cmd string, id string) string {
return ""
}
// Gets the paused state of this buffer
// go-routine safe.
func (b *Bufferflow3Devo) GetPaused() bool {
b.lock.Lock()
defer b.lock.Unlock()
return b.Paused
}
// Sets the paused state of this buffer
// go-routine safe.
func (b *Bufferflow3Devo) SetPaused(isPaused bool, semRelease int) {
b.lock.Lock()
defer b.lock.Unlock()
b.Paused = isPaused
// only release semaphore if we are being told to unpause
if b.Paused == false {
// the BlockUntilReady thread should be sitting waiting
// so when we send this should trigger it
b.sem <- semRelease
log.Printf("\tJust sent release to b.sem with val:%v, so we will not block the sending to serial port anymore.", semRelease)
}
}
func (b *Bufferflow3Devo) GetManualPaused() bool {
b.manualLock.Lock()
defer b.manualLock.Unlock()
return b.ManualPaused
}
func (b *Bufferflow3Devo) SetManualPaused(isPaused bool) {
b.manualLock.Lock()
defer b.manualLock.Unlock()
b.ManualPaused = isPaused
}