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

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

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

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

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

@ -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:
System.err.println(colorize + text + "\u001B[0m");
if (logLevel == LogLevel.Debug && !debugMode) {
break;
case Action:
}
System.err.println(colorize + text + "\u001B[0m");
break;
}
switch (logLevel) {
case Debug:
case Progression:
break;
case Error:
case Info:

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

Loading…
Cancel
Save