From 0e1a451214fa84674914ed0e292b124b1391834d Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 16 Jan 2020 12:35:06 +0100 Subject: [PATCH] Avoid duplication of code --- src/clientP2P/ClientP2P.java | 33 +++++------------------------ src/serverP2P/ServerP2P.java | 34 +++++------------------------- src/tools/Directories.java | 40 ++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 57 deletions(-) create mode 100644 src/tools/Directories.java diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 7a74036..396d7ec 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -1,45 +1,22 @@ package clientP2P; import java.io.File; import clientP2P.ClientManagementUDP; +import tools.Directories; public class ClientP2P { private String host; private int port; - private String directory; + private String dataHomeDirectory; public ClientP2P() { host = "localhost"; port = 40000; - String d; - /* Follow XDG Base Directory Specification - * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html - */ - if (System.getProperty("os.name").equals("Linux")) { - d = System.getenv().get("XDG_DATA_HOME"); - if (d == null || d.equals("")) { - d = System.getenv().get("HOME"); - if (d != null && (!d.equals(""))) { - d += "/.local/share"; - } else { - d += "."; - } - } - } 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"; - } else { - d = "."; - } - d += "/P2P_JAVA_PROJECT_CLIENT/"; - // create directory if not already exists - new File(d).mkdirs(); - directory = d; - System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directory); + dataHomeDirectory = new Directories("P2P_JAVA_PROJECT_CLIENT").getDataHomeDirectory(); + System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + dataHomeDirectory); } public static void main(String [] args) { ClientP2P c = new ClientP2P(); - ClientManagementUDP cm = new ClientManagementUDP(c.directory, c.host, c.port); + ClientManagementUDP cm = new ClientManagementUDP(c.dataHomeDirectory, 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 7725619..c147ad8 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -1,43 +1,19 @@ package serverP2P; import java.io.File; import serverP2P.ServerManagementUDP; +import tools.Directories; public class ServerP2P { private int port; - private String directory; + private String dataHomeDirectory; public ServerP2P() { port = 40000; - String d; - /* Follow XDG Base Directory Specification - * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html - */ - String os = System.getProperty("os.name"); - if (os.equals("Linux")) { - d = System.getenv().get("XDG_DATA_HOME"); - if (d == null || d.equals("")) { - d = System.getenv().get("HOME"); - if (d != null && (!d.equals(""))) { - d += "/.local/share"; - } else { - d += "."; - } - } - } 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"; - } else { - d = "."; - } - d += "/P2P_JAVA_PROJECT_SERVER/"; - // create directory if not already exists - new File(d).mkdirs(); - directory = d; - System.out.println("Server will listen on port " + port + " and serve files from " + directory); + dataHomeDirectory = new Directories("P2P_JAVA_PROJECT_SERVER").getDataHomeDirectory(); + System.out.println("Server will listen on port " + port + " and serve files from " + dataHomeDirectory); } public static void main(String [] args) { ServerP2P s = new ServerP2P(); - ServerManagementUDP sm = new ServerManagementUDP(s.directory, s.port); + ServerManagementUDP sm = new ServerManagementUDP(s.dataHomeDirectory, 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 new file mode 100644 index 0000000..f4e8ead --- /dev/null +++ b/src/tools/Directories.java @@ -0,0 +1,40 @@ +package tools; + +public class Directories { + private String projectName; + private String dataHomeDirectory; + + public Directories(String projectName) { + this.projectName = projectName; + setDataHomeDirectory(); + } + private void setDataHomeDirectory() { + /* Follow XDG Base Directory Specification + * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html + */ + 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"; + } + } 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"; + } else { + d = "."; + } + d += "/" + projectName + "/"; + // create directory if not already exists + new File(d).mkdirs(); + dataHomeDirectory = d; + } + + public String getDataHomeDirectory() { + return dataHomeDirectory(); + } + + + +}