Skip to content

Commit 71b5b64

Browse files
committed
Improve fixing thrown errors
1 parent d8f2920 commit 71b5b64

File tree

4 files changed

+86
-53
lines changed

4 files changed

+86
-53
lines changed

src/main/java/com/lbry/globe/api/API.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.lbry.globe.object.Service;
55

66
import java.io.BufferedReader;
7+
import java.io.File;
78
import java.io.FileOutputStream;
89
import java.io.FileReader;
910
import java.net.Inet6Address;
@@ -42,20 +43,23 @@ public static void fillPoints(JSONArray points){
4243
}
4344

4445
public static void loadNodes(){
45-
try{
46-
BufferedReader br = new BufferedReader(new FileReader("nodes.json"));
47-
StringBuilder sb = new StringBuilder();
48-
String line;
49-
while((line = br.readLine())!=null){
50-
sb.append(line);
51-
}
52-
JSONObject obj = new JSONObject(sb.toString());
53-
for(String key : obj.keySet()){
54-
API.NODES.put(InetAddress.getByName(key),Node.fromJSONObject(obj.getJSONObject(key)));
46+
File file = new File("nodes.json");
47+
if(file.exists()){
48+
try{
49+
BufferedReader br = new BufferedReader(new FileReader(file));
50+
StringBuilder sb = new StringBuilder();
51+
String line;
52+
while((line = br.readLine())!=null){
53+
sb.append(line);
54+
}
55+
JSONObject obj = new JSONObject(sb.toString());
56+
for(String key : obj.keySet()){
57+
API.NODES.put(InetAddress.getByName(key),Node.fromJSONObject(obj.getJSONObject(key)));
58+
}
59+
br.close();
60+
}catch(Exception e){
61+
API.LOGGER.log(Level.WARNING,"Failed loading nodes.",e);
5562
}
56-
br.close();
57-
}catch(Exception e){
58-
API.LOGGER.log(Level.WARNING,"Failed loading nodes.",e);
5963
}
6064
}
6165

src/main/java/com/lbry/globe/thread/BlockchainNodeFinderThread.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.lbry.globe.api.API;
44
import com.lbry.globe.object.Node;
55
import com.lbry.globe.object.Service;
6+
import com.lbry.globe.util.Environment;
67
import com.lbry.globe.util.GeoIP;
78

89
import java.io.BufferedReader;
@@ -19,33 +20,36 @@
1920
public class BlockchainNodeFinderThread implements Runnable{
2021

2122
@Override
22-
public void run() {
23-
while(true){
24-
try{
25-
HttpURLConnection conn = (HttpURLConnection) new URI(System.getenv("BLOCKCHAIN_RPC_URL")).toURL().openConnection();
26-
conn.setDoOutput(true);
27-
conn.addRequestProperty("Authorization","Basic "+ Base64.getEncoder().encodeToString((System.getenv("BLOCKCHAIN_USERNAME")+":"+System.getenv("BLOCKCHAIN_PASSWORD")).getBytes()));
28-
conn.connect();
29-
conn.getOutputStream().write(new JSONObject().put("id",new Random().nextInt()).put("method","getnodeaddresses").put("params",new JSONArray().put(2147483647)).toString().getBytes());
30-
InputStream in = conn.getInputStream();
31-
if(in==null){
32-
in = conn.getErrorStream();
23+
public void run(){
24+
String rpcURL = Environment.getVariable("BLOCKCHAIN_RPC_URL");
25+
if(rpcURL!=null){
26+
while(true){
27+
try{
28+
HttpURLConnection conn = (HttpURLConnection) new URI(rpcURL).toURL().openConnection();
29+
conn.setDoOutput(true);
30+
conn.addRequestProperty("Authorization","Basic "+ Base64.getEncoder().encodeToString((Environment.getVariable("BLOCKCHAIN_USERNAME")+":"+Environment.getVariable("BLOCKCHAIN_PASSWORD")).getBytes()));
31+
conn.connect();
32+
conn.getOutputStream().write(new JSONObject().put("id",new Random().nextInt()).put("method","getnodeaddresses").put("params",new JSONArray().put(2147483647)).toString().getBytes());
33+
InputStream in = conn.getInputStream();
34+
if(in==null){
35+
in = conn.getErrorStream();
36+
}
37+
BufferedReader br = new BufferedReader(new InputStreamReader(in));
38+
StringBuilder sb = new StringBuilder();
39+
String line;
40+
while((line = br.readLine())!=null){
41+
sb.append(line);
42+
}
43+
JSONObject json = new JSONObject(sb.toString());
44+
manageBlockchainNodes(json.getJSONArray("result"));
45+
}catch(Exception e){
46+
e.printStackTrace();
3347
}
34-
BufferedReader br = new BufferedReader(new InputStreamReader(in));
35-
StringBuilder sb = new StringBuilder();
36-
String line;
37-
while((line = br.readLine())!=null){
38-
sb.append(line);
48+
try {
49+
Thread.sleep(10_000);
50+
} catch (InterruptedException e) {
51+
throw new RuntimeException(e);
3952
}
40-
JSONObject json = new JSONObject(sb.toString());
41-
manageBlockchainNodes(json.getJSONArray("result"));
42-
}catch(Exception e){
43-
e.printStackTrace();
44-
}
45-
try {
46-
Thread.sleep(10_000);
47-
} catch (InterruptedException e) {
48-
throw new RuntimeException(e);
4953
}
5054
}
5155
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.lbry.globe.util;
2+
3+
public class Environment{
4+
5+
public static String getVariable(String key){
6+
return Environment.getVariable(key,null);
7+
}
8+
9+
public static String getVariable(String key,String defaultValue){
10+
String value = System.getenv(key);
11+
if(value==null){
12+
return defaultValue;
13+
}
14+
return value;
15+
}
16+
17+
}

src/main/java/com/lbry/globe/util/GeoIP.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class GeoIP{
1414

1515
private static final Map<InetAddress,JSONObject> CACHE = new TreeMap<>(Comparator.comparing(InetAddress::getHostAddress));
1616
private static final Logger LOGGER = Logger.getLogger("GeoIP");
17-
private static final String TOKEN = System.getenv("IPINFO_TOKEN");
17+
private static final String TOKEN = Environment.getVariable("IPINFO_TOKEN");
1818

1919
public static JSONObject getCachedGeoIPInformation(InetAddress ip){
2020
JSONObject result = CACHE.get(ip);
@@ -33,7 +33,12 @@ public static JSONObject getCachedGeoIPInformation(InetAddress ip){
3333
public static JSONObject getGeoIPInformation(InetAddress ip) throws IOException,URISyntaxException{
3434
HttpURLConnection conn = (HttpURLConnection) new URI("https://ipinfo.io/"+ip.getHostAddress()+"?token="+GeoIP.TOKEN).toURL().openConnection();
3535
conn.connect();
36-
InputStream in = conn.getInputStream();
36+
InputStream in = null;
37+
try{
38+
in = conn.getInputStream();
39+
}catch(Exception e){
40+
GeoIP.LOGGER.log(Level.WARNING,"GeoIP service returned status code '"+conn.getResponseCode()+"'.");
41+
}
3742
if(in==null){
3843
in = conn.getErrorStream();
3944
}
@@ -56,20 +61,23 @@ public static Double[] getCoordinateFromLocation(String location){
5661
}
5762

5863
public static void loadCache(){
59-
try{
60-
BufferedReader br = new BufferedReader(new FileReader("geoip.json"));
61-
StringBuilder sb = new StringBuilder();
62-
String line;
63-
while((line = br.readLine())!=null){
64-
sb.append(line);
65-
}
66-
JSONObject obj = new JSONObject(sb.toString());
67-
for(String key : obj.keySet()){
68-
GeoIP.CACHE.put(InetAddress.getByName(key),obj.getJSONObject(key));
64+
File file = new File("geoip.json");
65+
if(file.exists()){
66+
try{
67+
BufferedReader br = new BufferedReader(new FileReader(file));
68+
StringBuilder sb = new StringBuilder();
69+
String line;
70+
while((line = br.readLine())!=null){
71+
sb.append(line);
72+
}
73+
JSONObject obj = new JSONObject(sb.toString());
74+
for(String key : obj.keySet()){
75+
GeoIP.CACHE.put(InetAddress.getByName(key),obj.getJSONObject(key));
76+
}
77+
br.close();
78+
}catch(Exception e){
79+
GeoIP.LOGGER.log(Level.WARNING,"Failed loading GeoIP cache.",e);
6980
}
70-
br.close();
71-
}catch(Exception e){
72-
GeoIP.LOGGER.log(Level.WARNING,"Failed loading GeoIP cache.",e);
7381
}
7482
}
7583

0 commit comments

Comments
 (0)