예제 #1
0
  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();
      }
    }
  }