Projet_JAVA_P2P_STRI2A/src/clientP2P/ClientP2P.java

97 lines
3.2 KiB
Java
Raw Normal View History

2020-01-14 11:10:11 +01:00
package clientP2P;
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 java.util.Scanner;
import java.util.List;
import tools.HostItem;
import tools.HostList;
2020-03-18 17:46:37 +01:00
import java.lang.NumberFormatException;
2019-12-12 11:57:02 +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";
private Logger logger;
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;
public void initLogger() {
if (directories == null && logger == null) {
directories = new Directories("P2P_JAVA_PROJECT" + port);
logger = new Logger(directories.getDataHomeDirectory() + "server.log");
}
}
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);
logger.write("incorrect port " + oldPort + " using default port " + defaultPort, LogLevel.Info);
}
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");
hostList = HostList.getServList();
2020-01-12 23:29:49 +01:00
}
2020-01-14 11:10:11 +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);
}
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + subdir, c.port, c.logger);
ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + subdir, c.port, c.logger);
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();
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-12 17:52:31 +01:00
ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.hostList, c.directories.getDataHomeDirectory() + c.parts + "/");
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-18 16:26:21 +01:00
ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.hostList, c.directories.getDataHomeDirectory() + c.parts + "/");
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
}
}