示例#1
0
  void handleGossip(Connection connection) throws IOException {
    ARRGCacheEntry peerEntry = new ARRGCacheEntry(connection.in());

    int receiveCount = connection.in().readInt();

    ARRGCacheEntry[] receivedEntries = new ARRGCacheEntry[receiveCount];

    for (int i = 0; i < receiveCount; i++) {
      receivedEntries[i] = new ARRGCacheEntry(connection.in());
    }

    connection.sendOKReply();

    self.writeTo(connection.out());

    ARRGCacheEntry[] sendEntries = cache.getRandomEntries(GOSSIP_SIZE, true);
    connection.out().writeInt(sendEntries.length);
    for (ARRGCacheEntry entry : sendEntries) {
      entry.writeTo(connection.out());
    }

    connection.out().flush();

    connection.close();

    cache.add(peerEntry);
    cache.add(receivedEntries);

    resetLastGossip();

    logger.debug("bootstrap service for " + poolName + " received request from " + peerEntry);
  }
示例#2
0
  private void gossip(VirtualSocketAddress victim, int timeout, boolean fillTimeout)
      throws IOException {
    long start = System.currentTimeMillis();

    if (victim == null) {
      logger.debug("no victim specified");
      return;
    }

    if (victim.equals(self.getAddress())) {
      logger.debug("not gossiping with outselves");
      return;
    }

    logger.debug("gossiping with " + victim);

    ARRGCacheEntry[] sendEntries = cache.getRandomEntries(GOSSIP_SIZE, true);

    Connection connection = null;
    try {

      connection = new Connection(victim, timeout, fillTimeout, socketFactory);

      // header

      connection.out().writeByte(Protocol.MAGIC_BYTE);
      connection.out().writeByte(Protocol.OPCODE_ARRG_GOSSIP);
      connection.out().writeUTF(poolName);

      // data

      self.writeTo(connection.out());

      connection.out().writeInt(sendEntries.length);
      for (ARRGCacheEntry entry : sendEntries) {
        entry.writeTo(connection.out());
      }

      connection.getAndCheckReply();

      ARRGCacheEntry peerEntry = new ARRGCacheEntry(connection.in());

      int receiveCount = connection.in().readInt();

      ARRGCacheEntry[] receivedEntries = new ARRGCacheEntry[receiveCount];

      for (int i = 0; i < receiveCount; i++) {
        receivedEntries[i] = new ARRGCacheEntry(connection.in());
      }

      connection.close();

      cache.add(peerEntry);
      cache.add(receivedEntries);

      resetLastGossip();

      if (statistics != null) {
        statistics.add(
            Protocol.OPCODE_ARRG_GOSSIP,
            System.currentTimeMillis() - start,
            connection.read(),
            connection.written(),
            false);
      }
    } finally {
      if (connection != null) {
        connection.close();
      }
    }
  }