From 8d5ef8a52bdeb0834839273cef3c103c7002bed3 Mon Sep 17 00:00:00 2001 From: Flavien Haas Date: Mon, 30 Mar 2020 18:35:10 +0200 Subject: [PATCH] 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);