private void processDB() { FileManager fm = new FileManager(header.get(1), 0, true); if (fm.readChunk(Integer.parseInt(header.get(2)), Main.getDatabase().getUsername())) { Chunk ch = new Chunk(header.get(1), Integer.parseInt(header.get(2)), 0); Main.getDatabase().addChunk(ch); String msg = "DB " + header.get(1) + " " + header.get(2) + Main.getCRLF() + Main.getCRLF(); byte[] msgByte = msg.getBytes(StandardCharsets.ISO_8859_1); byte[] msgFull = Main.appendArray(msgByte, fm.getChunkData()); Random r = new Random(); try { sleep(r.nextInt(400)); } catch (InterruptedException e) { e.printStackTrace(); } if (!ch.getSent()) { Main.getRestore().send(msgFull); } Main.getDatabase().removeChunk(header.get(1), Integer.parseInt(header.get(2))); } }
private void getChunkProcess() { Chunk ch = null; Boolean found = false; for (int i = 0; i < Main.getDatabase().getChunksSize(); i++) { ch = Main.getDatabase().getChunk(i); if (ch.getFileId().equals(header.get(2)) && (ch.getChunkNo() == Integer.parseInt(header.get(3)))) { found = true; break; } } if (found) { FileManager chunk = new FileManager(header.get(2), 0, true); chunk.readChunk(Integer.parseInt(header.get(3)), Main.getDatabase().getUsername()); String ip = ""; try { ip = InetAddress.getLocalHost().toString().split("/")[1]; } catch (UnknownHostException e) { e.printStackTrace(); } int tcp_port = Main.getTCPport(); String message = "ME " + header.get(2) + " " + header.get(3) + " " + ip + " " + tcp_port + Main.getCRLF() + Main.getCRLF(); byte[] msg = message.getBytes(StandardCharsets.ISO_8859_1); Main.getRestore().send(msg); Main.getLogger().log("Received: " + message); try { TCPCommunicator tcpSocket = new TCPCommunicator(ip, tcp_port, true); String chunkMsg = "CHUNK " + header.get(2) + " " + header.get(3) + Main.getCRLF() + Main.getCRLF(); Main.getLogger().log("Sent: " + chunkMsg); byte[] chunkBytes = Main.appendArray(chunkMsg.getBytes(StandardCharsets.ISO_8859_1), chunk.getChunkData()); tcpSocket.send(chunkBytes); tcpSocket.close(); } catch (SocketTimeoutException e) { System.out.println("I was not the chosen one!"); } catch (IOException e) { e.printStackTrace(); } } }