github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/network/simulation/connect.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2018 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package simulation
    26  
    27  import (
    28  	"strings"
    29  
    30  	"github.com/ethereum/go-ethereum/p2p/discover"
    31  )
    32  
    33  //ConnectToPivotNode将节点与提供的节点ID连接起来
    34  //到透视节点,已由simulation.setPivotNode方法设置。
    35  //它在构建星型网络拓扑时很有用
    36  //当模拟动态添加和删除节点时。
    37  func (s *Simulation) ConnectToPivotNode(id discover.NodeID) (err error) {
    38  	pid := s.PivotNodeID()
    39  	if pid == nil {
    40  		return ErrNoPivotNode
    41  	}
    42  	return s.connect(*pid, id)
    43  }
    44  
    45  //ConnectToLastNode将节点与提供的节点ID连接起来
    46  //到上一个节点,并避免连接到自身。
    47  //它在构建链网络拓扑结构时很有用
    48  //当模拟动态添加和删除节点时。
    49  func (s *Simulation) ConnectToLastNode(id discover.NodeID) (err error) {
    50  	ids := s.UpNodeIDs()
    51  	l := len(ids)
    52  	if l < 2 {
    53  		return nil
    54  	}
    55  	lid := ids[l-1]
    56  	if lid == id {
    57  		lid = ids[l-2]
    58  	}
    59  	return s.connect(lid, id)
    60  }
    61  
    62  //connecttorandomnode将节点与provided nodeid连接起来
    63  //向上的随机节点发送。
    64  func (s *Simulation) ConnectToRandomNode(id discover.NodeID) (err error) {
    65  	n := s.RandomUpNode(id)
    66  	if n == nil {
    67  		return ErrNodeNotFound
    68  	}
    69  	return s.connect(n.ID, id)
    70  }
    71  
    72  //ConnectNodesFull将所有节点连接到另一个。
    73  //它在网络中提供了完整的连接
    74  //这应该是很少需要的。
    75  func (s *Simulation) ConnectNodesFull(ids []discover.NodeID) (err error) {
    76  	if ids == nil {
    77  		ids = s.UpNodeIDs()
    78  	}
    79  	l := len(ids)
    80  	for i := 0; i < l; i++ {
    81  		for j := i + 1; j < l; j++ {
    82  			err = s.connect(ids[i], ids[j])
    83  			if err != nil {
    84  				return err
    85  			}
    86  		}
    87  	}
    88  	return nil
    89  }
    90  
    91  //connectnodeschain连接链拓扑中的所有节点。
    92  //如果ids参数为nil,则所有打开的节点都将被连接。
    93  func (s *Simulation) ConnectNodesChain(ids []discover.NodeID) (err error) {
    94  	if ids == nil {
    95  		ids = s.UpNodeIDs()
    96  	}
    97  	l := len(ids)
    98  	for i := 0; i < l-1; i++ {
    99  		err = s.connect(ids[i], ids[i+1])
   100  		if err != nil {
   101  			return err
   102  		}
   103  	}
   104  	return nil
   105  }
   106  
   107  //ConnectNodesRing连接环拓扑中的所有节点。
   108  //如果ids参数为nil,则所有打开的节点都将被连接。
   109  func (s *Simulation) ConnectNodesRing(ids []discover.NodeID) (err error) {
   110  	if ids == nil {
   111  		ids = s.UpNodeIDs()
   112  	}
   113  	l := len(ids)
   114  	if l < 2 {
   115  		return nil
   116  	}
   117  	for i := 0; i < l-1; i++ {
   118  		err = s.connect(ids[i], ids[i+1])
   119  		if err != nil {
   120  			return err
   121  		}
   122  	}
   123  	return s.connect(ids[l-1], ids[0])
   124  }
   125  
   126  //connectnodestar连接星形拓扑中的所有节点
   127  //中心位于提供的节点ID。
   128  //如果ids参数为nil,则所有打开的节点都将被连接。
   129  func (s *Simulation) ConnectNodesStar(id discover.NodeID, ids []discover.NodeID) (err error) {
   130  	if ids == nil {
   131  		ids = s.UpNodeIDs()
   132  	}
   133  	l := len(ids)
   134  	for i := 0; i < l; i++ {
   135  		if id == ids[i] {
   136  			continue
   137  		}
   138  		err = s.connect(id, ids[i])
   139  		if err != nil {
   140  			return err
   141  		}
   142  	}
   143  	return nil
   144  }
   145  
   146  //ConnectNodessTarPivot连接星形拓扑中的所有节点
   147  //中心位于已设置的轴节点。
   148  //如果ids参数为nil,则所有打开的节点都将被连接。
   149  func (s *Simulation) ConnectNodesStarPivot(ids []discover.NodeID) (err error) {
   150  	id := s.PivotNodeID()
   151  	if id == nil {
   152  		return ErrNoPivotNode
   153  	}
   154  	return s.ConnectNodesStar(*id, ids)
   155  }
   156  
   157  //连接连接两个节点,但忽略已连接的错误。
   158  func (s *Simulation) connect(oneID, otherID discover.NodeID) error {
   159  	return ignoreAlreadyConnectedErr(s.Net.Connect(oneID, otherID))
   160  }
   161  
   162  func ignoreAlreadyConnectedErr(err error) error {
   163  	if err == nil || strings.Contains(err.Error(), "already connected") {
   164  		return nil
   165  	}
   166  	return err
   167  }