Fix off by 1 lengths; fix debug color

pull/86/head
Louis Royer 4 years ago
parent d6fbfa6683
commit d9c4c8ba40

@ -50,8 +50,8 @@ public class DiscoverRequest extends Payload {
*/ */
protected byte[] toPacket() throws InternalError { protected byte[] toPacket() throws InternalError {
// compute total size // compute total size
int size = PAYLOAD_START_POSITION + filename.length(); int size = 1 + PAYLOAD_START_POSITION + filename.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 // set request/response code
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
// set Payload size // set Payload size

@ -74,8 +74,8 @@ public class DiscoverResponse extends Payload {
hostListSize += (2 + hostItem.getHostname().length() + 1); hostListSize += (2 + hostItem.getHostname().length() + 1);
} }
// compute total size // compute total size
int size = FILENAME_POSITION + filename.length() + hostListSize; int size = 1 + FILENAME_POSITION + filename.length() + hostListSize;
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 // set request/response code
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
// set Payload size // set Payload size

@ -77,7 +77,7 @@ public class FilePart extends Payload {
protected byte[] toPacket() throws InternalError { protected byte[] toPacket() throws InternalError {
// compute total size // compute total size
int size = FILENAME_POSITION + filename.length() + partialContent.length; 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 // set request/response code
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
// set Payload size // set Payload size

@ -90,7 +90,7 @@ public class HashRequest extends Payload {
i++; i++;
} }
int size = FILENAME_POSITION + filenameSize + BytesArrayTools.computeStringArraySize(algoListStr, "\n"); 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 // set request/response code
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
// set Payload size // set Payload size

@ -98,7 +98,7 @@ public class HashResponse extends Payload {
for (byte[] s : hashes.values()) { for (byte[] s : hashes.values()) {
size += 4 + s.length; 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 // set request/response code
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;

@ -34,7 +34,7 @@ public class Logger {
* @param text Text to log * @param text Text to log
*/ */
public void write(String text, LogLevel logLevel) { public void write(String text, LogLevel logLevel) {
String colorize = "\u001B[0"; String colorize = "\u001B[0m";
String msg = "[" + new Timestamp(System.currentTimeMillis()) + "] " + text + "\n"; String msg = "[" + new Timestamp(System.currentTimeMillis()) + "] " + text + "\n";
String level = null; String level = null;
switch (logLevel) { switch (logLevel) {
@ -55,6 +55,7 @@ public class Logger {
break; break;
case Debug: case Debug:
level = "[Debug]"; level = "[Debug]";
colorize = "\u001B[36m"; // CYAN
break; break;
default: default:
System.err.println("Error: incorrect logLevel"); System.err.println("Error: incorrect logLevel");

@ -86,11 +86,11 @@ public class TrackerManagementTCP extends TrackerManagement {
public void run() { public void run() {
boolean end = false; boolean end = false;
writeLog("[ " + addr + "] New connection", LogLevel.Action); writeLog("[" + addr + "] New connection", LogLevel.Action);
do { do {
end = handleClientRequest(); end = handleClientRequest();
} while(!end); } while(!end);
writeLog("[ " + addr + "] End of connection", LogLevel.Action); writeLog("[" + addr + "] End of connection", LogLevel.Action);
} }
/** Respond to next request incomming on socket s. /** Respond to next request incomming on socket s.

Loading…
Cancel
Save