Skip to content

Commit

Permalink
Merge pull request #7 from alyokaz/remove-hardcoded-ports
Browse files Browse the repository at this point in the history
Enable servers to self select open port on startup
  • Loading branch information
alyokaz committed Sep 1, 2023
2 parents 3be2972 + 5dc7c6d commit 36ef8c8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 103 deletions.
32 changes: 17 additions & 15 deletions src/main/java/com/alyokaz/aktorrent/AKTorrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.*;


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 All @@ -38,9 +41,9 @@ public void startClient() {
executor.execute(new DownloadHandler(address, fileService))));
}

public void seedFile(File file) {
public int seedFile(File file) {
fileService.addFile(file);
startServer();
return startServer();
}

public void downloadFile(FileInfo fileInfo) {
Expand All @@ -58,25 +61,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.port = serverSocket.getLocalPort();
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(port));
this.udpServer.start();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (this.udpServer == null) {
try {
this.udpServer = new PingServer(new DatagramSocket(PORT));
this.udpServer.start();
} catch (SocketException e) {
throw new RuntimeException(e);
}
}
return this.port;
}

public void shutDown() {
Expand Down
51 changes: 28 additions & 23 deletions src/test/java/intergration/CLIIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,25 @@
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.util.Optional;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class CLIIntegrationTests {

private static final String LOCAL_HOST = "127.0.0.1";
private static final int NODE_A_PORT = 4441;
private static final int NODE_B_PORT = 4442;

private static final int NODE_C_PORT = 4443;

private static final int NODE_D_PORT = 4444;

private static final int BUFFER_SIZE = 1000000;

private static final String FILENAME = "test_file.mp4";

private static final String FILENAME_2 = "test_file_2.mp4";

@Test
public void testCliSendReceiveFile() throws IOException, InterruptedException {
CountDownLatch countDownLatch_A = new CountDownLatch(1);
CountDownLatch countDownLatch_B = new CountDownLatch(1);
BlockingQueue<Integer> port = new LinkedBlockingQueue<>();

new Thread(()-> {
InputStream in = new InputStream() {
Expand All @@ -58,7 +52,9 @@ public int read() {
};

PrintStream out = new PrintStream(new ByteArrayOutputStream(1024));
CLI cli = new CLI(in, out, new AKTorrent(NODE_A_PORT));
AKTorrent node = new AKTorrent();
port.add(node.startServer());
CLI cli = new CLI(in, out, node);
try {
cli.start();
} catch (IOException e) {
Expand All @@ -68,8 +64,8 @@ public int read() {

countDownLatch_B.await();

AKTorrent client = new AKTorrent(NODE_B_PORT);
client.addPeer(LOCAL_HOST, NODE_A_PORT);
AKTorrent client = new AKTorrent();
client.addPeer(LOCAL_HOST, port.take());
File file = getFile(FILENAME);
client.downloadFile(FileService.getFileInfo(file));

Expand All @@ -86,13 +82,18 @@ public int read() {
public void canDownloadFileByNumber() throws InterruptedException {
CountDownLatch exit = new CountDownLatch(1);

AKTorrent server = new AKTorrent(NODE_A_PORT);
AKTorrent server = new AKTorrent();
File file = getFile(FILENAME);
server.seedFile(file);
BlockingQueue<Integer> port = new LinkedBlockingQueue<>();
port.add(server.seedFile(file));

Thread clientThread = new Thread(() -> {
AKTorrent client = new AKTorrent(NODE_B_PORT);
client.addPeer(LOCAL_HOST, NODE_A_PORT);
AKTorrent client = new AKTorrent();
try {
client.addPeer(LOCAL_HOST, port.take());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
CLI cli = new CLI(
new ByteArrayInputStream(("2\n1").getBytes()),
new PrintStream(new ByteArrayOutputStream()),
Expand Down Expand Up @@ -126,23 +127,27 @@ public void canDownloadFileByNumber() throws InterruptedException {
@Test
public void canAddPeer() throws InterruptedException {
CountDownLatch exitLatch = new CountDownLatch(1);
BlockingQueue<Integer> port = new LinkedBlockingQueue<>();

AKTorrent server = new AKTorrent(NODE_A_PORT);
server.startServer();
AKTorrent server = new AKTorrent();
port.add(server.startServer());

AKTorrent client = new AKTorrent(NODE_B_PORT);
AKTorrent client = new AKTorrent();
new Thread(() -> {
CLI cli = new CLI(new ByteArrayInputStream(("3\n" + LOCAL_HOST + " " + NODE_A_PORT).getBytes()),
try {
final int serverPort = port.take();
CLI cli = new CLI(new ByteArrayInputStream(("3\n" + LOCAL_HOST + " " + serverPort).getBytes()),
new PrintStream(new ByteArrayOutputStream()),
client);
try {

cli.start();
assertTrue(client.getConnectedPeers().contains(new InetSocketAddress(LOCAL_HOST, NODE_A_PORT)));
assertTrue(client.getConnectedPeers().contains(new InetSocketAddress(LOCAL_HOST, serverPort)));
exitLatch.countDown();
} catch (IOException e) {
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}).start();

exitLatch.await();
server.shutDown();
}
Expand Down
Loading

0 comments on commit 36ef8c8

Please sign in to comment.