77
88import java .io .InputStream ;
99import java .net .InetAddress ;
10+ import java .net .InetSocketAddress ;
1011import java .net .Socket ;
1112import java .util .*;
13+ import java .util .concurrent .CompletableFuture ;
14+ import java .util .concurrent .Executors ;
15+ import java .util .concurrent .TimeUnit ;
1216
17+ import io .netty .util .concurrent .DefaultThreadFactory ;
18+ import io .netty .util .concurrent .ThreadPerTaskExecutor ;
1319import org .json .JSONArray ;
1420import org .json .JSONObject ;
1521
22+ import javax .net .SocketFactory ;
23+
1624public class HubNodeFinderThread implements Runnable {
1725
1826 public static final String [] HUBS = {
@@ -34,36 +42,59 @@ public class HubNodeFinderThread implements Runnable{
3442
3543 private static final Map <InetAddress ,Long > LAST_SEEN = new TreeMap <>(Comparator .comparing (InetAddress ::getHostAddress ));
3644
45+ private static final Map <InetAddress ,Socket > SOCKETS = new TreeMap <>(Comparator .comparing (InetAddress ::getHostAddress ));
46+
3747 @ Override
38- public void run () {
39- while (true ){
48+ public void run (){
49+ Executors .newSingleThreadScheduledExecutor (new DefaultThreadFactory ("Hub Sender" )).scheduleWithFixedDelay (() -> {
50+ System .out .println ("[HUB] BULK PING" );
4051 for (String hostname : HubNodeFinderThread .HUBS ){
4152 try {
4253 for (InetAddress ip : InetAddress .getAllByName (hostname )){
43- new Thread (() -> {
44- try {
45- Socket s = new Socket (ip ,50001 );
46- JSONObject obj = new JSONObject ();
47- obj .put ("id" ,new Random ().nextInt ());
48- obj .put ("method" ,"server.banner" );
49- obj .put ("params" ,new JSONArray ());
50- s .getOutputStream ().write ((obj +"\n " ).getBytes ());
51- s .getOutputStream ().flush ();
52- InputStream in = s .getInputStream ();
53- StringBuilder sb = new StringBuilder ();
54- int b ;
55- while ((b = in .read ())!='\n' ){
56- sb .append (new String (new byte []{(byte ) (b & 0xFF )}));
57- }
58- in .close ();
59- JSONObject respObj = new JSONObject (sb .toString ());
60- boolean successful = respObj .has ("result" ) && !respObj .isNull ("result" );
61- if (successful ){
62- LAST_SEEN .put (ip ,System .currentTimeMillis ());
54+ if (!HubNodeFinderThread .SOCKETS .containsKey (ip )){
55+ HubNodeFinderThread .SOCKETS .put (ip ,new Socket ());
56+ }
57+ try {
58+ if (!HubNodeFinderThread .SOCKETS .get (ip ).isConnected () || HubNodeFinderThread .SOCKETS .get (ip ).isClosed ()){
59+ if (HubNodeFinderThread .SOCKETS .get (ip ).isClosed ()){
60+ HubNodeFinderThread .SOCKETS .put (ip ,new Socket ());
6361 }
64- } catch ( Exception e ){
62+ HubNodeFinderThread . SOCKETS . get ( ip ). connect ( new InetSocketAddress ( ip , 50001 ), 1000 );
6563 }
66- }).start ();
64+ }catch (Exception ignored ){}
65+
66+ Socket s = HubNodeFinderThread .SOCKETS .get (ip );
67+ if (s ==null || !s .isConnected () || s .isClosed ()){
68+ continue ;
69+ }
70+ System .out .println (" - [Hub] To: " +s .getRemoteSocketAddress ());
71+
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 ();
78+ }
79+ for (InetAddress ip : InetAddress .getAllByName (hostname )){
80+ Socket s = HubNodeFinderThread .SOCKETS .get (ip );
81+ if (s ==null || !s .isConnected () || s .isClosed ()){
82+ continue ;
83+ }
84+ System .out .println (" - [Hub] From: " +s .getRemoteSocketAddress ());
85+
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 ());
97+ }
6798 }
6899 }catch (Exception e ){
69100 e .printStackTrace ();
@@ -123,13 +154,7 @@ public void run() {
123154 }
124155
125156 API .saveNodes ();
126-
127- try {
128- Thread .sleep (10_000 );
129- } catch (InterruptedException e ) {
130- throw new RuntimeException (e );
131- }
132- }
157+ },0 ,10 ,TimeUnit .SECONDS );
133158 }
134159
135160}
0 commit comments