From 613766ee46ce1fb1f6377d13777ff839c38fdb65 Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Mon, 13 Apr 2020 20:46:45 +0200 Subject: [PATCH 1/3] Add a way to use with nat --- README.md | 1 + src/clientP2P/ClientP2P.java | 55 +++++++++++++++++++------- src/serverP2P/ServerManagement.java | 13 +++++- src/serverP2P/ServerManagementTCP.java | 33 ++++++++++------ src/serverP2P/ServerManagementUDP.java | 32 ++++++++++----- 5 files changed, 97 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 77c7d57..c00993b 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Documentation of the protocol used can be found in [doc/protocol.md](doc/protoco ### Application CLI (Client+Server) - Interactive mode: `java clientP2P.ClientP2P` - CLI Arguments mode: `java clientP2P.ClientP2P -- ` (default tracker port 6969 (range 7000 -> 7999) and server port: server 7070 (range 7000->7070)) +- CLI Arguments mode when using with a nat: `java clientP2P.ClientP2P -- ` (default tracker port 6969 (range 7000 -> 7999) and server port: server 7070 (range 7000->7070)) ### Application GUI (Client+Server) - `java gui.Gui` diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 330b2f4..8c4d1da 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -29,6 +29,7 @@ public class ClientP2P { private Directories directories; private HostItem tracker; private HostItem server; + private HostItem serverBinding; private Scanner scanner; final static boolean DEBUG = false; @@ -46,14 +47,17 @@ public class ClientP2P { } /** Constructor. - * @param hostnameServer hostname to bind - * @param portServer port to bind + * @param hostnameServer hostname used to register + * @param portServer port used to register + * @param hostnameServerBinding hostname to bind + * @param portServerBinding port to bind * @param hostnameTracker hostname of tracker * @param portTracker port of tracker */ - public ClientP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker) { + public ClientP2P(String hostnameServer, int portServer, String hostnameServerBinding, int portServerBinding, String hostnameTracker, int portTracker) { scanner = new Scanner(System.in); server = new HostItem(hostnameServer, portServer); + serverBinding = new HostItem(hostnameServerBinding, portServerBinding); tracker = new HostItem(hostnameTracker, portTracker); initDirectoriesAndLoggers(); System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory()); @@ -72,6 +76,12 @@ public class ClientP2P { " " + " " + " "); + System.out.println("or"); + System.out.println("\tjava clientP2P.ClientP2P -- " + + " " + + " " + + " " + + " "); System.out.println("(" + trackerPortRange + " and " + serverPortRange +")"); } @@ -82,6 +92,8 @@ public class ClientP2P { */ public static void main(String [] args) { final String defaultHostname = "localhost"; + String hostnameServBind = ""; + int portServBind = 0; String hostnameServer = ""; int portServer = 0; String hostnameTracker = ""; @@ -94,13 +106,22 @@ public class ClientP2P { if ((args.length != 6) && (args.length != 0)) { ClientP2P.printUsage(serverPortRange, trackerPortRange); System.exit(1); - } - else if (args.length == 6) { - protocolClient = args[1]; - hostnameServer = args[2]; - portServer = Integer.valueOf(Integer.parseInt(args[3])); - hostnameTracker = args[4]; - portTracker = Integer.valueOf(Integer.parseInt(args[5])); + } else if (args.length == 6) { + protocolClient = args[1]; + hostnameServer = args[2]; + portServer = Integer.valueOf(Integer.parseInt(args[3])); + hostnameTracker = args[4]; + portTracker = Integer.valueOf(Integer.parseInt(args[5])); + hostnameServBind = hostnameServer; + portServBind = portServer; + } else if (args.length == 8) { + protocolClient = args[1]; + hostnameServer = args[2]; + portServer = Integer.valueOf(Integer.parseInt(args[3])); + hostnameTracker = args[4]; + portTracker = Integer.valueOf(Integer.parseInt(args[5])); + hostnameServBind = args[6]; + portServBind = Integer.valueOf(Integer.parseInt(args[7])); } else { System.out.println("Client, wich transport protocol do you want to use (default = TCP): "); protocolClient = scanner.nextLine(); @@ -132,19 +153,22 @@ public class ClientP2P { } else { portTracker = Integer.valueOf(Integer.parseInt(portTrackerStr)); } + hostnameServBind = hostnameServer; + portServBind = portServer; } boolean serverInRange = serverPortRange.isPortInRange(portServer); + boolean serverBindInRange = serverPortRange.isPortInRange(portServBind); boolean trackerInRange = trackerPortRange.isPortInRange(portTracker); - if (serverInRange && trackerInRange) { + if (serverInRange && trackerInRange & serverBindInRange) { System.out.println("using hostname : " + hostnameServer); System.out.println("using port : " + portServer); System.out.println("tracker hostname : " + hostnameTracker); System.out.println("tracker port : " + portTracker); - ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker); - ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); - ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); + ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameServBind, portServBind, hostnameTracker, portTracker); + ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.serverBinding, c.server, c.tracker, c.loggerServer); + ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), c.serverBinding, c.server, c.tracker, c.loggerServer); Thread tudp = new Thread(smudp); tudp.setName("server UDP P2P-JAVA-PROJECT"); tudp.start(); @@ -193,6 +217,9 @@ public class ClientP2P { if (!trackerInRange) { System.err.println("TRACKER: Port not in range. " + trackerPortRange); } + if (!serverBindInRange) { + System.err.println("SERVER-BINDING: Port not in range. " + serverPortRange); + } System.exit(2); } } diff --git a/src/serverP2P/ServerManagement.java b/src/serverP2P/ServerManagement.java index 787ea2d..7303953 100644 --- a/src/serverP2P/ServerManagement.java +++ b/src/serverP2P/ServerManagement.java @@ -45,20 +45,29 @@ public abstract class ServerManagement extends ServeErrors implements Runnable { protected Logger logger; protected String baseDirectory; protected HostItem server; + protected HostItem serverBinding; protected HostItem tracker; protected Random punisher = new Random(); protected RatioWatcher ratioWatcher; - /** Constructor */ - public ServerManagement(String baseDirectory, HostItem server, HostItem tracker, Logger logger) { + /** Constructor + * @param baseDirectory the root directory where files are stored + * @param serverBinding ther server will bind on this hostItem + * @param server the server hostItem used to register on tracker + * @param logger Logger item + * @param tracker Tracker + */ + public ServerManagement(String baseDirectory, HostItem serverBinding, HostItem server, HostItem tracker, Logger logger) { super(); assert baseDirectory != null : "baseDirectory is null"; + assert serverBinding != null : "serverBinding is null"; assert server != null : "server is null"; assert tracker != null : "tracker is null"; assert logger != null : "logger is null"; stop = false; this.baseDirectory = baseDirectory; this.server = server; + this.serverBinding = serverBinding; this.tracker = tracker; this.logger = logger; } diff --git a/src/serverP2P/ServerManagementTCP.java b/src/serverP2P/ServerManagementTCP.java index 55f81e0..33d2f02 100644 --- a/src/serverP2P/ServerManagementTCP.java +++ b/src/serverP2P/ServerManagementTCP.java @@ -47,26 +47,37 @@ import serverP2P.FileWatcherTCP; * @version 1.0 */ public class ServerManagementTCP extends ServerManagement { - private ServerSocket socket; - /** Constructor for TCP implementation, with baseDirectory and TCPPort parameters. + /** Constructor for TCP implementation. * @param baseDirectory the root directory where files are stored - * @param hostName the server will bind on this address - * @param TCPPort the server will listen on this port + * @param serverBinding ther server will bind on this hostItem + * @param server the server hostItem used to register on tracker + * @param logger Logger item + * @param tracker Tracker + */ + public ServerManagementTCP(String baseDirectory, HostItem serverBinding, HostItem server, HostItem tracker, Logger logger) { + super(baseDirectory, serverBinding, server, tracker, logger); + initSocket(); + } + + /** Constructor for TCP implementation. + * @param baseDirectory the root directory where files are stored + * @param server the server hostItem used to register on tracker * @param logger Logger item * @param tracker Tracker */ public ServerManagementTCP(String baseDirectory, HostItem server, HostItem tracker, Logger logger) { - super(baseDirectory, server, tracker, logger); - assert baseDirectory != null : "baseDirectory is null"; - assert server != null : "server is null"; - assert tracker != null : "tracker is null"; - assert logger != null : "logger is null"; + super(baseDirectory, server, server, tracker, logger); + initSocket(); + } + + /** Initialize socket */ + private void initSocket() { try { - socket = new ServerSocket(server.getPort(), 10, server.getInetAddress()); + socket = new ServerSocket(serverBinding.getPort(), 10, serverBinding.getInetAddress()); } catch (SocketException e) { - writeLog("Error: cannot listen on " + server, LogLevel.Error); + writeLog("Error: cannot listen on " + serverBinding, LogLevel.Error); System.exit(-1); } catch (IOException e) { writeLog("Error: cannot openning socket", LogLevel.Error); diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index c9c3d7b..578de84 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -47,23 +47,35 @@ public class ServerManagementUDP extends ServerManagement { private DatagramSocket socket; - /** Constructor for UDP implementation, with baseDirectory and UDPPort parameters. + /** Constructor for UDP implementation. * @param baseDirectory the root directory where files are stored - * @param hostName the server will bind on this address - * @param port the server will listen on this port + * @param server the server hostItem used to register on tracker * @param logger Logger item * @param tracker Tracker */ public ServerManagementUDP(String baseDirectory, HostItem server, HostItem tracker, Logger logger) { - super(baseDirectory, server, tracker, logger); - assert baseDirectory != null : "baseDirectory is null"; - assert server != null : "server is null"; - assert tracker != null : "tracker is null"; - assert logger != null : "logger is null"; + super(baseDirectory, server, server, tracker, logger); + initSocket(); + } + + /** Constructor for UDP implementation. + * @param baseDirectory the root directory where files are stored + * @param serverBinding ther server will bind on this hostItem + * @param server the server hostItem used to register on tracker + * @param logger Logger item + * @param tracker Tracker + */ + public ServerManagementUDP(String baseDirectory, HostItem serverBinding, HostItem server, HostItem tracker, Logger logger) { + super(baseDirectory, serverBinding, server, tracker, logger); + initSocket(); + } + + /** Initialize socket */ + private void initSocket() { try { - socket = new DatagramSocket(server.getPort(), server.getInetAddress()); + socket = new DatagramSocket(serverBinding.getPort(), serverBinding.getInetAddress()); } catch (SocketException e) { - logger.writeUDP("Error: cannot listen on " + server, LogLevel.Error); + logger.writeUDP("Error: cannot listen on " + serverBinding, LogLevel.Error); System.exit(-1); } } -- 2.30.2 From f8ab972f3484b614c0413744014faf832322e38b Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Mon, 13 Apr 2020 22:17:11 +0200 Subject: [PATCH 2/3] Clearer denomination for public versus binding server params --- README.md | 2 +- src/clientP2P/ClientP2P.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c00993b..bcfccf8 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Documentation of the protocol used can be found in [doc/protocol.md](doc/protoco ### Application CLI (Client+Server) - Interactive mode: `java clientP2P.ClientP2P` - CLI Arguments mode: `java clientP2P.ClientP2P -- ` (default tracker port 6969 (range 7000 -> 7999) and server port: server 7070 (range 7000->7070)) -- CLI Arguments mode when using with a nat: `java clientP2P.ClientP2P -- ` (default tracker port 6969 (range 7000 -> 7999) and server port: server 7070 (range 7000->7070)) +- CLI Arguments mode when using with a nat: `java clientP2P.ClientP2P -- ` (default tracker port 6969 (range 7000 -> 7999) and server port: server 7070 (range 7000->7070)) ### Application GUI (Client+Server) - `java gui.Gui` diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 8c4d1da..b339a0d 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -79,9 +79,9 @@ public class ClientP2P { System.out.println("or"); System.out.println("\tjava clientP2P.ClientP2P -- " + " " + - " " + + " " + " " + - " "); + " "); System.out.println("(" + trackerPortRange + " and " + serverPortRange +")"); } -- 2.30.2 From a9e22d5fae6bd513bd4d744e2f36688f107a5032 Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Mon, 13 Apr 2020 22:29:52 +0200 Subject: [PATCH 3/3] Fix number of args --- src/clientP2P/ClientP2P.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index b339a0d..801b284 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -103,7 +103,7 @@ public class ClientP2P { final ServerPortRange serverPortRange = new ServerPortRange(); final TrackerPortRange trackerPortRange = new TrackerPortRange(); - if ((args.length != 6) && (args.length != 0)) { + if ((args.length != 6) && (args.length != 0) && (args.length != 8)) { ClientP2P.printUsage(serverPortRange, trackerPortRange); System.exit(1); } else if (args.length == 6) { -- 2.30.2