Implement getnettotals RPC.
Also, change the display handler for getnettotals in btcctl to the JSON display handler for better display. Closes #84.
This commit is contained in:
parent
f8c843e2e3
commit
6f5f582c42
3 changed files with 48 additions and 4 deletions
17
rpcserver.go
17
rpcserver.go
|
@ -64,6 +64,7 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
|
||||||
"getgenerate": handleGetGenerate,
|
"getgenerate": handleGetGenerate,
|
||||||
"gethashespersec": handleGetHashesPerSec,
|
"gethashespersec": handleGetHashesPerSec,
|
||||||
"getinfo": handleGetInfo,
|
"getinfo": handleGetInfo,
|
||||||
|
"getnettotals": handleGetNetTotals,
|
||||||
"getpeerinfo": handleGetPeerInfo,
|
"getpeerinfo": handleGetPeerInfo,
|
||||||
"getrawmempool": handleGetRawMempool,
|
"getrawmempool": handleGetRawMempool,
|
||||||
"getrawtransaction": handleGetRawTransaction,
|
"getrawtransaction": handleGetRawTransaction,
|
||||||
|
@ -133,7 +134,6 @@ var rpcAskWallet = map[string]bool{
|
||||||
// Commands that are temporarily unimplemented.
|
// Commands that are temporarily unimplemented.
|
||||||
var rpcUnimplemented = map[string]bool{
|
var rpcUnimplemented = map[string]bool{
|
||||||
"getmininginfo": true,
|
"getmininginfo": true,
|
||||||
"getnettotals": true,
|
|
||||||
"getnetworkhashps": true,
|
"getnetworkhashps": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -727,8 +727,8 @@ func handleGetAddedNodeInfo(s *rpcServer, cmd btcjson.Cmd) (interface{}, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Without the dns flag, the result is just a slice of the adddresses
|
// Without the dns flag, the result is just a slice of the addresses as
|
||||||
// as strings.
|
// strings.
|
||||||
if !c.Dns {
|
if !c.Dns {
|
||||||
results := make([]string, 0, len(peers))
|
results := make([]string, 0, len(peers))
|
||||||
for _, peer := range peers {
|
for _, peer := range peers {
|
||||||
|
@ -978,6 +978,17 @@ func handleGetInfo(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handleGetNetTotals implements the getnettotals command.
|
||||||
|
func handleGetNetTotals(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
|
||||||
|
netTotals := s.server.NetTotals()
|
||||||
|
reply := &btcjson.GetNetTotalsResult{
|
||||||
|
TotalBytesRecv: netTotals.TotalBytesRecv,
|
||||||
|
TotalBytesSent: netTotals.TotalBytesSent,
|
||||||
|
TimeMillis: time.Now().UnixNano() / 1000,
|
||||||
|
}
|
||||||
|
return reply, nil
|
||||||
|
}
|
||||||
|
|
||||||
// handleGetPeerInfo implements the getpeerinfo command.
|
// handleGetPeerInfo implements the getpeerinfo command.
|
||||||
func handleGetPeerInfo(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
|
func handleGetPeerInfo(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
|
||||||
return s.server.PeerInfo(), nil
|
return s.server.PeerInfo(), nil
|
||||||
|
|
33
server.go
33
server.go
|
@ -300,6 +300,19 @@ type getAddedNodesMsg struct {
|
||||||
reply chan []*peer
|
reply chan []*peer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NetTotals contains information about the total bytes received and sent across
|
||||||
|
// the network.
|
||||||
|
type NetTotals struct {
|
||||||
|
TotalBytesRecv uint64
|
||||||
|
TotalBytesSent uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
// getNetTotals is a message type to be sent across the query channel for
|
||||||
|
// retrieving the current total bytes sent and received from all peers.
|
||||||
|
type getNetTotals struct {
|
||||||
|
reply chan *NetTotals
|
||||||
|
}
|
||||||
|
|
||||||
// handleQuery is the central handler for all queries and commands from other
|
// handleQuery is the central handler for all queries and commands from other
|
||||||
// goroutines related to peer state.
|
// goroutines related to peer state.
|
||||||
func (s *server) handleQuery(querymsg interface{}, state *peerState) {
|
func (s *server) handleQuery(querymsg interface{}, state *peerState) {
|
||||||
|
@ -403,6 +416,18 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
|
||||||
peers = append(peers, peer)
|
peers = append(peers, peer)
|
||||||
}
|
}
|
||||||
msg.reply <- peers
|
msg.reply <- peers
|
||||||
|
|
||||||
|
// Request the total bytes sent and received.
|
||||||
|
case getNetTotals:
|
||||||
|
// Respond with a ....
|
||||||
|
netTotals := NetTotals{}
|
||||||
|
state.forAllPeers(func(p *peer) {
|
||||||
|
if p.Connected() {
|
||||||
|
netTotals.TotalBytesRecv += p.bytesReceived
|
||||||
|
netTotals.TotalBytesSent += p.bytesSent
|
||||||
|
}
|
||||||
|
})
|
||||||
|
msg.reply <- &netTotals
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -694,6 +719,14 @@ func (s *server) RemoveAddr(addr string) error {
|
||||||
return <-replyChan
|
return <-replyChan
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NetTotals returns the sum of all bytes received and sent across the network
|
||||||
|
// for all peers.
|
||||||
|
func (s *server) NetTotals() *NetTotals {
|
||||||
|
reply := make(chan *NetTotals)
|
||||||
|
s.query <- getNetTotals{reply: reply}
|
||||||
|
return <-reply
|
||||||
|
}
|
||||||
|
|
||||||
// Start begins accepting connections from peers.
|
// Start begins accepting connections from peers.
|
||||||
func (s *server) Start() {
|
func (s *server) Start() {
|
||||||
// Already started?
|
// Already started?
|
||||||
|
|
|
@ -66,7 +66,7 @@ var commandHandlers = map[string]*handlerData{
|
||||||
"getgenerate": {0, 0, displayGeneric, nil, makeGetGenerate, ""},
|
"getgenerate": {0, 0, displayGeneric, nil, makeGetGenerate, ""},
|
||||||
"gethashespersec": {0, 0, displayGeneric, nil, makeGetHashesPerSec, ""},
|
"gethashespersec": {0, 0, displayGeneric, nil, makeGetHashesPerSec, ""},
|
||||||
"getinfo": {0, 0, displayJSONDump, nil, makeGetInfo, ""},
|
"getinfo": {0, 0, displayJSONDump, nil, makeGetInfo, ""},
|
||||||
"getnettotals": {0, 0, displayGeneric, nil, makeGetNetTotals, ""},
|
"getnettotals": {0, 0, displayJSONDump, nil, makeGetNetTotals, ""},
|
||||||
"getnewaddress": {0, 1, displayGeneric, nil, makeGetNewAddress, "[account]"},
|
"getnewaddress": {0, 1, displayGeneric, nil, makeGetNewAddress, "[account]"},
|
||||||
"getpeerinfo": {0, 0, displayJSONDump, nil, makeGetPeerInfo, ""},
|
"getpeerinfo": {0, 0, displayJSONDump, nil, makeGetPeerInfo, ""},
|
||||||
"getrawchangeaddress": {0, 0, displayGeneric, nil, makeGetRawChangeAddress, ""},
|
"getrawchangeaddress": {0, 0, displayGeneric, nil, makeGetRawChangeAddress, ""},
|
||||||
|
|
Loading…
Add table
Reference in a new issue