-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp2p-example.java
More file actions
40 lines (34 loc) · 1.55 KB
/
p2p-example.java
File metadata and controls
40 lines (34 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import io.ipfs.api.IPFS;
import io.ipfs.api.MerkleNode;
import io.ipfs.api.NamedStreamable;
import io.ipfs.multihash.Multihash;
import net.tomp2p.dht.FutureGet;
import net.tomp2p.dht.PeerBuilderDHT;
import net.tomp2p.dht.PeerDHT;
import net.tomp2p.p2p.PeerBuilder;
import net.tomp2p.peers.Number160;
import net.tomp2p.peers.PeerAddress;
public class Main {
public static void main(String[] args) throws Exception {
// Create a libp2p host with a random peer ID and listen on a random port
PeerDHT peer = new PeerBuilderDHT(new PeerBuilder(Number160.createHash(args[0])).ports(4001).start()).start();
// Parse the peer ID of the other host from the command-line arguments
PeerAddress otherPeer = new PeerAddress(Number160.createHash(args[1]));
// Print the host's peer ID and listen address
System.out.println("Peer ID: " + peer.peerID());
System.out.println("Listen address: " + peer.peerAddress());
// Send a message to the other host
FutureGet futureGet = peer.peer().get(otherPeer).start();
futureGet.awaitUninterruptibly();
String message = (String) futureGet.data().object();
System.out.println("Message from other host: " + message);
// Send a response to the other host
peer.peer().put(otherPeer).data(new Data("Hello, world!")).start().awaitUninterruptibly();
}
}