X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/aebc4d3647b9823ae8cbb57b21b1af83bfd011fb..6c67b0a53e6b894342e91a8533f3bee3adf64346:/hotline/ui.go diff --git a/hotline/ui.go b/hotline/ui.go index 79cb5e6..709137e 100644 --- a/hotline/ui.go +++ b/hotline/ui.go @@ -1,12 +1,11 @@ package hotline import ( + "bufio" "fmt" - "github.com/davecgh/go-spew/spew" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" - "gopkg.in/yaml.v2" - "io" + "gopkg.in/yaml.v3" "io/ioutil" "os" "strconv" @@ -108,7 +107,7 @@ func (ui *UI) showBookmarks() *tview.List { func (ui *UI) getTrackerList() *tview.List { listing, err := GetListing(ui.HLClient.pref.Tracker) if err != nil { - spew.Dump(err) + // TODO } list := tview.NewList() @@ -198,29 +197,42 @@ func (ui *UI) joinServer(addr, login, password string) error { } go func() { - for { - err := ui.HLClient.ReadLoop() + // Create a new scanner for parsing incoming bytes into transaction tokens + scanner := bufio.NewScanner(ui.HLClient.Connection) + scanner.Split(transactionScanner) + + // Scan for new transactions and handle them as they come in. + for scanner.Scan() { + // Make a new []byte slice and copy the scanner bytes to it. This is critical to avoid a data race as the + // scanner re-uses the buffer for subsequent scans. + buf := make([]byte, len(scanner.Bytes())) + copy(buf, scanner.Bytes()) + + var t Transaction + _, err := t.Write(buf) if err != nil { - ui.HLClient.Logger.Errorw("read error", "err", err) - - if err == io.EOF { - loginErrModal := tview.NewModal(). - AddButtons([]string{"Ok"}). - SetText("The server connection has closed."). - SetDoneFunc(func(buttonIndex int, buttonLabel string) { - ui.Pages.SwitchToPage("home") - }) - loginErrModal.Box.SetTitle("Server Connection Error") - - ui.Pages.AddPage("loginErr", loginErrModal, false, true) - ui.App.Draw() - return - } - ui.Pages.SwitchToPage("home") - - return + break + } + if err := ui.HLClient.HandleTransaction(&t); err != nil { + ui.HLClient.Logger.Errorw("Error handling transaction", "err", err) } } + + if scanner.Err() == nil { + loginErrModal := tview.NewModal(). + AddButtons([]string{"Ok"}). + SetText("The server connection has closed."). + SetDoneFunc(func(buttonIndex int, buttonLabel string) { + ui.Pages.SwitchToPage("home") + }) + loginErrModal.Box.SetTitle("Server Connection Error") + + ui.Pages.AddPage("loginErr", loginErrModal, false, true) + ui.App.Draw() + return + } + ui.Pages.SwitchToPage("home") + }() return nil @@ -325,6 +337,7 @@ func (ui *UI) renderServerUI() *tview.Flex { if buttonIndex == 1 { _ = ui.HLClient.Disconnect() ui.Pages.RemovePage(pageServerUI) + ui.Pages.SwitchToPage("home") } else { ui.Pages.HidePage("modal") }