From 81801e640aff4cd58a1370a4e850db35d8637ce6 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 26 Oct 2020 17:40:59 +0000 Subject: [PATCH] can shut down from either side now --- internal/server/server.go | 13 +++++---- internal/ui/ui.go | 61 ++++++++++++++++++++------------------- internal/wire/client.go | 2 ++ 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index b4fd32e..62d35ed 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -144,12 +144,15 @@ func (s *Server) Shutdown() { go func() { defer wg.Done() - s.Info("broadcasting shutdown to all active sessions") - s.Lock() - for id, sn := range s.sessions { - s.Info("sending done signal to session: %d", id) - sn.done <- true + if len(s.sessions) > 0 { + s.Info("broadcasting shutdown to %d active sessions", len(s.sessions)) + for id, sn := range s.sessions { + s.Info("sending done signal to session: %d", id) + sn.done <- true + } + } else { + s.Info("no active sessions") } s.Unlock() diff --git a/internal/ui/ui.go b/internal/ui/ui.go index 891ac5a..edbbbee 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -1,9 +1,6 @@ package ui import ( - "context" - "time" - "github.com/gdamore/tcell/v2" "github.com/jordanorelli/astro-domu/internal/exit" "github.com/jordanorelli/astro-domu/internal/wire" @@ -24,20 +21,11 @@ func (ui *UI) Run() { Port: 12805, } - _, err := ui.client.Dial() + notifications, err := ui.client.Dial() if err != nil { - ui.Error("unable to dial server: %v", err) - return + exit.WithMessage(1, "unable to dial server: %v", err) } - - ctx := context.Background() - - ctx, cancel := context.WithCancel(ctx) - defer func() { - ui.Debug("canceling client context") - cancel() - time.Sleep(time.Second) - }() + go ui.handleNotifications(notifications) screen, err := tcell.NewScreen() if err != nil { @@ -56,19 +44,30 @@ func (ui *UI) Run() { ui.Debug("screen colors: %v", screen.Colors()) ui.Debug("screen has mouse: %v", screen.HasMouse()) - defer func() { - ui.Debug("finalizing screen") - screen.Fini() - }() - ui.mode = &boxWalker{ width: 10, height: 6, } - ui.menu() - ui.client.Close() - ui.Debug("clearing screen") - screen.Clear() + ui.Info("running ui") + if ui.run() { + ui.Info("user requested close") + ui.Info("closing client") + ui.client.Close() + ui.Info("client closed") + ui.Info("finalizing screen") + } + ui.Info("run loop done, shutting down") + screen.Fini() +} + +func (ui *UI) handleNotifications(c <-chan wire.Response) { + for n := range c { + ui.Info("ignoring notification: %v", n) + } + ui.Info("notifications channel is closed so we must be done") + ui.Info("clearing and finalizing screen from notifications goroutine") + ui.screen.Clear() + ui.screen.Fini() } // writeString writes a string in the given style from left to right beginning @@ -88,23 +87,25 @@ func (ui *UI) writeString(x, y int, s string, style tcell.Style) { } } -func (ui *UI) menu() { +func (ui *UI) run() bool { ui.screen.Clear() - _, height := ui.screen.Size() - ui.writeString(0, height-1, "fart", tcell.StyleDefault) - ui.screen.Sync() + ui.mode.draw(ui) for { e := ui.screen.PollEvent() if e == nil { - break + ui.Info("run loop sees nil event, breaking out") + // someone else shut us down, so return false + return false } switch v := e.(type) { case *tcell.EventKey: key := v.Key() if key == tcell.KeyCtrlC { - return + ui.Info("saw ctrl+c keyboard input, shutting down") + // we want to shut things down + return true } } diff --git a/internal/wire/client.go b/internal/wire/client.go index 78967c0..47c8e1d 100644 --- a/internal/wire/client.go +++ b/internal/wire/client.go @@ -84,6 +84,8 @@ func (c *Client) Send(v Value) (Response, error) { func (c *Client) Close() { c.done <- true } func (c *Client) readLoop(notifications chan<- Response) { + defer close(notifications) + for { _, r, err := c.conn.NextReader() if err != nil {