Exemple #1
0
  /*
   * Termina a realização da Tarefa.
   * Retorna true se foi terminada com sucesso false caso contrário
   */
  public boolean endTask(String id) throws InterruptedException {
    Map<String, Integer> objectsToSupply;
    String type;
    lock.lock();
    try {
      if (!this.tasksRunning.containsKey(Integer.valueOf(id))) {
        return false;
      } else {
        type = this.tasksRunning.get(Integer.valueOf(id));
        objectsToSupply = this.tasks.get(type).getObjects();
      }
    } finally {
      lock.unlock();
    }

    // Supply de todos os objetos
    for (Map.Entry<String, Integer> entry : objectsToSupply.entrySet()) {
      warehouse.supply(entry.getKey(), entry.getValue());
    }

    lock.lock();
    try {
      this.tasksRunning.remove(Integer.valueOf(id));
      this.tasks.get(type).signalP();
    } finally {
      lock.unlock();
    }

    return true;
  }
Exemple #2
0
  /*
   * Inicia a realização de uma Tarefa.
   * Retorna o ID se foi iniciada com sucesso -1 caso contrário
   */
  public int beginTask(String type) {
    Map<String, Integer> objectsToConsume;
    lock.lock();
    try {
      if (!this.tasks.containsKey(type)) return -1;
      else {
        objectsToConsume = this.tasks.get(type).getObjects();
      }
    } finally {
      lock.unlock();
    }

    try {
      warehouse.consume(objectsToConsume);
    } catch (Exception e) {
      return -1;
    }

    lock.lock();
    try {
      countID++;
      this.tasksRunning.put(countID, type);
      return countID; // Retornar o ID da tarefa
    } finally {
      lock.unlock();
    }
  }
Exemple #3
0
 /* Termina a sessão de um utilizador */
 public void logout(String nick) {
   lock.lock();
   try {
     this.users.get(nick).setAtivo(false);
   } finally {
     lock.unlock();
   }
 }
Exemple #4
0
 /* Devolve lista com o tipo de tarefas em desenvolvimento */
 public HashMap<Integer, String> listTaskRunnig() {
   HashMap<Integer, String> tasklist = new HashMap<>();
   lock.lock();
   try {
     for (Map.Entry<Integer, String> entry : this.tasksRunning.entrySet()) {
       tasklist.put(entry.getKey(), entry.getValue());
     }
     return tasklist;
   } finally {
     lock.unlock();
   }
 }
  /**
   * Set local batch size for this sequences.
   *
   * @param size Sequence batch size. Must be more then 0.
   */
  @Override
  public void batchSize(int size) {
    A.ensure(size > 0, " Batch size can't be less then 0: " + size);

    lock.lock();

    try {
      batchSize = size;
    } finally {
      lock.unlock();
    }
  }
  /** {@inheritDoc} */
  @Override
  public long get() throws GridException {
    checkRemoved();

    lock.lock();

    try {
      return locVal;
    } finally {
      lock.unlock();
    }
  }
Exemple #7
0
  /*Devolve uma lista do stock corrente*/
  public HashMap<String, Integer> listStock() {
    HashMap<String, Integer> liststock = new HashMap<>();

    lock.lock();
    try {
      for (Map.Entry<String, Product> entry : this.warehouse.getStock().entrySet()) {
        liststock.put(entry.getKey(), entry.getValue().getQuantity());
      }
      return liststock;
    } finally {
      lock.unlock();
    }
  }
Exemple #8
0
  /* Devolve lista com o tipo de tarefas */
  public List<String> listTask() {
    ArrayList<String> tasklist = new ArrayList<>();

    lock.lock();
    try {
      for (Task task : this.tasks.values()) {
        tasklist.add(task.getType());
      }
      return tasklist;
    } finally {
      lock.unlock();
    }
  }
Exemple #9
0
  /* Valida a existencia e os dados de um Utilizador */
  public boolean validateUser(String nick, String pass) {
    boolean res = false;

    lock.lock();
    try {
      if (this.users.containsKey(nick)) {
        if (this.users.get(nick).getPassword().equals(pass)) {
          this.users.get(nick).setAtivo(true);
          res = true;
        }
      }
    } finally {
      lock.unlock();
    }
    return res;
  }
Exemple #10
0
  /* Regista um novo utilizador no sistema */
  public boolean registerUser(String nick, String pass) {
    boolean res = false;
    if (nick.equals("") || (pass.equals(""))) return res;

    lock.lock();
    try {
      if (!this.users.containsKey(nick)) {
        User u = new User(nick, pass);
        this.users.put(nick, u);
        res = true;
      }
    } finally {
      lock.unlock();
    }
    return res;
  }
