From d5104b612c847dac9447c57fb6fc3f37c5b1df87 Mon Sep 17 00:00:00 2001 From: Leonard <Leonard@Leonard.Leo> Date: Sun, 14 Jun 2020 19:02:54 +0300 Subject: [PATCH] server done --- .../Communication/Client.java | 18 ++-- .../Server/ClientHandler.java | 28 ++++++ .../galaxytruckerreloaded/Server/Server.java | 95 +++++++++++++++++-- 3 files changed, 122 insertions(+), 19 deletions(-) create mode 100644 core/src/com/galaxytrucker/galaxytruckerreloaded/Server/ClientHandler.java diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Communication/Client.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Communication/Client.java index 11a370e4..b5a7bba5 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Communication/Client.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Communication/Client.java @@ -1,11 +1,15 @@ package com.galaxytrucker.galaxytruckerreloaded.Communication; +import lombok.NonNull; + import java.net.Socket; public class Client { + /** Client socket for network communication */ private Socket socket; + /** ClientControllerCommunicator for logic handling */ private ClientControllerCommunicator clientControllerCommunicator; /** @@ -25,20 +29,14 @@ public class Client { /** Constructor * Loop for receiving Data * */ - public Client(){ + public Client(@NonNull String ipAddress, @NonNull int port) throws IllegalArgumentException{ clientControllerCommunicator = new ClientControllerCommunicator(); - // Connect to server try { - // Send login request - // receive true/false - // send ship request - //clientControllerCommunicator.setClientShip(); + this.socket = new Socket(ipAddress,port); } catch (Exception e){ - - } - while (true){ - sendAndReceive(); + e.printStackTrace(); + throw new IllegalArgumentException("Couldn't initialize connection to server"); } } } diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/ClientHandler.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/ClientHandler.java new file mode 100644 index 00000000..c85118a7 --- /dev/null +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/ClientHandler.java @@ -0,0 +1,28 @@ +package com.galaxytrucker.galaxytruckerreloaded.Server; + +import java.net.Socket; + +public class ClientHandler implements Runnable { + + /** Client socket */ + private Socket clientSocket; + + /** The server */ + public Server server; + + /** ServerServiceCommunicator */ + private ServerServiceCommunicator serverServiceCommunicator; + + /** Constructor + * @param clientSocket - the client's socket + * @param server - the server */ + public ClientHandler(Socket clientSocket,Server server){ + this.clientSocket = clientSocket; + this.server = server; + } + + /** Run */ + public void run(){ + + } +} diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Server.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Server.java index 3bcc4ce1..d31ea9b7 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Server.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Server.java @@ -1,28 +1,105 @@ package com.galaxytrucker.galaxytruckerreloaded.Server; +import com.galaxytrucker.galaxytruckerreloaded.Communication.Client; +import lombok.NonNull; + +import java.net.ServerSocket; import java.net.Socket; /** * This class creates the game server and handles storing the data */ -public class Server { +public class Server implements Runnable{ + + /** Server socket for network communication */ + private ServerSocket serverSocket; + + /** Server thread */ + private Thread serverThread; - /** Server service communicator */ + /** ServerServiceCommunicator */ private ServerServiceCommunicator serverServiceCommunicator; + /** Server port */ + private int port; + + /** Is the server running? */ + private boolean running = true; + /** Main method */ - public static void main(String[] args){} + public static void main(String[] args){ + Server server = new Server(); + server.setPort(5050); + server.serverServiceCommunicator = new ServerServiceCommunicator(); + new Thread(server).start(); + try { + Thread.sleep(1000); + } + catch (Exception f){ + System.out.println(f); + } + Client client = new Client("localhost",5050); + } - /** Client handler - * @param socket - the client's socket */ - private void clientHandler(Socket socket){ + /** Start server on specified port + * @param port - the port to bind */ + private void bindPort(int port) { + try { + this.serverSocket = new ServerSocket(port); + } + catch (Exception e){ + e.printStackTrace(); + } + } + + /** Is the server running? */ + public synchronized boolean isRunning(){ + return this.running; + } + /** Kill server */ + public synchronized void killServer(){ + try { + this.serverSocket.close(); + this.running = false; + } + catch (Exception e){ + e.printStackTrace(); + } } - /** Receive some data from the client and return a response - * @param socket - the client socket */ - private void receiveAndSendData(Socket socket){ + /** Get the serverServiceCommunicator */ + public synchronized ServerServiceCommunicator getServerServiceCommunicator(){ + return this.serverServiceCommunicator; } + /** Run the server */ + public void run(){ + synchronized (this){ + this.serverThread = Thread.currentThread(); + } + bindPort(this.port); + System.out.println("Server initialized on " + serverSocket.getInetAddress().getHostAddress() + ":" + this.port + ", listening for connections..."); + while (isRunning()){ + Socket clientSocket = null; + try { + clientSocket = serverSocket.accept(); + System.out.println("Accepted new connection from "+ clientSocket.getInetAddress().getHostAddress()); + } + catch (Exception e){ + e.printStackTrace(); + } + Server server = this; + new Thread( + new ClientHandler(clientSocket,server) + ).start(); + } + } + + /** Set the server port + * @param port - the server port */ + public void setPort(int port){ + this.port = port; + } } -- GitLab