A method of P2P to connect to service peers. This method uses the Provide() functionality of the Kademlia DHT directly to announce the ability to provide the service and then disovers all peers that provide the same. The peer discovery is handled by a go-routine that will read from a channel of peer
()
| 133 | // The peer discovery is handled by a go-routine that will read from a channel |
| 134 | // of peer address information until the peer channel closes |
| 135 | func (p2p *P2P) AnnounceConnect() { |
| 136 | // Generate the Service CID |
| 137 | cidvalue := generateCID(p2p.service) |
| 138 | // Trace log |
| 139 | logrus.Debug("cidvalue ", cidvalue.String()) |
| 140 | logrus.Traceln("Generated the Service CID.") |
| 141 | |
| 142 | // Announce that this host can provide the service CID |
| 143 | err := p2p.KadDHT.Provide(p2p.Ctx, cidvalue, true) |
| 144 | if err != nil { |
| 145 | logrus.WithFields(logrus.Fields{ |
| 146 | "error": err.Error(), |
| 147 | }).Fatalln("Failed to Announce Service CID!") |
| 148 | } |
| 149 | // Debug log |
| 150 | logrus.Debugln("Announced the p2p Service.") |
| 151 | // Sleep to give time for the advertisement to propagate |
| 152 | time.Sleep(time.Second * 5) |
| 153 | |
| 154 | // Find the other providers for the service CID |
| 155 | peerchan := p2p.KadDHT.FindProvidersAsync(p2p.Ctx, cidvalue, 0) |
| 156 | // Trace log |
| 157 | logrus.Traceln("Discovered p2p Service Peers.") |
| 158 | |
| 159 | // Connect to peers as they are discovered |
| 160 | go handlePeerDiscovery(p2p.Host, peerchan) |
| 161 | // Debug log |
| 162 | logrus.Debugln("Started Peer Connection Handler.") |
| 163 | } |
| 164 | |
| 165 | // A function that generates the p2p configuration options and creates a |
| 166 | // libp2p host object for the given context. The created host is returned |
no test coverage detected