From 387a487d8370f1b6c54fc0f173d8a177af9b50ee Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 24 Mar 2020 14:00:24 +0100 Subject: [PATCH] reintroduce usage of arguments in serverP2P --- src/serverP2P/ServerP2P.java | 115 ++++++++++++++++++++--------------- src/tools/TestPort.java | 7 +-- src/tracker/Tracker.java | 4 +- 3 files changed, 70 insertions(+), 56 deletions(-) diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 82dead1..04b3fbc 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -1,6 +1,7 @@ package serverP2P; import java.util.Scanner; +import javafx.util.Pair; import serverP2P.ServerManagementUDP; import serverP2P.ServerManagementTCP; import tools.Directories; @@ -47,63 +48,79 @@ public class ServerP2P { final String defaultHostname = "localhost"; final String defaultPortServer = "7070"; final String defaultPortTracker = "6969"; + String hostnameServer = ""; + String portServer = ""; + String hostnameTracker = ""; + String portTracker = ""; 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); + if ((args.length != 5) && (args.length != 0)){ + System.out.println("usage : java serveurP2P.ServeurP2P (interactive) or java serveurP2P.ServeurP2P -- "); } - 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); + else{ + if(args.length == 5){ + hostnameServer = args[1]; + portServer = args[2]; + hostnameTracker = args[3]; + portTracker = args[4]; + } + else{ + System.out.println("Server, enter hostname to bind (default = localhost): "); + hostnameServer = scanner.nextLine(); + if(hostnameServer.equals("")){ + hostnameServer = defaultHostname; + System.out.println("using default hostname : " + hostnameServer); + } + System.out.println("enter port (default = 7070): "); + portServer = scanner.nextLine(); + if(portServer.equals("")){ + portServer = defaultPortServer; + System.out.println("using default port : " + portServer); + } + System.out.println("enter hostname of tracker (default = localhost): "); + hostnameTracker = scanner.nextLine(); + if(hostnameTracker.equals("")){ + hostnameTracker = defaultHostname; + System.out.println("tracker default hostname : " + hostnameTracker); + } + System.out.println("enter tracker's port (default = 6969): "); + portTracker = scanner.nextLine(); + if(portTracker.equals("")){ + portTracker = defaultPortTracker; + System.out.println("using default port : " + portTracker); + } } - } - 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("using hostname : " + hostnameServer); + Pair PtestPortServer = testPort.testPortServer(Integer.parseInt(portServer)); + if(PtestPortServer.getKey() == 0){ + System.out.println("using port : " + portServer); + } + else { + System.out.println(PtestPortServer.getValue()); + portServer = defaultPortServer; + } 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 { + + Pair PtestPortTracker = testPort.testPortTracker(Integer.parseInt(portTracker)); + if(PtestPortTracker.getKey() == 0){ System.out.println("using port : " + portTracker); } - } + else { + System.out.println(PtestPortTracker.getValue()); + portTracker = defaultPortTracker; + } - 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(); - } + 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(); + } + } } diff --git a/src/tools/TestPort.java b/src/tools/TestPort.java index 8a31892..ae71813 100644 --- a/src/tools/TestPort.java +++ b/src/tools/TestPort.java @@ -10,12 +10,11 @@ import javafx.util.Pair; */ public class TestPort { - public boolean testPortServer(int port, String message) { + public Pair testPortServer(int port) { if((port > 7000) && (port < 7999)){ - return true; + return new Pair<>(0,""); } - message = message + "Wrong port (7000 -> 7999), using default port 7070"; - return false; + return new Pair<>(1, "Wrong port (7000 -> 7999), using default port 7070"); } public Pair testPortTracker(int port) { diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 53658e1..787923c 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -48,10 +48,8 @@ public class Tracker { String port; Tracker t; - System.out.println("args.lenght : " + args.length); - if ((args.length != 3) && (args.length != 0)){ - System.out.println("usage : java tracker.Tracker or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999)"); + System.out.println("usage : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999)"); } else{ if(args.length == 3){