use of org.apache.tuweni.discovery.DNSDaemon in project besu by hyperledger.
the class DefaultP2PNetwork method start.
@Override
public void start() {
if (!started.compareAndSet(false, true)) {
LOG.warn("Attempted to start an already started " + getClass().getSimpleName());
return;
}
final String address = config.getDiscovery().getAdvertisedHost();
final int configuredDiscoveryPort = config.getDiscovery().getBindPort();
final int configuredRlpxPort = config.getRlpx().getBindPort();
Optional.ofNullable(config.getDiscovery().getDNSDiscoveryURL()).ifPresent(disco -> {
LOG.info("Starting DNS discovery with URL {}", disco);
config.getDnsDiscoveryServerOverride().ifPresent(dnsServer -> LOG.info("Starting DNS discovery with DNS Server override {}", dnsServer));
dnsDaemon = new DNSDaemon(disco, createDaemonListener(), 0L, 60000L, config.getDnsDiscoveryServerOverride().orElse(null));
dnsDaemon.start();
});
final int listeningPort = rlpxAgent.start().join();
final int discoveryPort = peerDiscoveryAgent.start((configuredDiscoveryPort == 0 && configuredRlpxPort == 0) ? listeningPort : configuredDiscoveryPort).join();
final Consumer<? super NatManager> natAction = natManager -> {
final UpnpNatManager upnpNatManager = (UpnpNatManager) natManager;
upnpNatManager.requestPortForward(discoveryPort, NetworkProtocol.UDP, NatServiceType.DISCOVERY);
upnpNatManager.requestPortForward(listeningPort, NetworkProtocol.TCP, NatServiceType.RLPX);
};
natService.ifNatEnvironment(NatMethod.UPNP, natAction);
natService.ifNatEnvironment(NatMethod.UPNPP2PONLY, natAction);
setLocalNode(address, listeningPort, discoveryPort);
peerBondedObserverId = OptionalLong.of(peerDiscoveryAgent.observePeerBondedEvents(this::handlePeerBondedEvent));
// Periodically check maintained connections
final int checkMaintainedConnectionsSec = config.getCheckMaintainedConnectionsFrequencySec();
peerConnectionScheduler.scheduleWithFixedDelay(this::checkMaintainedConnectionPeers, 2, checkMaintainedConnectionsSec, TimeUnit.SECONDS);
// Periodically initiate outgoing connections to discovered peers
final int checkConnectionsSec = config.getInitiateConnectionsFrequencySec();
peerConnectionScheduler.scheduleWithFixedDelay(this::attemptPeerConnections, checkConnectionsSec, checkConnectionsSec, TimeUnit.SECONDS);
}
Aggregations