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

101 lines
2.6 KiB

// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
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"
)
10 years ago
const (
NetApiVersion = "1.0"
)
10 years ago
var (
// mapping between methods and handlers
netMapping = map[string]nethandler{
10 years ago
"net_peerCount": (*netApi).PeerCount,
"net_listening": (*netApi).IsListening,
"net_version": (*netApi).Version,
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 {
return shared.NetApiName
10 years ago
}
10 years ago
func (self *netApi) ApiVersion() string {
return NetApiVersion
}
10 years ago
// Number of connected peers
10 years ago
func (self *netApi) PeerCount(req *shared.Request) (interface{}, error) {
return newHexNum(self.xeth.PeerCount()), nil
10 years ago
}
10 years ago
func (self *netApi) IsListening(req *shared.Request) (interface{}, error) {
10 years ago
return self.xeth.IsListening(), nil
}
func (self *netApi) Version(req *shared.Request) (interface{}, error) {
return self.xeth.NetworkVersion(), nil
}