retrieve support for agurments

This commit is contained in:
Flavien Haas 2020-03-24 11:32:42 +01:00
parent d0eff567f5
commit d4bcff6f8c
2 changed files with 44 additions and 62 deletions

View File

@ -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;
}
}

View File

@ -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);
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 -- <hostname> <PORT> (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);
}
}
} while (!testLegitIP.TestIP(hostname))
System.out.println("using hostname : " + hostname);
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);
}
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();
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();
}
}
}