|
|
@ -11,8 +11,13 @@ import java.util.Scanner;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import tools.HostItem;
|
|
|
|
import tools.HostItem;
|
|
|
|
import tools.HostList;
|
|
|
|
import tools.HostList;
|
|
|
|
import java.lang.NumberFormatException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Client + Server implementation.
|
|
|
|
|
|
|
|
* @author Louis Royer
|
|
|
|
|
|
|
|
* @author Flavien Haas
|
|
|
|
|
|
|
|
* @author JS Auge
|
|
|
|
|
|
|
|
* @version 1.0
|
|
|
|
|
|
|
|
*/
|
|
|
|
public class ClientP2P {
|
|
|
|
public class ClientP2P {
|
|
|
|
static private final String subdir = "seeded/";
|
|
|
|
static private final String subdir = "seeded/";
|
|
|
|
static private String parts = ".parts";
|
|
|
|
static private String parts = ".parts";
|
|
|
@ -23,6 +28,10 @@ public class ClientP2P {
|
|
|
|
private List<HostItem> hostList;
|
|
|
|
private List<HostItem> hostList;
|
|
|
|
private static final int defaultPort = 20000;
|
|
|
|
private static final int defaultPort = 20000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Initialize logger if directories and logger are null,
|
|
|
|
|
|
|
|
* else fail silently.
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void initLogger() {
|
|
|
|
public void initLogger() {
|
|
|
|
if (directories == null && logger == null) {
|
|
|
|
if (directories == null && logger == null) {
|
|
|
|
directories = new Directories("P2P_JAVA_PROJECT" + port);
|
|
|
|
directories = new Directories("P2P_JAVA_PROJECT" + port);
|
|
|
@ -30,6 +39,9 @@ public class ClientP2P {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Constructor with portStr as parameter.
|
|
|
|
|
|
|
|
* @param portStr String containing port for server listenning.
|
|
|
|
|
|
|
|
*/
|
|
|
|
public ClientP2P(String portStr) {
|
|
|
|
public ClientP2P(String portStr) {
|
|
|
|
try{
|
|
|
|
try{
|
|
|
|
port = Integer.valueOf(Integer.parseInt(portStr));
|
|
|
|
port = Integer.valueOf(Integer.parseInt(portStr));
|
|
|
@ -49,6 +61,11 @@ public class ClientP2P {
|
|
|
|
System.out.println("Please enter list of servers to use; first one will be used to ask list of files");
|
|
|
|
System.out.println("Please enter list of servers to use; first one will be used to ask list of files");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Main program entry point.
|
|
|
|
|
|
|
|
* 1rst parameter is optionnal, and is used to
|
|
|
|
|
|
|
|
* define port used by the server module to listen. If not provided, default to another port.
|
|
|
|
|
|
|
|
* @param args server listenning port
|
|
|
|
|
|
|
|
*/
|
|
|
|
public static void main(String [] args) {
|
|
|
|
public static void main(String [] args) {
|
|
|
|
ClientP2P c;
|
|
|
|
ClientP2P c;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -57,21 +74,25 @@ public class ClientP2P {
|
|
|
|
c = new ClientP2P("" + defaultPort);
|
|
|
|
c = new ClientP2P("" + defaultPort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Server threads
|
|
|
|
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + subdir, c.port, c.logger);
|
|
|
|
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + subdir, c.port, c.logger);
|
|
|
|
ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + subdir, c.port, c.logger);
|
|
|
|
ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + subdir, c.port, c.logger);
|
|
|
|
Thread tudp = new Thread(smudp);
|
|
|
|
Thread tudp = new Thread(smudp);
|
|
|
|
tudp.setName("server UDP P2P-JAVA-PROJECT");
|
|
|
|
tudp.setName("server UDP P2P-JAVA-PROJECT (port: " + c.port + ")");
|
|
|
|
tudp.start();
|
|
|
|
tudp.start();
|
|
|
|
Thread ttcp = new Thread(smtcp);
|
|
|
|
Thread ttcp = new Thread(smtcp);
|
|
|
|
ttcp.setName("server TCP P2P-JAVA-PROJECT");
|
|
|
|
ttcp.setName("server TCP P2P-JAVA-PROJECT (port: " + c.port + ")");
|
|
|
|
ttcp.start();
|
|
|
|
ttcp.start();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait a bit before printing client interface
|
|
|
|
|
|
|
|
// This is not required, but allow to have a cleaner interface
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Thread.sleep(100);
|
|
|
|
Thread.sleep(100);
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// initialize Host lists
|
|
|
|
c.hostList = HostList.getServList();
|
|
|
|
c.hostList = HostList.getServList();
|
|
|
|
System.out.println("Client : Which transport protocol do you want to use? [TCP/udp]");
|
|
|
|
System.out.println("Client : Which transport protocol do you want to use? [TCP/udp]");
|
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|