11package com .lbry .globe .thread ;
22
33import com .lbry .globe .api .API ;
4+ import com .lbry .globe .kademlia .KademliaBucket ;
5+ import com .lbry .globe .kademlia .KademliaTriple ;
46import com .lbry .globe .object .Node ;
57import com .lbry .globe .object .Service ;
68import com .lbry .globe .util .DHT ;
1820
1921public class DHTNodeFinderThread implements Runnable {
2022
23+ // Using just a single bucket is wrong, but easy for now.
24+ private static final KademliaBucket SINGLE_BUCKET = DHT .KADEMLIA .getBucket (0 );
25+
2126 public static final String [] BOOTSTRAP = {
2227 "dht.lbry.grin.io:4444" , // Grin
2328 "dht.lbry.madiator.com:4444" , // Madiator
@@ -27,14 +32,22 @@ public class DHTNodeFinderThread implements Runnable{
2732 "lbrynet3.lbry.com:4444" , // EU
2833 "lbrynet4.lbry.com:4444" , // ASIA
2934 "dht.lizard.technology:4444" , // Jack
30- "s2.lbry.network:4444" ,
35+ "s2.lbry.network:4444" , // LBRY Foundation
3136 };
3237
3338 @ Override
3439 public void run (){
3540 for (String bootstrap : DHTNodeFinderThread .BOOTSTRAP ){
3641 URI uri = URI .create ("udp://" +bootstrap );
37- DHT .getPeers ().put (new InetSocketAddress (uri .getHost (),uri .getPort ()),true );
42+ try {
43+ DHT .ping (DHT .getSocket (),new InetSocketAddress (uri .getHost (),uri .getPort ())).thenAccept ((UDP .Packet packet ) -> {
44+ byte [] receivingBytes = packet .getData ();
45+ DHT .Message <?> message = DHT .Message .fromBencode (receivingBytes );
46+ SINGLE_BUCKET .insertAtTail (new KademliaTriple (packet .getAddress ().getAddress (),packet .getAddress ().getPort (),message .getNodeID ()));
47+ }).exceptionally ((Throwable e ) -> null );
48+ }catch (Exception e ){
49+ System .out .println ("Failed bootstrap ping" );
50+ }
3851 }
3952
4053 this .startSender ();
@@ -45,12 +58,10 @@ private void startSender(){
4558 Executors .newSingleThreadScheduledExecutor (new NamedThreadFactory ("DHT Sender" )).scheduleWithFixedDelay (() -> {
4659 System .out .println ("[DHT] BULK PING" );
4760 API .saveNodes ();
48- for (InetSocketAddress socketAddress : DHT .getPeers ().keySet ()){
49- String hostname = socketAddress .getHostName ();
50- int port = socketAddress .getPort ();
61+ for (KademliaTriple triple : SINGLE_BUCKET .getList ()){
5162 try {
52- for (InetAddress ip : InetAddress .getAllByName (hostname )){
53- InetSocketAddress destination = new InetSocketAddress (ip ,port );
63+ for (InetAddress ip : InetAddress .getAllByName (triple . getIPAddress (). getHostName () )){
64+ InetSocketAddress destination = new InetSocketAddress (ip ,triple . getUDPPort () );
5465 this .doPing (destination );
5566 }
5667 }catch (Exception e ){
@@ -107,14 +118,18 @@ private void doFindNode(InetSocketAddress destination) throws IOException{
107118 for (List <Object > n : nodes ){
108119 String hostname = (String ) n .get (1 );
109120 int port = (int ) ((long ) n .get (2 ));
110- InetSocketAddress existingSocketAddr = null ;
111- for (InetSocketAddress addr : DHT . getPeers (). keySet ()){
112- if (addr . getHostName ().equals (hostname ) && addr . getPort ()==port ){
113- existingSocketAddr = addr ;
121+ KademliaTriple existingTriple = null ;
122+ for (KademliaTriple triple : SINGLE_BUCKET . getList ()){
123+ if (triple . getIPAddress (). getHostName ().equals (hostname ) && triple . getUDPPort ()==port ){
124+ existingTriple = triple ;
114125 }
115126 }
116- if (existingSocketAddr ==null ){
117- DHT .getPeers ().put (new InetSocketAddress (hostname ,port ),false );
127+ if (existingTriple ==null ){
128+ try {
129+ SINGLE_BUCKET .insertAtTail (new KademliaTriple (InetAddress .getByName (hostname ),port ,message .getNodeID ()));
130+ }catch (Exception e ){
131+ e .printStackTrace ();
132+ }
118133 }
119134 }
120135 }).exceptionally ((Throwable e ) -> null );
0 commit comments