Example #1
0
  /**
   * Removes randomly chosen entries until from the cache and commands until the size is not larger
   * than MAX_CACHE_SIZE and MAX_COMMANDS_SIZE respectively.
   */
  private void cutToSize() {

    if (MAX_CACHE_SIZE < cache.size())
      synchronized (cache) {
        Vector keyList = new Vector(cache.keySet());
        Collections.shuffle(keyList);
        while (MAX_CACHE_SIZE < cache.size()) {
          cache.remove(keyList.get(0));
          keyList.remove(0);
        }
      }
    if (MAX_COMMANDS_SIZE < commands.size())
      synchronized (commands) {
        Vector keyList = new Vector(commands.keySet());
        Collections.shuffle(keyList);
        while (MAX_COMMANDS_SIZE < commands.size()) {
          commands.remove(keyList.get(0));
          keyList.remove(0);
        }
      }
  }
Example #2
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;
    }
  }