Factorize code for opening directory
This commit is contained in:
parent
8f239e23f5
commit
b3cc441d9e
@ -1,34 +1,23 @@
|
|||||||
package clientP2P;
|
package clientP2P;
|
||||||
import clientP2P.ClientManagementUDP;
|
import clientP2P.ClientManagementUDP;
|
||||||
import tools.Directories;
|
import tools.Directories;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class ClientP2P {
|
public class ClientP2P {
|
||||||
private String host;
|
private String host;
|
||||||
private int port;
|
private int port;
|
||||||
private String dataHomeDirectory;
|
private String dataHomeDirectory;
|
||||||
public ClientP2P() {
|
public ClientP2P() {
|
||||||
Directories d = new Directories("P2P_JAVA_PROJECT_CLIENT");
|
Directories directories = new Directories("P2P_JAVA_PROJECT_CLIENT");
|
||||||
String os = System.getProperty("os.name");
|
|
||||||
|
|
||||||
host = "localhost";
|
host = "localhost";
|
||||||
port = 40000;
|
port = 40000;
|
||||||
dataHomeDirectory = d.getDataHomeDirectory();
|
System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directories.getDataHomeDirectory());
|
||||||
System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + dataHomeDirectory);
|
directories.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");
|
|
||||||
d.openDataHomeDirectory();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String [] args) {
|
public static void main(String [] args) {
|
||||||
ClientP2P c = new ClientP2P();
|
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);
|
Thread t = new Thread(cm);
|
||||||
t.setName("client P2P-JAVA-PROJECT");
|
t.setName("client P2P-JAVA-PROJECT");
|
||||||
t.start();
|
t.start();
|
||||||
|
@ -1,31 +1,21 @@
|
|||||||
package serverP2P;
|
package serverP2P;
|
||||||
import serverP2P.ServerManagementUDP;
|
import serverP2P.ServerManagementUDP;
|
||||||
import tools.Directories;
|
import tools.Directories;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class ServerP2P {
|
public class ServerP2P {
|
||||||
private int port;
|
private int port;
|
||||||
private String dataHomeDirectory;
|
private String dataHomeDirectory;
|
||||||
public ServerP2P() {
|
public ServerP2P() {
|
||||||
Directories d = new Directories("P2P_JAVA_PROJECT_SERVER");
|
Directories directories = new Directories("P2P_JAVA_PROJECT_SERVER");
|
||||||
String os = System.getProperty("os.name");
|
|
||||||
|
|
||||||
port = 40000;
|
port = 40000;
|
||||||
dataHomeDirectory = d.getDataHomeDirectory();
|
dataHomeDirectory = d.getDataHomeDirectory();
|
||||||
System.out.println("Server will listen on port " + port + " and serve files from " + dataHomeDirectory);
|
System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory());
|
||||||
if (os.equals("Linux")||os.equals("Mac")||os.equals("Mac OS X")) {
|
directories.askOpenDataHomeDirectory();
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
public static void main(String [] args) {
|
public static void main(String [] args) {
|
||||||
ServerP2P s = new ServerP2P();
|
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);
|
Thread t = new Thread(sm);
|
||||||
t.setName("server P2P-JAVA-PROJECT");
|
t.setName("server P2P-JAVA-PROJECT");
|
||||||
t.start();
|
t.start();
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
package tools;
|
package tools;
|
||||||
|
import java.util.Scanner;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.lang.Runtime;
|
import java.lang.Runtime;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -13,12 +14,14 @@ import java.io.IOException;
|
|||||||
public class Directories {
|
public class Directories {
|
||||||
private String projectName;
|
private String projectName;
|
||||||
private String dataHomeDirectory;
|
private String dataHomeDirectory;
|
||||||
|
private String os;
|
||||||
|
|
||||||
/** Constructor with projectName parameter.
|
/** Constructor with projectName parameter.
|
||||||
* @param projectName name of the project
|
* @param projectName name of the project
|
||||||
*/
|
*/
|
||||||
public Directories(String projectName) {
|
public Directories(String projectName) {
|
||||||
this.projectName = projectName;
|
this.projectName = projectName;
|
||||||
|
os = System.getProperty("os.name");
|
||||||
setDataHomeDirectory();
|
setDataHomeDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,24 +31,21 @@ public class Directories {
|
|||||||
/* Follow XDG Base Directory Specification
|
/* Follow XDG Base Directory Specification
|
||||||
* https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
* https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||||
*/
|
*/
|
||||||
String d;
|
|
||||||
String os = System.getProperty("os.name");
|
|
||||||
if (os.equals("Linux")) {
|
if (os.equals("Linux")) {
|
||||||
d = System.getenv().get("XDG_DATA_HOME");
|
dataHomeDirectory = System.getenv().get("XDG_DATA_HOME");
|
||||||
if (d == null || d.equals("")) {
|
if (dataHomeDirectory == null || dataHomeDirectory.equals("")) {
|
||||||
d = System.getProperty("user.home") + "/.local/share";
|
dataHomeDirectory = System.getProperty("user.home") + "/.local/share";
|
||||||
}
|
}
|
||||||
} else if (os.equals("Mac")||os.equals("Mac OS X")) {
|
} else if (os.equals("Mac")||os.equals("Mac OS X")) {
|
||||||
/* Apple MacOS X User Data Directory
|
/* Apple MacOS X User Data Directory
|
||||||
* https://developer.apple.com/library/archive/qa/qa1170/_index.html */
|
* https://developer.apple.com/library/archive/qa/qa1170/_index.html */
|
||||||
d = System.getProperty("user.home") + "/Library";
|
dataHomeDirectory = System.getProperty("user.home") + "/Library";
|
||||||
} else {
|
} else {
|
||||||
d = ".";
|
dataHomeDirectory = ".";
|
||||||
}
|
}
|
||||||
d += "/" + projectName + "/";
|
dataHomeDirectory += "/" + projectName + "/";
|
||||||
// create directory if not already exists
|
// create directory if not already exists
|
||||||
new File(d).mkdirs();
|
new File(dataHomeDirectory).mkdirs();
|
||||||
dataHomeDirectory = d;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Getter for dataHomeDirectory.
|
/** Getter for dataHomeDirectory.
|
||||||
@ -55,10 +55,10 @@ public class Directories {
|
|||||||
return dataHomeDirectory;
|
return dataHomeDirectory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void openDataHomeDirectory() {
|
/** Opens dataHomeDirectory if supported.
|
||||||
|
*/
|
||||||
|
private void openDataHomeDirectory() {
|
||||||
try {
|
try {
|
||||||
String os = System.getProperty("os.name");
|
|
||||||
|
|
||||||
if (os.equals("Linux")) {
|
if (os.equals("Linux")) {
|
||||||
Runtime runtime = Runtime.getRuntime();
|
Runtime runtime = Runtime.getRuntime();
|
||||||
runtime.exec(new String[] { "xdg-open", dataHomeDirectory });
|
runtime.exec(new String[] { "xdg-open", dataHomeDirectory });
|
||||||
@ -66,8 +66,23 @@ public class Directories {
|
|||||||
Runtime runtime = Runtime.getRuntime();
|
Runtime runtime = Runtime.getRuntime();
|
||||||
runtime.exec(new String[] { "open", dataHomeDirectory });
|
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…
Reference in New Issue
Block a user