Improve pinging hubs
This commit is contained in:
parent
d4eeff7488
commit
364866d905
4 changed files with 70 additions and 41 deletions
|
@ -17,6 +17,7 @@ import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
import io.netty.handler.codec.http.HttpRequestDecoder;
|
import io.netty.handler.codec.http.HttpRequestDecoder;
|
||||||
import io.netty.handler.codec.http.HttpResponseEncoder;
|
import io.netty.handler.codec.http.HttpResponseEncoder;
|
||||||
|
import io.netty.util.concurrent.DefaultThreadFactory;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
@ -35,7 +36,7 @@ public class Main implements Runnable{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(){
|
public void run(){
|
||||||
EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
|
EventLoopGroup group = new MultiThreadIoEventLoopGroup(new DefaultThreadFactory("Netty Event Loop"),NioIoHandler.newFactory());
|
||||||
this.runTCPServerHTTP(group);
|
this.runTCPServerHTTP(group);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,17 +60,17 @@ public class Main implements Runnable{
|
||||||
|
|
||||||
public static void main(String... args){
|
public static void main(String... args){
|
||||||
Main.LOGGER.info("Loading nodes cache");
|
Main.LOGGER.info("Loading nodes cache");
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(API::saveNodes));
|
Runtime.getRuntime().addShutdownHook(new Thread(API::saveNodes,"Save Nodes"));
|
||||||
API.loadNodes();
|
API.loadNodes();
|
||||||
Main.LOGGER.info("Loading GeoIP cache");
|
Main.LOGGER.info("Loading GeoIP cache");
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(GeoIP::saveCache));
|
Runtime.getRuntime().addShutdownHook(new Thread(GeoIP::saveCache,"Save Cache"));
|
||||||
GeoIP.loadCache();
|
GeoIP.loadCache();
|
||||||
Main.LOGGER.info("Starting finder thread for blockchain nodes");
|
Main.LOGGER.info("Starting finder thread for blockchain nodes");
|
||||||
new Thread(new BlockchainNodeFinderThread()).start();
|
new Thread(new BlockchainNodeFinderThread(),"Block Node Finder").start();
|
||||||
Main.LOGGER.info("Starting finder thread for DHT nodes");
|
Main.LOGGER.info("Starting finder thread for DHT nodes");
|
||||||
new Thread(new DHTNodeFinderThread()).start();
|
new DHTNodeFinderThread().run();
|
||||||
Main.LOGGER.info("Starting finder thread for hub nodes");
|
Main.LOGGER.info("Starting finder thread for hub nodes");
|
||||||
new Thread(new HubNodeFinderThread()).start();
|
new HubNodeFinderThread().run();
|
||||||
Main.LOGGER.info("Starting server");
|
Main.LOGGER.info("Starting server");
|
||||||
new Main(args).run();
|
new Main(args).run();
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import io.netty.util.concurrent.DefaultThreadFactory;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class DHTNodeFinderThread implements Runnable{
|
public class DHTNodeFinderThread implements Runnable{
|
||||||
|
@ -45,8 +46,8 @@ public class DHTNodeFinderThread implements Runnable{
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startSender(){
|
private void startSender(){
|
||||||
Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(() -> {
|
Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("DHT Sender")).scheduleWithFixedDelay(() -> {
|
||||||
System.out.println("[BULK PING]");
|
System.out.println("[DHT] BULK PING");
|
||||||
API.saveNodes();
|
API.saveNodes();
|
||||||
for(InetSocketAddress socketAddress : DHT.getPeers().keySet()){
|
for(InetSocketAddress socketAddress : DHT.getPeers().keySet()){
|
||||||
String hostname = socketAddress.getHostName();
|
String hostname = socketAddress.getHostName();
|
||||||
|
@ -139,7 +140,7 @@ public class DHTNodeFinderThread implements Runnable{
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
},"DHT Receiver").start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleIncomingMessages(){
|
private void handleIncomingMessages(){
|
||||||
|
|
|
@ -7,12 +7,20 @@ import com.lbry.globe.util.GeoIP;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import io.netty.util.concurrent.DefaultThreadFactory;
|
||||||
|
import io.netty.util.concurrent.ThreadPerTaskExecutor;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import javax.net.SocketFactory;
|
||||||
|
|
||||||
public class HubNodeFinderThread implements Runnable{
|
public class HubNodeFinderThread implements Runnable{
|
||||||
|
|
||||||
public static final String[] HUBS = {
|
public static final String[] HUBS = {
|
||||||
|
@ -34,21 +42,47 @@ public class HubNodeFinderThread implements Runnable{
|
||||||
|
|
||||||
private static final Map<InetAddress,Long> LAST_SEEN = new TreeMap<>(Comparator.comparing(InetAddress::getHostAddress));
|
private static final Map<InetAddress,Long> LAST_SEEN = new TreeMap<>(Comparator.comparing(InetAddress::getHostAddress));
|
||||||
|
|
||||||
|
private static final Map<InetAddress,Socket> SOCKETS = new TreeMap<>(Comparator.comparing(InetAddress::getHostAddress));
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run(){
|
||||||
while(true){
|
Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("Hub Sender")).scheduleWithFixedDelay(() -> {
|
||||||
|
System.out.println("[HUB] BULK PING");
|
||||||
for(String hostname : HubNodeFinderThread.HUBS){
|
for(String hostname : HubNodeFinderThread.HUBS){
|
||||||
try{
|
try{
|
||||||
for(InetAddress ip : InetAddress.getAllByName(hostname)){
|
for(InetAddress ip : InetAddress.getAllByName(hostname)){
|
||||||
new Thread(() -> {
|
if(!HubNodeFinderThread.SOCKETS.containsKey(ip)){
|
||||||
|
HubNodeFinderThread.SOCKETS.put(ip,new Socket());
|
||||||
|
}
|
||||||
try{
|
try{
|
||||||
Socket s = new Socket(ip,50001);
|
if(!HubNodeFinderThread.SOCKETS.get(ip).isConnected() || HubNodeFinderThread.SOCKETS.get(ip).isClosed()){
|
||||||
|
if(HubNodeFinderThread.SOCKETS.get(ip).isClosed()){
|
||||||
|
HubNodeFinderThread.SOCKETS.put(ip,new Socket());
|
||||||
|
}
|
||||||
|
HubNodeFinderThread.SOCKETS.get(ip).connect(new InetSocketAddress(ip,50001),1000);
|
||||||
|
}
|
||||||
|
}catch(Exception ignored){}
|
||||||
|
|
||||||
|
Socket s = HubNodeFinderThread.SOCKETS.get(ip);
|
||||||
|
if(s==null || !s.isConnected() || s.isClosed()){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
System.out.println(" - [Hub] To: "+s.getRemoteSocketAddress());
|
||||||
|
|
||||||
JSONObject obj = new JSONObject();
|
JSONObject obj = new JSONObject();
|
||||||
obj.put("id",new Random().nextInt());
|
obj.put("id",new Random().nextInt());
|
||||||
obj.put("method","server.banner");
|
obj.put("method","server.banner");
|
||||||
obj.put("params",new JSONArray());
|
obj.put("params",new JSONArray());
|
||||||
s.getOutputStream().write((obj+"\n").getBytes());
|
s.getOutputStream().write((obj+"\n").getBytes());
|
||||||
s.getOutputStream().flush();
|
s.getOutputStream().flush();
|
||||||
|
}
|
||||||
|
for(InetAddress ip : InetAddress.getAllByName(hostname)){
|
||||||
|
Socket s = HubNodeFinderThread.SOCKETS.get(ip);
|
||||||
|
if(s==null || !s.isConnected() || s.isClosed()){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
System.out.println(" - [Hub] From: "+s.getRemoteSocketAddress());
|
||||||
|
|
||||||
InputStream in = s.getInputStream();
|
InputStream in = s.getInputStream();
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
int b;
|
int b;
|
||||||
|
@ -61,9 +95,6 @@ public class HubNodeFinderThread implements Runnable{
|
||||||
if(successful){
|
if(successful){
|
||||||
LAST_SEEN.put(ip,System.currentTimeMillis());
|
LAST_SEEN.put(ip,System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -123,13 +154,7 @@ public class HubNodeFinderThread implements Runnable{
|
||||||
}
|
}
|
||||||
|
|
||||||
API.saveNodes();
|
API.saveNodes();
|
||||||
|
},0,10,TimeUnit.SECONDS);
|
||||||
try {
|
|
||||||
Thread.sleep(10_000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.lbry.globe.util;
|
package com.lbry.globe.util;
|
||||||
|
|
||||||
|
import io.netty.util.concurrent.DefaultThreadFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
@ -12,7 +14,7 @@ public class DHT{
|
||||||
|
|
||||||
public static byte[] NODE_ID = new byte[48];
|
public static byte[] NODE_ID = new byte[48];
|
||||||
|
|
||||||
private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("Timeout Future"));
|
||||||
private static final TimeoutFutureManager<RPCID,UDP.Packet> futureManager = new TimeoutFutureManager<>(executor);
|
private static final TimeoutFutureManager<RPCID,UDP.Packet> futureManager = new TimeoutFutureManager<>(executor);
|
||||||
private static final Map<InetSocketAddress,Boolean> peers = new ConcurrentHashMap<>();
|
private static final Map<InetSocketAddress,Boolean> peers = new ConcurrentHashMap<>();
|
||||||
private static final DatagramSocket socket;
|
private static final DatagramSocket socket;
|
||||||
|
|
Loading…
Add table
Reference in a new issue