Projet_JAVA_P2P_STRI2A/src/clientP2P/ClientP2P.java

202 lines
7.1 KiB
Java
Raw Normal View History

2020-01-14 11:10:11 +01:00
package clientP2P;
2020-03-19 17:49:39 +01:00
import java.util.Scanner;
import java.util.List;
2020-01-14 11:10:11 +01:00
import clientP2P.ClientManagementUDP;
2020-02-29 16:57:19 +01:00
import clientP2P.ClientManagementTCP;
2020-03-24 16:04:10 +01:00
2020-03-18 17:46:37 +01:00
import serverP2P.ServerManagementUDP;
import serverP2P.ServerManagementTCP;
import tools.Logger;
import tools.LogLevel;
2020-01-16 12:35:06 +01:00
import tools.Directories;
import tools.HostItem;
2020-03-24 17:32:33 +01:00
import tools.ServerPortRange;
import tools.TrackerPortRange;
2019-12-12 11:57:02 +01:00
2020-03-19 13:30:49 +01:00
/** Client + Server implementation.
* @author Louis Royer
* @author Flavien Haas
* @author JS Auge
* @version 1.0
*/
2020-03-19 17:49:39 +01:00
2020-01-14 11:10:11 +01:00
public class ClientP2P {
private String logDir = "logs/";
private String partsDir = ".parts/";
2020-03-19 17:49:39 +01:00
private Logger loggerServer;
private Logger loggerClient;
2020-01-21 19:56:50 +01:00
private Directories directories;
private HostItem tracker;
2020-03-24 17:32:33 +01:00
private HostItem server;
2020-03-21 15:48:18 +01:00
private Scanner scanner;
2020-03-24 17:32:33 +01:00
2020-03-19 17:49:39 +01:00
/** Initialize loggers if directories and logger are null,
2020-03-19 13:30:49 +01:00
* else fail silently.
*/
public void initDirectoriesAndLoggers() {
2020-03-19 17:49:39 +01:00
if (directories == null && loggerServer == null && loggerClient == null) {
2020-03-24 17:32:33 +01:00
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");
directories.createSubdir(partsDir);
2020-03-18 17:46:37 +01:00
}
}
2020-03-24 17:32:33 +01:00
/** Constructor.
* @param hostnameServer hostname to bind
* @param portServer port to bind
* @param hostnameTracker hostname of tracker
* @param portTracker port of tracker
2020-03-19 13:30:49 +01:00
*/
2020-03-24 17:32:33 +01:00
public ClientP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker) {
2020-03-21 15:48:18 +01:00
scanner = new Scanner(System.in);
2020-03-24 17:32:33 +01:00
server = new HostItem(hostnameServer, portServer);
tracker = new HostItem(hostnameTracker, portTracker);
initDirectoriesAndLoggers();
2020-03-24 16:04:10 +01:00
System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory());
directories.askOpenDataHomeDirectory(null, scanner);
2020-01-12 23:29:49 +01:00
}
2020-01-14 11:10:11 +01:00
2020-03-24 17:32:33 +01:00
/** 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 +")");
}
2020-03-19 13:30:49 +01:00
/** 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
2020-03-19 17:49:39 +01:00
*/
2019-12-12 11:57:02 +01:00
public static void main(String [] args) {
2020-03-24 16:04:10 +01:00
final String defaultHostname = "localhost";
String hostnameServer = "";
2020-03-24 17:32:33 +01:00
int portServer = 0;
2020-03-24 16:04:10 +01:00
String hostnameTracker = "";
2020-03-24 17:32:33 +01:00
int portTracker = 0;
2020-03-24 16:04:10 +01:00
String protocolClient = "";
Scanner scanner = new Scanner(System.in);
2020-03-24 17:32:33 +01:00
final ServerPortRange serverPortRange = new ServerPortRange();
final TrackerPortRange trackerPortRange = new TrackerPortRange();
2020-03-24 16:04:10 +01:00
if ((args.length != 6) && (args.length != 0)){
2020-03-24 17:32:33 +01:00
ClientP2P.printUsage(serverPortRange, trackerPortRange);
System.exit(1);
2020-03-18 17:46:37 +01:00
}
2020-03-24 17:32:33 +01:00
else if(args.length == 6){
2020-03-24 16:04:10 +01:00
protocolClient = args[1];
hostnameServer = args[2];
2020-03-24 17:32:33 +01:00
portServer = Integer.valueOf(Integer.parseInt(args[3]));
2020-03-24 16:04:10 +01:00
hostnameTracker = args[4];
2020-03-24 17:32:33 +01:00
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);
2020-03-24 16:04:10 +01:00
}
2020-03-24 17:32:33 +01:00
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));
2020-03-20 15:57:09 +01:00
}
2020-03-24 17:32:33 +01:00
System.out.println("enter hostname of tracker (default = localhost): ");
hostnameTracker = scanner.nextLine();
if(hostnameTracker.equals("")){
hostnameTracker = defaultHostname;
System.out.println("tracker default hostname : " + hostnameTracker);
2020-03-24 16:04:10 +01:00
}
2020-03-24 17:32:33 +01:00
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));
2020-03-24 16:04:10 +01:00
}
2020-03-24 17:32:33 +01:00
}
2020-03-24 16:04:10 +01:00
2020-03-24 17:32:33 +01:00
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();
}
2020-03-24 16:04:10 +01:00
2020-03-24 17:32:33 +01:00
ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker);
2020-03-24 16:04:10 +01:00
2020-03-24 17:32:33 +01:00
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();
2020-03-18 17:46:37 +01:00
2020-03-24 17:32:33 +01:00
// 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();
}
2020-03-18 18:31:15 +01:00
2020-03-24 17:32:33 +01:00
Thread tclient;
switch(protocolClient){
case "UDP":
case "udp":
case "upd": // to avoid users typos
case "2" :
System.out.println("Starting with UDP");
2020-03-27 16:22:42 +01:00
ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server);
2020-03-24 17:32:33 +01:00
tclient = new Thread(cmudp);
break;
case "TCP":
case "tcp":
case "1":
default:
System.out.println("Starting with TCP");
2020-03-27 16:22:42 +01:00
ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server);
2020-03-24 17:32:33 +01:00
tclient = new Thread(cmtcp);
break;
2020-03-20 15:57:09 +01:00
}
2020-03-24 17:32:33 +01:00
tclient.setName("client P2P-JAVA-PROJECT");
tclient.start();
try {
tclient.join();
} catch (InterruptedException e) {}
smudp.setStop();
smtcp.setStop();
2019-12-12 11:57:02 +01:00
}
}