From e4f637f31707de4fec08c23805b3d29fd8ad267b Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 30 Mar 2020 15:34:50 +0200 Subject: [PATCH 1/3] programs quit when ports entered are wrong --- src/clientP2P/ClientP2P.java | 113 +++++++++++++++++------------------ src/serverP2P/ServerP2P.java | 48 +++++++-------- src/tracker/Tracker.java | 18 +++--- 3 files changed, 84 insertions(+), 95 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index ec63ef3..f7a236b 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -132,68 +132,63 @@ public class ClientP2P { } } - System.out.println("using hostname : " + hostnameServer); - if(serverPortRange.isPortInRange(portServer)){ - System.out.println("using port : " + portServer); - } - else { - System.out.println("Port not in range. " + serverPortRange); - portServer = serverPortRange.getDefaultPort(); - } - System.out.println("tracker hostname : " + hostnameTracker); - - if(trackerPortRange.isPortInRange(portTracker)){ - System.out.println("using port : " + portTracker); - } - else { - System.out.println("Port not in range. " + trackerPortRange); - portTracker = trackerPortRange.getDefaultPort(); - } + if (serverPortRange.isPortInRange(portServer)){ + if(trackerPortRange.isPortInRange(portTracker)){ + System.out.println("using hostname : " + hostnameServer); + System.out.println("using port : " + portServer); + System.out.println("tracker hostname : " + hostnameTracker); + System.out.println("tracker port : " + portTracker); + ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker); + ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); + ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); + 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(); - ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker); + // Wait a bit before printing client interface + // This is not required, but allow to have a cleaner interface + try { + Thread.sleep(200); + } catch(InterruptedException e) { + Thread.currentThread().interrupt(); + } - ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); - ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); - 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(); - - // Wait a bit before printing client interface - // This is not required, but allow to have a cleaner interface - try { - Thread.sleep(200); - } catch(InterruptedException e) { - Thread.currentThread().interrupt(); + Thread tclient; + switch(protocolClient){ + case "UDP": + case "udp": + case "upd": // to avoid users typos + case "2" : + System.out.println("Starting with UDP"); + ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); + tclient = new Thread(cmudp); + break; + case "TCP": + case "tcp": + case "1": + default: + System.out.println("Starting with TCP"); + ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); + tclient = new Thread(cmtcp); + break; + } + tclient.setName("client P2P-JAVA-PROJECT"); + tclient.start(); + try { + tclient.join(); + } catch (InterruptedException e) {} + smudp.setStop(); + smtcp.setStop(); + } + else{ + System.out.println("\n TRACKER: Port not in range. " + trackerPortRange); + } } - - Thread tclient; - switch(protocolClient){ - case "UDP": - case "udp": - case "upd": // to avoid users typos - case "2" : - System.out.println("Starting with UDP"); - ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); - tclient = new Thread(cmudp); - break; - case "TCP": - case "tcp": - case "1": - default: - System.out.println("Starting with TCP"); - ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); - tclient = new Thread(cmtcp); - break; + else{ + System.out.println("\n SERVER: Port not in range. " + serverPortRange); } - tclient.setName("client P2P-JAVA-PROJECT"); - tclient.start(); - try { - tclient.join(); - } catch (InterruptedException e) {} - smudp.setStop(); - smtcp.setStop(); } } diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 7c5a3d3..a1c02ee 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -90,39 +90,35 @@ public class ServerP2P { System.out.println("enter tracker's port (default = " + trackerPortRange.getDefaultPort() + "): "); String portTrackerStr = scanner.nextLine(); if(portTrackerStr.equals("")){ - portTracker = serverPortRange.getDefaultPort(); + portTracker = trackerPortRange.getDefaultPort(); System.out.println("using default port : " + portTracker); } else { portTracker = Integer.valueOf(Integer.parseInt(portTrackerStr)); } } - System.out.println("using hostname : " + hostnameServer); - if(serverPortRange.isPortInRange(portServer)) { - System.out.println("using port : " + portServer); - } - else { - System.out.println("Port not in range. " + serverPortRange); - portServer = serverPortRange.getDefaultPort(); - } - System.out.println("tracker hostname : " + hostnameTracker); - - if(trackerPortRange.isPortInRange(portTracker)) { - System.out.println("using port : " + portTracker); + if (serverPortRange.isPortInRange(portServer)){ + if(trackerPortRange.isPortInRange(portTracker)){ + System.out.println("using hostname : " + hostnameServer); + System.out.println("using port : " + portServer); + System.out.println("tracker hostname : " + hostnameTracker); + System.out.println("tracker port : " + portTracker); + ServerP2P s = new ServerP2P(hostnameServer, portServer, hostnameTracker, portTracker); + ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); + ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); + 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(); + } + else{ + System.out.println("\n TRACKER: Port not in range. " + trackerPortRange); + } } - else { - System.out.println("Port not in range. " + trackerPortRange); - portTracker = trackerPortRange.getDefaultPort(); + else{ + System.out.println("\n SERVER: Port not in range. " + serverPortRange); } - - ServerP2P s = new ServerP2P(hostnameServer, portServer, hostnameTracker, portTracker); - ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); - ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); - 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/tracker/Tracker.java b/src/tracker/Tracker.java index 92ff5f5..8b096a6 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -75,19 +75,17 @@ public class Tracker { if(trackerPortRange.isPortInRange(port)) { System.out.println("using port : " + port); t = new Tracker(hostname, port); + TrackerManagementUDP tmudp = new TrackerManagementUDP(t.tracker, t.logger); + TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.tracker, 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(); } else { System.out.println("Port not in range. " + trackerPortRange); - t = new Tracker(hostname, trackerPortRange.getDefaultPort()); } - - TrackerManagementUDP tmudp = new TrackerManagementUDP(t.tracker, t.logger); - TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.tracker, 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(); } } -- 2.30.2 From 8d5ef8a52bdeb0834839273cef3c103c7002bed3 Mon Sep 17 00:00:00 2001 From: Flavien Haas Date: Mon, 30 Mar 2020 18:35:10 +0200 Subject: [PATCH 2/3] resolveissue94 (#98) Fix #94. - Server doesnt ask to open his home directory when arguments are passed. - Tracker doesnt ask to open his home directory when arguments are passed. Co-authored-by: Louis Royer Co-authored-by: flavien --- src/serverP2P/ServerP2P.java | 11 ++++++++--- src/tracker/Tracker.java | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index a1c02ee..7835f62 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -29,8 +29,9 @@ public class ServerP2P { * @param portServer binded port * @param hostnameTracker tracker hostname * @param portTracker tracker port + * @param isInteractive true if application in interactive mode */ - public ServerP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker) { + public ServerP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker, boolean isInteractive) { Scanner scanner = new Scanner(System.in); server = new HostItem(hostnameServer, portServer); tracker = new HostItem(hostnameTracker, portTracker); @@ -38,7 +39,9 @@ public class ServerP2P { directories.createSubdir(subdir); logger = new Logger(directories.getDataHomeDirectory() + "server.log"); System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory() + subdir); - directories.askOpenDataHomeDirectory(subdir, scanner); + if(isInteractive){ + directories.askOpenDataHomeDirectory(subdir, scanner); + } scanner.close(); } @@ -56,6 +59,7 @@ public class ServerP2P { String hostnameTracker = ""; int portTracker = 0; Scanner scanner = new Scanner(System.in); + boolean isInteractive = false; if ((args.length != 5) && (args.length != 0)){ System.out.println("usage : java serveurP2P.ServeurP2P (interactive) or java serveurP2P.ServeurP2P -- (" + serverPortRange + " and " + trackerPortRange +")"); @@ -67,6 +71,7 @@ public class ServerP2P { hostnameTracker = args[3]; portTracker = Integer.valueOf(Integer.parseInt(args[4])); } else { + isInteractive = true; System.out.println("Server, enter hostname to bind (default = localhost): "); hostnameServer = scanner.nextLine(); if(hostnameServer.equals("")){ @@ -103,7 +108,7 @@ public class ServerP2P { System.out.println("using port : " + portServer); System.out.println("tracker hostname : " + hostnameTracker); System.out.println("tracker port : " + portTracker); - ServerP2P s = new ServerP2P(hostnameServer, portServer, hostnameTracker, portTracker); + ServerP2P s = new ServerP2P(hostnameServer, portServer, hostnameTracker, portTracker, isInteractive); ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); Thread tudp = new Thread(smudp); diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 8b096a6..9dec113 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -24,14 +24,17 @@ public class Tracker { /** Constructor with portStr containing a port number. * @param hostname hostname to bind * @param port port to bind + * @param isInteractive true if application is in interactive mode */ - public Tracker(String hostname, int port) { + public Tracker(String hostname, int port, boolean isInteractive) { tracker = new HostItem(hostname, port); directories = new Directories("P2P_JAVA_PROJECT_TRACKER_" + port); logger = new Logger(directories.getDataHomeDirectory() + "tracker.log"); System.out.println("Tracker will listen on port " + port + " and write logs into " + directories.getDataHomeDirectory()); Scanner scanner = new Scanner(System.in); - directories.askOpenDataHomeDirectory(null, scanner); + if(isInteractive){ + directories.askOpenDataHomeDirectory(null, scanner); + } scanner.close(); } @@ -47,6 +50,7 @@ public class Tracker { String hostname = ""; int port = 0; Tracker t; + boolean isInteractive = false; if ((args.length != 3) && (args.length != 0)){ System.out.println("usage : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- (" + trackerPortRange +")"); @@ -55,6 +59,7 @@ public class Tracker { hostname = args[1]; port = Integer.valueOf(Integer.parseInt(args[2])); } else { + isInteractive = true; System.out.println("Tracker Server, enter hostname to bind (default = localhost): "); hostname = scanner.nextLine(); if(hostname.equals("")){ @@ -74,7 +79,7 @@ public class Tracker { System.out.println("using hostname : " + hostname); if(trackerPortRange.isPortInRange(port)) { System.out.println("using port : " + port); - t = new Tracker(hostname, port); + t = new Tracker(hostname, port, isInteractive); TrackerManagementUDP tmudp = new TrackerManagementUDP(t.tracker, t.logger); TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.tracker, t.logger); Thread tudp = new Thread(tmudp); -- 2.30.2 From e56aad552679583cb3aa5aa4d65ccee25ed513de Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Mon, 30 Mar 2020 18:53:41 +0200 Subject: [PATCH 3/3] Fix style --- src/clientP2P/ClientP2P.java | 123 ++++++++++++++++++----------------- src/serverP2P/ServerP2P.java | 73 +++++++++++---------- src/tracker/Tracker.java | 21 +++--- 3 files changed, 113 insertions(+), 104 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index f7a236b..3ce9879 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -89,28 +89,28 @@ public class ClientP2P { final ServerPortRange serverPortRange = new ServerPortRange(); final TrackerPortRange trackerPortRange = new TrackerPortRange(); - if ((args.length != 6) && (args.length != 0)){ + if ((args.length != 6) && (args.length != 0)) { ClientP2P.printUsage(serverPortRange, trackerPortRange); System.exit(1); } - else if(args.length == 6){ + else if (args.length == 6) { protocolClient = args[1]; hostnameServer = args[2]; portServer = Integer.valueOf(Integer.parseInt(args[3])); hostnameTracker = args[4]; portTracker = Integer.valueOf(Integer.parseInt(args[5])); - } else{ + } else { System.out.println("Client, wich transport protocol do you want to use (default = TCP): "); protocolClient = scanner.nextLine(); System.out.println("server side, enter hostname to bind (default = localhost): "); hostnameServer = scanner.nextLine(); - if(hostnameServer.equals("")){ + if (hostnameServer.equals("")) { hostnameServer = defaultHostname; System.out.println("using default hostname : " + hostnameServer); } System.out.println("enter port (default = " + serverPortRange.getDefaultPort() +"): "); String portServerStr = scanner.nextLine(); - if(portServerStr.equals("")){ + if (portServerStr.equals("")) { portServer = serverPortRange.getDefaultPort(); System.out.println("using default port : " + portServer); } else { @@ -118,13 +118,13 @@ public class ClientP2P { } System.out.println("enter hostname of tracker (default = localhost): "); hostnameTracker = scanner.nextLine(); - if(hostnameTracker.equals("")){ + if (hostnameTracker.equals("")) { hostnameTracker = defaultHostname; System.out.println("tracker default hostname : " + hostnameTracker); } System.out.println("enter tracker's port (default = "+trackerPortRange.getDefaultPort() + "): "); String portTrackerStr = scanner.nextLine(); - if(portTrackerStr.equals("")){ + if (portTrackerStr.equals("")) { portTracker = trackerPortRange.getDefaultPort(); System.out.println("using default port : " + portTracker); } else { @@ -132,63 +132,66 @@ public class ClientP2P { } } - if (serverPortRange.isPortInRange(portServer)){ - if(trackerPortRange.isPortInRange(portTracker)){ - System.out.println("using hostname : " + hostnameServer); - System.out.println("using port : " + portServer); - System.out.println("tracker hostname : " + hostnameTracker); - System.out.println("tracker port : " + portTracker); - ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker); - ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); - ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); - 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(); + boolean serverInRange = serverPortRange.isPortInRange(portServer); + boolean trackerInRange = trackerPortRange.isPortInRange(portTracker); - // Wait a bit before printing client interface - // This is not required, but allow to have a cleaner interface - try { - Thread.sleep(200); - } catch(InterruptedException e) { - Thread.currentThread().interrupt(); - } + if (serverInRange && trackerInRange) { + System.out.println("using hostname : " + hostnameServer); + System.out.println("using port : " + portServer); + System.out.println("tracker hostname : " + hostnameTracker); + System.out.println("tracker port : " + portTracker); + ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker); + ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); + ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); + 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(); - Thread tclient; - switch(protocolClient){ - case "UDP": - case "udp": - case "upd": // to avoid users typos - case "2" : - System.out.println("Starting with UDP"); - ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); - tclient = new Thread(cmudp); - break; - case "TCP": - case "tcp": - case "1": - default: - System.out.println("Starting with TCP"); - ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); - tclient = new Thread(cmtcp); - break; - } - tclient.setName("client P2P-JAVA-PROJECT"); - tclient.start(); - try { - tclient.join(); - } catch (InterruptedException e) {} - smudp.setStop(); - smtcp.setStop(); + // Wait a bit before printing client interface + // This is not required, but allow to have a cleaner interface + try { + Thread.sleep(200); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); } - else{ - System.out.println("\n TRACKER: Port not in range. " + trackerPortRange); + + Thread tclient; + switch (protocolClient) { + case "UDP": + case "udp": + case "upd": // to avoid users typos + case "2" : + System.out.println("Starting with UDP"); + ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); + tclient = new Thread(cmudp); + break; + case "TCP": + case "tcp": + case "1": + default: + System.out.println("Starting with TCP"); + ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); + tclient = new Thread(cmtcp); + break; } - } - else{ - System.out.println("\n SERVER: Port not in range. " + serverPortRange); + tclient.setName("client P2P-JAVA-PROJECT"); + tclient.start(); + try { + tclient.join(); + } catch (InterruptedException e) {} + smudp.setStop(); + smtcp.setStop(); + } else { + if (!serverInRange) { + System.err.println("SERVER: Port not in range. " + serverPortRange); + } + if (!trackerInRange) { + System.err.println("TRACKER: Port not in range. " + trackerPortRange); + } + System.exit(2); } } } diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 7835f62..90b7451 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -39,7 +39,7 @@ public class ServerP2P { directories.createSubdir(subdir); logger = new Logger(directories.getDataHomeDirectory() + "server.log"); System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory() + subdir); - if(isInteractive){ + if (isInteractive) { directories.askOpenDataHomeDirectory(subdir, scanner); } scanner.close(); @@ -61,40 +61,43 @@ public class ServerP2P { Scanner scanner = new Scanner(System.in); boolean isInteractive = false; - if ((args.length != 5) && (args.length != 0)){ + if ((args.length != 5) && (args.length != 0)) { System.out.println("usage : java serveurP2P.ServeurP2P (interactive) or java serveurP2P.ServeurP2P -- (" + serverPortRange + " and " + trackerPortRange +")"); System.exit(1); } - else if(args.length == 5){ - hostnameServer = args[1]; - portServer = Integer.valueOf(Integer.parseInt(args[2])); - hostnameTracker = args[3]; - portTracker = Integer.valueOf(Integer.parseInt(args[4])); + else if (args.length == 5) { + isInteractive = false; + hostnameServer = args[1]; + portServer = Integer.valueOf(Integer.parseInt(args[2])); + hostnameTracker = args[3]; + portTracker = Integer.valueOf(Integer.parseInt(args[4])); } else { isInteractive = true; + // configure application System.out.println("Server, enter hostname to bind (default = localhost): "); hostnameServer = scanner.nextLine(); - if(hostnameServer.equals("")){ + if (hostnameServer.equals("")) { hostnameServer = defaultHostname; System.out.println("using default hostname : " + hostnameServer); } - System.out.println("enter port (default = " + serverPortRange.getDefaultPort() + "): "); + System.out.println("Enter port (default = " + serverPortRange.getDefaultPort() + "): "); String portServerStr = scanner.nextLine(); - if(portServerStr.equals("")){ + if (portServerStr.equals("")) { portServer = serverPortRange.getDefaultPort(); System.out.println("using default port : " + portServer); } else { portServer = Integer.valueOf(Integer.parseInt(portServerStr)); } - System.out.println("enter hostname of tracker (default = localhost): "); + // configure tracker + System.out.println("Enter hostname of tracker (default = localhost): "); hostnameTracker = scanner.nextLine(); - if(hostnameTracker.equals("")){ + if (hostnameTracker.equals("")) { hostnameTracker = defaultHostname; System.out.println("tracker default hostname : " + hostnameTracker); } - System.out.println("enter tracker's port (default = " + trackerPortRange.getDefaultPort() + "): "); + System.out.println("Enter tracker's port (default = " + trackerPortRange.getDefaultPort() + "): "); String portTrackerStr = scanner.nextLine(); - if(portTrackerStr.equals("")){ + if (portTrackerStr.equals("")) { portTracker = trackerPortRange.getDefaultPort(); System.out.println("using default port : " + portTracker); } else { @@ -102,28 +105,30 @@ public class ServerP2P { } } - if (serverPortRange.isPortInRange(portServer)){ - if(trackerPortRange.isPortInRange(portTracker)){ - System.out.println("using hostname : " + hostnameServer); - System.out.println("using port : " + portServer); - System.out.println("tracker hostname : " + hostnameTracker); - System.out.println("tracker port : " + portTracker); - ServerP2P s = new ServerP2P(hostnameServer, portServer, hostnameTracker, portTracker, isInteractive); - ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); - ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); - 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(); + boolean serverInRange = serverPortRange.isPortInRange(portServer); + boolean trackerInRange = trackerPortRange.isPortInRange(portTracker); + if (serverInRange && trackerInRange) { + System.out.println("using hostname : " + hostnameServer); + System.out.println("using port : " + portServer); + System.out.println("tracker hostname : " + hostnameTracker); + System.out.println("tracker port : " + portTracker); + ServerP2P s = new ServerP2P(hostnameServer, portServer, hostnameTracker, portTracker, isInteractive); + ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); + ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); + 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(); + } else { + if (!serverInRange) { + System.err.println("SERVER: Port not in range. " + serverPortRange); } - else{ - System.out.println("\n TRACKER: Port not in range. " + trackerPortRange); + if (!trackerInRange) { + System.err.println("TRACKER: Port not in range. " + trackerPortRange); } - } - else{ - System.out.println("\n SERVER: Port not in range. " + serverPortRange); + System.exit(2); } } } diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 9dec113..ba8ff4a 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -32,7 +32,7 @@ public class Tracker { logger = new Logger(directories.getDataHomeDirectory() + "tracker.log"); System.out.println("Tracker will listen on port " + port + " and write logs into " + directories.getDataHomeDirectory()); Scanner scanner = new Scanner(System.in); - if(isInteractive){ + if (isInteractive) { directories.askOpenDataHomeDirectory(null, scanner); } scanner.close(); @@ -52,23 +52,24 @@ public class Tracker { Tracker t; boolean isInteractive = false; - if ((args.length != 3) && (args.length != 0)){ + if ((args.length != 3) && (args.length != 0)) { System.out.println("usage : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- (" + trackerPortRange +")"); System.exit(1); - } else if (args.length == 3){ - hostname = args[1]; - port = Integer.valueOf(Integer.parseInt(args[2])); + } else if (args.length == 3) { + isInteractive = false; + hostname = args[1]; + port = Integer.valueOf(Integer.parseInt(args[2])); } else { isInteractive = true; System.out.println("Tracker Server, enter hostname to bind (default = localhost): "); hostname = scanner.nextLine(); - if(hostname.equals("")){ + if (hostname.equals("")) { hostname = defaultHostname; System.out.println("using default hostname : " + hostname); } System.out.println("enter port (default = " + trackerPortRange.getDefaultPort() +"): "); String portStr = scanner.nextLine(); - if(portStr.equals("")){ + if (portStr.equals("")) { port = trackerPortRange.getDefaultPort(); System.out.println("using default port : " + port); } else { @@ -77,7 +78,7 @@ public class Tracker { } System.out.println("using hostname : " + hostname); - if(trackerPortRange.isPortInRange(port)) { + if (trackerPortRange.isPortInRange(port)) { System.out.println("using port : " + port); t = new Tracker(hostname, port, isInteractive); TrackerManagementUDP tmudp = new TrackerManagementUDP(t.tracker, t.logger); @@ -88,9 +89,9 @@ public class Tracker { Thread ttcp = new Thread(tmtcp); ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); ttcp.start(); - } - else { + } else { System.out.println("Port not in range. " + trackerPortRange); + System.exit(2); } } } -- 2.30.2