Projet_JAVA_P2P_STRI2A/src/clientP2P/ClientP2P.java

184 lines
6.6 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-03-24 16:04:10 +01:00
import javafx.util.Pair;
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 16:04:10 +01:00
import tools.TestPort;
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-03-24 16:04:10 +01:00
private int portServer;
2020-01-21 19:56:50 +01:00
private Directories directories;
private HostItem tracker;
2020-03-21 15:48:18 +01:00
private Scanner scanner;
2020-03-18 17:46:37 +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 16:04:10 +01:00
directories = new Directories("P2P_JAVA_PROJECT_" + portServer);
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 16:04:10 +01:00
/** Constructor with portServerstr as parameter.
* @param portServerstr String containing port for server listenning.
2020-03-19 13:30:49 +01:00
*/
2020-03-24 16:04:10 +01:00
public ClientP2P(String portServerstr, String hostnameTracker, String portTracker) {
2020-03-21 15:48:18 +01:00
scanner = new Scanner(System.in);
2020-03-24 16:04:10 +01:00
portServer = Integer.valueOf(Integer.parseInt(portServerstr));
tracker = new HostItem(hostnameTracker, Integer.valueOf(Integer.parseInt(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-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";
final String defaultPortServer = "7070";
final String defaultPortTracker = "6969";
String hostnameServer = "";
String portServer = "";
String hostnameTracker = "";
String portTracker = "";
String protocolClient = "";
Scanner scanner = new Scanner(System.in);
TestPort testPort = new TestPort();
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))");
2020-03-18 17:46:37 +01:00
}
2020-03-20 15:57:09 +01:00
else{
2020-03-24 16:04:10 +01:00
if(args.length == 6){
protocolClient = args[1];
hostnameServer = args[2];
portServer = args[3];
hostnameTracker = args[4];
portTracker = 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);
}
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);
}
2020-03-20 15:57:09 +01:00
}
2020-03-18 17:46:37 +01:00
2020-03-24 16:04:10 +01:00
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);
}
else {
System.out.println(PtestPortServer.getValue());
portServer = defaultPortServer;
}
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;
}
ClientP2P c = new ClientP2P(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);
2020-03-20 15:57:09 +01:00
Thread tudp = new Thread(smudp);
2020-03-24 16:04:10 +01:00
tudp.setName("server UDP P2P-JAVA-PROJECT");
2020-03-20 15:57:09 +01:00
tudp.start();
Thread ttcp = new Thread(smtcp);
2020-03-24 16:04:10 +01:00
ttcp.setName("server TCP P2P-JAVA-PROJECT");
2020-03-20 15:57:09 +01:00
ttcp.start();
2020-03-18 17:46:37 +01:00
2020-03-20 15:57:09 +01:00
// Wait a bit before printing client interface
// This is not required, but allow to have a cleaner interface
try {
Thread.sleep(200);
2020-03-20 15:57:09 +01:00
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
2020-03-18 18:31:15 +01:00
2020-03-24 16:04:10 +01:00
Thread tclient;
switch(protocolClient){
2020-03-20 15:57:09 +01:00
case "UDP":
case "udp":
2020-03-24 16:04:10 +01:00
case "upd":
2020-03-20 15:57:09 +01:00
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);
2020-03-24 16:04:10 +01:00
tclient = new Thread(cmudp);
2020-03-20 15:57:09 +01:00
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);
2020-03-24 16:04:10 +01:00
tclient = new Thread(cmtcp);
2020-03-20 15:57:09 +01:00
break;
2020-03-24 16:04:10 +01:00
}
tclient.setName("client P2P-JAVA-PROJECT");
tclient.start();
2020-03-21 15:48:18 +01:00
try {
2020-03-24 16:04:10 +01:00
tclient.join();
2020-03-21 15:48:18 +01:00
} catch (InterruptedException e) {}
smudp.setStop();
smtcp.setStop();
2020-03-20 15:57:09 +01:00
}
2019-12-12 11:57:02 +01:00
}
}