Browse Source

Updated. Merged windows and linux.

Steve Thielemann 3 years ago
parent
commit
ae0a101d73
9 changed files with 132 additions and 140 deletions
  1. 0 59
      door/door_linux.go
  2. 1 39
      door/door_test.go
  3. 27 34
      door/door_windows.go
  4. 32 0
      door/fifobuffer.go
  5. 40 0
      door/fifobuffer_test.go
  6. 11 0
      door/help_linux_test.go
  7. 15 0
      door/help_windows_test.go
  8. 5 3
      door/input_test.go
  9. 1 5
      door/menu_test.go

+ 0 - 59
door/door_linux.go

@@ -43,65 +43,6 @@ 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) {
-	if f.index >= len(f.data) {
-		log.Panicf("Exceeded FIFOBuffer index %d size %d %#v", f.index, len(f.data), f.data)
-	}
-	f.data[f.index] = value
-	f.index++
-}
-
-func (f *FIFOBuffer) Pop() int {
-	if f.index == 0 {
-		log.Panicf("Pop from empty FIFOBuffer (size %d).", len(f.data))
-	}
-	f.index--
-	return f.data[f.index]
-}
-
-/*
-type LIFOBuffer struct {
-	data  []int
-	index int
-}
-
-func (b *LIFOBuffer) Empty() bool {
-	return b.index == 0
-}
-
-func (b *LIFOBuffer) Push(value int) {
-	if b.index+1 > len(b.data) {
-		b.data = append(b.data, value)
-		b.index++
-	} else {
-		b.data[b.index] = value
-		b.index++
-	}
-}
-
-func (b *LIFOBuffer) Pop() int {
-	if b.index == 0 {
-		log.Panic("Attempting to Pop from empty LIFOBuffer.")
-	}
-
-	b.index--
-	return b.data[b.index]
-}
-*/
-
 /*
 door32.sys:
 

+ 1 - 39
door/door_test.go

@@ -5,46 +5,11 @@ import (
 	"flag"
 	"fmt"
 	"io/ioutil"
-	"net"
 	"os"
 	"testing"
 	"time"
 )
 
-func TestFIFOEmpty(t *testing.T) {
-	buffer := NewFIFOBuffer(3)
-
-	defer func() {
-		if r := recover(); r == nil {
-			t.Error("Pop of empty FIFO Buffer did not panic.")
-		}
-	}()
-
-	buffer.Push(1)
-	x := buffer.Pop()
-	if x != 1 {
-		t.Errorf("Buffer did not return expected value 1: %d", x)
-	}
-
-	_ = buffer.Pop()
-}
-
-func TestFIFOOverflow(t *testing.T) {
-	buffer := NewFIFOBuffer(3)
-
-	defer func() {
-		if r := recover(); r == nil {
-			t.Error("Pop of empty FIFO Buffer did not panic.")
-		}
-	}()
-
-	buffer.Push(1)
-	buffer.Push(2)
-	buffer.Push(3)
-	buffer.Push(4)
-
-}
-
 func TestGoto(t *testing.T) {
 	GotoMap := map[string][]int{"\x1b[10;20H": {20, 10},
 		"\x1b[20;10H":  {10, 20},
@@ -197,10 +162,7 @@ func TestDetectFail(t *testing.T) {
 	defer client.Close()
 
 	// Send nothing
-
-	client_conn := client.(*net.TCPConn)
-	client_file, err := client_conn.File()
-	var fd int = int(client_file.Fd())
+	var fd int = socket_to_fd(client)
 
 	// Create door32.sys file
 	dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 13}

+ 27 - 34
door/door_windows.go

@@ -22,7 +22,9 @@ import (
 	"bufio"
 	"flag"
 	"fmt"
+	"log"
 	"os"
+	"path/filepath"
 	"strconv"
 	"strings"
 	"syscall"
@@ -41,34 +43,6 @@ var Height int                        // Screen height detected
 var Width int                         // Screen width detected
 var Inactivity int64 = 120            // Inactivity timeout
 
-type LIFOBuffer struct {
-	data  []int
-	index int
-}
-
-func (b *LIFOBuffer) Empty() bool {
-	return b.index == 0
-}
-
-func (b *LIFOBuffer) Push(value int) {
-	if b.index+1 > len(b.data) {
-		b.data = append(b.data, value)
-		b.index++
-	} else {
-		b.data[b.index] = value
-		b.index++
-	}
-}
-
-func (b *LIFOBuffer) Pop() int {
-	if b.index == 0 {
-		panic("Attempting to Pop from empty LIFOBuffer.")
-	}
-
-	b.index--
-	return b.data[b.index]
-}
-
 /*
 door32.sys:
 
@@ -106,7 +80,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
@@ -174,7 +148,8 @@ func (d *Door) detect() {
 
 	for {
 		r := d.getch()
-		if r == -1 {
+		if r < 0 {
+			// Timeout or Disconnect
 			break
 		}
 		results += string(byte(r))
@@ -236,8 +211,14 @@ func (d *Door) detect() {
 
 // Initialize door framework.  Parse commandline, read dropfile,
 // detect terminal capabilities.
-func (d *Door) Init() {
+func (d *Door) Init(doorname string) {
 	var dropfile string
+	d.Pushback = NewFIFOBuffer(5)
+
+	// Get path to binary, and chdir to it.
+	binaryPath, _ := os.Executable()
+	binaryPath = filepath.Dir(binaryPath)
+	_ = os.Chdir(binaryPath)
 
 	flag.StringVar(&dropfile, "d", "", "Path to dropfile")
 	flag.Parse()
@@ -245,13 +226,25 @@ func (d *Door) Init() {
 		flag.PrintDefaults()
 		os.Exit(2)
 	}
-	fmt.Printf("Loading: %s\n", dropfile)
+	// fmt.Printf("Loading: %s\n", dropfile)
 
 	d.ReadDropfile(dropfile)
 
-	fmt.Printf("BBS %s, User %s / Handle %s / File %d\n", d.Config.BBSID, d.Config.Real_name, d.Config.Handle, d.Config.Comm_handle)
-	readerChannel = make(chan byte)
+	// doorname - node #?
+	logfilename := fmt.Sprintf("%s-%d.log", doorname, d.Config.Node)
+	logf, err := os.OpenFile(logfilename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
+	if err != nil {
+		log.Panicf("Error creating log file %s: %v", logfilename, err)
+	}
+
+	log.SetOutput(logf)
+	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
 
+	log.Printf("Loading dropfile %s\n", dropfile)
+	log.Printf("BBS %s, User %s / Handle %s / File %d\n", d.Config.BBSID, d.Config.Real_name, d.Config.Handle, d.Config.Comm_handle)
+
+	// Init Windows Reader Channel
+	readerChannel = make(chan byte)
 	go Reader(syscall.Handle(d.Config.Comm_handle))
 
 	d.detect()

+ 32 - 0
door/fifobuffer.go

@@ -0,0 +1,32 @@
+package door
+
+import "log"
+
+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) {
+	if f.index >= len(f.data) {
+		log.Panicf("Exceeded FIFOBuffer index %d size %d %#v", f.index, len(f.data), f.data)
+	}
+	f.data[f.index] = value
+	f.index++
+}
+
+func (f *FIFOBuffer) Pop() int {
+	if f.index == 0 {
+		log.Panicf("Pop from empty FIFOBuffer (size %d).", len(f.data))
+	}
+	f.index--
+	return f.data[f.index]
+}

+ 40 - 0
door/fifobuffer_test.go

@@ -0,0 +1,40 @@
+package door
+
+// Need net, flag for setupSockets
+import (
+	"testing"
+)
+
+func TestFIFOEmpty(t *testing.T) {
+	buffer := NewFIFOBuffer(3)
+
+	defer func() {
+		if r := recover(); r == nil {
+			t.Error("Pop of empty FIFO Buffer did not panic.")
+		}
+	}()
+
+	buffer.Push(1)
+	x := buffer.Pop()
+	if x != 1 {
+		t.Errorf("Buffer did not return expected value 1: %d", x)
+	}
+
+	_ = buffer.Pop()
+}
+
+func TestFIFOOverflow(t *testing.T) {
+	buffer := NewFIFOBuffer(3)
+
+	defer func() {
+		if r := recover(); r == nil {
+			t.Error("Pop of empty FIFO Buffer did not panic.")
+		}
+	}()
+
+	buffer.Push(1)
+	buffer.Push(2)
+	buffer.Push(3)
+	buffer.Push(4)
+
+}

+ 11 - 0
door/help_linux_test.go

@@ -0,0 +1,11 @@
+package door
+
+input (
+	"net"
+)
+
+func socket_to_fd(socket net.Conn) int {
+	client_conn := socket.(*net.TCPConn)
+	client_file, _ := client_conn.File()
+	return int(client_file.Fd())
+}

+ 15 - 0
door/help_windows_test.go

@@ -0,0 +1,15 @@
+package door
+
+import (
+	"net"
+)
+
+func socket_to_fd(socket net.Conn) int {
+	client_conn := socket.(*net.TCPConn)
+	raw, _ := client_conn.SyscallConn()
+	var fd int
+	raw.Control(func(sfd uintptr) {
+		fd = int(sfd)
+	})
+	return fd
+}

+ 5 - 3
door/input_test.go

@@ -187,9 +187,11 @@ func TestDoorInputConnection(t *testing.T) {
 		t.Errorf("Disconnected flag shows: %t (should be true)", d.Disconnected)
 	}
 
-	if !d.HasKey() {
-		t.Error("HasKey should return true (disconnected).")
-	}
+	/*
+		if !d.HasKey() {
+			t.Error("HasKey should return true (disconnected).")
+		}
+	*/
 
 	hungup = d.GetKey()
 

+ 1 - 5
door/menu_test.go

@@ -4,7 +4,6 @@ import (
 	"flag"
 	"fmt"
 	"io/ioutil"
-	"net"
 	"os"
 	"strings"
 	"testing"
@@ -67,10 +66,7 @@ func TestMenuConnection(t *testing.T) {
 	server.Write(buffer)
 
 	// Access Fd (File descriptor) of client for dropfile
-	client_conn := client.(*net.TCPConn)
-	client_file, err := client_conn.File()
-
-	var fd int = int(client_file.Fd())
+	var fd int = socket_to_fd(client)
 
 	// Create door32.sys file
 	dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 12}