2019-06-16 15:23:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-06-20 20:03:27 +00:00
|
|
|
"time"
|
2019-06-20 21:01:34 +00:00
|
|
|
//"github.com/kr/pretty"
|
|
|
|
"github.com/jroimartin/gocui"
|
2019-06-16 15:23:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type rcpResp map[string]interface{}
|
|
|
|
|
2019-06-16 16:02:50 +00:00
|
|
|
const host = "127.0.0.1"
|
|
|
|
const port = 8545
|
2019-06-20 20:03:27 +00:00
|
|
|
const interval = 3
|
|
|
|
|
|
|
|
var threadDone = make(chan struct{})
|
2019-06-16 15:23:24 +00:00
|
|
|
|
|
|
|
func main() {
|
2019-06-19 14:58:48 +00:00
|
|
|
g, err := gocui.NewGui(gocui.OutputNormal)
|
2019-06-16 15:23:24 +00:00
|
|
|
if err != nil {
|
2019-06-19 14:58:48 +00:00
|
|
|
log.Panicln(err)
|
2019-06-16 15:23:24 +00:00
|
|
|
}
|
2019-06-19 14:58:48 +00:00
|
|
|
defer g.Close()
|
2019-06-16 16:02:50 +00:00
|
|
|
|
2019-06-19 14:58:48 +00:00
|
|
|
g.SetManagerFunc(layout)
|
|
|
|
|
2019-06-20 20:03:27 +00:00
|
|
|
if err := keybindings(g); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf("http://%s:%d", host, port)
|
|
|
|
c, err := newClient(url)
|
|
|
|
if err != nil {
|
2019-06-19 14:58:48 +00:00
|
|
|
log.Panicln(err)
|
2019-06-16 15:23:24 +00:00
|
|
|
}
|
2019-06-19 14:58:48 +00:00
|
|
|
|
2019-06-20 20:03:27 +00:00
|
|
|
// Start RPC calling routine
|
|
|
|
go fetchPeers(c, g)
|
|
|
|
|
2019-06-19 14:58:48 +00:00
|
|
|
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 20:03:27 +00:00
|
|
|
func fetchPeers(c *client, g *gocui.Gui) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-threadDone:
|
|
|
|
return
|
|
|
|
case <-time.After(interval * time.Second):
|
|
|
|
peers, err := c.getPeers()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
writePeers(g, peers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 21:01:34 +00:00
|
|
|
func writePeers(g *gocui.Gui, peers []Peer) {
|
2019-06-20 20:03:27 +00:00
|
|
|
g.Update(func(g *gocui.Gui) error {
|
|
|
|
v, err := g.View("main")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Clear()
|
2019-06-20 21:01:34 +00:00
|
|
|
for _, peer := range peers {
|
2019-06-20 21:14:50 +00:00
|
|
|
fmt.Fprintf(v, "%s\n", peer.AsTable())
|
2019-06-20 21:01:34 +00:00
|
|
|
}
|
2019-06-20 20:03:27 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func keybindings(g *gocui.Gui) error {
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-19 14:58:48 +00:00
|
|
|
func layout(g *gocui.Gui) error {
|
|
|
|
maxX, maxY := g.Size()
|
2019-06-20 20:03:27 +00:00
|
|
|
if v, err := g.SetView("main", 0, 0, maxX-1, maxY-1); err != nil {
|
2019-06-19 14:58:48 +00:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-20 20:03:27 +00:00
|
|
|
fmt.Fprintln(v, "Loading peers...")
|
2019-06-19 14:58:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func quit(g *gocui.Gui, v *gocui.View) error {
|
2019-06-20 20:03:27 +00:00
|
|
|
close(threadDone)
|
2019-06-19 14:58:48 +00:00
|
|
|
return gocui.ErrQuit
|
2019-06-16 15:23:24 +00:00
|
|
|
}
|