net.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. package api
  17. import (
  18. "github.com/ethereum/go-ethereum/eth"
  19. "github.com/ethereum/go-ethereum/rpc/codec"
  20. "github.com/ethereum/go-ethereum/rpc/shared"
  21. "github.com/ethereum/go-ethereum/xeth"
  22. )
  23. const (
  24. NetApiVersion = "1.0"
  25. )
  26. var (
  27. // mapping between methods and handlers
  28. netMapping = map[string]nethandler{
  29. "net_peerCount": (*netApi).PeerCount,
  30. "net_listening": (*netApi).IsListening,
  31. "net_version": (*netApi).Version,
  32. }
  33. )
  34. // net callback handler
  35. type nethandler func(*netApi, *shared.Request) (interface{}, error)
  36. // net api provider
  37. type netApi struct {
  38. xeth *xeth.XEth
  39. ethereum *eth.Ethereum
  40. methods map[string]nethandler
  41. codec codec.ApiCoder
  42. }
  43. // create a new net api instance
  44. func NewNetApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *netApi {
  45. return &netApi{
  46. xeth: xeth,
  47. ethereum: eth,
  48. methods: netMapping,
  49. codec: coder.New(nil),
  50. }
  51. }
  52. // collection with supported methods
  53. func (self *netApi) Methods() []string {
  54. methods := make([]string, len(self.methods))
  55. i := 0
  56. for k := range self.methods {
  57. methods[i] = k
  58. i++
  59. }
  60. return methods
  61. }
  62. // Execute given request
  63. func (self *netApi) Execute(req *shared.Request) (interface{}, error) {
  64. if callback, ok := self.methods[req.Method]; ok {
  65. return callback(self, req)
  66. }
  67. return nil, shared.NewNotImplementedError(req.Method)
  68. }
  69. func (self *netApi) Name() string {
  70. return shared.NetApiName
  71. }
  72. func (self *netApi) ApiVersion() string {
  73. return NetApiVersion
  74. }
  75. // Number of connected peers
  76. func (self *netApi) PeerCount(req *shared.Request) (interface{}, error) {
  77. return newHexNum(self.xeth.PeerCount()), nil
  78. }
  79. func (self *netApi) IsListening(req *shared.Request) (interface{}, error) {
  80. return self.xeth.IsListening(), nil
  81. }
  82. func (self *netApi) Version(req *shared.Request) (interface{}, error) {
  83. return self.xeth.NetworkVersion(), nil
  84. }