|
|
@ -80,67 +80,10 @@ public class ServerManagementUDP implements Runnable {
|
|
|
|
Payload p = pd.getPayload();
|
|
|
|
Payload p = pd.getPayload();
|
|
|
|
switch (p.getRequestResponseCode()) {
|
|
|
|
switch (p.getRequestResponseCode()) {
|
|
|
|
case LOAD_REQUEST:
|
|
|
|
case LOAD_REQUEST:
|
|
|
|
logger.writeUDP("Received LOAD_REQUEST", LogLevel.Action);
|
|
|
|
LoadRequestManagement(logger, p, pd);
|
|
|
|
assert p instanceof LoadRequest : "payload must be an instance of LoadRequest";
|
|
|
|
|
|
|
|
if (!(p instanceof LoadRequest)) {
|
|
|
|
|
|
|
|
sendInternalError(pd);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
String filename = ((LoadRequest)p).getFilename();
|
|
|
|
|
|
|
|
long offset = ((LoadRequest)p).getOffset();
|
|
|
|
|
|
|
|
long maxSizePartialContent = ((LoadRequest)p).getMaxSizePartialContent();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
byte[] fullLoad = Files.readAllBytes(Paths.get(baseDirectory + filename));
|
|
|
|
|
|
|
|
long sizeToSend = 0;
|
|
|
|
|
|
|
|
if (fullLoad.length - offset < maxSizePartialContent) {
|
|
|
|
|
|
|
|
logger.writeUDP("Sending last partialContent", LogLevel.Debug);
|
|
|
|
|
|
|
|
sizeToSend = fullLoad.length - offset;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
sizeToSend = maxSizePartialContent;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.writeUDP("maxSizePartialContent: " + maxSizePartialContent, LogLevel.Debug);
|
|
|
|
|
|
|
|
logger.writeUDP("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 {
|
|
|
|
|
|
|
|
if (load.length == 0) {
|
|
|
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_FILE)));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)(new FilePart(filename, fullLoad.length, offset, load))));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
|
|
|
|
logger.writeUDP(e2, LogLevel.Error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
logger.writeUDP("File requested not found: `" + filename + "` " + Arrays.binarySearch(fileList, filename), LogLevel.Debug);
|
|
|
|
|
|
|
|
logger.writeUDP("File list:", LogLevel.Debug);
|
|
|
|
|
|
|
|
for (String f: fileList) {
|
|
|
|
|
|
|
|
logger.writeUDP("- " + f, LogLevel.Debug);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw new IOException(); // to send a NOT_FOUND in the catch block
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.NOT_FOUND)));
|
|
|
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
|
|
|
|
logger.writeUDP(e2, LogLevel.Error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case LIST_REQUEST:
|
|
|
|
case LIST_REQUEST:
|
|
|
|
logger.writeUDP("Received LIST_REQUEST", LogLevel.Action);
|
|
|
|
ListRequestManagement(logger, pd);
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (fileList.length == 0) {
|
|
|
|
|
|
|
|
logger.writeUDP("Sending EMPTY_DIRECTORY", LogLevel.Action);
|
|
|
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
logger.writeUDP("Sending LIST_RESPONSE", LogLevel.Action);
|
|
|
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)(new FileList(fileList))));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
|
|
|
|
logger.writeUDP(e2, LogLevel.Error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case HASH_REQUEST:
|
|
|
|
case HASH_REQUEST:
|
|
|
|
logger.writeUDP("Received HASH_REQUEST", LogLevel.Action);
|
|
|
|
logger.writeUDP("Received HASH_REQUEST", LogLevel.Action);
|
|
|
@ -205,6 +148,71 @@ public class ServerManagementUDP implements Runnable {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void LoadRequestManagement(Logger logger, Payload p, ProtocolP2PPacketUDP pd){
|
|
|
|
|
|
|
|
logger.writeUDP("Received LOAD_REQUEST", LogLevel.Action);
|
|
|
|
|
|
|
|
assert p instanceof LoadRequest : "payload must be an instance of LoadRequest";
|
|
|
|
|
|
|
|
if (!(p instanceof LoadRequest)) {
|
|
|
|
|
|
|
|
sendInternalError(pd);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
String filename = ((LoadRequest)p).getFilename();
|
|
|
|
|
|
|
|
long offset = ((LoadRequest)p).getOffset();
|
|
|
|
|
|
|
|
long maxSizePartialContent = ((LoadRequest)p).getMaxSizePartialContent();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
byte[] fullLoad = Files.readAllBytes(Paths.get(baseDirectory + filename));
|
|
|
|
|
|
|
|
long sizeToSend = 0;
|
|
|
|
|
|
|
|
if (fullLoad.length - offset < maxSizePartialContent) {
|
|
|
|
|
|
|
|
logger.writeUDP("Sending last partialContent", LogLevel.Debug);
|
|
|
|
|
|
|
|
sizeToSend = fullLoad.length - offset;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
sizeToSend = maxSizePartialContent;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.writeUDP("maxSizePartialContent: " + maxSizePartialContent, LogLevel.Debug);
|
|
|
|
|
|
|
|
logger.writeUDP("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 {
|
|
|
|
|
|
|
|
if (load.length == 0) {
|
|
|
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_FILE)));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)(new FilePart(filename, fullLoad.length, offset, load))));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
|
|
|
|
logger.writeUDP(e2, LogLevel.Error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
logger.writeUDP("File requested not found: `" + filename + "` " + Arrays.binarySearch(fileList, filename), LogLevel.Debug);
|
|
|
|
|
|
|
|
logger.writeUDP("File list:", LogLevel.Debug);
|
|
|
|
|
|
|
|
for (String f: fileList) {
|
|
|
|
|
|
|
|
logger.writeUDP("- " + f, LogLevel.Debug);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw new IOException(); // to send a NOT_FOUND in the catch block
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.NOT_FOUND)));
|
|
|
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
|
|
|
|
logger.writeUDP(e2, LogLevel.Error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void ListRequestManagement(Logger logger, ProtocolP2PPacketUDP pd){
|
|
|
|
|
|
|
|
logger.writeUDP("Received LIST_REQUEST", LogLevel.Action);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (fileList.length == 0) {
|
|
|
|
|
|
|
|
logger.writeUDP("Sending EMPTY_DIRECTORY", LogLevel.Action);
|
|
|
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
logger.writeUDP("Sending LIST_RESPONSE", LogLevel.Action);
|
|
|
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)(new FileList(fileList))));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
|
|
|
|
logger.writeUDP(e2, LogLevel.Error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Send hash response to hash request
|
|
|
|
/** Send hash response to hash request
|
|
|
|
* @param pd Request received
|
|
|
|
* @param pd Request received
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|