From 226520e209aad5c5347e0fa5def4c06f19a5582a Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 23 Mar 2020 17:15:03 +0100 Subject: [PATCH] simplified cli usage of server --- src/serverP2P/ServerP2P.java | 78 +++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 7d607b9..82dead1 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -1,10 +1,13 @@ package serverP2P; + +import java.util.Scanner; import serverP2P.ServerManagementUDP; import serverP2P.ServerManagementTCP; import tools.Directories; import tools.Logger; import tools.HostItem; -import java.util.Scanner; +import tools.TestPort; + /** Server only implementation * First argument of main method is port listened by the server, and is mandatory. @@ -37,23 +40,70 @@ public class ServerP2P { /** Main program entry point * first parameter is port number and is mandatory - * to test, run with: java -ea serverP2P.ServerP2P -- + * to test, run with: java -ea serverP2P.ServerP2P * @param args parameters */ public static void main(String [] args) { - if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){ - System.out.println("usage : java serveurP2P.ServeurP2P or java serveurP2P.ServeurP2P -- (default for server localhost:7070 and tracker localhost:6969, range 7000 -> 7999)"); + final String defaultHostname = "localhost"; + final String defaultPortServer = "7070"; + final String defaultPortTracker = "6969"; + Scanner scanner = new Scanner(System.in); + TestPort testPort = new TestPort(); + + System.out.println("Server, enter hostname to bind (default = localhost): "); + String hostnameServer = scanner.nextLine(); + if(hostnameServer.equals("")){ + hostnameServer = defaultHostname; + System.out.println("using default hostname : " + hostnameServer); + } else{ + System.out.println("using hostname : " + hostnameServer); + } + System.out.println("enter port (default = 7070): "); + String portServer = scanner.nextLine(); + if(portServer.equals("")){ + portServer = defaultPortServer; + System.out.println("using default port : " + portServer); } - else{ - ServerP2P s = new ServerP2P(args[1], args[2], args[3]); - ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, "localhost", s.port, s.logger, s.tracker); - ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, "localhost", s.port, s.logger, s.tracker); - Thread tudp = new Thread(smudp); - tudp.setName("server UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(smtcp); - ttcp.setName("server TCP P2P-JAVA-PROJECT"); - ttcp.start(); + else { + if(!testPort.testPortServer(Integer.parseInt(portServer))){ + System.out.println("Wrong port (7000 -> 7999), using default port 7070"); + portServer = defaultPortServer; + } else { + System.out.println("using port : " + portServer); + } } + + System.out.println("enter hostname of tracker (default = localhost): "); + String hostnameTracker = scanner.nextLine(); + if(hostnameTracker.equals("")){ + hostnameTracker = defaultHostname; + System.out.println("tracker default hostname : " + hostnameTracker); + } else{ + System.out.println("tracker hostname : " + hostnameTracker); + } + System.out.println("enter tracker's port (default = 6969): "); + String portTracker = scanner.nextLine(); + if(portTracker.equals("")){ + portTracker = defaultPortTracker; + System.out.println("using default port : " + portTracker); + } + else { + if(!testPort.testPortTracker(Integer.parseInt(portTracker))){ + System.out.println("Wrong port (6000 -> 6999), using default port 6969"); + portTracker = defaultPortTracker; + } else { + System.out.println("using port : " + portTracker); + } + } + + ServerP2P s = new ServerP2P(portServer, hostnameTracker, portTracker); + ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.port, s.logger, s.tracker); + ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.port, s.logger, s.tracker); + Thread tudp = new Thread(smudp); + tudp.setName("server UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(smtcp); + ttcp.setName("server TCP P2P-JAVA-PROJECT"); + ttcp.start(); } }