Exemple #11
0
 int waitTasks(List<Integer> wL) throws InterruptedException {
   lock.lock();
   try {
     boolean i = true;
     while (i) {
       i = false;
       for (Integer id : wL) {
         if (countID < id) return id;
         String type = this.tasksRunning.get(id);
         if (type != null) {
           this.tasks.get(type).awaitP();
           i = true;
           break;
         }
       }
     }
     return -1;
   } finally {
     lock.unlock();
   }
 }
Exemple #12
0
  /*
   * Cria uma nova Tarefa.
   * Retorna true se foi criada com sucesso false caso contrário
   */
  public boolean newTask(String type, Packet objects) {
    if (type.equals("")) return false;
    lock.lock();
    try {
      if (this.tasks.containsKey(type)) {
        return false;
      } else {
        HashMap<String, String> aux = objects.getArgs();
        HashMap<String, Integer> objs = new HashMap<>();
        for (Map.Entry<String, String> entry : aux.entrySet()) {
          objs.put(entry.getKey(), Integer.parseInt((entry.getValue())));
        }
        Task t = new Task(type, objs, lock);

        this.tasks.put(type, t);

        return true;
      }
    } finally {
      lock.unlock();
    }
  }
  /**
   * Asynchronous sequence update operation. Will add given amount to the sequence value.
   *
   * @param l Increment amount.
   * @param updateCall Cache call that will update sequence reservation count in accordance with l.
   * @param updated If {@code true}, will return sequence value after update, otherwise will return
   *     sequence value prior to update.
   * @return Future indicating sequence value.
   * @throws GridException If update failed.
   */
  private GridFuture<Long> internalUpdateAsync(
      long l, @Nullable Callable<Long> updateCall, boolean updated) throws GridException {
    checkRemoved();

    A.ensure(l > 0, " Parameter mustn't be less then 1: " + l);

    lock.lock();

    try {
      // If reserved range isn't exhausted.
      if (locVal + l <= upBound) {
        long curVal = locVal;

        locVal += l;

        return new GridFinishedFuture<Long>(ctx.kernalContext(), updated ? locVal : curVal);
      }
    } finally {
      lock.unlock();
    }

    if (updateCall == null) updateCall = internalUpdate(l, updated);

    while (true) {
      if (updateGuard.compareAndSet(false, true)) {
        try {
          // This call must be outside lock.
          return ctx.closures().callLocalSafe(updateCall, true);
        } finally {
          lock.lock();

          try {
            updateGuard.set(false);

            cond.signalAll();
          } finally {
            lock.unlock();
          }
        }
      } else {
        lock.lock();

        try {
          while (locVal >= upBound && updateGuard.get()) {
            try {
              cond.await(500, MILLISECONDS);
            } catch (InterruptedException e) {
              throw new GridInterruptedException(e);
            }
          }

          checkRemoved();

          // If reserved range isn't exhausted.
          if (locVal + l <= upBound) {
            long curVal = locVal;

            locVal += l;

            return new GridFinishedFuture<Long>(ctx.kernalContext(), updated ? locVal : curVal);
          }
        } finally {
          lock.unlock();
        }
      }
    }
  }
  /**
   * Synchronous sequence update operation. Will add given amount to the sequence value.
   *
   * @param l Increment amount.
   * @param updateCall Cache call that will update sequence reservation count in accordance with l.
   * @param updated If {@code true}, will return sequence value after update, otherwise will return
   *     sequence value prior to update.
   * @return Sequence value.
   * @throws GridException If update failed.
   */
  private long internalUpdate(long l, @Nullable Callable<Long> updateCall, boolean updated)
      throws GridException {
    checkRemoved();

    assert l > 0;

    lock.lock();

    try {
      // If reserved range isn't exhausted.
      if (locVal + l <= upBound) {
        long curVal = locVal;

        locVal += l;

        return updated ? locVal : curVal;
      }
    } finally {
      lock.unlock();
    }

    if (updateCall == null) updateCall = internalUpdate(l, updated);

    while (true) {
      if (updateGuard.compareAndSet(false, true)) {
        try {
          // This call must be outside lock.
          return CU.outTx(updateCall, ctx);
        } finally {
          lock.lock();

          try {
            updateGuard.set(false);

            cond.signalAll();
          } finally {
            lock.unlock();
          }
        }
      } else {
        lock.lock();

        try {
          while (locVal >= upBound && updateGuard.get()) {
            try {
              cond.await(500, MILLISECONDS);
            } catch (InterruptedException e) {
              throw new GridInterruptedException(e);
            }
          }

          checkRemoved();

          // If reserved range isn't exhausted.
          if (locVal + l <= upBound) {
            long curVal = locVal;

            locVal += l;

            return updated ? locVal : curVal;
          }
        } finally {
          lock.unlock();
        }
      }
    }
  }