Add caching for nodes
This commit is contained in:
parent
67649bc264
commit
de57da088f
8 changed files with 102 additions and 1 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -39,4 +39,5 @@ build/
|
|||
.DS_Store
|
||||
|
||||
### Custom ###
|
||||
geoip.json
|
||||
geoip.json
|
||||
nodes.json
|
|
@ -1,5 +1,6 @@
|
|||
package com.lbry.globe;
|
||||
|
||||
import com.lbry.globe.api.API;
|
||||
import com.lbry.globe.handler.HTTPHandler;
|
||||
import com.lbry.globe.logging.LogLevel;
|
||||
import com.lbry.globe.thread.BlockchainNodeFinderThread;
|
||||
|
@ -56,6 +57,9 @@ public class Main implements Runnable{
|
|||
}
|
||||
|
||||
public static void main(String... args){
|
||||
Main.LOGGER.info("Loading nodes cache");
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(API::saveNodes));
|
||||
API.loadNodes();
|
||||
Main.LOGGER.info("Loading GeoIP cache");
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(GeoIP::saveCache));
|
||||
GeoIP.loadCache();
|
||||
|
|
|
@ -3,17 +3,24 @@ package com.lbry.globe.api;
|
|||
import com.lbry.globe.object.Node;
|
||||
import com.lbry.globe.object.Service;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.lbry.globe.util.GeoIP;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class API{
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger("API");
|
||||
public static final Map<InetAddress, Node> NODES = new TreeMap<>(Comparator.comparing(InetAddress::getHostAddress));
|
||||
|
||||
public static void fillPoints(JSONArray points){
|
||||
|
@ -35,4 +42,36 @@ public class API{
|
|||
}
|
||||
}
|
||||
|
||||
public static void loadNodes(){
|
||||
try{
|
||||
BufferedReader br = new BufferedReader(new FileReader("nodes.json"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while((line = br.readLine())!=null){
|
||||
sb.append(line);
|
||||
}
|
||||
JSONObject obj = new JSONObject(sb.toString());
|
||||
for(String key : obj.keySet()){
|
||||
API.NODES.put(InetAddress.getByName(key),Node.fromJSONObject(obj.getJSONObject(key)));
|
||||
}
|
||||
br.close();
|
||||
}catch(Exception e){
|
||||
API.LOGGER.log(Level.WARNING,"Failed loading nodes.",e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveNodes(){
|
||||
try{
|
||||
FileOutputStream fos = new FileOutputStream("nodes.json");
|
||||
JSONObject obj = new JSONObject();
|
||||
for(Map.Entry<InetAddress,Node> entry : API.NODES.entrySet()){
|
||||
obj.put(entry.getKey().getHostAddress(),entry.getValue().toJSONObject());
|
||||
}
|
||||
fos.write(obj.toString().getBytes());
|
||||
fos.close();
|
||||
}catch(Exception e){
|
||||
API.LOGGER.log(Level.WARNING,"Failed saving nodes.",e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,6 +4,9 @@ import java.net.InetAddress;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Node{
|
||||
|
||||
private final InetAddress address;
|
||||
|
@ -41,4 +44,37 @@ public class Node{
|
|||
return this.services;
|
||||
}
|
||||
|
||||
public JSONObject toJSONObject(){
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("address",this.address.toString());
|
||||
obj.put("latitude",this.latitude);
|
||||
obj.put("longitude",this.longitude);
|
||||
JSONArray servicesArr = new JSONArray();
|
||||
for(Service service : this.services){
|
||||
servicesArr.put(service.toJSONObject());
|
||||
}
|
||||
obj.put("services",servicesArr);
|
||||
return obj;
|
||||
}
|
||||
|
||||
public static Node fromJSONObject(JSONObject obj){
|
||||
System.out.println(obj);
|
||||
Node node = new Node(Node.addressFromString(obj.getString("address")),obj.has("latitude")?obj.getDouble("latitude"):null,obj.has("longitude")?obj.getDouble("longitude"):null);
|
||||
JSONArray servicesArr = obj.getJSONArray("services");
|
||||
for(int i=0;i<servicesArr.length();i++){
|
||||
node.services.add(Service.fromJSONObject(servicesArr.getJSONObject(i)));
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private static InetAddress addressFromString(String str){
|
||||
String[] parts = str.split("/",2);
|
||||
try{
|
||||
return InetAddress.getByAddress(parts[0],InetAddress.getByName(parts[1]).getAddress());
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,8 @@ package com.lbry.globe.object;
|
|||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Service{
|
||||
|
||||
private final UUID id;
|
||||
|
@ -41,4 +43,19 @@ public class Service{
|
|||
return this.lastSeen;
|
||||
}
|
||||
|
||||
public JSONObject toJSONObject(){
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("id",this.id.toString());
|
||||
obj.put("port",this.port);
|
||||
obj.put("type",this.type);
|
||||
obj.put("lastSeen",this.lastSeen);
|
||||
return obj;
|
||||
}
|
||||
|
||||
public static Service fromJSONObject(JSONObject obj){
|
||||
Service service = new Service(UUID.fromString(obj.getString("id")),obj.getInt("port"),obj.getString("type"));
|
||||
service.lastSeen = obj.getLong("lastSeen");
|
||||
return service;
|
||||
}
|
||||
|
||||
}
|
|
@ -106,6 +106,7 @@ public class BlockchainNodeFinderThread implements Runnable{
|
|||
}
|
||||
n.getServices().removeAll(removedService);
|
||||
}
|
||||
API.saveNodes();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -147,6 +147,7 @@ public class DHTNodeFinderThread implements Runnable{
|
|||
}
|
||||
}
|
||||
|
||||
API.saveNodes();
|
||||
//TODO: REMOVE MARKED AS DELETED
|
||||
|
||||
System.out.println("----");
|
||||
|
|
|
@ -122,6 +122,8 @@ public class HubNodeFinderThread implements Runnable{
|
|||
}
|
||||
}
|
||||
|
||||
API.saveNodes();
|
||||
|
||||
try {
|
||||
Thread.sleep(10_000);
|
||||
} catch (InterruptedException e) {
|
||||
|
|
Loading…
Add table
Reference in a new issue