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.Logger;
|
|
|
|
import tools.LogLevel;
|
2020-01-16 12:35:06 +01:00
|
|
|
import tools.Directories;
|
2020-03-11 20:19:18 +01:00
|
|
|
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-20 11:27:57 +01:00
|
|
|
private String subdir = "seeded/";
|
|
|
|
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;
|
2020-03-11 20:19:18 +01:00
|
|
|
private List<HostItem> hostList;
|
2020-03-18 17:46:37 +01:00
|
|
|
private static final int defaultPort = 20000;
|
2020-03-20 00:11:13 +01:00
|
|
|
private HostItem tracker;
|
2020-03-18 17:46:37 +01:00
|
|
|
|
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) {
|
2020-03-20 00:11:13 +01:00
|
|
|
tracker = new HostItem("localhost", 30000); // TODO : make it configurable
|
2020-03-18 17:46:37 +01:00
|
|
|
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);
|
2020-03-11 20:19:18 +01:00
|
|
|
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);
|
2020-03-11 20:19:18 +01:00
|
|
|
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-20 15:57:09 +01:00
|
|
|
|
|
|
|
if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){
|
|
|
|
System.out.println("usage : java -ea clientP2P.ClientP2P -- <PORT> ");
|
2020-03-18 17:46:37 +01:00
|
|
|
}
|
2020-03-20 15:57:09 +01:00
|
|
|
else{
|
|
|
|
ClientP2P c;
|
|
|
|
try {
|
|
|
|
c = new ClientP2P(args[1]);
|
|
|
|
} catch (IndexOutOfBoundsException e){
|
|
|
|
c = new ClientP2P("" + defaultPort);
|
|
|
|
}
|
2020-03-18 17:46:37 +01:00
|
|
|
|
2020-03-20 15:57:09 +01:00
|
|
|
// Server threads
|
2020-03-20 11:27:57 +01:00
|
|
|
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + c.subdir, c.port, c.loggerServer, c.tracker);
|
|
|
|
ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + c.subdir, c.port, c.loggerServer, c.tracker);
|
2020-03-20 15:57:09 +01:00
|
|
|
Thread tudp = new Thread(smudp);
|
|
|
|
tudp.setName("server UDP P2P-JAVA-PROJECT (port: " + c.port + ")");
|
|
|
|
tudp.start();
|
|
|
|
Thread ttcp = new Thread(smtcp);
|
|
|
|
ttcp.setName("server TCP P2P-JAVA-PROJECT (port: " + c.port + ")");
|
|
|
|
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(100);
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
}
|
2020-03-18 18:31:15 +01:00
|
|
|
|
2020-03-20 15:57:09 +01:00
|
|
|
// initialize Host lists
|
|
|
|
c.hostList = HostList.getServList();
|
|
|
|
System.out.println("Client : Which transport protocol do you want to use? [TCP/udp]");
|
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
String transportchoosen = sc.nextLine();
|
2020-03-20 11:27:57 +01:00
|
|
|
sc.close();
|
2020-03-20 15:57:09 +01:00
|
|
|
Thread t;
|
|
|
|
switch(transportchoosen){
|
|
|
|
case "UDP":
|
|
|
|
case "udp":
|
|
|
|
case "upd": // alias typo
|
|
|
|
case "2" :
|
|
|
|
System.out.println("Starting with UDP");
|
|
|
|
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:
|
|
|
|
System.out.println("Starting with TCP");
|
|
|
|
ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.hostList, c.directories.getDataHomeDirectory() + c.parts + "/", c.loggerClient);
|
|
|
|
t = new Thread(cmtcp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t.setName("client P2P-JAVA-PROJECT");
|
|
|
|
t.start();
|
|
|
|
}
|
2019-12-12 11:57:02 +01:00
|
|
|
}
|
|
|
|
}
|