net.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. 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. }
  32. )
  33. // net callback handler
  34. type nethandler func(*netApi, *shared.Request) (interface{}, error)
  35. // net api provider
  36. type netApi struct {
  37. xeth *xeth.XEth
  38. ethereum *eth.Ethereum
  39. methods map[string]nethandler
  40. codec codec.ApiCoder
  41. }
  42. // create a new net api instance
  43. func NewNetApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *netApi {
  44. return &netApi{
  45. xeth: xeth,
  46. ethereum: eth,
  47. methods: netMapping,
  48. codec: coder.New(nil),
  49. }
  50. }
  51. // collection with supported methods
  52. func (self *netApi) Methods() []string {
  53. methods := make([]string, len(self.methods))
  54. i := 0
  55. for k := range self.methods {
  56. methods[i] = k
  57. i++
  58. }
  59. return methods
  60. }
  61. // Execute given request
  62. func (self *netApi) Execute(req *shared.Request) (interface{}, error) {
  63. if callback, ok := self.methods[req.Method]; ok {
  64. return callback(self, req)
  65. }
  66. return nil, shared.NewNotImplementedError(req.Method)
  67. }
  68. func (self *netApi) Name() string {
  69. return shared.NetApiName
  70. }
  71. func (self *netApi) ApiVersion() string {
  72. return NetApiVersion
  73. }
  74. // Number of connected peers
  75. func (self *netApi) PeerCount(req *shared.Request) (interface{}, error) {
  76. return newHexNum(self.xeth.PeerCount()), nil
  77. }
  78. func (self *netApi) IsListening(req *shared.Request) (interface{}, error) {
  79. return self.xeth.IsListening(), nil
  80. }