github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/network/simulation/connect.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:47</date> 10 //</624342673305636864> 11 12 13 package simulation 14 15 import ( 16 "strings" 17 18 "github.com/ethereum/go-ethereum/p2p/discover" 19 ) 20 21 //ConnectToPivotNode将节点与提供的节点ID连接起来 22 //到透视节点,已由simulation.setPivotNode方法设置。 23 //它在构建星型网络拓扑时很有用 24 //当模拟动态添加和删除节点时。 25 func (s *Simulation) ConnectToPivotNode(id discover.NodeID) (err error) { 26 pid := s.PivotNodeID() 27 if pid == nil { 28 return ErrNoPivotNode 29 } 30 return s.connect(*pid, id) 31 } 32 33 //ConnectToLastNode将节点与提供的节点ID连接起来 34 //到上一个节点,并避免连接到自身。 35 //它在构建链网络拓扑结构时很有用 36 //当模拟动态添加和删除节点时。 37 func (s *Simulation) ConnectToLastNode(id discover.NodeID) (err error) { 38 ids := s.UpNodeIDs() 39 l := len(ids) 40 if l < 2 { 41 return nil 42 } 43 lid := ids[l-1] 44 if lid == id { 45 lid = ids[l-2] 46 } 47 return s.connect(lid, id) 48 } 49 50 //connecttorandomnode将节点与provided nodeid连接起来 51 //向上的随机节点发送。 52 func (s *Simulation) ConnectToRandomNode(id discover.NodeID) (err error) { 53 n := s.RandomUpNode(id) 54 if n == nil { 55 return ErrNodeNotFound 56 } 57 return s.connect(n.ID, id) 58 } 59 60 //ConnectNodesFull将所有节点连接到另一个。 61 //它在网络中提供了完整的连接 62 //这应该是很少需要的。 63 func (s *Simulation) ConnectNodesFull(ids []discover.NodeID) (err error) { 64 if ids == nil { 65 ids = s.UpNodeIDs() 66 } 67 l := len(ids) 68 for i := 0; i < l; i++ { 69 for j := i + 1; j < l; j++ { 70 err = s.connect(ids[i], ids[j]) 71 if err != nil { 72 return err 73 } 74 } 75 } 76 return nil 77 } 78 79 //connectnodeschain连接链拓扑中的所有节点。 80 //如果ids参数为nil,则所有打开的节点都将被连接。 81 func (s *Simulation) ConnectNodesChain(ids []discover.NodeID) (err error) { 82 if ids == nil { 83 ids = s.UpNodeIDs() 84 } 85 l := len(ids) 86 for i := 0; i < l-1; i++ { 87 err = s.connect(ids[i], ids[i+1]) 88 if err != nil { 89 return err 90 } 91 } 92 return nil 93 } 94 95 //ConnectNodesRing连接环拓扑中的所有节点。 96 //如果ids参数为nil,则所有打开的节点都将被连接。 97 func (s *Simulation) ConnectNodesRing(ids []discover.NodeID) (err error) { 98 if ids == nil { 99 ids = s.UpNodeIDs() 100 } 101 l := len(ids) 102 if l < 2 { 103 return nil 104 } 105 for i := 0; i < l-1; i++ { 106 err = s.connect(ids[i], ids[i+1]) 107 if err != nil { 108 return err 109 } 110 } 111 return s.connect(ids[l-1], ids[0]) 112 } 113 114 //connectnodestar连接星形拓扑中的所有节点 115 //中心位于提供的节点ID。 116 //如果ids参数为nil,则所有打开的节点都将被连接。 117 func (s *Simulation) ConnectNodesStar(id discover.NodeID, ids []discover.NodeID) (err error) { 118 if ids == nil { 119 ids = s.UpNodeIDs() 120 } 121 l := len(ids) 122 for i := 0; i < l; i++ { 123 if id == ids[i] { 124 continue 125 } 126 err = s.connect(id, ids[i]) 127 if err != nil { 128 return err 129 } 130 } 131 return nil 132 } 133 134 //ConnectNodessTarPivot连接星形拓扑中的所有节点 135 //中心位于已设置的轴节点。 136 //如果ids参数为nil,则所有打开的节点都将被连接。 137 func (s *Simulation) ConnectNodesStarPivot(ids []discover.NodeID) (err error) { 138 id := s.PivotNodeID() 139 if id == nil { 140 return ErrNoPivotNode 141 } 142 return s.ConnectNodesStar(*id, ids) 143 } 144 145 //连接连接两个节点,但忽略已连接的错误。 146 func (s *Simulation) connect(oneID, otherID discover.NodeID) error { 147 return ignoreAlreadyConnectedErr(s.Net.Connect(oneID, otherID)) 148 } 149 150 func ignoreAlreadyConnectedErr(err error) error { 151 if err == nil || strings.Contains(err.Error(), "already connected") { 152 return nil 153 } 154 return err 155 } 156