Fix #17 #104

Merged
louis_royer merged 1 commits from fix17 into master 5 years ago

@ -132,7 +132,7 @@ public abstract class ClientDownload extends ServeErrors implements Runnable {
try { try {
sockList.get(rand.nextInt(sockList.size())).assignTask(offset); sockList.get(rand.nextInt(sockList.size())).assignTask(offset);
offsetsPending.add(offset); offsetsPending.add(offset);
writeLog("Assigned task: #"+ offset, LogLevel.Info); writeLog("Assigned task: #"+ offset, LogLevel.Debug);
} catch(InterruptedException e) { } catch(InterruptedException e) {
writeLog(e, LogLevel.Error); writeLog(e, LogLevel.Error);
throw new InternalError(); throw new InternalError();
@ -180,7 +180,7 @@ public abstract class ClientDownload extends ServeErrors implements Runnable {
} }
ratioUpdater.put(c.getServer(), c.getReceivedBytesCount()); 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()) { if (offsetsToAsk.isEmpty() && offsetsPending.isEmpty()) {
stop = true; stop = true;
} }
@ -380,6 +380,7 @@ public abstract class ClientDownload extends ServeErrors implements Runnable {
while(!stop) { while(!stop) {
assignTasks(); assignTasks();
checkTasksStatus(); checkTasksStatus();
printProgression();
} }
writeLog("Reassembling file parts.", LogLevel.Info); writeLog("Reassembling file parts.", LogLevel.Info);
reassembleFile(); 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). /** Initialize infos about file to download (size, hash512sum, partslist to dl).
* Also download first partfile (to get size). * Also download first partfile (to get size).
* @throws InternalError * @throws InternalError

@ -113,13 +113,13 @@ public abstract class ClientDownloadPart extends ServeErrors implements Runnable
try { try {
doTasks(); doTasks();
synchronized(manager) { synchronized(manager) {
manager.notify(); manager.notifyAll();
} }
} catch(InterruptedException e) { } catch(InterruptedException e) {
try { try {
setStop(); setStop();
synchronized(manager) { synchronized(manager) {
manager.notify(); manager.notifyAll();
} }
} catch (InterruptedException e2) { } catch (InterruptedException e2) {
} }
@ -224,7 +224,7 @@ public abstract class ClientDownloadPart extends ServeErrors implements Runnable
* @return ProtocolP2PPacketTCP used to send request * @return ProtocolP2PPacketTCP used to send request
*/ */
protected ProtocolP2PPacket<?> reqPart(Long offset) { protected ProtocolP2PPacket<?> reqPart(Long offset) {
writeLog("New request: #" + offset, LogLevel.Info); writeLog("New request: #" + offset, LogLevel.Debug);
// maintain tracking of tasks // maintain tracking of tasks
if (toDoTasks.contains(offset)) { if (toDoTasks.contains(offset)) {
try { try {

@ -29,6 +29,7 @@ public class ClientP2P {
private HostItem tracker; private HostItem tracker;
private HostItem server; private HostItem server;
private Scanner scanner; private Scanner scanner;
final static boolean DEBUG = false;
/** Initialize loggers if directories and logger are null, /** Initialize loggers if directories and logger are null,
* else fail silently. * else fail silently.
@ -37,8 +38,8 @@ public class ClientP2P {
if (directories == null && loggerServer == null && loggerClient == null) { if (directories == null && loggerServer == null && loggerClient == null) {
directories = new Directories("P2P_JAVA_PROJECT_" + server.getPort()); directories = new Directories("P2P_JAVA_PROJECT_" + server.getPort());
directories.createSubdir(logDir); directories.createSubdir(logDir);
loggerServer = new Logger(directories.getDataHomeDirectory() + logDir + "server.log"); loggerServer = new Logger(directories.getDataHomeDirectory() + logDir + "server.log", DEBUG);
loggerClient = new Logger(directories.getDataHomeDirectory() + logDir + "client.log"); loggerClient = new Logger(directories.getDataHomeDirectory() + logDir + "client.log", DEBUG);
directories.createSubdir(partsDir); directories.createSubdir(partsDir);
} }
} }

@ -23,6 +23,7 @@ public class ServerP2P {
private Logger logger; private Logger logger;
private HostItem tracker; private HostItem tracker;
private HostItem server; private HostItem server;
final static boolean DEBUG = true;
/** Constructor with portServerstr containing a port number. /** Constructor with portServerstr containing a port number.
* @param hostnameServer binded hostname * @param hostnameServer binded hostname
@ -37,7 +38,7 @@ public class ServerP2P {
tracker = new HostItem(hostnameTracker, portTracker); tracker = new HostItem(hostnameTracker, portTracker);
directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + portServer); directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + portServer);
directories.createSubdir(subdir); 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); System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory() + subdir);
if (isInteractive) { if (isInteractive) {
directories.askOpenDataHomeDirectory(subdir, scanner); directories.askOpenDataHomeDirectory(subdir, scanner);

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

@ -16,13 +16,16 @@ import java.sql.Timestamp;
*/ */
public class Logger { public class Logger {
private Path logFile; private Path logFile;
private boolean debugMode;
/** Constructor with logFile. /** Constructor with logFile.
* @param logFile name of the file to store logs. * @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"; assert logFile != null : "Logfile name is null";
this.logFile = Paths.get(logFile); this.logFile = Paths.get(logFile);
this.debugMode = debugMode;
try { try {
this.logFile.toFile().createNewFile(); this.logFile.toFile().createNewFile();
} catch (IOException e) { } catch (IOException e) {
@ -57,6 +60,9 @@ public class Logger {
level = "[Debug]"; level = "[Debug]";
colorize = "\u001B[36m"; // CYAN colorize = "\u001B[36m"; // CYAN
break; break;
case Progression:
level = "[Progression]";
break;
default: default:
System.err.println("Error: incorrect logLevel"); System.err.println("Error: incorrect logLevel");
} }
@ -64,18 +70,23 @@ public class Logger {
msg = level + " " + msg; msg = level + " " + msg;
} }
switch (logLevel) { switch (logLevel) {
case Action:
break;
case Debug:
case Error: case Error:
case Info: case Info:
case Warning: case Warning:
case Debug: case Progression:
default: default:
if (logLevel == LogLevel.Debug && !debugMode) {
break;
}
System.err.println(colorize + text + "\u001B[0m"); System.err.println(colorize + text + "\u001B[0m");
break; break;
case Action:
break;
} }
switch (logLevel) { switch (logLevel) {
case Debug: case Debug:
case Progression:
break; break;
case Error: case Error:
case Info: case Info:

@ -20,7 +20,7 @@ public class Tracker {
private HostItem tracker; private HostItem tracker;
private Directories directories; private Directories directories;
private Logger logger; private Logger logger;
final static boolean DEBUG = true;
/** Constructor with portStr containing a port number. /** Constructor with portStr containing a port number.
* @param hostname hostname to bind * @param hostname hostname to bind
* @param port port to bind * @param port port to bind
@ -29,7 +29,7 @@ public class Tracker {
public Tracker(String hostname, int port, boolean isInteractive) { public Tracker(String hostname, int port, boolean isInteractive) {
tracker = new HostItem(hostname, port); tracker = new HostItem(hostname, port);
directories = new Directories("P2P_JAVA_PROJECT_TRACKER_" + 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()); System.out.println("Tracker will listen on port " + port + " and write logs into " + directories.getDataHomeDirectory());
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
if (isInteractive) { if (isInteractive) {

Loading…
Cancel
Save