コード例 #1
0
ファイル: Manager.java プロジェクト: JCortz/SD
  /*
   * 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;
  }
コード例 #2
0
ファイル: Manager.java プロジェクト: JCortz/SD
  /*
   * 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();
    }
  }
コード例 #3
0
 public void run() {
   while (true) {
     LinkedList<PacketCallbackStruct> list = null;
     try {
       queuedPacketCallbacksLock.lock();
       try {
         queuedPacketCallbacksNotEmpty.await();
       } catch (Exception e) {
         Log.error(
             "RDPServer.PacketCallbackThread: queuedPacketCallbacksNotEmpty.await() caught exception "
                 + e.getMessage());
       }
       list = queuedPacketCallbacks;
       queuedPacketCallbacks = new LinkedList<PacketCallbackStruct>();
     } finally {
       queuedPacketCallbacksLock.unlock();
     }
     if (Log.loggingNet)
       Log.net("RDPServer.PacketCallbackThread: Got " + list.size() + " queued packets");
     for (PacketCallbackStruct pcs : list) {
       try {
         callbackProcessPacket(pcs.cb, pcs.con, pcs.packet);
       } catch (Exception e) {
         Log.exception("RDPServer.PacketCallbackThread: ", e);
       }
     }
   }
 }
コード例 #4
0
ファイル: Manager.java プロジェクト: JCortz/SD
 /* Termina a sessão de um utilizador */
 public void logout(String nick) {
   lock.lock();
   try {
     this.users.get(nick).setAtivo(false);
   } finally {
     lock.unlock();
   }
 }
コード例 #5
0
  public void setDone(boolean b) {
    try {
      lock.lock();
      done = b;

      if (!done) cv.signal();
    } finally {
      lock.unlock();
    }
  }
コード例 #6
0
 private void awaitAvailableConnection(long timeout, TimeUnit unit) throws InterruptedException {
   waitLock.lock();
   waiter++;
   try {
     hasAvailableConnection.await(timeout, unit);
   } finally {
     waiter--;
     waitLock.unlock();
   }
 }
コード例 #7
0
  /**
   * Sets the value of statistic
   *
   * @param stat the name of the statistic
   * @param value the value of the statistic
   */
  public void setStat(String stat, Object value) {
    Lock lock = this.lock.writeLock();

    lock.lock();
    try {
      unlockedSetStat(stat, value);
    } finally {
      lock.unlock();
    }
  }
コード例 #8
0
  /** Notify the worker that new entry to delete appeared. */
  void signal() {
    lock.lock();

    try {
      force = true;

      cond.signalAll();
    } finally {
      lock.unlock();
    }
  }
コード例 #9
0
  private void signalAllAvailableConnection() {
    // Quick check if it's worth signaling to avoid locking
    if (waiter == 0) return;

    waitLock.lock();
    try {
      hasAvailableConnection.signalAll();
    } finally {
      waitLock.unlock();
    }
  }
コード例 #10
0
ファイル: Manager.java プロジェクト: JCortz/SD
 /* 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();
   }
 }
コード例 #11
0
  /**
   * Returns the map with the names of the statistics and their values.
   *
   * @return the map with the names of the statistics and their values.
   */
  public Map<String, Object> getStats() {
    Lock lock = this.lock.readLock();
    Map<String, Object> stats;

    lock.lock();
    try {
      stats = new HashMap<String, Object>(this.stats);
    } finally {
      lock.unlock();
    }
    return stats;
  }
コード例 #12
0
  /**
   * Returns the value of the statistic.
   *
   * @param stat the name of the statistic.
   * @return the value.
   */
  public Object getStat(String stat) {
    Lock lock = this.lock.readLock();
    Object value;

    lock.lock();
    try {
      value = stats.get(stat);
    } finally {
      lock.unlock();
    }
    return value;
  }
