Skip to content

Commit

Permalink
accept incoming tracker connections
Browse files Browse the repository at this point in the history
  • Loading branch information
laur89 committed Feb 3, 2023
1 parent 292549b commit 852c04f
Showing 1 changed file with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.*;

import static java.util.concurrent.TimeUnit.MINUTES;

Expand All @@ -35,9 +34,10 @@ public class ConnectionHandler {
public static final int PORT_RANGE_START = 49152;
public static final int PORT_RANGE_END = 65534;

private ServerSocketChannel channel;
private ServerSocketChannel listenChannel;
@Getter private InetAddress ipAddress;
private Thread ipFetcherThread;
private Thread serverThread;
private static final String[] IP_PROVIDERS = new String[]{
"http://whatismyip.akamai.com",
"http://ipecho.net/plain",
Expand All @@ -51,19 +51,19 @@ public class ConnectionHandler {
};

public int getPort() {
return this.channel.socket().getLocalPort();
return this.listenChannel.socket().getLocalPort();
}

public void start() throws IOException {
this.channel = this.bindToPort();
this.listenChannel = this.bindToPort();
log.info("Listening for incoming peer connections on port {}", getPort());

this.ipAddress = fetchIp();
log.info("IP reported to tracker will be: {}", this.getIpAddress().getHostAddress());

// TODO: use @Scheduled
this.ipFetcherThread = new Thread(() -> {
while (this.ipFetcherThread == null || !this.ipFetcherThread.isInterrupted()) {
while (this.ipFetcherThread != null && !this.ipFetcherThread.isInterrupted()) {
try {
MINUTES.sleep(90); // TODO: move to config
this.ipAddress = this.fetchIp();
Expand All @@ -76,6 +76,27 @@ public void start() throws IOException {
});

this.ipFetcherThread.start();

this.serverThread = new Thread(() -> {
try {
Selector selector = Selector.open();
this.listenChannel.register(selector, SelectionKey.OP_ACCEPT);

while (this.serverThread != null && !this.serverThread.isInterrupted()) {
selector.select();
for (SelectionKey key : selector.selectedKeys()) {
if (this.listenChannel != null && key.isAcceptable()) {
this.listenChannel.accept();
}
}
}
} catch (IOException ioe) {
log.debug("Problem processing incoming tracker connection", ioe);
throw new RuntimeException(ioe);
}
});

this.serverThread.start();
}

@VisibleForTesting
Expand Down Expand Up @@ -157,20 +178,25 @@ ServerSocketChannel bindToPort() throws IOException {
public void close() {
log.debug("Closing ConnectionHandler...");
try {
if (this.channel != null) {
this.channel.close();
if (this.listenChannel != null) {
this.listenChannel.close();
}
} catch (final Exception e) {
log.warn("ConnectionHandler channel has failed to release channel, but the shutdown will proceed", e);
} finally {
this.channel = null;
this.listenChannel = null;
}

try {
if (this.serverThread != null) {
this.serverThread.interrupt();
}

if (this.ipFetcherThread != null) {
this.ipFetcherThread.interrupt();
}
} finally {
this.serverThread = null;
this.ipFetcherThread = null;
}
log.debug("ConnectionHandler closed");
Expand Down

0 comments on commit 852c04f

Please sign in to comment.