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

90 lines
2.0 KiB

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