Skip to content

Commit

Permalink
Implement MDNS based discovery for local networks
Browse files Browse the repository at this point in the history
Remove Perf.kt
  • Loading branch information
ianopolous committed May 2, 2024
1 parent 1da5bbf commit 12aef00
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 284 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/peergos/EmbeddedIpfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.libp2p.core.crypto.*;
import io.libp2p.core.multiformats.*;
import io.libp2p.core.multistream.*;
import io.libp2p.discovery.*;
import io.libp2p.protocol.*;
import org.peergos.blockstore.*;
import org.peergos.blockstore.metadatadb.BlockMetadataStore;
Expand All @@ -26,6 +27,7 @@
import org.peergos.protocol.ipns.*;
import org.peergos.util.Logging;

import java.net.*;
import java.nio.file.*;
import java.sql.Connection;
import java.sql.DriverManager;
Expand Down Expand Up @@ -152,6 +154,18 @@ public void start() {
LOG.info("Bootstrapping IPFS kademlia");
dht.bootstrap(node);
dht.startBootstrapThread(node);
InetAddress address = null;
MDnsDiscovery mdns = new MDnsDiscovery(node, "_ipfs-discovery._udp.local.", 60, address);
mdns.getNewPeerFoundListeners().add(peerInfo -> {
PeerId remote = PeerId.fromBase58(peerInfo.getPeerId().toBase58().substring(1)); // Not what's wrong with peerInfo, but this works
if (! remote.equals(node.getPeerId())) {
LOG.info(node.getPeerId() + " found local peer: " + peerInfo.getPeerId().toBase58() + ", addrs: " + peerInfo.getAddresses());
KademliaController ctr = dht.dial(node, remote, peerInfo.getAddresses().toArray(new Multiaddr[0])).getController().join();
ctr.closerPeers(node.getPeerId().getBytes()).join();
}
return null;
});
mdns.start();

blockProvider.ifPresent(p -> p.start());
}
Expand Down
116 changes: 0 additions & 116 deletions src/main/java/org/peergos/client/PerfTest.java

This file was deleted.

12 changes: 0 additions & 12 deletions src/main/java/org/peergos/protocol/perf/Correlation.java

This file was deleted.

156 changes: 0 additions & 156 deletions src/main/java/org/peergos/protocol/perf/Perf.kt

This file was deleted.

19 changes: 19 additions & 0 deletions src/test/java/org/peergos/EmbeddedIpfsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ public void largeBlock() throws Exception {
node2.stop();
}

@Test
public void mdnsDiscovery() throws Exception {
EmbeddedIpfs node1 = build(Collections.emptyList(), List.of(new MultiAddress("/ip4/127.0.0.1/tcp/" + TestPorts.getPort())));
node1.start();
EmbeddedIpfs node2 = build(Collections.emptyList(), List.of(new MultiAddress("/ip4/127.0.0.1/tcp/" + TestPorts.getPort())));
node2.start();

Thread.sleep(5_000);
Cid block = node2.blockstore.put(new byte[1024], Cid.Codec.Raw).join();
PeerId peerId2 = node2.node.getPeerId();
List<HashedBlock> retrieved = ForkJoinPool.commonPool().submit(
() -> node1.getBlocks(List.of(new Want(block)), Set.of(peerId2), false))
.get(5, TimeUnit.SECONDS);
Assert.assertTrue(retrieved.size() == 1);

node1.stop();
node2.stop();
}

@Test
public void publishValue() throws Exception {
EmbeddedIpfs node1 = build(BootstrapTest.BOOTSTRAP_NODES, List.of(new MultiAddress("/ip4/127.0.0.1/tcp/" + TestPorts.getPort())));
Expand Down

0 comments on commit 12aef00

Please # to comment.