From 26458eafb530582b744929e6b03b0042c977f7cd Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 10 May 2016 20:31:10 +0200 Subject: Create entry points structures --- src/ch/epfl/xblast/ArgumentChecker.java | 29 +++++++++++++++++++++++++++++ src/ch/epfl/xblast/client/Client.java | 30 ++++++++++++++++++++++++++++++ src/ch/epfl/xblast/client/Main.java | 23 +++++++++++++++++++++++ src/ch/epfl/xblast/server/Main.java | 24 ++++++++++++++++++++++++ src/ch/epfl/xblast/server/Server.java | 30 ++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 src/ch/epfl/xblast/client/Client.java create mode 100644 src/ch/epfl/xblast/client/Main.java create mode 100644 src/ch/epfl/xblast/server/Main.java create mode 100644 src/ch/epfl/xblast/server/Server.java (limited to 'src/ch') diff --git a/src/ch/epfl/xblast/ArgumentChecker.java b/src/ch/epfl/xblast/ArgumentChecker.java index 7e77b61..56f8acd 100644 --- a/src/ch/epfl/xblast/ArgumentChecker.java +++ b/src/ch/epfl/xblast/ArgumentChecker.java @@ -59,4 +59,33 @@ public final class ArgumentChecker { return s; } + /** + * Returns the element from the array at the requested index, or null if it cannot be retrieved. + * + * @param array the array + * @param index the index + * @param the type of element + * @return the requested element, or null + */ + public static T getOrNull(T[] array, int index) { + if (Objects.isNull(array) || index < 0 || index >= array.length) + return null; + else + return array[index]; + } + + /** + * Parses and returns an integer, or return null on error. + * + * @param str the integer to parse + * @return the parsed integer, or null + */ + public static Integer parseIntOrNull(String str) { + try { + return Integer.parseInt(str); + } catch (NumberFormatException | NullPointerException e) { + return null; + } + } + } diff --git a/src/ch/epfl/xblast/client/Client.java b/src/ch/epfl/xblast/client/Client.java new file mode 100644 index 0000000..dc08f1b --- /dev/null +++ b/src/ch/epfl/xblast/client/Client.java @@ -0,0 +1,30 @@ +package ch.epfl.xblast.client; + +import ch.epfl.xblast.server.Server; + +import java.net.InetSocketAddress; +import java.util.Optional; + +/** + * The Client class. + * + * @author Pacien TRAN-GIRARD (261948) + */ +public class Client { + + private static final String DEFAULT_SERVER_HOST = "localhost"; + private static final int DEFAULT_SERVER_PORT = Server.DEFAULT_PORT; + + private final InetSocketAddress serverAddr; + + public Client(String host, Integer port) { + this.serverAddr = new InetSocketAddress( + Optional.ofNullable(host).orElse(DEFAULT_SERVER_HOST), + Optional.ofNullable(port).orElse(DEFAULT_SERVER_PORT)); + } + + public void run() { + // TODO + } + +} diff --git a/src/ch/epfl/xblast/client/Main.java b/src/ch/epfl/xblast/client/Main.java new file mode 100644 index 0000000..19145c5 --- /dev/null +++ b/src/ch/epfl/xblast/client/Main.java @@ -0,0 +1,23 @@ +package ch.epfl.xblast.client; + +import ch.epfl.xblast.ArgumentChecker; + +/** + * Entry point class of the client. + * + * @author Pacien TRAN-GIRARD (261948) + */ +public final class Main { + + private Main() { + // Static class + } + + public static void main(String[] args) { + String host = ArgumentChecker.getOrNull(args, 0); + Integer port = ArgumentChecker.parseIntOrNull(ArgumentChecker.getOrNull(args, 1)); + + (new Client(host, port)).run(); + } + +} diff --git a/src/ch/epfl/xblast/server/Main.java b/src/ch/epfl/xblast/server/Main.java new file mode 100644 index 0000000..98bbb3d --- /dev/null +++ b/src/ch/epfl/xblast/server/Main.java @@ -0,0 +1,24 @@ +package ch.epfl.xblast.server; + +import ch.epfl.xblast.ArgumentChecker; + +/** + * Entry point class of the server. + * + * @author Pacien TRAN-GIRARD (261948) + */ +public final class Main { + + private Main() { + // Static class + } + + public static void main(String[] args) { + Integer expectedClients = ArgumentChecker.parseIntOrNull(ArgumentChecker.getOrNull(args, 0)); + String iface = ArgumentChecker.getOrNull(args, 1); + Integer port = ArgumentChecker.parseIntOrNull(ArgumentChecker.getOrNull(args, 2)); + + (new Server(iface, port, expectedClients)).run(); + } + +} diff --git a/src/ch/epfl/xblast/server/Server.java b/src/ch/epfl/xblast/server/Server.java new file mode 100644 index 0000000..76f332e --- /dev/null +++ b/src/ch/epfl/xblast/server/Server.java @@ -0,0 +1,30 @@ +package ch.epfl.xblast.server; + +import ch.epfl.xblast.PlayerID; + +import java.net.InetSocketAddress; +import java.util.Optional; + +/** + * The Server class. + * + * @author Pacien TRAN-GIRARD (261948) + */ +public class Server { + + public static final int DEFAULT_PORT = 2016; + public static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length; + + private final InetSocketAddress iface; + private final int expectedClients; + + public Server(String iface, Integer port, Integer expectedClients) { + this.iface = new InetSocketAddress(iface, Optional.ofNullable(port).orElse(DEFAULT_PORT)); + this.expectedClients = Optional.ofNullable(expectedClients).orElse(DEFAULT_EXPECTED_CLIENTS); + } + + public void run() { + // TODO + } + +} -- cgit v1.2.3