Пример #1
0
  /**
   * Switch the given connection to the given pool.
   *
   * @param connection
   * @param newPool
   */
  public void switchPoolForConnection(WorkerConnection connection, Pool newPool)
      throws TooManyWorkersException, ChangeExtranonceNotSupportedException {
    // If the old pool is the same as the new pool, do nothing.
    if (!newPool.equals(connection.getPool())) {
      // Remove the connection from the old pool connection list.
      Set<WorkerConnection> oldPoolConnections = getPoolWorkerConnections(connection.getPool());
      if (oldPoolConnections != null) {
        oldPoolConnections.remove(connection);
      }

      // Then rebind the connection to this pool. An exception is thrown
      // if the rebind fails since the connection does not support the
      // extranonce change.
      connection.rebindToPool(newPool);

      // And finally add the worker connection to the pool's worker
      // connections
      Set<WorkerConnection> newPoolConnections = getPoolWorkerConnections(newPool);
      newPoolConnections.add(connection);

      // Ask to the pool to authorize the worker
      // Create a fake authorization request since when a connection is
      // rebound, the miner does not send auhtorization request (since it
      // has already done it). But it may be the first time this
      // connection is bound to this pool, so the username on this
      // connection is not yet authorized on the pool.
      for (Entry<String, String> entry : connection.getAuthorizedWorkers().entrySet()) {
        MiningAuthorizeRequest fakeRequest = new MiningAuthorizeRequest();
        fakeRequest.setUsername(entry.getKey());
        fakeRequest.setPassword(entry.getValue());
        try {
          onAuthorizeRequest(connection, fakeRequest);
        } catch (AuthorizationException e) {
          LOGGER.error(
              "Authorization of user {} failed on pool {} when rebinding connection {}. Closing the connection. Cause: {}",
              entry.getKey(),
              newPool.getName(),
              connection.getConnectionName(),
              e.getMessage());
          connection.close();
        }
      }
    }
  }
  @Override
  public void onPoolUpdated(Pool poolUpdated) {
    List<Pool> pools = getProxyManager().getPools();
    checkPoolPriorities(pools);
    Collections.sort(
        pools,
        new Comparator<Pool>() {
          public int compare(Pool o1, Pool o2) {
            return o1.getPriority().compareTo(o2.getPriority());
          }
        });
    int newPriority = poolUpdated.getPriority();
    int previousPriority = newPriority;
    for (Pool pool : pools) {
      // Move the priority of other pools with lower or
      // equals priority
      if (pool.getPriority() == previousPriority && !pool.equals(poolUpdated)) {
        pool.setPriority(pool.getPriority() + 1);
        previousPriority = pool.getPriority();
      }
    }

    super.onPoolUpdated(poolUpdated);
  }