From f95f4245a58214dbbf4f30f701d3c962577bdf1d Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 23 Mar 2020 16:36:35 +0100 Subject: [PATCH] simplified cli usage of tracker --- src/tracker/Tracker.java | 63 ++++++++++++++++----------- src/tracker/TrackerManagementTCP.java | 4 +- src/tracker/TrackerManagementUDP.java | 4 +- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 2ad06d4..60ce316 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -19,7 +19,6 @@ public class Tracker { private int port; private Directories directories; private Logger logger; - private static final int defaultPort = 6969; /** Constructor with portStr containing a port number. * @param portStr String containing port number of listening. @@ -36,34 +35,48 @@ public class Tracker { /** 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 tracker.Tracker or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999)"); + final String defaultPort = "6969"; + final String defaultHostname = "localhost"; + Scanner scanner = new Scanner(System.in); + Tracker t; + + System.out.println("Tracker Server, enter hostname to bind : "); + String hostname = scanner.nextLine(); + if(hostname.equals("")){ + hostname = defaultHostname; + System.out.println("using default hostname : " + hostname); + } else{ + System.out.println("using hostname : " + hostname); + } + System.out.println("enter port : "); + String port = scanner.nextLine(); + if(port.equals("")){ + port = defaultPort; + System.out.println("using default port : " + port); + } else { + System.out.println("using port : " + hostname); + } + TestPort testPortTracker = new TestPort(); + if(testPortTracker.testPortTracker(Integer.parseInt(port))){ + t = new Tracker(port); + } + else { + System.out.println("Wrong port (6000 -> 6999), using default port 6969"); + t = new Tracker(defaultPort); + t.logger.write("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); } - else{ - Tracker t; - TestPort testPortTracker = new TestPort(); - if(testPortTracker.testPortTracker(Integer.parseInt(args[1]))){ - t = new Tracker(args[1]); - } - else { - System.out.println("Wrong port (6000 -> 6999), using default port 6969"); - t = new Tracker(String.valueOf(defaultPort)); - t.logger.write("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); - } - TrackerManagementUDP tmudp = new TrackerManagementUDP(t.port, t.logger); - TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.port, t.logger); - Thread tudp = new Thread(tmudp); - tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(tmtcp); - ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); - ttcp.start(); + TrackerManagementUDP tmudp = new TrackerManagementUDP(hostname, t.port, t.logger); + TrackerManagementTCP tmtcp = new TrackerManagementTCP(hostname, t.port, t.logger); + Thread tudp = new Thread(tmudp); + tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(tmtcp); + ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); + ttcp.start(); } - } - } diff --git a/src/tracker/TrackerManagementTCP.java b/src/tracker/TrackerManagementTCP.java index 064d594..afba7bd 100644 --- a/src/tracker/TrackerManagementTCP.java +++ b/src/tracker/TrackerManagementTCP.java @@ -41,8 +41,8 @@ public class TrackerManagementTCP extends TrackerManagement { * @param port Port used to listen. * @param logger Logger object */ - public TrackerManagementTCP(int port, Logger logger) { - super(new HostItem("localhost", port), logger); + public TrackerManagementTCP(String hostname, int port, Logger logger) { + super(new HostItem(hostname, port), logger); try { socket = new ServerSocket(tracker.getPort(), 10, tracker.getInetAddress()); } catch (SocketException e) { diff --git a/src/tracker/TrackerManagementUDP.java b/src/tracker/TrackerManagementUDP.java index 6ed2235..6a07bcb 100644 --- a/src/tracker/TrackerManagementUDP.java +++ b/src/tracker/TrackerManagementUDP.java @@ -37,8 +37,8 @@ public class TrackerManagementUDP extends TrackerManagement { * @param port Port used to listen. * @param logger Logger object */ - public TrackerManagementUDP(int port, Logger logger) { - super(new HostItem("localhost", port), logger); + public TrackerManagementUDP(String hostname, int port, Logger logger) { + super(new HostItem(hostname, port), logger); try { socket = new DatagramSocket(tracker.getPort(), tracker.getInetAddress()); } catch (SocketException e) {