diff --git a/main.go b/main.go index ec7bc30..c25c71c 100644 --- a/main.go +++ b/main.go @@ -82,6 +82,7 @@ func main() { log.Panicln(err) } log.Println("Listening to connections at '"+*host+"' on port", strconv.Itoa(*port)) + log.Println("(If you want to run this on a different address or port, the command line flags --host and --port are available to you.)") defer l.Close() for { @@ -119,8 +120,6 @@ func main() { go ioHandler(w, &WatcherControl) log.Println("main thread looping") - // wg.Wait() - // log.Printf("Goodbye %s!", conn.RemoteAddr()) } } @@ -143,7 +142,6 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) { paused := false ControlLoop: for { - // log.Println("checking watcher waitio") select { case msg := <-MenuControl: log.Println("msg on MenuControl:", msg) @@ -167,9 +165,7 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) { } default: - // log.Println("no msg on MenuControl, continuing") if !paused { - // log.Println("menu watchercontrol waiting for IO") select { case res := <-inChan: if res.Error != nil { @@ -187,16 +183,15 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) { case "adventure": // start the door and wait - log.Println("starting door handler...") - var wg sync.WaitGroup + log.Println("starting door handler...") wg.Add(1) go doorHandler(ctx, conn, w, &wg) log.Println("menu handler waiting for wg to return from door") wg.Wait() log.Println("returning from door") - //MenuControl <- "unpause" - err := w.Write(ctx, conn, []byte("\n\n> ")) + // welcome back and prompt + err := w.Write(ctx, conn, []byte("Welcome back from your adventure!\n\n> ")) if err != nil { log.Printf("error writing to connection: %v", err) } @@ -228,20 +223,6 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) { // noop log.Printf("received on inChan: conn: %v, buffer: %v\n", res.Conn.RemoteAddr(), string(res.Buffer)) } - /* - case res := <-outChan: - // log.Println("OpResult on outChan:", res) - if res.Error != nil { - log.Println("error on outChan: ", res.Error) - } - if res.Operation == gaio.OpWrite && res.Size > 0 && res.Conn == conn { - // write confirmation received - //log.Printf("received on outChan: conn: %v, buffer: %v\n", res.Conn.RemoteAddr(), string(res.Buffer)) - } else { - // noop - log.Printf("watcher sent unknown on outChan: %v\n", res) - } - */ default: // noop - wait a lil bit time.Sleep(time.Millisecond) @@ -331,14 +312,8 @@ func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync. // Make sure to close the pty at the end. defer func() { _ = ptmx.Close() }() // Best effort. - /* - stdout, _ := cmd.StdoutPipe() - stdin, _ := cmd.StdinPipe() - */ - //CmdOutChan := make(chan []byte) Terminator := make(chan bool) - // cmd.Start() wg.Add(1) go func() { defer wg.Done() @@ -351,11 +326,9 @@ func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync. log.Println("i") if ok { if result.Operation == gaio.OpRead && result.Size > 0 { - // we asked for a read - send it to stdin l, err = ptmx.Write(result.Buffer[:result.Size]) if err != nil { if err == io.EOF { - // terminal closed? wtfever log.Println("EOF on cmd.write!!!") break IOLoop } @@ -397,15 +370,11 @@ func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync. log.Printf("wrote %d bytes to watcher: %v\n", l, string(outBuf[:l])) if outBuf[l-1] == byte('>') { // looks like a prompt to me, I hope! - // let's add a space just for fun - outBuf[0] = byte(' ') - w.Write(ctx, c, outBuf[0:1]) - // and now block until we get a read from the connection dammit + // let's get some input about it! waitingForInput = true } } else { // zero output? let's get some input? - // log.Println("0-read from cmd output - queueing watcher read") w.Read(ctx, c, nil) } } @@ -413,65 +382,6 @@ func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync. time.Sleep(time.Millisecond) } }() - - /* - // goroutine to read cmd stdout in a loop and write it to CmdOutChan. - // on io.EOF in the scanner, will send Terminator to kill the IOLoop - // that follows the goroutine - go func(Terminator chan bool, wg *sync.WaitGroup, stdout *io.ReadCloser) { - defer wg.Done() - log.Println("starting stdout reader goroutine") - scanner := bufio.NewScanner(*stdout) - scanner.Split(bufio.ScanLines) - - for scanner.Scan() { - m := scanner.Bytes() - m = append(m, '\n') - CmdOutChan <- m - time.Sleep(time.Millisecond) - } - log.Println("exiting stdout reader goroutine") - Terminator <- true - }(Terminator, &wg, &stdout) - - // goroutine to handle actual IO calls - wg.Add(1) - go func(Terminator chan bool, wg *sync.WaitGroup, stdout *io.ReadCloser) { - defer wg.Done() - log.Println("starting IOLoop") - IOLoop: - for { - select { - // if there's data coming out from cmd, write it to the watcher - case cmdOutput := <-CmdOutChan: - log.Printf("o") - err := w.Write(ctx, c, cmdOutput) - if err != nil { - log.Printf("err writing to watcher from cmd: %v\n", err) - } else { - log.Printf("send %d bytes from CmdOutChan from cmd stdout to watcher: %v\n", len(cmdOutput), string(cmdOutput)) - } - - // if there's data coming in from remote conn, send it to stdin for cmd - case result, ok := <-inChan: - log.Printf("i") - if ok { - if result.Operation == gaio.OpRead && result.Size > 0 { - // we asked for a read - send it to stdin - stdin.Write(result.Buffer[:result.Size]) - } - } else { - log.Println("yikes, problem getting a result from inChan in doorHandler!") - } - // if the Terminator shows up we're dead - case <-Terminator: - break IOLoop - } - time.Sleep(time.Millisecond) - } - log.Println("IOLoop exited") - }(Terminator, &wg, &stdout) - */ wg.Wait() log.Println("waiting for cmd") exitErr := cmd.Wait()