|
|
|
@ -2,7 +2,6 @@ package clientP2P;
|
|
|
|
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import javafx.util.Pair;
|
|
|
|
|
import clientP2P.ClientManagementUDP;
|
|
|
|
|
import clientP2P.ClientManagementTCP;
|
|
|
|
|
|
|
|
|
@ -12,7 +11,9 @@ import tools.Logger;
|
|
|
|
|
import tools.LogLevel;
|
|
|
|
|
import tools.Directories;
|
|
|
|
|
import tools.HostItem;
|
|
|
|
|
import tools.TestPort;
|
|
|
|
|
import tools.ServerPortRange;
|
|
|
|
|
import tools.TrackerPortRange;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Client + Server implementation.
|
|
|
|
|
* @author Louis Royer
|
|
|
|
@ -26,17 +27,17 @@ public class ClientP2P {
|
|
|
|
|
private String partsDir = ".parts/";
|
|
|
|
|
private Logger loggerServer;
|
|
|
|
|
private Logger loggerClient;
|
|
|
|
|
private int portServer;
|
|
|
|
|
private Directories directories;
|
|
|
|
|
private HostItem tracker;
|
|
|
|
|
private HostItem server;
|
|
|
|
|
private Scanner scanner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Initialize loggers if directories and logger are null,
|
|
|
|
|
* else fail silently.
|
|
|
|
|
*/
|
|
|
|
|
public void initDirectoriesAndLoggers() {
|
|
|
|
|
if (directories == null && loggerServer == null && loggerClient == null) {
|
|
|
|
|
directories = new Directories("P2P_JAVA_PROJECT_" + portServer);
|
|
|
|
|
directories = new Directories("P2P_JAVA_PROJECT_" + server.getPort());
|
|
|
|
|
directories.createSubdir(logDir);
|
|
|
|
|
loggerServer = new Logger(directories.getDataHomeDirectory() + logDir + "server.log");
|
|
|
|
|
loggerClient = new Logger(directories.getDataHomeDirectory() + logDir + "client.log");
|
|
|
|
@ -44,18 +45,36 @@ public class ClientP2P {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Constructor with portServerstr as parameter.
|
|
|
|
|
* @param portServerstr String containing port for server listenning.
|
|
|
|
|
/** Constructor.
|
|
|
|
|
* @param hostnameServer hostname to bind
|
|
|
|
|
* @param portServer port to bind
|
|
|
|
|
* @param hostnameTracker hostname of tracker
|
|
|
|
|
* @param portTracker port of tracker
|
|
|
|
|
*/
|
|
|
|
|
public ClientP2P(String portServerstr, String hostnameTracker, String portTracker) {
|
|
|
|
|
public ClientP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker) {
|
|
|
|
|
scanner = new Scanner(System.in);
|
|
|
|
|
portServer = Integer.valueOf(Integer.parseInt(portServerstr));
|
|
|
|
|
tracker = new HostItem(hostnameTracker, Integer.valueOf(Integer.parseInt(portTracker)));
|
|
|
|
|
server = new HostItem(hostnameServer, portServer);
|
|
|
|
|
tracker = new HostItem(hostnameTracker, portTracker);
|
|
|
|
|
initDirectoriesAndLoggers();
|
|
|
|
|
System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory());
|
|
|
|
|
directories.askOpenDataHomeDirectory(null, scanner);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Print cli usage
|
|
|
|
|
* @param serverPortRange range of server ports
|
|
|
|
|
* @param trackerPortRange range of tracker ports
|
|
|
|
|
*/
|
|
|
|
|
private static void printUsage(ServerPortRange serverPortRange, TrackerPortRange trackerPortRange) {
|
|
|
|
|
System.out.println("usage :");
|
|
|
|
|
System.out.println("\tjava clientP2P.ClientP2P");
|
|
|
|
|
System.out.println("or");
|
|
|
|
|
System.out.println("java clientP2P.ClientP2P -- " +
|
|
|
|
|
"<clientTransportProtocol> " +
|
|
|
|
|
"<integratedServerHOSTNAME> <integratedServerPORT> " +
|
|
|
|
|
"<trackerHOSTNAME> <trackerPORT> ");
|
|
|
|
|
System.out.println("(" + trackerPortRange + " and " + serverPortRange +")");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 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.
|
|
|
|
@ -63,121 +82,120 @@ public class ClientP2P {
|
|
|
|
|
*/
|
|
|
|
|
public static void main(String [] args) {
|
|
|
|
|
final String defaultHostname = "localhost";
|
|
|
|
|
final String defaultPortServer = "7070";
|
|
|
|
|
final String defaultPortTracker = "6969";
|
|
|
|
|
String hostnameServer = "";
|
|
|
|
|
String portServer = "";
|
|
|
|
|
int portServer = 0;
|
|
|
|
|
String hostnameTracker = "";
|
|
|
|
|
String portTracker = "";
|
|
|
|
|
int portTracker = 0;
|
|
|
|
|
String protocolClient = "";
|
|
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
|
TestPort testPort = new TestPort();
|
|
|
|
|
final ServerPortRange serverPortRange = new ServerPortRange();
|
|
|
|
|
final TrackerPortRange trackerPortRange = new TrackerPortRange();
|
|
|
|
|
|
|
|
|
|
if ((args.length != 6) && (args.length != 0)){
|
|
|
|
|
System.out.println("usage : java clientP2P.ClientP2P or java clientP2P.ClientP2P -- <clientTransportProtocol> <integratedServerHOSTNAME> <integratedServerPORT> <trackerHOSTNAME> <trackerPORT> (default tracker port 6969 (range 7000 -> 7999) and server port: server 7070 (range 7000->7070))");
|
|
|
|
|
ClientP2P.printUsage(serverPortRange, trackerPortRange);
|
|
|
|
|
System.exit(1);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
if(args.length == 6){
|
|
|
|
|
else if(args.length == 6){
|
|
|
|
|
protocolClient = args[1];
|
|
|
|
|
hostnameServer = args[2];
|
|
|
|
|
portServer = args[3];
|
|
|
|
|
portServer = Integer.valueOf(Integer.parseInt(args[3]));
|
|
|
|
|
hostnameTracker = args[4];
|
|
|
|
|
portTracker = args[5];
|
|
|
|
|
portTracker = Integer.valueOf(Integer.parseInt(args[5]));
|
|
|
|
|
} else{
|
|
|
|
|
System.out.println("Client, wich transport protocol do you want to use (default = TCP): ");
|
|
|
|
|
protocolClient = scanner.nextLine();
|
|
|
|
|
System.out.println("server side, enter hostname to bind (default = localhost): ");
|
|
|
|
|
hostnameServer = scanner.nextLine();
|
|
|
|
|
if(hostnameServer.equals("")){
|
|
|
|
|
hostnameServer = defaultHostname;
|
|
|
|
|
System.out.println("using default hostname : " + hostnameServer);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
System.out.println("Client, wich transport protocol do you want to use (default = TCP): ");
|
|
|
|
|
protocolClient = scanner.nextLine();
|
|
|
|
|
System.out.println("server side, enter hostname to bind (default = localhost): ");
|
|
|
|
|
hostnameServer = scanner.nextLine();
|
|
|
|
|
if(hostnameServer.equals("")){
|
|
|
|
|
hostnameServer = defaultHostname;
|
|
|
|
|
System.out.println("using default hostname : " + hostnameServer);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("enter port (default = 7070): ");
|
|
|
|
|
portServer = scanner.nextLine();
|
|
|
|
|
if(portServer.equals("")){
|
|
|
|
|
portServer = defaultPortServer;
|
|
|
|
|
System.out.println("using default port : " + portServer);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("enter hostname of tracker (default = localhost): ");
|
|
|
|
|
hostnameTracker = scanner.nextLine();
|
|
|
|
|
if(hostnameTracker.equals("")){
|
|
|
|
|
hostnameTracker = defaultHostname;
|
|
|
|
|
System.out.println("tracker default hostname : " + hostnameTracker);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("enter tracker's port (default = 6969): ");
|
|
|
|
|
portTracker = scanner.nextLine();
|
|
|
|
|
if(portTracker.equals("")){
|
|
|
|
|
portTracker = defaultPortTracker;
|
|
|
|
|
System.out.println("using default port : " + portTracker);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("enter port (default = " + serverPortRange.getDefaultPort() +"): ");
|
|
|
|
|
String portServerStr = scanner.nextLine();
|
|
|
|
|
if(portServerStr.equals("")){
|
|
|
|
|
portServer = serverPortRange.getDefaultPort();
|
|
|
|
|
System.out.println("using default port : " + portServer);
|
|
|
|
|
} else {
|
|
|
|
|
portServer = Integer.valueOf(Integer.parseInt(portServerStr));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("using hostname : " + hostnameServer);
|
|
|
|
|
Pair<Integer, String> PtestPortServer = testPort.testPortServer(Integer.parseInt(portServer));
|
|
|
|
|
if(PtestPortServer.getKey() == 0){
|
|
|
|
|
System.out.println("using port : " + portServer);
|
|
|
|
|
System.out.println("enter hostname of tracker (default = localhost): ");
|
|
|
|
|
hostnameTracker = scanner.nextLine();
|
|
|
|
|
if(hostnameTracker.equals("")){
|
|
|
|
|
hostnameTracker = defaultHostname;
|
|
|
|
|
System.out.println("tracker default hostname : " + hostnameTracker);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
System.out.println(PtestPortServer.getValue());
|
|
|
|
|
portServer = defaultPortServer;
|
|
|
|
|
System.out.println("enter tracker's port (default = "+trackerPortRange.getDefaultPort() + "): ");
|
|
|
|
|
String portTrackerStr = scanner.nextLine();
|
|
|
|
|
if(portTrackerStr.equals("")){
|
|
|
|
|
portTracker = trackerPortRange.getDefaultPort();
|
|
|
|
|
System.out.println("using default port : " + portTracker);
|
|
|
|
|
} else {
|
|
|
|
|
portTracker = Integer.valueOf(Integer.parseInt(portTrackerStr));
|
|
|
|
|
}
|
|
|
|
|
System.out.println("tracker hostname : " + hostnameTracker);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pair<Integer, String> PtestPortTracker = testPort.testPortTracker(Integer.parseInt(portTracker));
|
|
|
|
|
if(PtestPortTracker.getKey() == 0){
|
|
|
|
|
System.out.println("using port : " + portTracker);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
System.out.println(PtestPortTracker.getValue());
|
|
|
|
|
portTracker = defaultPortTracker;
|
|
|
|
|
}
|
|
|
|
|
System.out.println("using hostname : " + hostnameServer);
|
|
|
|
|
if(serverPortRange.isPortInRange(portServer)){
|
|
|
|
|
System.out.println("using port : " + portServer);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
System.out.println("Port not in range. " + serverPortRange);
|
|
|
|
|
portServer = serverPortRange.getDefaultPort();
|
|
|
|
|
}
|
|
|
|
|
System.out.println("tracker hostname : " + hostnameTracker);
|
|
|
|
|
|
|
|
|
|
if(trackerPortRange.isPortInRange(portTracker)){
|
|
|
|
|
System.out.println("using port : " + portTracker);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
System.out.println("Port not in range. " + trackerPortRange);
|
|
|
|
|
portTracker = trackerPortRange.getDefaultPort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClientP2P c = new ClientP2P(portServer, hostnameTracker, portTracker);
|
|
|
|
|
ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker);
|
|
|
|
|
|
|
|
|
|
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), hostnameServer, c.portServer, c.loggerServer, c.tracker);
|
|
|
|
|
ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), hostnameServer, c.portServer, c.loggerServer, c.tracker);
|
|
|
|
|
Thread tudp = new Thread(smudp);
|
|
|
|
|
tudp.setName("server UDP P2P-JAVA-PROJECT");
|
|
|
|
|
tudp.start();
|
|
|
|
|
Thread ttcp = new Thread(smtcp);
|
|
|
|
|
ttcp.setName("server TCP P2P-JAVA-PROJECT");
|
|
|
|
|
ttcp.start();
|
|
|
|
|
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer);
|
|
|
|
|
ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer);
|
|
|
|
|
Thread tudp = new Thread(smudp);
|
|
|
|
|
tudp.setName("server UDP P2P-JAVA-PROJECT");
|
|
|
|
|
tudp.start();
|
|
|
|
|
Thread ttcp = new Thread(smtcp);
|
|
|
|
|
ttcp.setName("server TCP P2P-JAVA-PROJECT");
|
|
|
|
|
ttcp.start();
|
|
|
|
|
|
|
|
|
|
// Wait a bit before printing client interface
|
|
|
|
|
// This is not required, but allow to have a cleaner interface
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(200);
|
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
}
|
|
|
|
|
// Wait a bit before printing client interface
|
|
|
|
|
// This is not required, but allow to have a cleaner interface
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(200);
|
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread tclient;
|
|
|
|
|
switch(protocolClient){
|
|
|
|
|
case "UDP":
|
|
|
|
|
case "udp":
|
|
|
|
|
case "upd":
|
|
|
|
|
case "2" :
|
|
|
|
|
System.out.println("Starting with UDP");
|
|
|
|
|
ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner);
|
|
|
|
|
tclient = new Thread(cmudp);
|
|
|
|
|
break;
|
|
|
|
|
case "TCP":
|
|
|
|
|
case "tcp":
|
|
|
|
|
case "1":
|
|
|
|
|
default:
|
|
|
|
|
System.out.println("Starting with TCP");
|
|
|
|
|
ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner);
|
|
|
|
|
tclient = new Thread(cmtcp);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
tclient.setName("client P2P-JAVA-PROJECT");
|
|
|
|
|
tclient.start();
|
|
|
|
|
try {
|
|
|
|
|
tclient.join();
|
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
|
smudp.setStop();
|
|
|
|
|
smtcp.setStop();
|
|
|
|
|
Thread tclient;
|
|
|
|
|
switch(protocolClient){
|
|
|
|
|
case "UDP":
|
|
|
|
|
case "udp":
|
|
|
|
|
case "upd": // to avoid users typos
|
|
|
|
|
case "2" :
|
|
|
|
|
System.out.println("Starting with UDP");
|
|
|
|
|
ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner);
|
|
|
|
|
tclient = new Thread(cmudp);
|
|
|
|
|
break;
|
|
|
|
|
case "TCP":
|
|
|
|
|
case "tcp":
|
|
|
|
|
case "1":
|
|
|
|
|
default:
|
|
|
|
|
System.out.println("Starting with TCP");
|
|
|
|
|
ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner);
|
|
|
|
|
tclient = new Thread(cmtcp);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
tclient.setName("client P2P-JAVA-PROJECT");
|
|
|
|
|
tclient.start();
|
|
|
|
|
try {
|
|
|
|
|
tclient.join();
|
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
|
smudp.setStop();
|
|
|
|
|
smtcp.setStop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|