Official Go implementation of the Ethereum protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-ethereum/rpc/api/net.go

82 lines
1.9 KiB

10 years ago
package api
import (
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/shared"
"github.com/ethereum/go-ethereum/xeth"
)
var (
// mapping between methods and handlers
netMapping = map[string]nethandler{
"net_version": (*netApi).Version,
10 years ago
"net_peerCount": (*netApi).PeerCount,
"net_listening": (*netApi).IsListening,
"net_peers": (*netApi).Peers,
10 years ago
}
)
// net callback handler
10 years ago
type nethandler func(*netApi, *shared.Request) (interface{}, error)
10 years ago
// net api provider
10 years ago
type netApi struct {
10 years ago
xeth *xeth.XEth
ethereum *eth.Ethereum
methods map[string]nethandler
codec codec.ApiCoder
}
// create a new net api instance
10 years ago
func NewNetApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *netApi {
return &netApi{
10 years ago
xeth: xeth,
ethereum: eth,
methods: netMapping,
codec: coder.New(nil),
}
}
// collection with supported methods
10 years ago
func (self *netApi) Methods() []string {
10 years ago
methods := make([]string, len(self.methods))
i := 0
for k := range self.methods {
methods[i] = k
i++
}
return methods
}
// Execute given request
10 years ago
func (self *netApi) Execute(req *shared.Request) (interface{}, error) {
10 years ago
if callback, ok := self.methods[req.Method]; ok {
return callback(self, req)
}
return nil, shared.NewNotImplementedError(req.Method)
}
10 years ago
func (self *netApi) Name() string {
10 years ago
return NetApiName
}
// Network version
func (self *netApi) Version(req *shared.Request) (interface{}, error) {
10 years ago
return self.xeth.NetworkVersion(), nil
}
// Number of connected peers
10 years ago
func (self *netApi) PeerCount(req *shared.Request) (interface{}, error) {
10 years ago
return self.xeth.PeerCount(), nil
}
10 years ago
func (self *netApi) IsListening(req *shared.Request) (interface{}, error) {
10 years ago
return self.xeth.IsListening(), nil
}
10 years ago
func (self *netApi) Peers(req *shared.Request) (interface{}, error) {
10 years ago
return self.ethereum.PeersInfo(), nil
}