Browse Source

Testing output of input(5) routine. Buffer update.

Steve Thielemann 3 years ago
parent
commit
1ccfcef248
2 changed files with 60 additions and 1 deletions
  1. 33 1
      door/door.go
  2. 27 0
      door/input_test.go

+ 33 - 1
door/door.go

@@ -41,6 +41,36 @@ var Height int                        // Screen height detected
 var Width int                         // Screen width detected
 var Inactivity int64 = 120            // Inactivity timeout
 
+type FIFOBuffer struct {
+	data  []int
+	index int
+}
+
+func NewFIFOBuffer(maxsize int) FIFOBuffer {
+	return FIFOBuffer{data: make([]int, maxsize)}
+}
+
+func (f *FIFOBuffer) Empty() bool {
+	return f.index == 0
+}
+
+func (f *FIFOBuffer) Push(value int) {
+	f.data[f.index] = value
+	f.index++
+	if f.index > len(f.data) {
+		panic(fmt.Sprintf("Exceeded FIFOBuffer size %d %#v", len(f.data), f))
+	}
+}
+
+func (f *FIFOBuffer) Pop() int {
+	if f.index == 0 {
+		panic("Pop from empty FIFOBuffer.")
+	}
+	f.index--
+	return f.data[f.index]
+}
+
+/*
 type LIFOBuffer struct {
 	data  []int
 	index int
@@ -68,6 +98,7 @@ func (b *LIFOBuffer) Pop() int {
 	b.index--
 	return b.data[b.index]
 }
+*/
 
 /*
 door32.sys:
@@ -106,7 +137,7 @@ type Door struct {
 	Disconnected bool
 	TimeOut      time.Time // Fixed point in time, when time expires
 	StartTime    time.Time
-	Pushback     LIFOBuffer
+	Pushback     FIFOBuffer
 }
 
 // Return the amount of time left as time.Duration
@@ -233,6 +264,7 @@ func (d *Door) detect() {
 // detect terminal capabilities.
 func (d *Door) Init() {
 	var dropfile string
+	d.Pushback = NewFIFOBuffer(5)
 
 	flag.StringVar(&dropfile, "d", "", "Path to dropfile")
 	flag.Parse()

+ 27 - 0
door/input_test.go

@@ -6,6 +6,7 @@ import (
 	"net"
 	"os"
 	"testing"
+	"time"
 )
 
 func TestDoorInputConnection(t *testing.T) {
@@ -86,6 +87,13 @@ func TestDoorInputConnection(t *testing.T) {
 		t.Errorf("Height not 43: %d", Height)
 	}
 
+	// These are the commands sent to detect ... throw this all away.
+	buffer = make([]byte, 128)
+	server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
+	r, err := server.Read(buffer)
+	// t.Errorf("Buffer : %#v\n", buffer[:r])
+	server.SetReadDeadline(time.Time{})
+
 	keytest := map[string][]int{
 		"\x1b":                             []int{0x1b},
 		"\x0d\x00":                         []int{0x0d},
@@ -137,6 +145,14 @@ func TestDoorInputConnection(t *testing.T) {
 		}
 	}
 
+	buffer = make([]byte, 128)
+	server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
+	r, err = server.Read(buffer)
+	if r != 0 {
+		t.Errorf("Buffer After KeyTest: %#v\n", buffer[:r])
+	}
+	server.SetReadDeadline(time.Time{})
+
 	timeout := d.WaitKey(0, 50)
 	if timeout != -1 {
 		t.Errorf("Expected timeout, got %d / %X", timeout, timeout)
@@ -153,6 +169,17 @@ func TestDoorInputConnection(t *testing.T) {
 		t.Errorf("Expected Input(5) = 12345, but got %#v", input)
 	}
 
+	// I'm not sure what they extra characters are in the buffer here.
+	buffer = make([]byte, 128)
+	server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
+	r, err = server.Read(buffer)
+	result := string(buffer[:r])
+	expected := "     \x08\x08\x08\x08\x0812345\x07\x07\x07\x07\x07"
+	if result != expected {
+		t.Errorf("Buffer Input(5): Expected %#v, got %#v\n", expected, result)
+	}
+	server.SetReadDeadline(time.Time{})
+
 	buffer = []byte("12345678\x08\x089\r")
 	server.Write(buffer)
 	input = d.Input(5)