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