From 6f5f582c4241ebc3605199a1870251c264d00ae9 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Tue, 4 Feb 2014 01:26:12 -0600 Subject: [PATCH] Implement getnettotals RPC. Also, change the display handler for getnettotals in btcctl to the JSON display handler for better display. Closes #84. --- rpcserver.go | 17 ++++++++++++++--- server.go | 33 +++++++++++++++++++++++++++++++++ util/btcctl/btcctl.go | 2 +- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index bcc77514..921295f2 100644 --- a/rpcserver.go +++ b/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 diff --git a/server.go b/server.go index c02d8a2c..676fa9a0 100644 --- a/server.go +++ b/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? diff --git a/util/btcctl/btcctl.go b/util/btcctl/btcctl.go index 82b0663e..375f7061 100644 --- a/util/btcctl/btcctl.go +++ b/util/btcctl/btcctl.go @@ -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, ""},