2019-06-21 18:53:50 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/jroimartin/gocui"
|
|
|
|
|
"log"
|
2019-06-21 19:10:06 +00:00
|
|
|
|
"strings"
|
2019-06-21 18:53:50 +00:00
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2019-06-26 23:54:04 +00:00
|
|
|
|
type PeersState struct {
|
2019-06-27 02:31:40 +00:00
|
|
|
|
c *client
|
|
|
|
|
list []Peer
|
|
|
|
|
selected *Peer
|
2019-06-26 23:54:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewPeersState(host string, port int) *PeersState {
|
|
|
|
|
url := fmt.Sprintf("http://%s:%d", host, port)
|
|
|
|
|
c, err := newClient(url)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Panicln(err)
|
|
|
|
|
}
|
|
|
|
|
return &PeersState{c: c}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 00:23:36 +00:00
|
|
|
|
func (p *PeersState) FetchLoop(g *gocui.Gui) {
|
2019-06-21 18:53:50 +00:00
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-threadDone:
|
|
|
|
|
return
|
|
|
|
|
default:
|
2019-06-27 00:23:36 +00:00
|
|
|
|
peers := p.Fetch()
|
2019-06-26 23:54:04 +00:00
|
|
|
|
writePeers(g, peers)
|
2019-06-27 02:55:15 +00:00
|
|
|
|
writePeerDetails(g, p.selected)
|
2019-06-21 18:53:50 +00:00
|
|
|
|
}
|
|
|
|
|
<-time.After(interval * time.Second)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 00:23:36 +00:00
|
|
|
|
func (p *PeersState) Fetch() []Peer {
|
|
|
|
|
peers, err := p.c.getPeers()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Panicln(err)
|
|
|
|
|
}
|
|
|
|
|
p.list = peers
|
2019-06-27 02:31:40 +00:00
|
|
|
|
if p.selected == nil {
|
|
|
|
|
p.selected = &peers[0]
|
|
|
|
|
}
|
2019-06-27 00:23:36 +00:00
|
|
|
|
return peers
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-26 23:54:04 +00:00
|
|
|
|
func writePeers(g *gocui.Gui, peers []Peer) {
|
2019-06-21 18:53:50 +00:00
|
|
|
|
g.Update(func(g *gocui.Gui) error {
|
|
|
|
|
v, err := g.View("main")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
v.Clear()
|
|
|
|
|
maxWidth, _ := g.Size()
|
|
|
|
|
for _, peer := range peers {
|
|
|
|
|
fmt.Fprintf(v, "%s\n", peer.AsTable(maxWidth))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-06-21 19:10:06 +00:00
|
|
|
|
|
2019-06-27 02:31:40 +00:00
|
|
|
|
func writePeerDetails(g *gocui.Gui, peer *Peer) {
|
|
|
|
|
g.Update(func(g *gocui.Gui) error {
|
|
|
|
|
v, err := g.View("info")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
v.Clear()
|
2019-06-27 02:48:21 +00:00
|
|
|
|
fmt.Fprintf(v, strings.Repeat("%-8s: %s\n", 6),
|
2019-06-27 02:46:19 +00:00
|
|
|
|
"Name", peer.Name,
|
|
|
|
|
"ID", string(peer.Id),
|
|
|
|
|
"Enode", peer.Enode,
|
2019-06-27 02:48:21 +00:00
|
|
|
|
"Local", peer.Network.LocalAddress,
|
|
|
|
|
"Remote", peer.Network.RemoteAddress,
|
2019-06-27 02:46:19 +00:00
|
|
|
|
"Caps", strings.Join(peer.Caps, ", "))
|
2019-06-27 02:31:40 +00:00
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 20:47:37 +00:00
|
|
|
|
func boolToString(v bool, yes string, no string) string {
|
|
|
|
|
if v {
|
|
|
|
|
return yes
|
|
|
|
|
} else {
|
|
|
|
|
return no
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 19:10:06 +00:00
|
|
|
|
func (p Peer) AsTable(maxWidth int) string {
|
|
|
|
|
var id string
|
2019-06-21 20:47:37 +00:00
|
|
|
|
if maxWidth > 160 {
|
2019-06-21 19:10:06 +00:00
|
|
|
|
id = string(p.Id)
|
|
|
|
|
} else {
|
|
|
|
|
id = p.Id.String()
|
|
|
|
|
}
|
2019-06-27 02:31:40 +00:00
|
|
|
|
return fmt.Sprintf("%s| %-15s| %-21s| %-7s| %-8s",
|
2019-06-21 20:47:37 +00:00
|
|
|
|
id, p.Name,
|
|
|
|
|
p.Network.RemoteAddress,
|
|
|
|
|
boolToString(p.Network.Trusted, "trusted", "normal"),
|
2019-06-27 02:31:40 +00:00
|
|
|
|
boolToString(p.Network.Static, "static", "dynamic"))
|
2019-06-21 19:10:06 +00:00
|
|
|
|
}
|