From b3cc441d9ec5d2b3904219f17291ead7ba744a2d Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 19:48:46 +0100 Subject: [PATCH] Factorize code for opening directory --- src/clientP2P/ClientP2P.java | 21 ++++------------- src/serverP2P/ServerP2P.java | 18 ++++----------- src/tools/Directories.java | 45 ++++++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 45 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index e94299a..1d09a0c 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -1,34 +1,23 @@ package clientP2P; import clientP2P.ClientManagementUDP; import tools.Directories; -import java.util.Scanner; + public class ClientP2P { private String host; private int port; private String dataHomeDirectory; public ClientP2P() { - Directories d = new Directories("P2P_JAVA_PROJECT_CLIENT"); - String os = System.getProperty("os.name"); - + Directories directories = new Directories("P2P_JAVA_PROJECT_CLIENT"); host = "localhost"; port = 40000; - dataHomeDirectory = d.getDataHomeDirectory(); - System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + dataHomeDirectory); - if (os.equals("Linux")||os.equals("Mac")||os.equals("Mac OS X")) { - System.out.println("Do you want to open this directory? (y/N)"); - Scanner scanner = new Scanner(System.in); - String resp = scanner.nextLine(); - if (resp.equals("y") || resp.equals("Y")) { - System.out.println("Openning"); - d.openDataHomeDirectory(); - } - } + System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directories.getDataHomeDirectory()); + directories.askOpenDataHomeDirectory(); } public static void main(String [] args) { ClientP2P c = new ClientP2P(); - ClientManagementUDP cm = new ClientManagementUDP(c.dataHomeDirectory, c.host, c.port); + ClientManagementUDP cm = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.host, c.port); Thread t = new Thread(cm); t.setName("client P2P-JAVA-PROJECT"); t.start(); diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 601c54e..c681d66 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -1,31 +1,21 @@ package serverP2P; import serverP2P.ServerManagementUDP; import tools.Directories; -import java.util.Scanner; public class ServerP2P { private int port; private String dataHomeDirectory; public ServerP2P() { - Directories d = new Directories("P2P_JAVA_PROJECT_SERVER"); - String os = System.getProperty("os.name"); + Directories directories = new Directories("P2P_JAVA_PROJECT_SERVER"); port = 40000; dataHomeDirectory = d.getDataHomeDirectory(); - System.out.println("Server will listen on port " + port + " and serve files from " + dataHomeDirectory); - if (os.equals("Linux")||os.equals("Mac")||os.equals("Mac OS X")) { - System.out.println("Do you want to open this directory? (y/N)"); - Scanner scanner = new Scanner(System.in); - String resp = scanner.nextLine(); - if (resp.equals("y") || resp.equals("Y")) { - System.out.println("Openning"); - d.openDataHomeDirectory(); - } - } + System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory()); + directories.askOpenDataHomeDirectory(); } public static void main(String [] args) { ServerP2P s = new ServerP2P(); - ServerManagementUDP sm = new ServerManagementUDP(s.dataHomeDirectory, s.port); + ServerManagementUDP sm = new ServerManagementUDP(s.directories.getDataHomeDirectory(), s.port); Thread t = new Thread(sm); t.setName("server P2P-JAVA-PROJECT"); t.start(); diff --git a/src/tools/Directories.java b/src/tools/Directories.java index 42543a8..92a18b7 100644 --- a/src/tools/Directories.java +++ b/src/tools/Directories.java @@ -1,4 +1,5 @@ package tools; +import java.util.Scanner; import java.io.File; import java.lang.Runtime; import java.io.IOException; @@ -13,12 +14,14 @@ import java.io.IOException; public class Directories { private String projectName; private String dataHomeDirectory; + private String os; /** Constructor with projectName parameter. * @param projectName name of the project */ public Directories(String projectName) { this.projectName = projectName; + os = System.getProperty("os.name"); setDataHomeDirectory(); } @@ -28,24 +31,21 @@ public class Directories { /* Follow XDG Base Directory Specification * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html */ - String d; - String os = System.getProperty("os.name"); if (os.equals("Linux")) { - d = System.getenv().get("XDG_DATA_HOME"); - if (d == null || d.equals("")) { - d = System.getProperty("user.home") + "/.local/share"; + dataHomeDirectory = System.getenv().get("XDG_DATA_HOME"); + if (dataHomeDirectory == null || dataHomeDirectory.equals("")) { + dataHomeDirectory = System.getProperty("user.home") + "/.local/share"; } } else if (os.equals("Mac")||os.equals("Mac OS X")) { /* Apple MacOS X User Data Directory * https://developer.apple.com/library/archive/qa/qa1170/_index.html */ - d = System.getProperty("user.home") + "/Library"; + dataHomeDirectory = System.getProperty("user.home") + "/Library"; } else { - d = "."; + dataHomeDirectory = "."; } - d += "/" + projectName + "/"; + dataHomeDirectory += "/" + projectName + "/"; // create directory if not already exists - new File(d).mkdirs(); - dataHomeDirectory = d; + new File(dataHomeDirectory).mkdirs(); } /** Getter for dataHomeDirectory. @@ -55,10 +55,10 @@ public class Directories { return dataHomeDirectory; } - public void openDataHomeDirectory() { + /** Opens dataHomeDirectory if supported. + */ + private void openDataHomeDirectory() { try { - String os = System.getProperty("os.name"); - if (os.equals("Linux")) { Runtime runtime = Runtime.getRuntime(); runtime.exec(new String[] { "xdg-open", dataHomeDirectory }); @@ -66,8 +66,23 @@ public class Directories { Runtime runtime = Runtime.getRuntime(); runtime.exec(new String[] { "open", dataHomeDirectory }); } - } catch (IOException e) {} + } catch (IOException e) { + System.out.println("Error encountered while trying to open directory"); + } } - + + /** Asks the user to choose opening dataHomeDirectory or not. + */ + public void askOpenDataHomeDirectory() { + if (os.equals("Linux") || os.equals("Mac") || os.equals("Mac OS X")) { + System.out.println("Do you want to open this directory? (y/N)"); + Scanner scanner = new Scanner(System.in); + String resp = scanner.nextLine(); + if (resp.equals("y") || resp.equals("Y")) { + System.out.println("Openning"); + openDataHomeDirectory(); + } + } + }