Skip to content

Commit

Permalink
Auto port
Browse files Browse the repository at this point in the history
  • Loading branch information
alyokaz committed Aug 31, 2023
1 parent 6230955 commit b3b6ade
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
29 changes: 16 additions & 13 deletions src/main/java/com/alyokaz/aktorrent/AKTorrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
public class AKTorrent {

static final int BUFFER_SIZE = 1000000;
private final int PORT;
private int port;
private final ExecutorService executor = Executors.newCachedThreadPool();
private Server server;
private PingServer udpServer;
private final PeerService peerService = new PeerService();
private final FileService fileService = new FileService(peerService);

public AKTorrent(int port) {
this.PORT = port;
this.port = port;
}

public AKTorrent() {
this.port = 0;
}

public static void main(String[] args) throws IOException {
Expand Down Expand Up @@ -58,25 +62,24 @@ public Optional<File> getFile(String filename) {
return Optional.of(file);
}

public void startServer() {
public int startServer() {
if (this.server == null) {
try {
this.server = new Server(new ServerSocket(PORT), peerService, fileService);
ServerSocket serverSocket = new ServerSocket(port);
this.server = new Server(serverSocket, peerService, fileService);
this.peerService.discoverPeers();
this.fileService.updateAvailableFiles();
server.start();
if (this.udpServer == null) {
this.udpServer = new PingServer(new DatagramSocket(serverSocket.getLocalPort()));
this.udpServer.start();
}
this.port = serverSocket.getLocalPort();
} catch (IOException e) {
throw new RuntimeException(e + ", Port = " + PORT);
}
}
if (this.udpServer == null) {
try {
this.udpServer = new PingServer(new DatagramSocket(PORT));
this.udpServer.start();
} catch (SocketException e) {
throw new RuntimeException(e);
throw new RuntimeException(e + ", Port = " + port);
}
}
return this.port;
}

public void shutDown() {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/alyokaz/aktorrent/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ public void shutdown() {
throw new RuntimeException(e);
}
}

public int getPort() {
return this.serverSocket.getLocalPort();
}
}
15 changes: 10 additions & 5 deletions src/test/java/intergration/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,24 @@ public void pingPeerWhenAdded() {

@Test
public void pingByMultipleNodes() throws InterruptedException {
Thread.sleep(2000);
AKTorrent server = new AKTorrent(NODE_A_PORT);
server.startServer();
AKTorrent server = new AKTorrent();
final int serverPort = server.startServer();

Set<AKTorrent> nodes = new HashSet<>();
IntStream.range(0, 1000).forEach(i -> nodes.add(new AKTorrent(NODE_B_PORT + i)));
nodes.forEach(node -> node.addPeer(LOCAL_HOST, NODE_A_PORT));
nodes.forEach(node -> node.addPeer(LOCAL_HOST, serverPort));

InetSocketAddress expectedAddress = new InetSocketAddress(LOCAL_HOST, NODE_A_PORT);
InetSocketAddress expectedAddress = new InetSocketAddress(LOCAL_HOST, serverPort);
nodes.forEach(node -> assertTrue(node.getConnectedPeers().contains(expectedAddress)));
server.shutDown();
}

@Test
public void serverCanSelectsOwnPort() {
AKTorrent server = new AKTorrent(0);
int port = server.startServer();
assertTrue(port > 0);
}
private File getFile(String filename) {
return new File(getClass().getResource("/" + filename).getFile());
}
Expand Down

0 comments on commit b3b6ade

Please sign in to comment.