44import com .lbry .globe .object .Node ;
55import com .lbry .globe .object .Service ;
66import com .lbry .globe .util .GeoIP ;
7+ import com .lbry .globe .util .NamedThreadFactory ;
78
89import java .io .InputStream ;
910import java .net .InetAddress ;
1011import java .net .InetSocketAddress ;
1112import java .net .Socket ;
1213import java .util .*;
13- import java .util .concurrent .CompletableFuture ;
1414import java .util .concurrent .Executors ;
1515import java .util .concurrent .TimeUnit ;
1616
17- import io .netty .util .concurrent .DefaultThreadFactory ;
18- import io .netty .util .concurrent .ThreadPerTaskExecutor ;
1917import org .json .JSONArray ;
2018import org .json .JSONObject ;
2119
22- import javax .net .SocketFactory ;
23-
2420public class HubNodeFinderThread implements Runnable {
2521
2622 public static final String [] HUBS = {
@@ -46,11 +42,17 @@ public class HubNodeFinderThread implements Runnable{
4642
4743 @ Override
4844 public void run (){
49- Executors .newSingleThreadScheduledExecutor (new DefaultThreadFactory ("Hub Sender" )).scheduleWithFixedDelay (() -> {
45+ Executors .newSingleThreadScheduledExecutor (new NamedThreadFactory ("Hub Sender" )).scheduleWithFixedDelay (() -> {
5046 System .out .println ("[HUB] BULK PING" );
5147 for (String hostname : HubNodeFinderThread .HUBS ){
48+ InetAddress [] ips = null ;
5249 try {
53- for (InetAddress ip : InetAddress .getAllByName (hostname )){
50+ ips = InetAddress .getAllByName (hostname );
51+ }catch (Exception e ){
52+ e .printStackTrace ();
53+ }
54+ if (ips !=null ){
55+ for (InetAddress ip : ips ){
5456 if (!HubNodeFinderThread .SOCKETS .containsKey (ip )){
5557 HubNodeFinderThread .SOCKETS .put (ip ,new Socket ());
5658 }
@@ -69,35 +71,41 @@ public void run(){
6971 }
7072 System .out .println (" - [Hub] To: " +s .getRemoteSocketAddress ());
7173
72- JSONObject obj = new JSONObject ();
73- obj .put ("id" ,new Random ().nextInt ());
74- obj .put ("method" ,"server.banner" );
75- obj .put ("params" ,new JSONArray ());
76- s .getOutputStream ().write ((obj +"\n " ).getBytes ());
77- s .getOutputStream ().flush ();
74+ try {
75+ JSONObject obj = new JSONObject ();
76+ obj .put ("id" ,new Random ().nextInt ());
77+ obj .put ("method" ,"server.banner" );
78+ obj .put ("params" ,new JSONArray ());
79+ s .getOutputStream ().write ((obj +"\n " ).getBytes ());
80+ s .getOutputStream ().flush ();
81+ }catch (Exception e ){
82+ e .printStackTrace ();
83+ }
7884 }
79- for (InetAddress ip : InetAddress . getAllByName ( hostname ) ){
85+ for (InetAddress ip : ips ){
8086 Socket s = HubNodeFinderThread .SOCKETS .get (ip );
8187 if (s ==null || !s .isConnected () || s .isClosed ()){
8288 continue ;
8389 }
8490 System .out .println (" - [Hub] From: " +s .getRemoteSocketAddress ());
8591
86- InputStream in = s .getInputStream ();
87- StringBuilder sb = new StringBuilder ();
88- int b ;
89- while ((b = in .read ())!='\n' ){
90- sb .append (new String (new byte []{(byte ) (b & 0xFF )}));
91- }
92- in .close ();
93- JSONObject respObj = new JSONObject (sb .toString ());
94- boolean successful = respObj .has ("result" ) && !respObj .isNull ("result" );
95- if (successful ){
96- LAST_SEEN .put (ip ,System .currentTimeMillis ());
92+ try {
93+ InputStream in = s .getInputStream ();
94+ StringBuilder sb = new StringBuilder ();
95+ int b ;
96+ while ((b = in .read ())!='\n' ){
97+ sb .append (new String (new byte []{(byte ) (b & 0xFF )}));
98+ }
99+ in .close ();
100+ JSONObject respObj = new JSONObject (sb .toString ());
101+ boolean successful = respObj .has ("result" ) && !respObj .isNull ("result" );
102+ if (successful ){
103+ LAST_SEEN .put (ip ,System .currentTimeMillis ());
104+ }
105+ }catch (Exception e ){
106+ e .printStackTrace ();
97107 }
98108 }
99- }catch (Exception e ){
100- e .printStackTrace ();
101109 }
102110 }
103111
0 commit comments