Skip to content

Commit 79fc593

Browse files
committed
Improve DHT
1 parent 6fd1a3f commit 79fc593

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

src/main/java/com/lbry/globe/thread/DHTNodeFinderThread.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void run(){
3838
}
3939

4040
this.startSender();
41-
this.startReceiver();
41+
DHT.startReceiver();
4242
}
4343

4444
private void startSender(){
@@ -120,22 +120,4 @@ private void doFindNode(InetSocketAddress destination) throws IOException{
120120
}).exceptionally((Throwable e) -> null);
121121
}
122122

123-
private void startReceiver(){
124-
new Thread(() -> {
125-
while(DHT.getSocket().isBound()) {
126-
try {
127-
UDP.Packet receiverPacket = UDP.receive(DHT.getSocket());
128-
129-
byte[] receivingBytes = receiverPacket.getData();
130-
131-
DHT.Message<?> message = DHT.Message.fromBencode(receivingBytes);
132-
DHT.RPCID rpcid = new DHT.RPCID(message);
133-
DHT.getFutureManager().finishFuture(rpcid,receiverPacket);
134-
} catch (IOException e) {
135-
e.printStackTrace();
136-
}
137-
}
138-
},"DHT Receiver").start();
139-
}
140-
141123
}

src/main/java/com/lbry/globe/util/BencodeConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public static Map<String,?> decode(byte[] bytes){
3232
}
3333

3434
// Normal B-decoding
35-
return BencodeConverter.BENCODE.decode(bytes,Type.DICTIONARY);
35+
return BencodeConverter.walkAndConvertByteBufferToByteArrayOrString(BencodeConverter.BENCODE.decode(bytes,Type.DICTIONARY));
3636
}
3737

38-
public static <V> V walkAndConvertByteBufferToByteArrayOrString(Object value){
38+
private static <V> V walkAndConvertByteBufferToByteArrayOrString(Object value){
3939
if(value instanceof ByteBuffer){
4040
ByteBuffer bb = (ByteBuffer) value;
4141
byte[] ba = bb.array();

src/main/java/com/lbry/globe/util/DHT.java

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,27 @@ public static Map<InetSocketAddress,Boolean> getPeers(){
3838
}
3939

4040
public static CompletableFuture<UDP.Packet> ping(DatagramSocket socket,InetSocketAddress destination) throws IOException {
41-
byte[] rpcID = new byte[20];
42-
new Random().nextBytes(rpcID);
43-
44-
DHT.Message<String> pingMessage = new DHT.Message<>(DHT.Message.TYPE_REQUEST,rpcID,DHT.NODE_ID,"ping",Collections.singletonList(Collections.singletonMap("protocolVersion",1)));
41+
DHT.Message<String> pingMessage = new DHT.Message<>(DHT.Message.TYPE_REQUEST,DHT.RPCID.generate(),DHT.NODE_ID,"ping",Collections.singletonList(Collections.singletonMap("protocolVersion",1)));
4542

4643
return DHT.sendWithFuture(socket,destination,pingMessage);
4744
}
4845

4946
public static CompletableFuture<UDP.Packet> findNode(DatagramSocket socket,InetSocketAddress destination,byte[] key) throws IOException{
50-
byte[] rpcID = new byte[20];
51-
new Random().nextBytes(rpcID);
52-
53-
DHT.Message<String> findNodeMessage = new DHT.Message<>(DHT.Message.TYPE_REQUEST,rpcID,DHT.NODE_ID,"findNode",Arrays.asList(key,Collections.singletonMap("protocolVersion",1)));
47+
DHT.Message<String> findNodeMessage = new DHT.Message<>(DHT.Message.TYPE_REQUEST,DHT.RPCID.generate(),DHT.NODE_ID,"findNode",Arrays.asList(key,Collections.singletonMap("protocolVersion",1)));
5448

5549
return DHT.sendWithFuture(socket,destination,findNodeMessage);
5650
}
5751

5852
public static CompletableFuture<UDP.Packet> findValue(DatagramSocket socket,InetSocketAddress destination,byte[] key) throws IOException{
59-
byte[] rpcID = new byte[20];
60-
new Random().nextBytes(rpcID);
53+
DHT.Message<String> findValueMessage = new DHT.Message<>(DHT.Message.TYPE_REQUEST,DHT.RPCID.generate(),DHT.NODE_ID,"findValue",Arrays.asList(key,Collections.singletonMap("protocolVersion",1)));
54+
55+
return DHT.sendWithFuture(socket,destination,findValueMessage);
56+
}
6157

62-
DHT.Message<String> findNodeMessage = new DHT.Message<>(DHT.Message.TYPE_REQUEST,rpcID,DHT.NODE_ID,"findValue",Arrays.asList(key,Collections.singletonMap("protocolVersion",1)));
58+
public static CompletableFuture<UDP.Packet> store(DatagramSocket socket,InetSocketAddress destination) throws IOException{
59+
DHT.Message<String> storeMessage = new DHT.Message<>(DHT.Message.TYPE_REQUEST,DHT.RPCID.generate(),DHT.NODE_ID,"store");
6360

64-
return DHT.sendWithFuture(socket,destination,findNodeMessage);
61+
return DHT.sendWithFuture(socket,destination,storeMessage);
6562
}
6663

6764
protected static CompletableFuture<UDP.Packet> sendWithFuture(DatagramSocket socket,InetSocketAddress destination, DHT.Message<?> message) throws IOException{
@@ -144,12 +141,12 @@ public byte[] toBencode(){
144141
private DHT.Message<P> setFromBencode(byte[] data){
145142
Map<String,?> dictionary = BencodeConverter.decode(data);
146143
this.type = ((Long) dictionary.get("0")).intValue();
147-
this.rpcID = ((ByteBuffer) dictionary.get("1")).array();
148-
this.nodeID = ((ByteBuffer) dictionary.get("2")).array();
144+
this.rpcID = (byte[]) dictionary.get("1");
145+
this.nodeID = (byte[]) dictionary.get("2");
149146
this.payload = null;
150147
if(dictionary.containsKey("3")){
151148
Object payload = dictionary.get("3");
152-
this.payload = BencodeConverter.walkAndConvertByteBufferToByteArrayOrString(payload);
149+
this.payload = (P) payload;
153150
}
154151
this.arguments = null;
155152
if(dictionary.containsKey("4")){
@@ -175,6 +172,24 @@ public static DHT.Message<?> fromBencode(byte[] data){
175172

176173
}
177174

175+
public static void startReceiver(){
176+
new Thread(() -> {
177+
while(DHT.getSocket().isBound()) {
178+
try {
179+
UDP.Packet receiverPacket = UDP.receive(DHT.getSocket());
180+
181+
byte[] receivingBytes = receiverPacket.getData();
182+
183+
DHT.Message<?> message = DHT.Message.fromBencode(receivingBytes);
184+
DHT.RPCID rpcid = new DHT.RPCID(message);
185+
DHT.getFutureManager().finishFuture(rpcid,receiverPacket);
186+
} catch (IOException e) {
187+
e.printStackTrace();
188+
}
189+
}
190+
},"DHT Receiver").start();
191+
}
192+
178193
public static class RPCID{
179194

180195
private final byte[] id;
@@ -208,6 +223,12 @@ public String toString() {
208223
'}';
209224
}
210225

226+
public static byte[] generate(){
227+
byte[] rpcID = new byte[20];
228+
new Random().nextBytes(rpcID);
229+
return rpcID;
230+
}
231+
211232
}
212233

213234
}

0 commit comments

Comments
 (0)