connectSeeds connect the seeds in seedlist and call for nbr list
()
| 287 | |
| 288 | //connectSeeds connect the seeds in seedlist and call for nbr list |
| 289 | func (this *P2PServer) connectSeeds() { |
| 290 | seedNodes := make([]string, 0) |
| 291 | for _, n := range config.DefConfig.Genesis.SeedList { |
| 292 | ip, err := common.ParseIPAddr(n) |
| 293 | if err != nil { |
| 294 | log.Warnf("[p2p]seed peer %s address format is wrong", n) |
| 295 | continue |
| 296 | } |
| 297 | ns, err := net.LookupHost(ip) |
| 298 | if err != nil { |
| 299 | log.Warnf("[p2p]resolve err: %s", err.Error()) |
| 300 | continue |
| 301 | } |
| 302 | port, err := common.ParseIPPort(n) |
| 303 | if err != nil { |
| 304 | log.Warnf("[p2p]seed peer %s address format is wrong", n) |
| 305 | continue |
| 306 | } |
| 307 | seedNodes = append(seedNodes, ns[0]+port) |
| 308 | } |
| 309 | |
| 310 | connPeers := make(map[string]*peer.Peer) |
| 311 | np := this.network.GetNp() |
| 312 | np.Lock() |
| 313 | for _, tn := range np.List { |
| 314 | ipAddr, _ := tn.GetAddr16() |
| 315 | ip := net.IP(ipAddr[:]) |
| 316 | addrString := ip.To16().String() + ":" + strconv.Itoa(int(tn.GetPort())) |
| 317 | if tn.GetState() == common.ESTABLISH { |
| 318 | connPeers[addrString] = tn |
| 319 | } |
| 320 | } |
| 321 | np.Unlock() |
| 322 | |
| 323 | seedConnList := make([]*peer.Peer, 0) |
| 324 | seedDisconn := make([]string, 0) |
| 325 | isSeed := false |
| 326 | for _, nodeAddr := range seedNodes { |
| 327 | if p, ok := connPeers[nodeAddr]; ok { |
| 328 | seedConnList = append(seedConnList, p) |
| 329 | } else { |
| 330 | seedDisconn = append(seedDisconn, nodeAddr) |
| 331 | } |
| 332 | |
| 333 | if this.network.IsOwnAddress(nodeAddr) { |
| 334 | isSeed = true |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if len(seedConnList) > 0 { |
| 339 | rand.Seed(time.Now().UnixNano()) |
| 340 | index := rand.Intn(len(seedConnList)) |
| 341 | this.reqNbrList(seedConnList[index]) |
| 342 | if isSeed && len(seedDisconn) > 0 { |
| 343 | index := rand.Intn(len(seedDisconn)) |
| 344 | go this.network.Connect(seedDisconn[index]) |
| 345 | } |
| 346 | } else { //not found |
no test coverage detected