Beispiel #1
0
  /**
   * Initiates an information exchange with a known and living other base if such a base exists.
   *
   * @return true if refresh was succesful, false otherwise.
   */
  private boolean refresh() {

    final String logSender = observer + "#refresh";
    final String contrName = ((contributor != null) ? contributor.getName() : null);

    // --- refreshing local contribution and commands
    updateLocalInfo();

    // --- creating a random permutation of peers
    Vector peers = null;
    synchronized (cache) {
      peers = new Vector(cache.values());
      // just to be sure, shouldn't be there anyway
      if (contrName != null) peers.remove(cache.get(contrName));
    }
    Collections.shuffle(peers);
    if (peers.isEmpty()) {
      Logger.debug(logSender, "no peers in cache");
      return false;
    }

    // --- reset array representations
    cacheCollection = null;
    commandCollection = null;

    // --- trying to talk to random peer
    IRequest answer = null;
    Address peer = null;
    for (int i = 0; i < peers.size(); ++i) {
      if (!shouldLive) return false;

      peer = ((ContributionBox) peers.get(i)).contributor;
      Logger.debug(logSender, "asking " + peer);

      answer = observer.fireMessage(peer, "collectiveUpdate-" + name, this);
      while (answer.getStatus() == IRequest.WAITING) {
        try {
          Thread.sleep(100);
        } catch (Exception e) {
        }
      }
      if (answer.getStatus() == IRequest.DONE) break;
      Logger.debug(logSender, "not accessable: " + peer);
    }

    if (answer.getStatus() != IRequest.DONE) {
      Logger.debug(logSender, "no accessable peers");
      observer.collectiveUpdated(null);
      return false;
    } else {
      Collective c = (Collective) answer.getInfo("reply");

      // --- remove possible garbage
      if (contributor != null) {
        cache.remove(contributor.getName());
        c.cache.remove(contributor.getName());
      }
      cache.remove(peer.name);
      c.cache.remove(peer.name);

      repairSenderAddress(c, peer);
      merge(c);
      observer.collectiveUpdated((ContributionBox) cache.get(c.myContribution.contributor.name));
      return true;
    }
  }