|
3 | 3 | import com.lbry.globe.api.API; |
4 | 4 | import com.lbry.globe.object.Node; |
5 | 5 | import com.lbry.globe.object.Service; |
| 6 | +import com.lbry.globe.util.Environment; |
6 | 7 | import com.lbry.globe.util.GeoIP; |
7 | 8 |
|
8 | 9 | import java.io.BufferedReader; |
|
19 | 20 | public class BlockchainNodeFinderThread implements Runnable{ |
20 | 21 |
|
21 | 22 | @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(); |
33 | 47 | } |
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); |
39 | 52 | } |
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); |
49 | 53 | } |
50 | 54 | } |
51 | 55 | } |
|
0 commit comments