From 977ff2230057e405cc7937368c758569cffb7a30 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 19 Mar 2020 13:30:49 +0100 Subject: [PATCH] Add missing javadoc This closes #36 --- src/clientP2P/ClientDownloadPartTCP.java | 10 ++++++++- src/clientP2P/ClientDownloadPartUDP.java | 10 ++++++++- src/clientP2P/ClientDownloadTCP.java | 3 +++ src/clientP2P/ClientDownloadUDP.java | 3 +++ src/clientP2P/ClientManagementTCP.java | 2 -- src/clientP2P/ClientManagementUDP.java | 2 -- src/clientP2P/ClientP2P.java | 27 +++++++++++++++++++++--- src/serverP2P/ServerP2P.java | 22 ++++++++++++++----- 8 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/clientP2P/ClientDownloadPartTCP.java b/src/clientP2P/ClientDownloadPartTCP.java index 11a277c..6895c83 100644 --- a/src/clientP2P/ClientDownloadPartTCP.java +++ b/src/clientP2P/ClientDownloadPartTCP.java @@ -185,7 +185,11 @@ public class ClientDownloadPartTCP implements Runnable { } } - public ProtocolP2PPacketTCP reqPart(Long offset) { + /** Send a request for a specific offset. + * @param offset Offset of the file part to download + * @return ProtocolP2PPacketTCP used to send request + */ + private ProtocolP2PPacketTCP reqPart(Long offset) { System.err.println("New request: "+ offset); // maintain tracking of tasks if (toDoTasks.contains(offset)) { @@ -226,6 +230,10 @@ public class ClientDownloadPartTCP implements Runnable { } } + /** Download file part associated to the request send (d). + * @param d request packet + * @return true on failure, else false + */ public boolean downloadPart(ProtocolP2PPacketTCP d) { if (d == null) { System.err.println("Error: downloadPart -> d is null."); diff --git a/src/clientP2P/ClientDownloadPartUDP.java b/src/clientP2P/ClientDownloadPartUDP.java index 30c373c..aa85296 100644 --- a/src/clientP2P/ClientDownloadPartUDP.java +++ b/src/clientP2P/ClientDownloadPartUDP.java @@ -180,7 +180,11 @@ public class ClientDownloadPartUDP implements Runnable { } } - public ProtocolP2PPacketUDP reqPart(Long offset) { + /** Send a request for a specific offset. + * @param offset Offset of the file part to download + * @return ProtocolP2PPacketTCP used to send request + */ + private ProtocolP2PPacketUDP reqPart(Long offset) { System.err.println("New request: "+ offset); // maintain tracking of tasks if (toDoTasks.contains(offset)) { @@ -218,6 +222,10 @@ public class ClientDownloadPartUDP implements Runnable { } } + /** Download file part associated to the request send (d). + * @param d request packet + * @return true on failure, else false + */ public boolean downloadPart(ProtocolP2PPacketUDP d) { if (d == null) { System.err.println("Error: downloadPart -> d is null."); diff --git a/src/clientP2P/ClientDownloadTCP.java b/src/clientP2P/ClientDownloadTCP.java index 442d105..b52bbe7 100644 --- a/src/clientP2P/ClientDownloadTCP.java +++ b/src/clientP2P/ClientDownloadTCP.java @@ -352,6 +352,9 @@ public class ClientDownloadTCP implements Runnable { return success; } + /** Reassemble file from file parts. + * Set success to true if file is reassembled successfully. + */ private void reassembleFile() { boolean firstPart = true; boolean abort = false; diff --git a/src/clientP2P/ClientDownloadUDP.java b/src/clientP2P/ClientDownloadUDP.java index 2bd2f29..c6d335d 100644 --- a/src/clientP2P/ClientDownloadUDP.java +++ b/src/clientP2P/ClientDownloadUDP.java @@ -346,6 +346,9 @@ public class ClientDownloadUDP implements Runnable { return success; } + /** Reassemble file from file parts. + * Set success to true if file is reassembled successfully. + */ private void reassembleFile() { boolean firstPart = true; boolean abort = false; diff --git a/src/clientP2P/ClientManagementTCP.java b/src/clientP2P/ClientManagementTCP.java index 66878c7..cdabf6e 100644 --- a/src/clientP2P/ClientManagementTCP.java +++ b/src/clientP2P/ClientManagementTCP.java @@ -12,8 +12,6 @@ import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; import java.net.UnknownHostException; import java.util.Scanner; -//import java.net.InetAddress; -//import java.net.SocketException; import java.io.IOException; import java.nio.file.Files; import java.io.File; diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 2e7c045..c50c001 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -12,9 +12,7 @@ import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; import java.net.UnknownHostException; import java.util.Scanner; -//import java.net.InetAddress; import java.net.DatagramSocket; -//import java.net.SocketException; import java.io.IOException; import java.nio.file.Files; import java.io.File; diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 654829e..f857427 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -11,8 +11,13 @@ import java.util.Scanner; import java.util.List; import tools.HostItem; import tools.HostList; -import java.lang.NumberFormatException; +/** Client + Server implementation. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ public class ClientP2P { static private final String subdir = "seeded/"; static private String parts = ".parts"; @@ -23,6 +28,10 @@ public class ClientP2P { private List hostList; private static final int defaultPort = 20000; + + /** Initialize logger if directories and logger are null, + * else fail silently. + */ public void initLogger() { if (directories == null && logger == null) { directories = new Directories("P2P_JAVA_PROJECT" + port); @@ -30,6 +39,9 @@ public class ClientP2P { } } + /** Constructor with portStr as parameter. + * @param portStr String containing port for server listenning. + */ public ClientP2P(String portStr) { try{ port = Integer.valueOf(Integer.parseInt(portStr)); @@ -49,6 +61,11 @@ public class ClientP2P { System.out.println("Please enter list of servers to use; first one will be used to ask list of files"); } + /** 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 + */ public static void main(String [] args) { ClientP2P c; try { @@ -57,21 +74,25 @@ public class ClientP2P { c = new ClientP2P("" + defaultPort); } + // Server threads 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.setName("server UDP P2P-JAVA-PROJECT (port: " + c.port + ")"); tudp.start(); Thread ttcp = new Thread(smtcp); - ttcp.setName("server TCP P2P-JAVA-PROJECT"); + ttcp.setName("server TCP P2P-JAVA-PROJECT (port: " + c.port + ")"); ttcp.start(); + // 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(); } + // 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); diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 98b7b23..313d263 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -4,13 +4,22 @@ import serverP2P.ServerManagementTCP; import tools.Directories; import tools.Logger; +/** Server only implementation + * First argument of main method is port listened by the server, and is mandatory. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ public class ServerP2P { private int port; private Directories directories; static private final String subdir = "seeded/"; private Logger logger; - + /** Constructor with portStr containing a port number. + * @param portStr String containing port number of listening. + */ public ServerP2P(String portStr) { port = Integer.valueOf(Integer.parseInt(portStr)); directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + port); @@ -19,10 +28,13 @@ public class ServerP2P { System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory() + subdir); directories.askOpenDataHomeDirectory(subdir); } - public static void main(String [] args) { - /* first arg must be port number - * run with: java -ea serverP2P.ServerP2P -- - * */ + + /** Main program entry point + * first parameter is port number and is mandatory + * to test, run with: java -ea serverP2P.ServerP2P -- + * @param args parameters + */ + public static void main(String [] args) { ServerP2P s = new ServerP2P(args[1]); ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger); ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger); -- 2.30.2