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,
|
||||
"gethashespersec": handleGetHashesPerSec,
|
||||
"getinfo": handleGetInfo,
|
||||
"getnettotals": handleGetNetTotals,
|
||||
"getpeerinfo": handleGetPeerInfo,
|
||||
"getrawmempool": handleGetRawMempool,
|
||||
"getrawtransaction": handleGetRawTransaction,
|
||||
|
@ -133,7 +134,6 @@ var rpcAskWallet = map[string]bool{
|
|||
// Commands that are temporarily unimplemented.
|
||||
var rpcUnimplemented = map[string]bool{
|
||||
"getmininginfo": true,
|
||||
"getnettotals": 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
|
||||
// as strings.
|
||||
// Without the dns flag, the result is just a slice of the addresses as
|
||||
// strings.
|
||||
if !c.Dns {
|
||||
results := make([]string, 0, len(peers))
|
||||
for _, peer := range peers {
|
||||
|
@ -978,6 +978,17 @@ func handleGetInfo(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
|
|||
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.
|
||||
func handleGetPeerInfo(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
|
||||
return s.server.PeerInfo(), nil
|
||||
|
|
33
server.go
33
server.go
|
@ -300,6 +300,19 @@ type getAddedNodesMsg struct {
|
|||
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
|
||||
// goroutines related to peer state.
|
||||
func (s *server) handleQuery(querymsg interface{}, state *peerState) {
|
||||
|
@ -403,6 +416,18 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
|
|||
peers = append(peers, peer)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (s *server) Start() {
|
||||
// Already started?
|
||||
|
|
|
@ -66,7 +66,7 @@ var commandHandlers = map[string]*handlerData{
|
|||
"getgenerate": {0, 0, displayGeneric, nil, makeGetGenerate, ""},
|
||||
"gethashespersec": {0, 0, displayGeneric, nil, makeGetHashesPerSec, ""},
|
||||
"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]"},
|
||||
"getpeerinfo": {0, 0, displayJSONDump, nil, makeGetPeerInfo, ""},
|
||||
"getrawchangeaddress": {0, 0, displayGeneric, nil, makeGetRawChangeAddress, ""},
|
||||
|
|
Loading…
Reference in a new issue