diff --git a/build.gradle b/build.gradle index d2323b2..4532eb5 100644 --- a/build.gradle +++ b/build.gradle @@ -24,6 +24,7 @@ repositories { } dependencies { + implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'net.tomp2p:tomp2p-all:5.0-Beta8' implementation 'org.slf4j:slf4j-log4j12:+' } diff --git a/libs/FreePastry-2.1.jar b/libs/FreePastry-2.1.jar new file mode 100644 index 0000000..226a779 Binary files /dev/null and b/libs/FreePastry-2.1.jar differ diff --git a/libs/commons-jxpath-1.1.jar b/libs/commons-jxpath-1.1.jar new file mode 100644 index 0000000..9516e2a Binary files /dev/null and b/libs/commons-jxpath-1.1.jar differ diff --git a/libs/commons-logging.jar b/libs/commons-logging.jar new file mode 100644 index 0000000..b99c937 Binary files /dev/null and b/libs/commons-logging.jar differ diff --git a/src/main/java/io/github/chronosx88/dhtBootstrap/Main.java b/src/main/java/io/github/chronosx88/dhtBootstrap/Main.java index d313ca8..c871526 100644 --- a/src/main/java/io/github/chronosx88/dhtBootstrap/Main.java +++ b/src/main/java/io/github/chronosx88/dhtBootstrap/Main.java @@ -1,6 +1,8 @@ package io.github.chronosx88.dhtBootstrap; -import net.tomp2p.connection.*; +import net.tomp2p.connection.ChannelClientConfiguration; +import net.tomp2p.connection.ChannelServerConfiguration; +import net.tomp2p.connection.RSASignatureFactory; import net.tomp2p.dht.PeerBuilderDHT; import net.tomp2p.dht.PeerDHT; import net.tomp2p.nat.PeerBuilderNAT; @@ -9,6 +11,12 @@ import net.tomp2p.peers.Number160; import net.tomp2p.relay.RelayType; import net.tomp2p.relay.tcp.TCPRelayServerConfig; import net.tomp2p.replication.IndirectReplication; +import rice.environment.Environment; +import rice.pastry.NodeIdFactory; +import rice.pastry.PastryNode; +import rice.pastry.PastryNodeFactory; +import rice.pastry.socket.internet.InternetPastryNodeFactory; +import rice.pastry.standard.RandomNodeIdFactory; import java.io.File; import java.io.FileInputStream; @@ -20,6 +28,7 @@ import java.util.UUID; public class Main { private static PeerDHT peerDHT; + private static PastryNode pastryNode; private static Number160 peerID; private static Properties props; private static final String DATA_DIR_PATH = System.getProperty("user.home") + "/.local/share/Influence-Bootstrap/"; @@ -68,6 +77,7 @@ public class Main { .addRelayServerConfiguration(RelayType.OPENTCP, new TCPRelayServerConfig()) .start(); new IndirectReplication(peerDHT).start(); + createNewPastryBootstrapNode(); } catch (IOException e) { e.printStackTrace(); } @@ -84,4 +94,51 @@ public class Main { channelServerConfiguration.signatureFactory(new RSASignatureFactory()); return channelServerConfiguration; } + + private static void createNewPastryBootstrapNode() { + Environment env = new Environment(); + env.getParameters().setString("probe_for_external_address","true"); + env.getParameters().setString("nat_search_policy","never"); + // Generate the NodeIds Randomly + NodeIdFactory nidFactory = new RandomNodeIdFactory(env); + + // construct the PastryNodeFactory, this is how we use rice.pastry.socket + PastryNodeFactory factory = null; + try { + factory = new InternetPastryNodeFactory(nidFactory, 7244, env); + } catch (IOException e) { + e.printStackTrace(); + } + + // construct a node, but this does not cause it to boot + PastryNode node = null; + try { + node = factory.newNode(); + pastryNode = node; + } catch (IOException e) { + e.printStackTrace(); + } + + // in later tutorials, we will register applications before calling boot + node.boot(new InetSocketAddress(7244)); + + // the node may require sending several messages to fully boot into the ring + synchronized(node) { + while(!node.isReady() && !node.joinFailed()) { + // delay so we don't busy-wait + try { + node.wait(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + // abort if can't join + if (node.joinFailed()) { + System.out.println("[Pastry] Could not join the FreePastry ring. Reason:"+node.joinFailedReason()); + } + } + } + + System.out.println("[Pastry] Finished creating new bootstrap node "+node); + } }