void ReceiveFileChunksFromServer() throws Exception, ClassNotFoundException {
    try {
      if (flagFilename) {
        filename = (String) in.readObject();
        totalChunks = Integer.parseInt((String) in.readObject());
        flagFilename = false;
      }

      if (availableChunks == null) availableChunks = new File[totalChunks];

      if (requiredChunks == null) requiredChunks = new File[totalChunks];

      int partNumber = Integer.parseInt((String) in.readObject());

      File partFile = new File("chunks/" + filename + "." + partNumber);
      byte[] msg = (byte[]) in.readObject();
      Files.write(partFile.toPath(), msg);
      availableChunks[partNumber] = partFile;
      System.out.println("Received chunk " + partNumber);
    } catch (ClassNotFoundException e) {
      flag = true;
    } catch (Exception e) {
      flag = true;
    }
  }
Exemplo n.º 2
0
 private void setOwnership(File dir, String group, String owner) {
   try {
     Path path = dir.toPath();
     UserPrincipalLookupService lookupService =
         FileSystems.getDefault().getUserPrincipalLookupService();
     GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(group);
     UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(owner);
     PosixFileAttributeView pfav =
         Files.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
     pfav.setGroup(groupPrincipal);
     pfav.setOwner(userPrincipal);
   } catch (Exception ex) {
     cp.appendln("Unable to set the file group and owner for\n   " + dir);
   }
 }
  void ReceiveFileChunkFromNeighbor() throws Exception, ClassNotFoundException {
    try {
      int chunkNum = Integer.parseInt((String) inDown.readObject());
      if (chunkNum == -1) return;

      byte[] msg = (byte[]) inDown.readObject();

      File chunkFile = new File("chunks/" + filename + "." + chunkNum);
      Files.write(chunkFile.toPath(), msg);

      availableChunks[chunkNum] = chunkFile;
      System.out.println("Received chunk " + chunkNum + " from Client " + dwldNeighbor);
    } catch (ClassNotFoundException e) {
      flag = true;
    } catch (Exception e) {
      flag = true;
    }
  }
  public static void main(String[] args) throws Exception {

    ServerSocket Server = new ServerSocket(8080);
    String clientInput;
    File file;
    Path filePath;
    byte[] fileArray;

    while (true) {
      // Accepts a connection to a socket.
      // Program waits at this line until a connection is made.
      Socket connectedSocket = Server.accept();
      BufferedReader inFromClient =
          new BufferedReader(new InputStreamReader(connectedSocket.getInputStream()));
      DataOutputStream outToClient = new DataOutputStream(connectedSocket.getOutputStream());
      clientInput = inFromClient.readLine();
      file = new File(clientInput);
      filePath = file.toPath();
      fileArray = Files.readAllBytes(filePath);
      outToClient.write(fileArray);
      connectedSocket.close();
    }
  }