Merging client and server

pull/31/head
Flavien Haas 5 years ago committed by Louis
parent 87013ceea8
commit cb86478764

@ -1,32 +1,73 @@
package clientP2P;
import clientP2P.ClientManagementUDP;
import clientP2P.ClientManagementTCP;
import serverP2P.ServerManagementUDP;
import serverP2P.ServerManagementTCP;
import tools.Directories;
import tools.Logger;
import tools.LogLevel;
import tools.Directories;
import java.util.Scanner;
import java.util.List;
import tools.HostItem;
import tools.HostList;
import java.lang.NumberFormatException;
public class ClientP2P {
static private final String subdir = "seeded/";
static private String parts = ".parts";
private Logger logger;
private String host;
private int port;
private Directories directories;
private List<HostItem> hostList;
private String parts = ".parts";
public ClientP2P() {
directories = new Directories("P2P_JAVA_PROJECT_CLIENT");
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);
host = "localhost";
port = 40001;
System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directories.getDataHomeDirectory());
directories.askOpenDataHomeDirectory(null);
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();
}
public static void main(String [] args) {
ClientP2P c = new ClientP2P();
System.out.println("Which transport protocol do you want to use? [TCP/udp]");
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;

@ -30,7 +30,7 @@ public class Logger {
/** Appends log to filelog and print to stderr.
* @param text Text to log
*/
private void write(String text, LogLevel logLevel) {
public void write(String text, LogLevel logLevel) {
String msg = "[" + new Timestamp(System.currentTimeMillis()) + "] " + text + "\n";
String level = null;
switch (logLevel) {
@ -109,7 +109,7 @@ public class Logger {
public void writeUDP(String text, LogLevel logLevel) {
write("[UDP] " + text, logLevel);
}
/** Appends log to filelog and print to stderr.
* Adds [UDP] in log line.
* @param text Text to log

Loading…
Cancel
Save