Factorize code for opening directory

pull/1/head
Louis Royer 5 years ago
parent 8f239e23f5
commit b3cc441d9e

@ -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();

@ -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();

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

Loading…
Cancel
Save