From 039c3ff2b08d29e9b1b2a00e55f775527fab8588 Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Thu, 2 Apr 2020 15:58:10 +0200 Subject: [PATCH] Fix #17 (#104) Fix #17 --- src/clientP2P/ClientDownload.java | 25 +++++++++++++++++++++++-- src/clientP2P/ClientDownloadPart.java | 6 +++--- src/clientP2P/ClientP2P.java | 5 +++-- src/serverP2P/ServerP2P.java | 3 ++- src/tools/LogLevel.java | 3 ++- src/tools/Logger.java | 19 +++++++++++++++---- src/tracker/Tracker.java | 4 ++-- 7 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/clientP2P/ClientDownload.java b/src/clientP2P/ClientDownload.java index 282a4d7..95e4998 100644 --- a/src/clientP2P/ClientDownload.java +++ b/src/clientP2P/ClientDownload.java @@ -132,7 +132,7 @@ public abstract class ClientDownload extends ServeErrors implements Runnable { try { sockList.get(rand.nextInt(sockList.size())).assignTask(offset); offsetsPending.add(offset); - writeLog("Assigned task: #"+ offset, LogLevel.Info); + writeLog("Assigned task: #"+ offset, LogLevel.Debug); } catch(InterruptedException e) { writeLog(e, LogLevel.Error); throw new InternalError(); @@ -180,7 +180,7 @@ public abstract class ClientDownload extends ServeErrors implements Runnable { } ratioUpdater.put(c.getServer(), c.getReceivedBytesCount()); } - writeLog("Task check status: " + offsetsToAsk.size() + " to asks, " + offsetsPending.size() + " pending", LogLevel.Info); + writeLog("Task check status: " + offsetsToAsk.size() + " to asks, " + offsetsPending.size() + " pending", LogLevel.Debug); if (offsetsToAsk.isEmpty() && offsetsPending.isEmpty()) { stop = true; } @@ -380,6 +380,7 @@ public abstract class ClientDownload extends ServeErrors implements Runnable { while(!stop) { assignTasks(); checkTasksStatus(); + printProgression(); } writeLog("Reassembling file parts.", LogLevel.Info); reassembleFile(); @@ -390,6 +391,26 @@ public abstract class ClientDownload extends ServeErrors implements Runnable { } } + /** Print progression of the download. + */ + protected void printProgression() { + long progression = 0; + for(Long dl: ratioUpdater.values()) { + progression += dl.longValue(); + } + double percent = 100.0*progression/size; + String bar = "["; + for(int i=0;i<=100;i+=10) { + if (i>percent) { + bar += "-"; + } else { + bar += "="; + } + } + bar += "]"; + writeLog("[[" + filename + "]] " + bar + " " + String.format("%.2f", percent) + "%", LogLevel.Progression); + } + /** Initialize infos about file to download (size, hash512sum, partslist to dl). * Also download first partfile (to get size). * @throws InternalError diff --git a/src/clientP2P/ClientDownloadPart.java b/src/clientP2P/ClientDownloadPart.java index e9cd602..b3c66c0 100644 --- a/src/clientP2P/ClientDownloadPart.java +++ b/src/clientP2P/ClientDownloadPart.java @@ -113,13 +113,13 @@ public abstract class ClientDownloadPart extends ServeErrors implements Runnable try { doTasks(); synchronized(manager) { - manager.notify(); + manager.notifyAll(); } } catch(InterruptedException e) { try { setStop(); synchronized(manager) { - manager.notify(); + manager.notifyAll(); } } catch (InterruptedException e2) { } @@ -224,7 +224,7 @@ public abstract class ClientDownloadPart extends ServeErrors implements Runnable * @return ProtocolP2PPacketTCP used to send request */ protected ProtocolP2PPacket reqPart(Long offset) { - writeLog("New request: #" + offset, LogLevel.Info); + writeLog("New request: #" + offset, LogLevel.Debug); // maintain tracking of tasks if (toDoTasks.contains(offset)) { try { diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 3ce9879..1e4a210 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -29,6 +29,7 @@ public class ClientP2P { private HostItem tracker; private HostItem server; private Scanner scanner; + final static boolean DEBUG = false; /** Initialize loggers if directories and logger are null, * else fail silently. @@ -37,8 +38,8 @@ public class ClientP2P { if (directories == null && loggerServer == null && loggerClient == null) { directories = new Directories("P2P_JAVA_PROJECT_" + server.getPort()); directories.createSubdir(logDir); - loggerServer = new Logger(directories.getDataHomeDirectory() + logDir + "server.log"); - loggerClient = new Logger(directories.getDataHomeDirectory() + logDir + "client.log"); + loggerServer = new Logger(directories.getDataHomeDirectory() + logDir + "server.log", DEBUG); + loggerClient = new Logger(directories.getDataHomeDirectory() + logDir + "client.log", DEBUG); directories.createSubdir(partsDir); } } diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 90b7451..bb65331 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -23,6 +23,7 @@ public class ServerP2P { private Logger logger; private HostItem tracker; private HostItem server; + final static boolean DEBUG = true; /** Constructor with portServerstr containing a port number. * @param hostnameServer binded hostname @@ -37,7 +38,7 @@ public class ServerP2P { tracker = new HostItem(hostnameTracker, portTracker); directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + portServer); directories.createSubdir(subdir); - logger = new Logger(directories.getDataHomeDirectory() + "server.log"); + logger = new Logger(directories.getDataHomeDirectory() + "server.log", DEBUG); System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory() + subdir); if (isInteractive) { directories.askOpenDataHomeDirectory(subdir, scanner); diff --git a/src/tools/LogLevel.java b/src/tools/LogLevel.java index 4bc7169..6b6b16c 100644 --- a/src/tools/LogLevel.java +++ b/src/tools/LogLevel.java @@ -10,5 +10,6 @@ public enum LogLevel { Info, Warning, Action, - Debug + Debug, + Progression } diff --git a/src/tools/Logger.java b/src/tools/Logger.java index b08f15a..16dda46 100644 --- a/src/tools/Logger.java +++ b/src/tools/Logger.java @@ -16,13 +16,16 @@ import java.sql.Timestamp; */ public class Logger { private Path logFile; + private boolean debugMode; /** Constructor with logFile. * @param logFile name of the file to store logs. + * @param debugMode Enable printing of debug level */ - public Logger(String logFile) { + public Logger(String logFile, boolean debugMode) { assert logFile != null : "Logfile name is null"; this.logFile = Paths.get(logFile); + this.debugMode = debugMode; try { this.logFile.toFile().createNewFile(); } catch (IOException e) { @@ -57,6 +60,9 @@ public class Logger { level = "[Debug]"; colorize = "\u001B[36m"; // CYAN break; + case Progression: + level = "[Progression]"; + break; default: System.err.println("Error: incorrect logLevel"); } @@ -64,18 +70,23 @@ public class Logger { msg = level + " " + msg; } switch (logLevel) { + case Action: + break; + case Debug: case Error: case Info: case Warning: - case Debug: + case Progression: default: + if (logLevel == LogLevel.Debug && !debugMode) { + break; + } System.err.println(colorize + text + "\u001B[0m"); break; - case Action: - break; } switch (logLevel) { case Debug: + case Progression: break; case Error: case Info: diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index ba8ff4a..0c17c10 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -20,7 +20,7 @@ public class Tracker { private HostItem tracker; private Directories directories; private Logger logger; - + final static boolean DEBUG = true; /** Constructor with portStr containing a port number. * @param hostname hostname to bind * @param port port to bind @@ -29,7 +29,7 @@ public class Tracker { public Tracker(String hostname, int port, boolean isInteractive) { tracker = new HostItem(hostname, port); directories = new Directories("P2P_JAVA_PROJECT_TRACKER_" + port); - logger = new Logger(directories.getDataHomeDirectory() + "tracker.log"); + logger = new Logger(directories.getDataHomeDirectory() + "tracker.log", DEBUG); System.out.println("Tracker will listen on port " + port + " and write logs into " + directories.getDataHomeDirectory()); Scanner scanner = new Scanner(System.in); if (isInteractive) {