Add scheduling

This commit is contained in:
Ben van Hartingsveldt 2025-07-12 19:23:40 +02:00
parent eaa2f7e296
commit d4eeff7488
No known key found for this signature in database
GPG key ID: 261AA214130CE7AB

View file

@ -11,6 +11,8 @@ import java.io.IOException;
import java.net.*; import java.net.*;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.json.JSONObject; import org.json.JSONObject;
@ -43,29 +45,22 @@ public class DHTNodeFinderThread implements Runnable{
} }
private void startSender(){ private void startSender(){
new Thread(() -> { Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(() -> {
while(true){ System.out.println("[BULK PING]");
System.out.println("[BULK PING]"); API.saveNodes();
for(InetSocketAddress socketAddress : DHT.getPeers().keySet()){ for(InetSocketAddress socketAddress : DHT.getPeers().keySet()){
String hostname = socketAddress.getHostName(); String hostname = socketAddress.getHostName();
int port = socketAddress.getPort(); int port = socketAddress.getPort();
try{ try{
for(InetAddress ip : InetAddress.getAllByName(hostname)){ for(InetAddress ip : InetAddress.getAllByName(hostname)){
InetSocketAddress destination = new InetSocketAddress(ip,port); InetSocketAddress destination = new InetSocketAddress(ip,port);
this.doPing(destination); this.doPing(destination);
}
}catch(Exception e){
e.printStackTrace();
} }
}catch(Exception e){
e.printStackTrace();
} }
try {
Thread.sleep(15_000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
API.saveNodes();
} }
}).start(); },0,15,TimeUnit.SECONDS);
} }
private void doPing(InetSocketAddress destination) throws IOException{ private void doPing(InetSocketAddress destination) throws IOException{