func (p *ProtocolHandler) findPeers() {

	if p.connManager.Options.MDNS.Enabled {
		service, err := mdnsdiscovery.NewMdnsService(p.ctx, p.host.GetHost(), p.connManager.Options.MDNS.Interval, fmt.Sprintf("phore-%s-discovery._udp", p.ID))
		if err != nil {
			logrus.Warn(err)
		}

		service.RegisterNotifee(p)
	}

	for {
		findPeerCtx, cancel := context.WithTimeout(p.ctx, findPeerCycle)

		peers, err := p.discovery.FindPeers(findPeerCtx, string(p.ID), discovery.Limit(p.MaximumPeers))
		if err != nil {
			logrus.WithField("type", "error finding peers").Error(err)
			cancel()
			break
		}
		// now we want to go through each peer and try to connect to them

		// wait for either the parent to finish (meaning we should stop accepting new peers) or the cycle to end
		// meaning we should find more peers
	peerLoop:
		for {
			select {
			case pi, ok := <-peers:
				if !ok {
					time.Sleep(time.Second * 5)
					break peerLoop
				}
				p.HandlePeerFound(pi)
			case <-p.ctx.Done():
				cancel()
				return
			case <-findPeerCtx.Done():
				break peerLoop
			}
		}
	}
}
// findPeers looks for peers advertising our protocol ID and connects to them if needed.
findPeers looks for peers advertising our protocol ID and connects to them if needed.
// findPeers looks for peers advertising our protocol ID and connects to them if needed .
func (p *ProtocolHandler) findPeers() {
	if p.connManager.Options.MDNS.Enabled {
		service, err := mdnsdiscovery.NewMdnsService(p.ctx, p.host.GetHost(), p.connManager.Options.MDNS.Interval, fmt.Sprintf("phore-%s-discovery._udp", p.ID))
		if err != nil {
			logrus.Warn(err)
		}
		service.RegisterNotifee(p)
	}
	for {
		findPeerCtx, cancel := context.WithTimeout(p.ctx, findPeerCycle)
		peers, err := p.discovery.FindPeers(findPeerCtx, string(p.ID), discovery.Limit(p.MaximumPeers))
		if err != nil {
			logrus.WithField("type", "error finding peers").Error(err)
			cancel()
			break
		}
	peerLoop:
		for {
			select {
			case pi, ok := <-peers:
				if !ok {
					time.Sleep(time.Second * 5)
					break peerLoop
				}
				p.HandlePeerFound(pi)
			case <-p.ctx.Done():
				cancel()
				return
			case <-findPeerCtx.Done():
				break peerLoop
			}
		}
	}
}
func ( p * ProtocolHandler ) findPeers ( ) { if p . connManager . Options . MDNS . Enabled { service , err := mdnsdiscovery . NewMdnsService ( p . ctx , p . host . GetHost ( ) , p . connManager . Options . MDNS . Interval , fmt . Sprintf ( " " , p . ID ) ) if err != nil { logrus . Warn ( err ) } service . RegisterNotifee ( p ) } for { findPeerCtx , cancel := context . WithTimeout ( p . ctx , findPeerCycle ) peers , err := p . discovery . FindPeers ( findPeerCtx , string ( p . ID ) , discovery . Limit ( p . MaximumPeers ) ) if err != nil { logrus . WithField ( " " , " " ) . Error ( err ) cancel ( ) break } peerLoop : for { select { case pi , ok := <- peers : if ! ok { time . Sleep ( time . Second * 5 ) break peerLoop } p . HandlePeerFound ( pi ) case <- p . ctx . Done ( ) : cancel ( ) return case <- findPeerCtx . Done ( ) : break peerLoop } } } }
findPeers looks for peers advertising our protocol ID and connects to them if needed.
findPeers looks for peers advertising our protocol ID and connects to them if needed .
// now we want to go through each peer and try to connect to them // wait for either the parent to finish (meaning we should stop accepting new peers) or the cycle to end // meaning we should find more peers