diff --git a/src/main/java/org/fungover/thunder/Client.java b/src/main/java/org/fungover/thunder/Client.java index 80049bc..4e3b7f4 100644 --- a/src/main/java/org/fungover/thunder/Client.java +++ b/src/main/java/org/fungover/thunder/Client.java @@ -1,7 +1,12 @@ package org.fungover.thunder; +import java.io.*; +import java.net.Socket; import java.util.HashSet; import java.util.Set; +import java.util.logging.Level; + +import static org.fungover.thunder.Main.logger; public class Client { private final String clientId; @@ -30,4 +35,25 @@ public void subscribeToTopic(String topic) { public Set getSubscribedTopics() { return new HashSet<>(subscribedTopics); } + + public void connectToServer(String serverAddress, int serverPort) throws IOException { + try (Socket socket = new Socket(serverAddress, serverPort); + BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) { + + out.println("Hello from " + clientId); + + String response = in.readLine(); + + if ("Connection successful".equals(response)) { + this.connected = true; + } + else { + this.connected = false; + } + } catch (IOException e) { + logger.log(Level.SEVERE, "Error during connection", e); + throw e; + } + } } diff --git a/src/main/java/org/fungover/thunder/ClientHandler.java b/src/main/java/org/fungover/thunder/ClientHandler.java index 596145a..fb0d575 100644 --- a/src/main/java/org/fungover/thunder/ClientHandler.java +++ b/src/main/java/org/fungover/thunder/ClientHandler.java @@ -53,5 +53,4 @@ public void removeDisconnectedClients() { public List getClients() { return clients; } - } diff --git a/src/main/java/org/fungover/thunder/Main.java b/src/main/java/org/fungover/thunder/Main.java index b558a37..3a8fc58 100644 --- a/src/main/java/org/fungover/thunder/Main.java +++ b/src/main/java/org/fungover/thunder/Main.java @@ -6,7 +6,7 @@ public class Main { - private static final Logger logger = Logger.getLogger(Main.class.getName()); + static final Logger logger = Logger.getLogger(Main.class.getName()); public static void main(String[] args) { Server server; @@ -17,5 +17,4 @@ public static void main(String[] args) { logger.log(Level.SEVERE, "Server not able to start", e); } } - } diff --git a/src/main/java/org/fungover/thunder/Server.java b/src/main/java/org/fungover/thunder/Server.java index 7c19e0f..34c4faf 100644 --- a/src/main/java/org/fungover/thunder/Server.java +++ b/src/main/java/org/fungover/thunder/Server.java @@ -3,6 +3,10 @@ import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.util.logging.Level; + +import static org.fungover.thunder.Main.logger; + public class Server { private final ServerSocket serverSocket; @@ -26,4 +30,11 @@ public void start() throws IOException { e.printStackTrace(); } } + public void stop(){ + try { + serverSocket.close(); + } catch (IOException e) { + logger.log(Level.SEVERE, e.getMessage(), e); + } + } } diff --git a/src/test/java/org/fungover/thunder/ClientHandlerTest.java b/src/test/java/org/fungover/thunder/ClientHandlerTest.java index 8d561ba..fa56746 100644 --- a/src/test/java/org/fungover/thunder/ClientHandlerTest.java +++ b/src/test/java/org/fungover/thunder/ClientHandlerTest.java @@ -5,13 +5,19 @@ import org.junit.jupiter.api.Test; import java.io.*; +import java.lang.reflect.Field; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; +import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.ExecutorService; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/src/test/java/org/fungover/thunder/ServerTest.java b/src/test/java/org/fungover/thunder/ServerTest.java new file mode 100644 index 0000000..7cee432 --- /dev/null +++ b/src/test/java/org/fungover/thunder/ServerTest.java @@ -0,0 +1,71 @@ +package org.fungover.thunder; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.fungover.thunder.Main.logger; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; + +class ServerTest { + private static Server server; + private static CountDownLatch serverReadyLatch; + + @BeforeAll + static void setUp() { + try { + server = new Server(); + serverReadyLatch = new CountDownLatch(1); + + ExecutorService executorService = Executors.newFixedThreadPool(5); + executorService.submit(() -> { + try { + server.start(); + serverReadyLatch.countDown(); + } catch (IOException e) { + logger.log(Level.SEVERE, "Error starting server", e); + } + }); + serverReadyLatch.await(2, TimeUnit.SECONDS); + } catch (IOException | InterruptedException e) { + logger.log(Level.SEVERE, "Error in setup", e); + } + } + @AfterAll + static void tearDown() { + if (server != null) { + server.stop(); + } + } + @Test + void testMultipleConnections() { + int numClients = 5; + ExecutorService executorService = Executors.newFixedThreadPool(numClients); + + for (int i = 0; i < numClients; i++) { + final int clientId = i; + executorService.submit(() -> { + try { + Client client = new Client("Client" + clientId); + client.connectToServer("localhost", 1883); + assertTrue(client.isConnected()); + } catch (IOException e) { + logger.log(Level.SEVERE, "Client connection failed", e); + } + }); + } + executorService.shutdown(); + try { + executorService.awaitTermination(2, TimeUnit.SECONDS); + } catch (InterruptedException e) { + logger.log(Level.SEVERE, "Error waiting for termination", e); + } + } +}