Projet_JAVA_P2P_STRI2A/src/clientP2P/ClientP2P.java

128 lines
4.2 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-18 17:46:37 +01:00
import serverP2P.ServerManagementUDP;
import serverP2P.ServerManagementTCP;
import tools.Directories;
import tools.Logger;
import tools.LogLevel;
2020-01-16 12:35:06 +01:00
import tools.Directories;
import tools.HostItem;
import tools.HostList;
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 {
2020-03-18 17:46:37 +01:00
static private final String subdir = "seeded/";
static private String parts = ".parts";
2020-03-19 17:49:39 +01:00
private Logger loggerServer;
private Logger loggerClient;
2020-01-12 23:29:49 +01:00
private String host;
private int port;
2020-01-21 19:56:50 +01:00
private Directories directories;
private List<HostItem> hostList;
2020-03-18 17:46:37 +01:00
private static final int defaultPort = 20000;
2020-03-19 13:30:49 +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.
*/
2020-03-18 17:46:37 +01:00
public void initLogger() {
2020-03-19 17:49:39 +01:00
if (directories == null && loggerServer == null && loggerClient == null) {
directories = new Directories("P2P_JAVA_PROJECT_" + port);
loggerServer = new Logger(directories.getDataHomeDirectory() + "server.log");
loggerClient = new Logger(directories.getDataHomeDirectory() + "client.log");
2020-03-18 17:46:37 +01:00
}
}
2020-03-19 13:30:49 +01:00
/** Constructor with portStr as parameter.
* @param portStr String containing port for server listenning.
*/
2020-03-18 17:46:37 +01:00
public ClientP2P(String portStr) {
try{
port = Integer.valueOf(Integer.parseInt(portStr));
} catch (NumberFormatException e){
int oldPort = port;
port = defaultPort;
initLogger();
System.err.println("Error incorrect port " + oldPort + " using default port " + defaultPort);
2020-03-19 17:49:39 +01:00
loggerServer.write("incorrect port " + oldPort + " using default port " + defaultPort, LogLevel.Info);
2020-03-18 17:46:37 +01:00
}
initLogger();
directories.createSubdir(subdir);
directories.createSubdir(parts);
2020-01-12 23:29:49 +01:00
host = "localhost";
2020-03-18 17:46:37 +01:00
System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory() + subdir);
directories.askOpenDataHomeDirectory(subdir);
System.out.println("Please enter list of servers to use; first one will be used to ask list of files");
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-18 17:46:37 +01:00
ClientP2P c;
try {
c = new ClientP2P(args[1]);
} catch (IndexOutOfBoundsException e){
c = new ClientP2P("" + defaultPort);
}
2020-03-19 13:30:49 +01:00
// Server threads
2020-03-19 17:49:39 +01:00
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + subdir, c.port, c.loggerServer);
ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + subdir, c.port, c.loggerServer);
2020-03-18 17:46:37 +01:00
Thread tudp = new Thread(smudp);
2020-03-19 13:30:49 +01:00
tudp.setName("server UDP P2P-JAVA-PROJECT (port: " + c.port + ")");
2020-03-18 17:46:37 +01:00
tudp.start();
Thread ttcp = new Thread(smtcp);
2020-03-19 13:30:49 +01:00
ttcp.setName("server TCP P2P-JAVA-PROJECT (port: " + c.port + ")");
2020-03-18 17:46:37 +01:00
ttcp.start();
2020-03-19 13:30:49 +01:00
// Wait a bit before printing client interface
// This is not required, but allow to have a cleaner interface
2020-03-18 18:31:15 +01:00
try {
Thread.sleep(100);
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
2020-03-19 13:30:49 +01:00
// initialize Host lists
2020-03-18 18:31:15 +01:00
c.hostList = HostList.getServList();
2020-03-18 17:46:37 +01:00
System.out.println("Client : Which transport protocol do you want to use? [TCP/udp]");
Scanner sc = new Scanner(System.in);
String transportchoosen = sc.nextLine();
Thread t;
switch(transportchoosen){
case "UDP":
case "udp":
2020-03-16 15:56:47 +01:00
case "upd": // alias typo
case "2" :
2020-02-29 16:57:19 +01:00
System.out.println("Starting with UDP");
2020-03-19 17:49:39 +01:00
ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.hostList, c.directories.getDataHomeDirectory() + c.parts + "/", c.loggerClient);
t = new Thread(cmudp);
break;
case "TCP":
case "tcp":
case "1":
default:
2020-02-29 16:57:19 +01:00
System.out.println("Starting with TCP");
2020-03-19 17:49:39 +01:00
ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.hostList, c.directories.getDataHomeDirectory() + c.parts + "/", c.loggerClient);
2020-02-29 16:57:19 +01:00
t = new Thread(cmtcp);
break;
}
2020-01-12 23:29:49 +01:00
t.setName("client P2P-JAVA-PROJECT");
t.start();
2019-12-12 11:57:02 +01:00
}
}