From 58add14fa9cfaa0214acc6e93a1ad0f49d604a20 Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Thu, 2 Apr 2020 16:36:04 +0200 Subject: [PATCH] Print progress bar less frequently --- src/clientP2P/ClientDownload.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/clientP2P/ClientDownload.java b/src/clientP2P/ClientDownload.java index 95e4998..3847cab 100644 --- a/src/clientP2P/ClientDownload.java +++ b/src/clientP2P/ClientDownload.java @@ -46,6 +46,7 @@ import tools.ServeErrors; import java.net.SocketException; import java.net.UnknownHostException; import java.io.IOException; +import java.time.Instant; /** Class to download file * @author Louis Royer @@ -70,6 +71,10 @@ public abstract class ClientDownload extends ServeErrors implements Runnable { protected Logger logger; protected HostItem client; protected HostItem tracker; + protected int lastPercentProgress; + protected Instant lastTimeProgress; + final static int DIFF_TIME_PROGRESS_MILLIS = 2000; + final static int DIFF_PERCENT_PROGRESS = 5; /** Constructor with parameters: filename, list of hosts, parts subdirectory and dirStorage * @param filename name of file to download @@ -89,6 +94,8 @@ public abstract class ClientDownload extends ServeErrors implements Runnable { this.client = client; this.tracker = tracker; this.stop = false; + lastPercentProgress = - DIFF_PERCENT_PROGRESS; + lastTimeProgress = Instant.now().minusMillis(DIFF_TIME_PROGRESS_MILLIS); } /** Success getter. @@ -399,6 +406,10 @@ public abstract class ClientDownload extends ServeErrors implements Runnable { progression += dl.longValue(); } double percent = 100.0*progression/size; + if (percent != 100 && percent < lastPercentProgress + DIFF_PERCENT_PROGRESS + && lastTimeProgress.plusMillis(DIFF_TIME_PROGRESS_MILLIS).isAfter(Instant.now())) { + return; + } String bar = "["; for(int i=0;i<=100;i+=10) { if (i>percent) { @@ -408,7 +419,9 @@ public abstract class ClientDownload extends ServeErrors implements Runnable { } } bar += "]"; - writeLog("[[" + filename + "]] " + bar + " " + String.format("%.2f", percent) + "%", LogLevel.Progression); + writeLog("[[" + filename + "]] " + bar + " " + String.format("%.2f", percent) + "%", LogLevel.Progression); + lastPercentProgress = (int)percent; + lastTimeProgress = Instant.now(); } /** Initialize infos about file to download (size, hash512sum, partslist to dl).