Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Buffer flow plugins added. TinyG has a plugin.
Added ability for buffer flow plugins. There is a new buffer flow plugin for TinyG that watches the {"qr":NN} response. When it sees the qr value go below 12 it pauses its own sending and queues up whatever is still coming in on the Websocket. This is fine because we've got plenty of RAM on the websocket server. The {"qr":NN} value is still sent back on the websocket as soon as it was before, so the host application should see no real difference as to how it worked before. The difference now though is that the serial sending knows to check if sending is paused to the serial port and queue. This makes sure no buffer overflows ever occur. The reason this was becoming important is that the lag time between the qr response and the sending of Gcode was too distant and this buffer flow needs resolution around 5ms. Normal latency on the Internet is like 20ms to 200ms, so it just wasn't fast enough. If the Javascript hosting the websocket was busy processing other events, then this lag time became even worse. So, now the Serial Port JSON Server simply helps out by lots of extra buffering. Go ahead and pound it even harder with more serial commands and see it fly. Former-commit-id: e3b08b3
- Loading branch information
Showing
11 changed files
with
336 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
//"log" | ||
//"time" | ||
) | ||
|
||
type Bufferflow interface { | ||
BlockUntilReady() // implement this method | ||
OnIncomingData(data string) // implement this method | ||
//Name string | ||
//Port string | ||
//myvar mytype string | ||
//pause bool // keep track if we're paused from sending | ||
//buffertype string // is it tinyg, grbl, or other? | ||
} | ||
|
||
/* | ||
// this method is a method of the struct above | ||
func (b *bufferflow) blockUntilReady() { | ||
log.Printf("Blocking until ready. Buffertype is:%v\n", b.buffertype) | ||
//time.Sleep(3000 * time.Millisecond) | ||
if b.buffertype == "dummypause" { | ||
buf := bufferflow_dummypause{Name: "blah"} | ||
buf.blockUntilReady() | ||
} | ||
log.Printf("Done blocking. Buffertype is:%v\n", b.buffertype) | ||
} | ||
func (b *bufferflow) onIncomingData(data) { | ||
log.Printf("onIncomingData. data:%v", data) | ||
if b.buffertype == "dummypause" { | ||
buf := bufferflow_dummypause{Name: "blah"} | ||
buf.waitUntilReady() | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"time" | ||
) | ||
|
||
type BufferflowDummypause struct { | ||
Name string | ||
Port string | ||
NumLines int | ||
Paused bool | ||
} | ||
|
||
func (b *BufferflowDummypause) Init() { | ||
} | ||
|
||
func (b *BufferflowDummypause) BlockUntilReady() { | ||
log.Printf("BlockUntilReady() start. numLines:%v\n", b.NumLines) | ||
log.Printf("buffer:%v\n", b) | ||
//for b.Paused { | ||
log.Println("We are paused. Yeilding send.") | ||
time.Sleep(3000 * time.Millisecond) | ||
//} | ||
log.Printf("BlockUntilReady() end\n") | ||
} | ||
|
||
func (b *BufferflowDummypause) OnIncomingData(data string) { | ||
log.Printf("OnIncomingData() start. data:%v\n", data) | ||
b.NumLines++ | ||
//time.Sleep(3000 * time.Millisecond) | ||
log.Printf("OnIncomingData() end. numLines:%v\n", b.NumLines) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type BufferflowTinyg struct { | ||
Name string | ||
Port string | ||
Paused bool | ||
StopSending int | ||
StartSending int | ||
sem chan int | ||
} | ||
|
||
var ( | ||
// the regular expression to find the qr value | ||
re, _ = regexp.Compile("\"qr\":(\\d+)") | ||
) | ||
|
||
func (b *BufferflowTinyg) Init() { | ||
b.StartSending = 16 | ||
b.StopSending = 14 | ||
b.sem = make(chan int) | ||
} | ||
|
||
func (b *BufferflowTinyg) BlockUntilReady() { | ||
log.Printf("BlockUntilReady() start\n") | ||
//log.Printf("buffer:%v\n", b) | ||
if b.Paused { | ||
//<-b.sem // will block until told from OnIncomingData to go | ||
|
||
for b.Paused { | ||
//log.Println("We are paused. Yeilding send.") | ||
time.Sleep(5 * time.Millisecond) | ||
} | ||
|
||
} else { | ||
// still yeild a bit cuz seeing we need to let tinyg | ||
// have a chance to respond | ||
time.Sleep(15 * time.Millisecond) | ||
} | ||
log.Printf("BlockUntilReady() end\n") | ||
} | ||
|
||
func (b *BufferflowTinyg) OnIncomingData(data string) { | ||
//log.Printf("OnIncomingData() start. data:%v\n", data) | ||
if re.Match([]byte(data)) { | ||
// we have a qr value | ||
//log.Printf("Found a qr value:%v", re) | ||
res := re.FindStringSubmatch(data) | ||
qr, err := strconv.Atoi(res[1]) | ||
if err != nil { | ||
log.Printf("Got error converting qr value. huh? err:%v\n", err) | ||
} else { | ||
log.Printf("The qr val is:\"%v\"", qr) | ||
if qr <= b.StopSending { | ||
b.Paused = true | ||
|
||
log.Println("Paused sending gcode") | ||
} else if qr >= b.StartSending { | ||
b.Paused = false | ||
//b.sem <- 1 // send channel a val to trigger the unblocking in BlockUntilReady() | ||
log.Println("Started sending gcode again") | ||
} else { | ||
log.Println("In a middle state where we're paused sending gcode but watching for the buffer to get high enough to start sending again") | ||
} | ||
} | ||
} | ||
// Look for {"qr":28} | ||
// Actually, if we hit qr:10, stop sending | ||
// when hit qr:16 start again | ||
//time.Sleep(3000 * time.Millisecond) | ||
//log.Printf("OnIncomingData() end.\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.