]> git.bitcoin.ninja Git - ldk-java/commitdiff
Small tweaks to NioPeerHandler
authorMatt Corallo <git@bluematt.me>
Tue, 2 Feb 2021 01:26:00 +0000 (20:26 -0500)
committerMatt Corallo <git@bluematt.me>
Tue, 2 Feb 2021 03:02:23 +0000 (22:02 -0500)
src/main/java/org/ldk/batteries/NioPeerHandler.java
src/test/java/org/ldk/HumanObjectPeerTest.java

index 299496229ad73980914ad71ce282fdde1e0842b5..14c5611d5daa0e6dc74c9af0cd0f1144c6fcdfea 100644 (file)
@@ -11,6 +11,10 @@ import java.nio.channels.Selector;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
 
+/**
+ * A NioPeerHandler maps LDK's PeerHandler to Java's NIO I/O interface. It spawns a single background thread which
+ * processes socket events and provides the data to LDK for decryption and processing.
+ */
 public class NioPeerHandler {
     private static class Peer {
         SocketDescriptor descriptor;
@@ -39,11 +43,13 @@ public class NioPeerHandler {
             public long send_data(byte[] data, boolean resume_read) {
                 if (resume_read) {
                     peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_READ);
+                    selector.wakeup();
                 }
                 try {
                     long written = chan.write(ByteBuffer.wrap(data));
                     if (written != data.length) {
                         peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_WRITE);
+                        selector.wakeup();
                     }
                     return written;
                 } catch (IOException e) {
@@ -88,7 +94,6 @@ public class NioPeerHandler {
      * @throws IOException If an internal java.nio error occurs.
      */
     public NioPeerHandler(PeerManager manager) throws IOException {
-long id = manager._test_only_get_ptr();
         this.peer_manager = manager;
         this.selector = Selector.open();
         io_thread = new Thread(() -> {
index 27ddc7c2a53e7f96f936651120ebc4cbc5f43737..807b33074b138b8e47adbe099615c2d5c9b7f6c6 100644 (file)
@@ -379,7 +379,7 @@ class HumanObjectPeerTestInstance {
         if (use_nio_peer_handler) {
             peer1.nio_peer_handler.check_events();
             peer2.nio_peer_handler.check_events();
-            try { Thread.sleep(500); } catch (InterruptedException e) { assert false; }
+            try { Thread.sleep(400); } catch (InterruptedException e) { assert false; }
         } else {
             synchronized (runqueue) {
                 ran = false;
@@ -610,6 +610,10 @@ class HumanObjectPeerTestInstance {
         }
 
         if (use_nio_peer_handler) {
+            state.peer1.peer_manager.disconnect_by_node_id(state.peer2.chan_manager.get_our_node_id(), false);
+            wait_events_processed(state.peer1, state.peer2);
+            assert state.peer1.peer_manager.get_peer_node_ids().length == 0;
+            assert state.peer2.peer_manager.get_peer_node_ids().length == 0;
             state.peer1.nio_peer_handler.interrupt();
             state.peer2.nio_peer_handler.interrupt();
         }