can shut down from either side now

master
Jordan Orelli 4 years ago
parent ecdf1af964
commit 81801e640a

@ -144,12 +144,15 @@ func (s *Server) Shutdown() {
go func() { go func() {
defer wg.Done() defer wg.Done()
s.Info("broadcasting shutdown to all active sessions")
s.Lock() s.Lock()
for id, sn := range s.sessions { if len(s.sessions) > 0 {
s.Info("sending done signal to session: %d", id) s.Info("broadcasting shutdown to %d active sessions", len(s.sessions))
sn.done <- true 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() s.Unlock()

@ -1,9 +1,6 @@
package ui package ui
import ( import (
"context"
"time"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/exit" "github.com/jordanorelli/astro-domu/internal/exit"
"github.com/jordanorelli/astro-domu/internal/wire" "github.com/jordanorelli/astro-domu/internal/wire"
@ -24,20 +21,11 @@ func (ui *UI) Run() {
Port: 12805, Port: 12805,
} }
_, err := ui.client.Dial() notifications, err := ui.client.Dial()
if err != nil { if err != nil {
ui.Error("unable to dial server: %v", err) exit.WithMessage(1, "unable to dial server: %v", err)
return
} }
go ui.handleNotifications(notifications)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer func() {
ui.Debug("canceling client context")
cancel()
time.Sleep(time.Second)
}()
screen, err := tcell.NewScreen() screen, err := tcell.NewScreen()
if err != nil { if err != nil {
@ -56,19 +44,30 @@ func (ui *UI) Run() {
ui.Debug("screen colors: %v", screen.Colors()) ui.Debug("screen colors: %v", screen.Colors())
ui.Debug("screen has mouse: %v", screen.HasMouse()) ui.Debug("screen has mouse: %v", screen.HasMouse())
defer func() {
ui.Debug("finalizing screen")
screen.Fini()
}()
ui.mode = &boxWalker{ ui.mode = &boxWalker{
width: 10, width: 10,
height: 6, height: 6,
} }
ui.menu() ui.Info("running ui")
ui.client.Close() if ui.run() {
ui.Debug("clearing screen") ui.Info("user requested close")
screen.Clear() 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 // 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() ui.screen.Clear()
_, height := ui.screen.Size() ui.mode.draw(ui)
ui.writeString(0, height-1, "fart", tcell.StyleDefault)
ui.screen.Sync()
for { for {
e := ui.screen.PollEvent() e := ui.screen.PollEvent()
if e == nil { 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) { switch v := e.(type) {
case *tcell.EventKey: case *tcell.EventKey:
key := v.Key() key := v.Key()
if key == tcell.KeyCtrlC { if key == tcell.KeyCtrlC {
return ui.Info("saw ctrl+c keyboard input, shutting down")
// we want to shut things down
return true
} }
} }

@ -84,6 +84,8 @@ func (c *Client) Send(v Value) (Response, error) {
func (c *Client) Close() { c.done <- true } func (c *Client) Close() { c.done <- true }
func (c *Client) readLoop(notifications chan<- Response) { func (c *Client) readLoop(notifications chan<- Response) {
defer close(notifications)
for { for {
_, r, err := c.conn.NextReader() _, r, err := c.conn.NextReader()
if err != nil { if err != nil {

Loading…
Cancel
Save