|
|
|
@ -29,6 +29,7 @@ import remoteException.VersionRemoteError;
|
|
|
|
|
import remoteException.EmptyFile;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import tools.Logger;
|
|
|
|
|
import tools.LogLevel;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for TCP.
|
|
|
|
@ -57,10 +58,10 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
try {
|
|
|
|
|
socket = new ServerSocket(TCPPort);
|
|
|
|
|
} catch (SocketException e) {
|
|
|
|
|
System.err.println("Error: cannot listen on port " + TCPPort + " (TCP)");
|
|
|
|
|
logger.writeTCP("Error: cannot listen on port " + TCPPort, LogLevel.Error);
|
|
|
|
|
System.exit(-1);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.err.println("Error: cannot openning TCP socket");
|
|
|
|
|
logger.writeTCP("Error: cannot openning socket", LogLevel.Error);
|
|
|
|
|
System.exit(-2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -68,14 +69,14 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
/** Implementation of runnable. This methods allows to run the server.
|
|
|
|
|
*/
|
|
|
|
|
public void run() {
|
|
|
|
|
logger.writeTCP("Server sucessfully started");
|
|
|
|
|
logger.writeTCP("Server sucessfully started", LogLevel.Info);
|
|
|
|
|
do {
|
|
|
|
|
try {
|
|
|
|
|
Socket s = socket.accept();
|
|
|
|
|
ClientHandler c = new ClientHandler(s);
|
|
|
|
|
(new Thread(c)).start();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.err.println("Error while accepting new connection");
|
|
|
|
|
logger.writeTCP("Error while accepting new connection", LogLevel.Warning);
|
|
|
|
|
}
|
|
|
|
|
} while(true);
|
|
|
|
|
}
|
|
|
|
@ -98,11 +99,11 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
|
|
boolean end = false;
|
|
|
|
|
logger.writeTCP(addr + "New connection");
|
|
|
|
|
logger.writeTCP(addr + "New connection", LogLevel.Action);
|
|
|
|
|
do {
|
|
|
|
|
end = handleRequest();
|
|
|
|
|
} while(!end);
|
|
|
|
|
logger.writeTCP(addr + "End of connection");
|
|
|
|
|
logger.writeTCP(addr + "End of connection", LogLevel.Action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Respond to next request incomming on socket s.
|
|
|
|
@ -115,15 +116,15 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
Payload p = pd.getPayload();
|
|
|
|
|
switch (p.getRequestResponseCode()) {
|
|
|
|
|
case LOAD_REQUEST:
|
|
|
|
|
logger.writeTCP(addr + "LOAD_REQUEST");
|
|
|
|
|
logger.writeTCP(addr + "LOAD_REQUEST", LogLevel.Action);
|
|
|
|
|
sendLoadResponse(pd);
|
|
|
|
|
break;
|
|
|
|
|
case LIST_REQUEST:
|
|
|
|
|
logger.writeTCP(addr + "LIST_REQUEST");
|
|
|
|
|
logger.writeTCP(addr + "LIST_REQUEST", LogLevel.Action);
|
|
|
|
|
sendListResponse(pd);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
logger.writeTCP(addr + "Received grabbage");
|
|
|
|
|
logger.writeTCP(addr + "Received grabbage", LogLevel.Action);
|
|
|
|
|
sendInternalError(pd);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
@ -161,10 +162,11 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
* @param pd ProtocolP2PPacketTCP to respond
|
|
|
|
|
*/
|
|
|
|
|
private void sendInternalError(ProtocolP2PPacketTCP pd) {
|
|
|
|
|
logger.writeTCP("Internal Error", LogLevel.Warning);
|
|
|
|
|
try {
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR)));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.err.println(e);
|
|
|
|
|
logger.writeTCP(e, LogLevel.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -174,14 +176,14 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
private void sendListResponse(ProtocolP2PPacketTCP pd) {
|
|
|
|
|
try {
|
|
|
|
|
if (fileList.length == 0) {
|
|
|
|
|
System.err.println("Sending EMPTY_DIRECTORY");
|
|
|
|
|
logger.writeTCP("Sending EMPTY_DIRECTORY", LogLevel.Action);
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("Sending LIST_RESPONSE");
|
|
|
|
|
logger.writeTCP("Sending LIST_RESPONSE", LogLevel.Action);
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)(new FileList(fileList))));
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
|
System.err.println(e2);
|
|
|
|
|
logger.writeTCP(e2, LogLevel.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -201,13 +203,13 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
byte[] fullLoad = Files.readAllBytes(Paths.get(baseDirectory + filename));
|
|
|
|
|
long sizeToSend = 0;
|
|
|
|
|
if (fullLoad.length - offset < maxSizePartialContent) {
|
|
|
|
|
System.out.println("Sending last partialContent");
|
|
|
|
|
logger.writeTCP("Sending last partialContent", LogLevel.Debug);
|
|
|
|
|
sizeToSend = fullLoad.length - offset;
|
|
|
|
|
} else {
|
|
|
|
|
sizeToSend = maxSizePartialContent;
|
|
|
|
|
}
|
|
|
|
|
System.out.println("maxSizePartialContent: " + maxSizePartialContent);
|
|
|
|
|
System.out.println("Sending " + filename + " from " + offset + " to " + (offset + sizeToSend));
|
|
|
|
|
logger.writeTCP("maxSizePartialContent: " + maxSizePartialContent, LogLevel.Debug);
|
|
|
|
|
logger.writeTCP("Sending " + filename + " from " + offset + " to " + (offset + sizeToSend), LogLevel.Debug);
|
|
|
|
|
byte[] load = Arrays.copyOfRange(fullLoad, (int)offset, (int)(offset + sizeToSend));
|
|
|
|
|
if (Arrays.binarySearch(fileList, filename) >= 0) {
|
|
|
|
|
try {
|
|
|
|
@ -217,13 +219,13 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)(new FilePart(filename, fullLoad.length, offset, load))));
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
|
System.err.println(e2);
|
|
|
|
|
logger.writeTCP(e2, LogLevel.Error);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
System.err.println("File requested not found: `" + filename + "` " + Arrays.binarySearch(fileList, filename));
|
|
|
|
|
System.err.println("File list:");
|
|
|
|
|
logger.writeTCP("File requested not found: `" + filename + "` " + Arrays.binarySearch(fileList, filename), LogLevel.Debug);
|
|
|
|
|
logger.writeTCP("File list:", LogLevel.Debug);
|
|
|
|
|
for (String f: fileList) {
|
|
|
|
|
System.err.println("- " + f);
|
|
|
|
|
logger.writeTCP("- " + f, LogLevel.Debug);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new IOException(); // to send a NOT_FOUND in the catch block
|
|
|
|
@ -232,7 +234,7 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
try {
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.NOT_FOUND)));
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
|
System.err.println(e2);
|
|
|
|
|
logger.writeTCP(e2, LogLevel.Debug);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|