peer = new PeerBuilder(Number160.createHash(clientId)).ports(clientPort).behindFirewall().start();
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
PeerAddress masterNodeAddress = new PeerAddress(Number160.createHash(seedNodeId), seedNodeIP, seedNodePort,
seedNodePort);
FutureDiscover futureDiscover = peer.discover().peerAddress(masterNodeAddress).start();
FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover);
futureNAT.awaitUninterruptibly();
if (futureNAT.isSuccess()) {
log.info("Automatic port forwarding is setup. Now we do a futureDiscover again. Address = " +
futureNAT.peerAddress());
futureDiscover = peer.discover().peerAddress(masterNodeAddress).start();
futureDiscover.awaitUninterruptibly();
if (futureDiscover.isSuccess()) {
log.info("Discover with automatic port forwarding was successful. Address = " + futureDiscover
.peerAddress());
//cachedPeers.put(id, peer);
return peer;
}
else {
log.warn("Discover with automatic port forwarding failed. Reason = " + futureDiscover
.failedReason());
peer.shutdown().awaitUninterruptibly();
return null;
}
}
else {
log.warn("StartSetupPortforwarding failed. Reason = " + futureNAT
.failedReason());
peer.shutdown().awaitUninterruptibly();
return null;
}
} catch (IOException e) {