diff --git a/src/tools/TestLegitIP.java b/src/tools/TestLegitIP.java deleted file mode 100644 index aa271f3..0000000 --- a/src/tools/TestLegitIP.java +++ /dev/null @@ -1,28 +0,0 @@ -package tools; - -/** Test if IP are legitimate. - * @author Louis Royer - * @author Flavien Haas - * @author JS Auge - * @version 1.0 - */ -public class TestLegitIP { - - public boolean TestIP(String hostname) { - if(hostname.equals("localhost")){ - return true; - } - else{ - String[] ipArray = hostname.split("\\."); - if(ipArray.length != 4){ - return false; - } - for(int i= 0; i < ipArray.length; i++){ - if((Integer.parseInt(ipArray[i]) > 255) || (Integer.parseInt(ipArray[i]) < 0)){ - return false; - } - } - } - return true; - } -} diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 7923024..b81a671 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -7,7 +7,6 @@ import tools.Directories; import tools.Logger; import tools.LogLevel; import tools.TestPort; -import tools.TestLegitIP; /** Tracker implementation * First argument of main method is port listened by the tracker, and is mandatory. @@ -43,44 +42,55 @@ public class Tracker { final String defaultPort = "6969"; final String defaultHostname = "localhost"; Scanner scanner = new Scanner(System.in); - TestLegitIP testLegitIP = new TestLegitIP(); TestPort testPortTracker = new TestPort(); + String hostname; + String port; Tracker t; - do{ - System.out.println("Tracker Server, enter hostname to bind (default = localhost): "); - String hostname = scanner.nextLine(); - if(hostname.equals("")){ - hostname = defaultHostname; - System.out.println("using default hostname : " + hostname); - } - } while (!testLegitIP.TestIP(hostname)) - System.out.println("using hostname : " + hostname); + System.out.println("args.lenght : " + args.length); - System.out.println("enter port (default = 6969): "); - String port = scanner.nextLine(); - if(port.equals("")){ - port = defaultPort; - System.out.println("using default port : " + port); - } else { - System.out.println("using port : " + port); - } - 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); + if ((args.length != 3) && (args.length != 0)){ + System.out.println("usage : java tracker.Tracker or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999)"); } + else{ + if(args.length == 3){ + hostname = args[1]; + port = args[2]; + } + else{ + System.out.println("Tracker Server, enter hostname to bind (default = localhost): "); + hostname = scanner.nextLine(); + if(hostname.equals("")){ + hostname = defaultHostname; + System.out.println("using default hostname : " + hostname); + } + System.out.println("enter port (default = 6969): "); + port = scanner.nextLine(); + if(port.equals("")){ + port = defaultPort; + System.out.println("using default port : " + port); + } + } - 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(); + System.out.println("using hostname : " + hostname); + if(testPortTracker.testPortTracker(Integer.parseInt(port))){ + System.out.println("using port : " + 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); + } + + 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(); + } } }