p2p.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2016 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. // Contains wrappers for the p2p package.
  17. package geth
  18. import (
  19. "errors"
  20. "github.com/ethereum/go-ethereum/p2p"
  21. )
  22. // NodeInfo represents pi short summary of the information known about the host.
  23. type NodeInfo struct {
  24. info *p2p.NodeInfo
  25. }
  26. func (ni *NodeInfo) GetID() string { return ni.info.ID }
  27. func (ni *NodeInfo) GetName() string { return ni.info.Name }
  28. func (ni *NodeInfo) GetEnode() string { return ni.info.Enode }
  29. func (ni *NodeInfo) GetIP() string { return ni.info.IP }
  30. func (ni *NodeInfo) GetDiscoveryPort() int { return ni.info.Ports.Discovery }
  31. func (ni *NodeInfo) GetListenerPort() int { return ni.info.Ports.Listener }
  32. func (ni *NodeInfo) GetListenerAddress() string { return ni.info.ListenAddr }
  33. func (ni *NodeInfo) GetProtocols() *Strings {
  34. protos := []string{}
  35. for proto := range ni.info.Protocols {
  36. protos = append(protos, proto)
  37. }
  38. return &Strings{protos}
  39. }
  40. // PeerInfo represents pi short summary of the information known about pi connected peer.
  41. type PeerInfo struct {
  42. info *p2p.PeerInfo
  43. }
  44. func (pi *PeerInfo) GetID() string { return pi.info.ID }
  45. func (pi *PeerInfo) GetName() string { return pi.info.Name }
  46. func (pi *PeerInfo) GetCaps() *Strings { return &Strings{pi.info.Caps} }
  47. func (pi *PeerInfo) GetLocalAddress() string { return pi.info.Network.LocalAddress }
  48. func (pi *PeerInfo) GetRemoteAddress() string { return pi.info.Network.RemoteAddress }
  49. // PeerInfos represents a slice of infos about remote peers.
  50. type PeerInfos struct {
  51. infos []*p2p.PeerInfo
  52. }
  53. // Size returns the number of peer info entries in the slice.
  54. func (pi *PeerInfos) Size() int {
  55. return len(pi.infos)
  56. }
  57. // Get returns the peer info at the given index from the slice.
  58. func (pi *PeerInfos) Get(index int) (info *PeerInfo, _ error) {
  59. if index < 0 || index >= len(pi.infos) {
  60. return nil, errors.New("index out of bounds")
  61. }
  62. return &PeerInfo{pi.infos[index]}, nil
  63. }