github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/net.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/eth"
    21  	"github.com/ethereum/go-ethereum/rpc/codec"
    22  	"github.com/ethereum/go-ethereum/rpc/shared"
    23  	"github.com/ethereum/go-ethereum/xeth"
    24  )
    25  
    26  const (
    27  	NetApiVersion = "1.0"
    28  )
    29  
    30  var (
    31  	// mapping between methods and handlers
    32  	netMapping = map[string]nethandler{
    33  		"net_peerCount": (*netApi).PeerCount,
    34  		"net_listening": (*netApi).IsListening,
    35  		"net_version":   (*netApi).Version,
    36  	}
    37  )
    38  
    39  // net callback handler
    40  type nethandler func(*netApi, *shared.Request) (interface{}, error)
    41  
    42  // net api provider
    43  type netApi struct {
    44  	xeth     *xeth.XEth
    45  	ethereum *eth.Ethereum
    46  	methods  map[string]nethandler
    47  	codec    codec.ApiCoder
    48  }
    49  
    50  // create a new net api instance
    51  func NewNetApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *netApi {
    52  	return &netApi{
    53  		xeth:     xeth,
    54  		ethereum: eth,
    55  		methods:  netMapping,
    56  		codec:    coder.New(nil),
    57  	}
    58  }
    59  
    60  // collection with supported methods
    61  func (self *netApi) Methods() []string {
    62  	methods := make([]string, len(self.methods))
    63  	i := 0
    64  	for k := range self.methods {
    65  		methods[i] = k
    66  		i++
    67  	}
    68  	return methods
    69  }
    70  
    71  // Execute given request
    72  func (self *netApi) Execute(req *shared.Request) (interface{}, error) {
    73  	if callback, ok := self.methods[req.Method]; ok {
    74  		return callback(self, req)
    75  	}
    76  
    77  	return nil, shared.NewNotImplementedError(req.Method)
    78  }
    79  
    80  func (self *netApi) Name() string {
    81  	return shared.NetApiName
    82  }
    83  
    84  func (self *netApi) ApiVersion() string {
    85  	return NetApiVersion
    86  }
    87  
    88  // Number of connected peers
    89  func (self *netApi) PeerCount(req *shared.Request) (interface{}, error) {
    90  	return newHexNum(self.xeth.PeerCount()), nil
    91  }
    92  
    93  func (self *netApi) IsListening(req *shared.Request) (interface{}, error) {
    94  	return self.xeth.IsListening(), nil
    95  }
    96  
    97  func (self *netApi) Version(req *shared.Request) (interface{}, error) {
    98  	return self.xeth.NetworkVersion(), nil
    99  }