github.com/iotexproject/iotex-core@v1.14.1-rc1/p2p/node/node.go (about)

     1  // Copyright (c) 2018 IoTeX
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package node
     7  
     8  // Node is the root struct to embed the network identifier
     9  type Node struct {
    10  	NetworkType string
    11  	Addr        string
    12  }
    13  
    14  // Network returns the transportation layer type (tcp by default)
    15  func (n *Node) Network() string {
    16  	if n.NetworkType == "" {
    17  		return "tcp"
    18  	}
    19  	return n.NetworkType
    20  }
    21  
    22  // String returns the network address (127.0.0.1:0 by default)
    23  func (n *Node) String() string {
    24  	if n.Addr == "" {
    25  		return "127.0.0.1:0"
    26  	}
    27  	return n.Addr
    28  }
    29  
    30  // NewTCPNode creates an instance of Peer with tcp transportation
    31  func NewTCPNode(addr string) *Node {
    32  	return NewNode("tcp", addr)
    33  }
    34  
    35  // NewNode creates an instance of Peer
    36  func NewNode(n string, addr string) *Node {
    37  	return &Node{NetworkType: n, Addr: addr}
    38  }