@@ -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