add doorHandler back in
This commit is contained in:
parent
1f3c0331e7
commit
2a295b43b9
79
main.go
79
main.go
@ -1,10 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -183,14 +185,15 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) {
|
|||||||
|
|
||||||
case "adventure":
|
case "adventure":
|
||||||
// start the door and wait
|
// start the door and wait
|
||||||
log.Printf("starting simulated door handler...")
|
log.Println("starting door handler...")
|
||||||
MenuControl <- "pause"
|
|
||||||
// if err := doorHandler(w); err != nil {
|
var wg sync.WaitGroup
|
||||||
// log.Printf("error from cmd `adventure`: %v", err)
|
wg.Add(1)
|
||||||
// }
|
go doorHandler(ctx, conn, w, &wg)
|
||||||
time.Sleep(5 * time.Second)
|
log.Println("menu handler waiting for wg to return from door")
|
||||||
log.Printf("returning from door")
|
wg.Wait()
|
||||||
MenuControl <- "unpause"
|
log.Println("returning from door")
|
||||||
|
//MenuControl <- "unpause"
|
||||||
err := w.Write(ctx, conn, []byte("\n\n> "))
|
err := w.Write(ctx, conn, []byte("\n\n> "))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error writing to connection: %v", err)
|
log.Printf("error writing to connection: %v", err)
|
||||||
@ -198,12 +201,12 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) {
|
|||||||
|
|
||||||
case "exit":
|
case "exit":
|
||||||
if err := w.Write(ctx, conn, exitHandler()); err != nil {
|
if err := w.Write(ctx, conn, exitHandler()); err != nil {
|
||||||
log.Printf("error sending exitHandler from cmd `exit`: %v", err)
|
log.Printf("error sending exitHandler from cmd `exit`: %v\n", err)
|
||||||
} else {
|
} else {
|
||||||
// let's wait before we actually close the connection to try and ensure the write gets through
|
// let's wait before we actually close the connection to try and ensure the write gets through
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
log.Println("exiting...")
|
log.Println("exiting...")
|
||||||
log.Printf("Goodbye %s!", conn.RemoteAddr())
|
log.Printf("Goodbye %s!\n", conn.RemoteAddr())
|
||||||
err := w.Free(conn)
|
err := w.Free(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("err closing connection: %v\n", err)
|
log.Printf("err closing connection: %v\n", err)
|
||||||
@ -299,25 +302,33 @@ func exitHandler() []byte {
|
|||||||
return []byte(exitMessage)
|
return []byte(exitMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// rework to mimic mainHandler
|
func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync.WaitGroup) error {
|
||||||
/*
|
defer menuwg.Done()
|
||||||
func doorHandler(w *gaio.Watcher) error {
|
|
||||||
const bannerText = "\r\nCOLOSSAL CAVE\r\n\r\n"
|
const bannerText = "\r\nCOLOSSAL CAVE\r\n\r\n"
|
||||||
err := sendClientText(*c, bannerText)
|
err := w.Write(ctx, c, []byte(bannerText))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error writing to connection: %v\n", err)
|
log.Printf("error writing to connection: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var inChan, _ chan gaio.OpResult
|
||||||
|
var ok bool
|
||||||
|
|
||||||
|
if inChan, _, ok = openconn.FromContext(ctx); !ok {
|
||||||
|
log.Println("Could not get inChan/outChan from context!", ok, ctx)
|
||||||
|
}
|
||||||
|
|
||||||
cmd := exec.Command("/usr/games/adventure")
|
cmd := exec.Command("/usr/games/adventure")
|
||||||
|
|
||||||
stdout, _ := cmd.StdoutPipe()
|
stdout, _ := cmd.StdoutPipe()
|
||||||
stdin, _ := cmd.StdinPipe()
|
stdin, _ := cmd.StdinPipe()
|
||||||
|
|
||||||
cmd.Start()
|
cmd.Start()
|
||||||
Terminator := make(chan bool)
|
|
||||||
wg := new(sync.WaitGroup)
|
var wg sync.WaitGroup
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(c net.Conn, Terminator *chan bool, wg *sync.WaitGroup) {
|
Terminator := make(chan bool)
|
||||||
|
go func(Terminator *chan bool, wg *sync.WaitGroup) {
|
||||||
log.Println("starting stdin reader goroutine")
|
log.Println("starting stdin reader goroutine")
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
Check:
|
Check:
|
||||||
@ -325,27 +336,21 @@ func doorHandler(w *gaio.Watcher) error {
|
|||||||
select {
|
select {
|
||||||
case <-*Terminator:
|
case <-*Terminator:
|
||||||
break Check
|
break Check
|
||||||
default:
|
case result, ok := <-inChan:
|
||||||
time.Sleep(time.Second)
|
if ok {
|
||||||
inBuf := make([]byte, 80)
|
if result.Operation == gaio.OpRead {
|
||||||
var l int
|
// we asked for a read - send it to stdin
|
||||||
if l, err = c.Read(inBuf); err != nil {
|
stdin.Write(result.Buffer[:result.Size])
|
||||||
if err == io.EOF {
|
|
||||||
log.Println("connection closed?")
|
|
||||||
break Check
|
|
||||||
} else {
|
|
||||||
log.Printf("error reading conn: %v\n", err)
|
|
||||||
break Check
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log.Println("yikes, problem getting a result from inChan in doorHandler!")
|
||||||
}
|
}
|
||||||
if l > 0 {
|
default:
|
||||||
log.Printf("writing %d bytes to stdin: %v\n", l, string(inBuf[:l]))
|
time.Sleep(time.Millisecond)
|
||||||
stdin.Write(inBuf[:l])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Println("exiting stdin reader goroutine")
|
log.Println("exiting stdin reader goroutine")
|
||||||
}(*c, &Terminator, wg)
|
}(&Terminator, &wg)
|
||||||
|
|
||||||
log.Println("starting stdout reader goroutine")
|
log.Println("starting stdout reader goroutine")
|
||||||
scanner := bufio.NewScanner(stdout)
|
scanner := bufio.NewScanner(stdout)
|
||||||
@ -353,9 +358,9 @@ func doorHandler(w *gaio.Watcher) error {
|
|||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
m := scanner.Bytes()
|
m := scanner.Bytes()
|
||||||
(*c).Write(m)
|
m = append(m, '\n')
|
||||||
(*c).Write([]byte{'\n'})
|
w.Write(ctx, c, m)
|
||||||
log.Printf("wrote %d bytes to stdout: %v\n", len(m)+1, string(m))
|
log.Printf("wrote %d bytes to watcher from door stdout: %v\n", len(m)+1, string(m))
|
||||||
}
|
}
|
||||||
log.Println("exiting stdout reader goroutine")
|
log.Println("exiting stdout reader goroutine")
|
||||||
Terminator <- true
|
Terminator <- true
|
||||||
@ -367,6 +372,6 @@ func doorHandler(w *gaio.Watcher) error {
|
|||||||
if exitErr != nil {
|
if exitErr != nil {
|
||||||
log.Printf("cmd exited with err: %v\n", exitErr)
|
log.Printf("cmd exited with err: %v\n", exitErr)
|
||||||
}
|
}
|
||||||
|
log.Println("leaving door handler")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
Loading…
Reference in New Issue
Block a user