Fix #17 (#104)
All checks were successful
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/head This commit looks good

Fix #17
This commit is contained in:
Louis Royer 2020-04-02 15:58:10 +02:00
parent 4928efa947
commit 039c3ff2b0
7 changed files with 50 additions and 15 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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);

View File

@ -10,5 +10,6 @@ public enum LogLevel {
Info,
Warning,
Action,
Debug
Debug,
Progression
}

View File

@ -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:

View File

@ -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) {