Exemplo n.º 1
0
  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)));
    }
  }
Exemplo n.º 2
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();
      }
    }
  }
Exemplo n.º 3
0
 public void drawWorld(Graphics2D g2d, World w) {
   Chunk[] c = w.getChunks();
   for (int ci = 0; ci < c.length; ci++) {
     Chunk ch = c[ci];
     for (int tx = 0; tx < ch.tiles.length; tx++) {
       for (int ty = 0; ty < ch.tiles[tx].length; ty++) {
         drawTile(g2d, ch.tiles[tx][ty]);
         if (Main.showChunks) {
           g2d.setColor(Main.getRandomColor(true));
           Rectangle r = ch.getR();
           g2d.drawRect(r.x, r.y, r.width, r.height);
         }
       }
     }
   }
 }
Exemplo n.º 4
0
  private void storedProcess() {

    for (int j = 0; j < Main.getBackup().getSending().size(); j++) {
      if (Main.getBackup().getSending().get(j).getFileHash().equals(header.get(2))) {
        Main.getBackup().getSending().get(j).incStoreds(1);
        break;
      }
    }

    for (int i = 0; i < Main.getDatabase().getChunksSize(); i++) {
      Chunk chunk = Main.getDatabase().getChunk(i);
      if (chunk.getFileId().equals(header.get(2))
          && (chunk.getChunkNo() == Integer.parseInt(header.get(3)))) {
        chunk.setKnownReps(1);
        break;
      }
    }
  }
Exemplo n.º 5
0
 private void removedProcess() {
   for (int i = 0; i < Main.getDatabase().getChunksSize(); i++) {
     Chunk chunk = Main.getDatabase().getChunk(i);
     if (chunk.getFileId().equals(header.get(2))
         && (chunk.getChunkNo() == Integer.parseInt(header.get(3)))) {
       chunk.setKnownReps(-1);
       if (chunk.getKnownReps() < chunk.getRepDegree()) {
         BackupSend send =
             new BackupSend(
                 chunk.getFileId(),
                 chunk.getRepDegree(),
                 false,
                 chunk.getChunkNo(),
                 chunk.getKnownReps());
         Main.getBackup().addSending(send);
         Main.getService().submit(send);
       }
       break;
     }
   }
 }