github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/mobile/p2p.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  // Contains wrappers for the p2p package.
    13  
    14  package geth
    15  
    16  import (
    17  	"errors"
    18  
    19  	"github.com/Sberex/go-sberex/p2p"
    20  )
    21  
    22  // NodeInfo represents pi short summary of the information known about the host.
    23  type NodeInfo struct {
    24  	info *p2p.NodeInfo
    25  }
    26  
    27  func (ni *NodeInfo) GetID() string              { return ni.info.ID }
    28  func (ni *NodeInfo) GetName() string            { return ni.info.Name }
    29  func (ni *NodeInfo) GetEnode() string           { return ni.info.Enode }
    30  func (ni *NodeInfo) GetIP() string              { return ni.info.IP }
    31  func (ni *NodeInfo) GetDiscoveryPort() int      { return ni.info.Ports.Discovery }
    32  func (ni *NodeInfo) GetListenerPort() int       { return ni.info.Ports.Listener }
    33  func (ni *NodeInfo) GetListenerAddress() string { return ni.info.ListenAddr }
    34  func (ni *NodeInfo) GetProtocols() *Strings {
    35  	protos := []string{}
    36  	for proto := range ni.info.Protocols {
    37  		protos = append(protos, proto)
    38  	}
    39  	return &Strings{protos}
    40  }
    41  
    42  // PeerInfo represents pi short summary of the information known about pi connected peer.
    43  type PeerInfo struct {
    44  	info *p2p.PeerInfo
    45  }
    46  
    47  func (pi *PeerInfo) GetID() string            { return pi.info.ID }
    48  func (pi *PeerInfo) GetName() string          { return pi.info.Name }
    49  func (pi *PeerInfo) GetCaps() *Strings        { return &Strings{pi.info.Caps} }
    50  func (pi *PeerInfo) GetLocalAddress() string  { return pi.info.Network.LocalAddress }
    51  func (pi *PeerInfo) GetRemoteAddress() string { return pi.info.Network.RemoteAddress }
    52  
    53  // PeerInfos represents a slice of infos about remote peers.
    54  type PeerInfos struct {
    55  	infos []*p2p.PeerInfo
    56  }
    57  
    58  // Size returns the number of peer info entries in the slice.
    59  func (pi *PeerInfos) Size() int {
    60  	return len(pi.infos)
    61  }
    62  
    63  // Get returns the peer info at the given index from the slice.
    64  func (pi *PeerInfos) Get(index int) (info *PeerInfo, _ error) {
    65  	if index < 0 || index >= len(pi.infos) {
    66  		return nil, errors.New("index out of bounds")
    67  	}
    68  	return &PeerInfo{pi.infos[index]}, nil
    69  }