コード例 #13
0
ファイル: Manager.java プロジェクト: JCortz/SD
  /* 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();
    }
  }
コード例 #14
0
ファイル: Manager.java プロジェクト: JCortz/SD
  /*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();
    }
  }
コード例 #15
0
  /** {@inheritDoc} */
  @Override
  public void close() throws GridException {
    closeLock.lock();

    try {
      if (routineId == null)
        throw new IllegalStateException("Can't cancel query that was not executed.");

      ctx.kernalContext().continuous().stopRoutine(routineId).get();
    } finally {
      closeLock.unlock();
    }
  }
コード例 #16
0
ファイル: Manager.java プロジェクト: JCortz/SD
  /* 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;
  }
コード例 #17
0
ファイル: Manager.java プロジェクト: JCortz/SD
  /* 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;
  }
コード例 #18
0
  /** {@inheritDoc} */
  @Override
  protected void body() throws InterruptedException {
    if (log.isDebugEnabled()) log.debug("Delete worker started.");

    while (!cancelled) {
      lock.lock();

      try {
        if (!cancelled && !force) cond.await(FREQUENCY, TimeUnit.MILLISECONDS);

        force = false; // Reset force flag.
      } finally {
        lock.unlock();
      }

      if (!cancelled) delete();
    }
  }
コード例 #19
0
 public void run() {
   try {
     lock.lock();
     while (true) {
       try {
         if (done) {
           cv.await();
         } else {
           nextCharacter();
           cv.await(getPauseTime(), TimeUnit.MILLISECONDS);
         }
       } catch (InterruptedException ie) {
         return;
       }
     }
   } finally {
     lock.unlock();
   }
 }
コード例 #20
0
ファイル: Manager.java プロジェクト: JCortz/SD
 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();
   }
 }
コード例 #21
0
ファイル: Manager.java プロジェクト: JCortz/SD
  /*
   * 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();
    }
  }
コード例 #22
0
ファイル: DataPipe.java プロジェクト: heavenlyhash/ahslib
 private final void lockWrite() {
   $lock.lock();
 }
コード例 #23
0
  /** {@inheritDoc} */
  @Override
  public void execute(@Nullable GridProjection prj) throws GridException {
    if (cb == null)
      throw new IllegalStateException("Mandatory local callback is not set for the query: " + this);

    if (prj == null) prj = ctx.grid();

    prj = prj.forCache(ctx.name());

    if (prj.nodes().isEmpty())
      throw new GridTopologyException("Failed to execute query (projection is empty): " + this);

    GridCacheMode mode = ctx.config().getCacheMode();

    if (mode == LOCAL || mode == REPLICATED) {
      Collection<GridNode> nodes = prj.nodes();

      GridNode node = nodes.contains(ctx.localNode()) ? ctx.localNode() : F.rand(nodes);

      assert node != null;

      if (nodes.size() > 1 && !ctx.cache().isDrSystemCache()) {
        if (node.id().equals(ctx.localNodeId()))
          U.warn(
              log,
              "Continuous query for "
                  + mode
                  + " cache can be run only on local node. "
                  + "Will execute query locally: "
                  + this);
        else
          U.warn(
              log,
              "Continuous query for "
                  + mode
                  + " cache can be run only on single node. "
                  + "Will execute query on remote node [qry="
                  + this
                  + ", node="
                  + node
                  + ']');
      }

      prj = prj.forNode(node);
    }

    closeLock.lock();

    try {
      if (routineId != null)
        throw new IllegalStateException("Continuous query can't be executed twice.");

      guard.block();

      GridContinuousHandler hnd =
          new GridCacheContinuousQueryHandler<>(ctx.name(), topic, cb, filter, prjPred);

      routineId =
          ctx.kernalContext()
              .continuous()
              .startRoutine(hnd, bufSize, timeInterval, autoUnsubscribe, prj.predicate())
              .get();
    } finally {
      closeLock.unlock();
    }
  }