diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2022-06-24 10:41:37 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2022-06-24 10:41:37 -0700 |
| commit | 3178ae580a3fe97d6a1167b4346d209f04e9b7e3 (patch) | |
| tree | da306047792334bbb97e07707d713d3fa582753a /hotline/ui.go | |
| parent | a1ac9a6f60c6881bb9fe97425f6aaa524b910035 (diff) | |
Implement bufio.Scanner for transaction parsing
Diffstat (limited to 'hotline/ui.go')
| -rw-r--r-- | hotline/ui.go | 53 |
1 files changed, 33 insertions, 20 deletions
diff --git a/hotline/ui.go b/hotline/ui.go index b9e45dd..6eba011 100644 --- a/hotline/ui.go +++ b/hotline/ui.go @@ -1,11 +1,11 @@ package hotline import ( + "bufio" "fmt" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" "gopkg.in/yaml.v3" - "io" "io/ioutil" "os" "strconv" @@ -197,29 +197,41 @@ func (ui *UI) joinServer(addr, login, password string) error { } go func() { - for { - err := ui.HLClient.ReadLoop() - 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") + // Create a new scanner for parsing incoming bytes into transaction tokens + scanner := bufio.NewScanner(ui.HLClient.Connection) + scanner.Split(transactionScanner) - ui.Pages.AddPage("loginErr", loginErrModal, false, true) - ui.App.Draw() - return - } - ui.Pages.SwitchToPage("home") + // 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()) - return + t, _, err := ReadTransaction(buf) + if err != nil { + 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 @@ -324,6 +336,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") } |