From d9c4c8ba40b478fc85e6e27b9caa5d7f9aa109ba Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Fri, 27 Mar 2020 17:40:58 +0100 Subject: [PATCH] Fix off by 1 lengths; fix debug color --- src/protocolP2P/DiscoverRequest.java | 4 ++-- src/protocolP2P/DiscoverResponse.java | 4 ++-- src/protocolP2P/FilePart.java | 2 +- src/protocolP2P/HashRequest.java | 2 +- src/protocolP2P/HashResponse.java | 2 +- src/tools/Logger.java | 3 ++- src/tracker/TrackerManagementTCP.java | 4 ++-- 7 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/protocolP2P/DiscoverRequest.java b/src/protocolP2P/DiscoverRequest.java index 7ef1751..3852b4e 100644 --- a/src/protocolP2P/DiscoverRequest.java +++ b/src/protocolP2P/DiscoverRequest.java @@ -50,8 +50,8 @@ public class DiscoverRequest extends Payload { */ protected byte[] toPacket() throws InternalError { // compute total size - int size = PAYLOAD_START_POSITION + filename.length(); - byte[] packet = new byte[size + 1]; // java initialize all to zero + int size = 1 + PAYLOAD_START_POSITION + filename.length(); + byte[] packet = new byte[size]; // java initialize all to zero // set request/response code packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // set Payload size diff --git a/src/protocolP2P/DiscoverResponse.java b/src/protocolP2P/DiscoverResponse.java index 4c73e36..b499f65 100644 --- a/src/protocolP2P/DiscoverResponse.java +++ b/src/protocolP2P/DiscoverResponse.java @@ -74,8 +74,8 @@ public class DiscoverResponse extends Payload { hostListSize += (2 + hostItem.getHostname().length() + 1); } // compute total size - int size = FILENAME_POSITION + filename.length() + hostListSize; - byte[] packet = new byte[size + 1]; // java initialize all to zero + int size = 1 + FILENAME_POSITION + filename.length() + hostListSize; + byte[] packet = new byte[size]; // java initialize all to zero // set request/response code packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // set Payload size diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index d168a05..a410f51 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -77,7 +77,7 @@ public class FilePart extends Payload { protected byte[] toPacket() throws InternalError { // compute total size int size = FILENAME_POSITION + filename.length() + partialContent.length; - byte[] packet = new byte[size + 1]; // java initialize all to zero + byte[] packet = new byte[size]; // java initialize all to zero // set request/response code packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // set Payload size diff --git a/src/protocolP2P/HashRequest.java b/src/protocolP2P/HashRequest.java index 1eec671..cad74f7 100644 --- a/src/protocolP2P/HashRequest.java +++ b/src/protocolP2P/HashRequest.java @@ -90,7 +90,7 @@ public class HashRequest extends Payload { i++; } int size = FILENAME_POSITION + filenameSize + BytesArrayTools.computeStringArraySize(algoListStr, "\n"); - byte[] packet = new byte[size + 1]; // java initialize all to zero + byte[] packet = new byte[size]; // java initialize all to zero // set request/response code packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // set Payload size diff --git a/src/protocolP2P/HashResponse.java b/src/protocolP2P/HashResponse.java index 743b13e..041c8a3 100644 --- a/src/protocolP2P/HashResponse.java +++ b/src/protocolP2P/HashResponse.java @@ -98,7 +98,7 @@ public class HashResponse extends Payload { for (byte[] s : hashes.values()) { size += 4 + s.length; } - byte[] packet = new byte[size + 1]; // java initialize all to zero + byte[] packet = new byte[size]; // java initialize all to zero // set request/response code packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; diff --git a/src/tools/Logger.java b/src/tools/Logger.java index ce0f82e..b08f15a 100644 --- a/src/tools/Logger.java +++ b/src/tools/Logger.java @@ -34,7 +34,7 @@ public class Logger { * @param text Text to log */ public void write(String text, LogLevel logLevel) { - String colorize = "\u001B[0"; + String colorize = "\u001B[0m"; String msg = "[" + new Timestamp(System.currentTimeMillis()) + "] " + text + "\n"; String level = null; switch (logLevel) { @@ -55,6 +55,7 @@ public class Logger { break; case Debug: level = "[Debug]"; + colorize = "\u001B[36m"; // CYAN break; default: System.err.println("Error: incorrect logLevel"); diff --git a/src/tracker/TrackerManagementTCP.java b/src/tracker/TrackerManagementTCP.java index 603bab5..0f8abec 100644 --- a/src/tracker/TrackerManagementTCP.java +++ b/src/tracker/TrackerManagementTCP.java @@ -86,11 +86,11 @@ public class TrackerManagementTCP extends TrackerManagement { public void run() { boolean end = false; - writeLog("[ " + addr + "] New connection", LogLevel.Action); + writeLog("[" + addr + "] New connection", LogLevel.Action); do { end = handleClientRequest(); } while(!end); - writeLog("[ " + addr + "] End of connection", LogLevel.Action); + writeLog("[" + addr + "] End of connection", LogLevel.Action); } /** Respond to next request incomming on socket s.