From 223e5126845076f3afd102675e550d54b10b288d Mon Sep 17 00:00:00 2001 From: flavien Date: Fri, 20 Mar 2020 17:03:30 +0100 Subject: [PATCH] refactor servermanagemntudp methods --- src/serverP2P/ServerManagementUDP.java | 126 +++++++++++++------------ 1 file changed, 67 insertions(+), 59 deletions(-) diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index c6b3078..986e8d0 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -80,67 +80,10 @@ public class ServerManagementUDP implements Runnable { Payload p = pd.getPayload(); switch (p.getRequestResponseCode()) { case LOAD_REQUEST: - 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); - } - } - } + LoadRequestManagement(logger, p, pd); break; case LIST_REQUEST: - 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); - } + ListRequestManagement(logger, pd); break; case HASH_REQUEST: 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 * @param pd Request received */ -- 2.30.2