From 19f9a6d428565ab3df5fa29cd784a5748411bfbb Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Mon, 28 Sep 2015 17:59:22 +0200 Subject: [PATCH 1/2] Use a bufferflow that sends collected messages every 16ms --- bufferflow.go | 3 +- bufferflow_timed.go | 96 +++++++++++++++++++++++++++++++++++++++++++++ serialport.go | 10 ++++- 3 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 bufferflow_timed.go diff --git a/bufferflow.go b/bufferflow.go index d5059989b..ff5bcf1b7 100644 --- a/bufferflow.go +++ b/bufferflow.go @@ -5,7 +5,7 @@ import ( //"time" ) -var availableBufferAlgorithms = []string{"default"} +var availableBufferAlgorithms = []string{"default", "timed"} type BufferMsg struct { Cmd string @@ -16,6 +16,7 @@ type BufferMsg struct { } type Bufferflow interface { + Init() BlockUntilReady(cmd string, id string) (bool, bool) // implement this method //JustQueue(cmd string, id string) bool // implement this method OnIncomingData(data string) // implement this method diff --git a/bufferflow_timed.go b/bufferflow_timed.go new file mode 100644 index 000000000..199c54879 --- /dev/null +++ b/bufferflow_timed.go @@ -0,0 +1,96 @@ +package main + +import ( + "encoding/json" + log "github.com/Sirupsen/logrus" + "time" +) + +type BufferflowTimed struct { + Name string + Port string + Output chan []byte + Input chan string +} + +var ( + bufferedOutput string +) + +func (b *BufferflowTimed) Init() { + log.Println("Initting timed buffer flow (output once every 16ms)") + bufferedOutput = "" + + go func() { + for data := range b.Input { + bufferedOutput = bufferedOutput + data + } + }() + + go func() { + c := time.Tick(16 * time.Millisecond) + log.Println(bufferedOutput) + for _ = range c { + m := SpPortMessage{bufferedOutput} + buf, _ := json.Marshal(m) + b.Output <- []byte(buf) + bufferedOutput = "" + } + }() + +} + +func (b *BufferflowTimed) BlockUntilReady(cmd string, id string) (bool, bool) { + //log.Printf("BlockUntilReady() start\n") + return true, false +} + +func (b *BufferflowTimed) OnIncomingData(data string) { + b.Input <- data +} + +// Clean out b.sem so it can truly block +func (b *BufferflowTimed) ClearOutSemaphore() { +} + +func (b *BufferflowTimed) BreakApartCommands(cmd string) []string { + return []string{cmd} +} + +func (b *BufferflowTimed) Pause() { + return +} + +func (b *BufferflowTimed) Unpause() { + return +} + +func (b *BufferflowTimed) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool { + return false +} + +func (b *BufferflowTimed) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool { + return false +} + +func (b *BufferflowTimed) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool { + return false +} + +func (b *BufferflowTimed) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool { + return false +} + +func (b *BufferflowTimed) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool { + return false +} + +func (b *BufferflowTimed) ReleaseLock() { +} + +func (b *BufferflowTimed) IsBufferGloballySendingBackIncomingData() bool { + return true +} + +func (b *BufferflowTimed) Close() { +} diff --git a/serialport.go b/serialport.go index a776e5251..9aafcfbc9 100755 --- a/serialport.go +++ b/serialport.go @@ -340,9 +340,15 @@ func spHandlerOpen(portname string, baud int, buftype string, isSecondary bool) // we can go up to 256,000 lines of gcode in the buffer p := &serport{sendBuffered: make(chan Cmd, 256000), sendNoBuf: make(chan Cmd), portConf: conf, portIo: sp, BufferType: buftype, IsPrimary: isPrimary, IsSecondary: isSecondary} - bw := &BufferflowDefault{} + var bw Bufferflow + + if buftype == "timed" { + bw = &BufferflowTimed{Name: "timed", Port: portname, Output: h.broadcastSys, Input: make(chan string)} + } else { + bw = &BufferflowDefault{Port: portname} + } + bw.Init() - bw.Port = portname p.bufferwatcher = bw sh.register <- p From 00249d2305c0a9117d299cbb52bbd44cde8c1866 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Tue, 29 Sep 2015 16:30:46 +0200 Subject: [PATCH 2/2] Release resources when stopped --- bufferflow_timed.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bufferflow_timed.go b/bufferflow_timed.go index 199c54879..6dd58dfa4 100644 --- a/bufferflow_timed.go +++ b/bufferflow_timed.go @@ -11,6 +11,7 @@ type BufferflowTimed struct { Port string Output chan []byte Input chan string + ticker *time.Ticker } var ( @@ -28,9 +29,8 @@ func (b *BufferflowTimed) Init() { }() go func() { - c := time.Tick(16 * time.Millisecond) - log.Println(bufferedOutput) - for _ = range c { + b.ticker = time.NewTicker(16 * time.Millisecond) + for _ = range b.ticker.C { m := SpPortMessage{bufferedOutput} buf, _ := json.Marshal(m) b.Output <- []byte(buf) @@ -93,4 +93,6 @@ func (b *BufferflowTimed) IsBufferGloballySendingBackIncomingData() bool { } func (b *BufferflowTimed) Close() { + b.ticker.Stop() + close(b.Input) }