net.go 2.59 KB
Newer Older
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 6 7 8
// 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.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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

Bas van Kervel's avatar
Bas van Kervel committed
17 18 19 20 21 22 23 24 25
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"
)

Bas van Kervel's avatar
Bas van Kervel committed
26 27 28 29
const (
	NetApiVersion = "1.0"
)

Bas van Kervel's avatar
Bas van Kervel committed
30 31 32
var (
	// mapping between methods and handlers
	netMapping = map[string]nethandler{
Bas van Kervel's avatar
Bas van Kervel committed
33 34
		"net_peerCount": (*netApi).PeerCount,
		"net_listening": (*netApi).IsListening,
35
		"net_version":   (*netApi).Version,
Bas van Kervel's avatar
Bas van Kervel committed
36 37 38 39
	}
)

// net callback handler
Bas van Kervel's avatar
Bas van Kervel committed
40
type nethandler func(*netApi, *shared.Request) (interface{}, error)
Bas van Kervel's avatar
Bas van Kervel committed
41 42

// net api provider
Bas van Kervel's avatar
Bas van Kervel committed
43
type netApi struct {
Bas van Kervel's avatar
Bas van Kervel committed
44 45 46 47 48 49 50
	xeth     *xeth.XEth
	ethereum *eth.Ethereum
	methods  map[string]nethandler
	codec    codec.ApiCoder
}

// create a new net api instance
Bas van Kervel's avatar
Bas van Kervel committed
51 52
func NewNetApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *netApi {
	return &netApi{
Bas van Kervel's avatar
Bas van Kervel committed
53 54 55 56 57 58 59 60
		xeth:     xeth,
		ethereum: eth,
		methods:  netMapping,
		codec:    coder.New(nil),
	}
}

// collection with supported methods
Bas van Kervel's avatar
Bas van Kervel committed
61
func (self *netApi) Methods() []string {
Bas van Kervel's avatar
Bas van Kervel committed
62 63 64 65 66 67 68 69 70 71
	methods := make([]string, len(self.methods))
	i := 0
	for k := range self.methods {
		methods[i] = k
		i++
	}
	return methods
}

// Execute given request
Bas van Kervel's avatar
Bas van Kervel committed
72
func (self *netApi) Execute(req *shared.Request) (interface{}, error) {
Bas van Kervel's avatar
Bas van Kervel committed
73 74 75 76 77 78 79
	if callback, ok := self.methods[req.Method]; ok {
		return callback(self, req)
	}

	return nil, shared.NewNotImplementedError(req.Method)
}

Bas van Kervel's avatar
Bas van Kervel committed
80
func (self *netApi) Name() string {
81
	return shared.NetApiName
Bas van Kervel's avatar
Bas van Kervel committed
82 83
}

Bas van Kervel's avatar
Bas van Kervel committed
84 85 86 87
func (self *netApi) ApiVersion() string {
	return NetApiVersion
}

Bas van Kervel's avatar
Bas van Kervel committed
88
// Number of connected peers
Bas van Kervel's avatar
Bas van Kervel committed
89
func (self *netApi) PeerCount(req *shared.Request) (interface{}, error) {
90
	return newHexNum(self.xeth.PeerCount()), nil
Bas van Kervel's avatar
Bas van Kervel committed
91 92
}

Bas van Kervel's avatar
Bas van Kervel committed
93
func (self *netApi) IsListening(req *shared.Request) (interface{}, error) {
Bas van Kervel's avatar
Bas van Kervel committed
94 95 96
	return self.xeth.IsListening(), nil
}

Bas van Kervel's avatar
Bas van Kervel committed
97 98 99
func (self *netApi) Version(req *shared.Request) (interface{}, error) {
	return self.xeth.NetworkVersion(), nil
}