simplified cli usage of tracker

This commit is contained in:
Flavien Haas 2020-03-23 16:36:35 +01:00 committed by Louis
parent 60e0e3a7da
commit f95f4245a5
3 changed files with 42 additions and 29 deletions

View File

@ -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 -- <portNumber>
* 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 -- <PORT> (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();
}
}
}

View File

@ -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) {

View File

@ -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) {