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.5 KiB

// Copyright 2015 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum 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.
//
// go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
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{
9 years ago
"net_peerCount": (*netApi).PeerCount,
"net_listening": (*netApi).IsListening,
"net_version": (*netApi).Version,
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
// 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
}
func (self *netApi) Version(req *shared.Request) (interface{}, error) {
return self.xeth.NetworkVersion(), nil
}