From cb86478764a2e5c5a5d97a6a4cf1e40e7523661b Mon Sep 17 00:00:00 2001 From: flavien Date: Wed, 18 Mar 2020 17:46:37 +0100 Subject: [PATCH] Merging client and server --- src/clientP2P/ClientP2P.java | 57 +++++++++++++++++++++++++++++++----- src/tools/Logger.java | 4 +-- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 24050d3..c6f8b63 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -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 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; diff --git a/src/tools/Logger.java b/src/tools/Logger.java index 047c4b8..63c2107 100644 --- a/src/tools/Logger.java +++ b/src/tools/Logger.java @@ -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