|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "time" |
| 6 | + |
| 7 | + log "github.com/Sirupsen/logrus" |
| 8 | +) |
| 9 | + |
| 10 | +type BufferflowTimed struct { |
| 11 | + Name string |
| 12 | + Port string |
| 13 | + Output chan []byte |
| 14 | + Input chan string |
| 15 | + ticker *time.Ticker |
| 16 | +} |
| 17 | + |
| 18 | +var ( |
| 19 | + bufferedOutput string |
| 20 | +) |
| 21 | + |
| 22 | +func (b *BufferflowTimed) Init() { |
| 23 | + log.Println("Initting timed buffer flow (output once every 16ms)") |
| 24 | + bufferedOutput = "" |
| 25 | + |
| 26 | + go func() { |
| 27 | + for data := range b.Input { |
| 28 | + bufferedOutput = bufferedOutput + data |
| 29 | + } |
| 30 | + }() |
| 31 | + |
| 32 | + go func() { |
| 33 | + b.ticker = time.NewTicker(16 * time.Millisecond) |
| 34 | + for _ = range b.ticker.C { |
| 35 | + if bufferedOutput != "" { |
| 36 | + m := SpPortMessage{bufferedOutput} |
| 37 | + buf, _ := json.Marshal(m) |
| 38 | + b.Output <- []byte(buf) |
| 39 | + bufferedOutput = "" |
| 40 | + } |
| 41 | + } |
| 42 | + }() |
| 43 | + |
| 44 | +} |
| 45 | + |
| 46 | +func (b *BufferflowTimed) BlockUntilReady(cmd string, id string) (bool, bool) { |
| 47 | + //log.Printf("BlockUntilReady() start\n") |
| 48 | + return true, false |
| 49 | +} |
| 50 | + |
| 51 | +func (b *BufferflowTimed) OnIncomingData(data string) { |
| 52 | + b.Input <- data |
| 53 | +} |
| 54 | + |
| 55 | +// Clean out b.sem so it can truly block |
| 56 | +func (b *BufferflowTimed) ClearOutSemaphore() { |
| 57 | +} |
| 58 | + |
| 59 | +func (b *BufferflowTimed) BreakApartCommands(cmd string) []string { |
| 60 | + return []string{cmd} |
| 61 | +} |
| 62 | + |
| 63 | +func (b *BufferflowTimed) Pause() { |
| 64 | + return |
| 65 | +} |
| 66 | + |
| 67 | +func (b *BufferflowTimed) Unpause() { |
| 68 | + return |
| 69 | +} |
| 70 | + |
| 71 | +func (b *BufferflowTimed) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool { |
| 72 | + return false |
| 73 | +} |
| 74 | + |
| 75 | +func (b *BufferflowTimed) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool { |
| 76 | + return false |
| 77 | +} |
| 78 | + |
| 79 | +func (b *BufferflowTimed) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool { |
| 80 | + return false |
| 81 | +} |
| 82 | + |
| 83 | +func (b *BufferflowTimed) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool { |
| 84 | + return false |
| 85 | +} |
| 86 | + |
| 87 | +func (b *BufferflowTimed) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool { |
| 88 | + return false |
| 89 | +} |
| 90 | + |
| 91 | +func (b *BufferflowTimed) ReleaseLock() { |
| 92 | +} |
| 93 | + |
| 94 | +func (b *BufferflowTimed) IsBufferGloballySendingBackIncomingData() bool { |
| 95 | + return true |
| 96 | +} |
| 97 | + |
| 98 | +func (b *BufferflowTimed) Close() { |
| 99 | + b.ticker.Stop() |
| 100 | + close(b.Input) |
| 101 | +} |
0 commit comments