Ensure listening sockets are closed to work around OSX breaking
[ldk-java] / src / main / java / org / ldk / batteries / NioPeerHandler.java
index eac4d8058a898fd7a8af0a61ddb8e2be68627f92..b722d96d8c1408568659466d87433821bf29bd18 100644 (file)
@@ -3,6 +3,7 @@ package org.ldk.batteries;
 import org.ldk.structs.*;
 
 import java.io.IOException;
+import java.util.LinkedList;
 import java.net.SocketAddress;
 import java.net.StandardSocketOptions;
 import java.nio.Buffer;
@@ -98,7 +99,6 @@ public class NioPeerHandler {
 
     /**
      * Constructs a new peer handler, spawning a thread to monitor for socket events.
-     * The background thread will call the PeerManager's timer_tick_occured() function for you on an appropriate schedule.
      *
      * @param manager The LDK PeerManager which connection data will be provided to.
      * @throws IOException If an internal java.nio error occurs.
@@ -108,7 +108,6 @@ public class NioPeerHandler {
         this.selector = Selector.open();
         io_thread = new Thread(() -> {
             ByteBuffer buf = ByteBuffer.allocate(8192);
-            long lastTimerTick = System.currentTimeMillis();
             while (true) {
                 try {
                     if (IS_ANDROID) {
@@ -192,10 +191,6 @@ public class NioPeerHandler {
                         // no need to do anything here with the PeerManager.
                     }
                 }
-                if (lastTimerTick < System.currentTimeMillis() - 30 * 1000) {
-                    peer_manager.timer_tick_occurred();
-                    lastTimerTick = System.currentTimeMillis();
-                }
                 peer_manager.process_events();
             }
         }, "NioPeerHandler NIO Thread");
@@ -236,6 +231,20 @@ public class NioPeerHandler {
         }
     }
 
+    /**
+     * Disconnects any connections currently open with the peer with the given node id.
+     *
+     * @param their_node_id must be a valid 33-byte public key
+     */
+    public void disconnect(byte[] their_node_id) {
+        this.peer_manager.disconnect_by_node_id(their_node_id, false);
+    }
+
+    /**
+     * Before shutdown, we have to ensure all of our listening sockets are closed manually, as they appear
+     * to otherwise remain open and lying around on OSX (though no other platform).
+     */
+    private LinkedList<ServerSocketChannel> listening_sockets = new LinkedList();
     /**
      * Binds a listening socket to the given address, accepting incoming connections and handling them on the background
      * thread.
@@ -245,9 +254,13 @@ public class NioPeerHandler {
      */
     public void bind_listener(SocketAddress socket_address) throws IOException {
         ServerSocketChannel listen_channel = ServerSocketChannel.open();
+        listen_channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
         listen_channel.bind(socket_address);
         listen_channel.configureBlocking(false);
         do_selector_action(() -> listen_channel.register(this.selector, SelectionKey.OP_ACCEPT));
+        synchronized(listening_sockets) {
+            listening_sockets.add(listen_channel);
+        }
     }
 
     /**
@@ -260,6 +273,14 @@ public class NioPeerHandler {
         try {
             io_thread.join();
         } catch (InterruptedException ignored) { }
+        synchronized(listening_sockets) {
+            try {
+                selector.close();
+                for (ServerSocketChannel chan : listening_sockets) {
+                    chan.close();
+                }
+            } catch (IOException ignored) {}
+        }
     }
 
     /**