github.com/core-coin/go-core/v2@v2.1.9/mobile/p2p.go (about)

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