Ejemplo n.º 1
0
  public Status listRemoteWorkdir() throws IOException, ConexionException, ClassNotFoundException {
    ControlMsg msg = new ControlMsg(Command.LR);
    msg.setFilename(this.filename);

    // Enviamos el paquete de control
    if (sendMsg(server, buffer, msg) == -1) {
      System.out.println("Error " + msg);
    }

    // Recibimos la respuesta
    msg = (ControlMsg) recieveMsg(server, buffer);

    return new Status(msg.getStatus(), msg.getDataResponse());
  }
Ejemplo n.º 2
0
  private Status reciveFile() throws IOException, ConexionException, ClassNotFoundException {
    ControlMsg msg = new ControlMsg(Command.R);
    Status status = new Status();
    String md5sum;
    long filesize, bytesRecieved = 0;
    StatusValue stat;

    msg.setFilename(this.filename);
    msg.setStatus(StatusValue.HELLO);

    if (sendMsg(server, buffer, msg) == -1) {
      System.out.println("Error " + msg);
    }
    msg = (ControlMsg) recieveMsg(server, buffer);
    stat = msg.getStatus();

    if (stat.equals(StatusValue.ERROR)) {
      return new Status(StatusValue.ERROR, msg.getResponse());
    }

    filesize = msg.getFileSize();
    md5sum = msg.getFileHash();

    /*
    this.pantalla.println("Filename: "+msg.getFilename());
    this.pantalla.println("Tamaño: "+filesize);
    this.pantalla.println("md5sum: "+md5sum);
    this.pantalla.println("Freespace: "+this.workDir.getUsableSpace());
    */

    if (this.workDir.getUsableSpace() < filesize) {
      msg.setStatus(StatusValue.DATAEND);
      if (sendMsg(server, buffer, msg) == -1) {
        System.out.println("Error " + msg);
      }
      return new Status(StatusValue.ERROR, "No hay suficiente espacio libre");
    }

    msg.setStatus(StatusValue.READY);
    if (sendMsg(server, buffer, msg) == -1) {
      System.out.println("Error " + msg);
    }

    msg = (ControlMsg) recieveMsg(server, buffer);
    stat = msg.getStatus();
    switch (stat) {
      case START:
        // Comenzamos a recibir el fichero
        this.pantalla.print("Descargando fichero " + this.filename + " ... ");
        final File file = new File(this.workDir, this.filename);
        file.createNewFile();

        final WritableByteChannel dest = Channels.newChannel(new FileOutputStream(file));
        buffer.clear();
        int n;
        do {
          n = dataChan.read(buffer);
          if (n == -1) {
            dest.close();
            file.delete();
            return new Status(ERROR, "Se ha cortado la conexión con el servidor.");
          }

          // System.out.println("Leidos "+n+" desde "+getId(dataChan));
          buffer.flip();
          n = dest.write(buffer);
          // System.out.println("Escritos "+n+" en "+file);
          buffer.compact();
          bytesRecieved += n;
        } while (bytesRecieved < filesize);

        buffer.flip();
        while (buffer.hasRemaining()) {
          System.out.println("He entrado en el hasRemaining.");
          n = dest.write(buffer);
          // System.out.println("Escritos "+n+" en "+file);
        }
        dest.close();

        String md5calulated = getMD5Checksum(file);
        if (md5calulated.equals(md5sum)) {
          status = new Status(OK, "Subida completada");
        } else {
          status = new Status(ERROR, "falló la comprobación md5. Fichero no almacenado");
          file.delete();
        }

        msg.setStatus(StatusValue.DATAEND);

        if (sendMsg(server, buffer, msg) == -1) {
          System.out.println("Error " + msg);
        }

        msg = (ControlMsg) recieveMsg(server, buffer);
        stat = msg.getStatus();
        if (stat == StatusValue.BYE) {
          this.pantalla.println("DONE");
        } else {
          return new Status(ERROR, msg.getResponse());
        }

        break;
      case ERROR:
      default:
        return new Status(stat, msg.getResponse());
    }

    return status;
  }
Ejemplo n.º 3
0
  private Status sendFile() throws IOException, ConexionException, ClassNotFoundException {
    ControlMsg msg = new ControlMsg(Command.S);
    Status status = new Status();
    File file = new File(this.workDir, this.filename);
    String md5sum = getMD5Checksum(file);
    long filesize = file.length();
    StatusValue stat;

    /*
    this.pantalla.println("Filename: "+filename);
    this.pantalla.println("Tamaño: "+filesize);
    this.pantalla.println("md5sum: "+md5sum);
    */

    msg.setFilename(filename);
    msg.setFileHash(md5sum);
    msg.setFileSize(filesize);
    msg.setStatus(StatusValue.HELLO);

    // Enviamos el paquete de control
    if (sendMsg(server, buffer, msg) == -1) {
      System.out.println("Error " + msg);
    }

    // Recibimos la respuesta
    msg = (ControlMsg) recieveMsg(server, buffer);

    stat = msg.getStatus();
    switch (stat) {
      case START:
        // Comenzamos a enviar el fichero
        System.out.println("Enviando fichero " + this.filename + " ... ");
        final ReadableByteChannel src = Channels.newChannel(new FileInputStream(file));
        buffer.clear();
        int n;
        while ((n = src.read(buffer)) != -1) {
          // while(src.read(buffer) != -1) {
          System.out.println("Leidos " + n + " desde " + file);
          buffer.flip();
          n = dataChan.write(buffer);
          // dataChan.write(buffer);
          System.out.println("Escritos " + n + " en " + getId(dataChan));
          buffer.compact();
          try {
            Thread.sleep(50);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }

        buffer.flip();
        while (buffer.hasRemaining()) {
          System.out.println("He entrado en el hasRemaining.");
          dataChan.write(buffer);
        }
        src.close();
        break;
      case ERROR:
      default:
        return new Status(stat, msg.getResponse());
    }

    // Vemos la respuesta del MD5
    msg = (ControlMsg) recieveMsg(server, buffer);

    stat = msg.getStatus();
    switch (stat) {
      case MD5OK:
        // this.pantalla.println("DONE");
        status = new Status(OK, "Subida completada");
        break;
      default:
        status = new Status(stat, msg.getResponse());
    }

    return status;
  }