Avoid duplication of code
This commit is contained in:
parent
14b7a2b814
commit
0e1a451214
@ -1,45 +1,22 @@
|
|||||||
package clientP2P;
|
package clientP2P;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import clientP2P.ClientManagementUDP;
|
import clientP2P.ClientManagementUDP;
|
||||||
|
import tools.Directories;
|
||||||
|
|
||||||
public class ClientP2P {
|
public class ClientP2P {
|
||||||
private String host;
|
private String host;
|
||||||
private int port;
|
private int port;
|
||||||
private String directory;
|
private String dataHomeDirectory;
|
||||||
public ClientP2P() {
|
public ClientP2P() {
|
||||||
host = "localhost";
|
host = "localhost";
|
||||||
port = 40000;
|
port = 40000;
|
||||||
String d;
|
dataHomeDirectory = new Directories("P2P_JAVA_PROJECT_CLIENT").getDataHomeDirectory();
|
||||||
/* Follow XDG Base Directory Specification
|
System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + dataHomeDirectory);
|
||||||
* 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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.directory, c.host, c.port);
|
ClientManagementUDP cm = new ClientManagementUDP(c.dataHomeDirectory, 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,43 +1,19 @@
|
|||||||
package serverP2P;
|
package serverP2P;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import serverP2P.ServerManagementUDP;
|
import serverP2P.ServerManagementUDP;
|
||||||
|
import tools.Directories;
|
||||||
|
|
||||||
public class ServerP2P {
|
public class ServerP2P {
|
||||||
private int port;
|
private int port;
|
||||||
private String directory;
|
private String dataHomeDirectory;
|
||||||
public ServerP2P() {
|
public ServerP2P() {
|
||||||
port = 40000;
|
port = 40000;
|
||||||
String d;
|
dataHomeDirectory = new Directories("P2P_JAVA_PROJECT_SERVER").getDataHomeDirectory();
|
||||||
/* Follow XDG Base Directory Specification
|
System.out.println("Server will listen on port " + port + " and serve files from " + dataHomeDirectory);
|
||||||
* 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);
|
|
||||||
}
|
}
|
||||||
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.directory, s.port);
|
ServerManagementUDP sm = new ServerManagementUDP(s.dataHomeDirectory, 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();
|
||||||
|
40
src/tools/Directories.java
Normal file
40
src/tools/Directories.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user