예제 #1
0
  @Test
  public void testStorables() throws Exception {
    Context dht = (Context) MojitoFactory.createDHT("bootstrap");
    dht.getStorableModelManager().addStorableModel(DHTValueType.TEXT, new Dht.Model());
    dht.bind(new InetSocketAddress("localhost", 8080));
    dht.start();

    Context node = (Context) MojitoFactory.createDHT("node");
    node.getStorableModelManager().addStorableModel(DHTValueType.TEXT, new Dht.Model());
    node.bind(new InetSocketAddress("localhost", 8081));
    node.start();
    node.bootstrap(new InetSocketAddress("localhost", 8080)).get();
    assertTrue(node.isBootstrapped());

    DHTValueImpl value =
        new DHTValueImpl(DHTValueType.TEXT, Version.ZERO, "hello world".getBytes());

    node.put(Keys.of("key"), value).get();

    FindValueResult result =
        node.get(EntityKey.createEntityKey(Keys.of("key"), DHTValueType.TEXT)).get();

    assertTrue(result.isSuccess());
    assertEquals(1, result.getEntities().size());
    assertEquals(
        "hello world", new String(result.getEntities().iterator().next().getValue().getValue()));

    node.close();
    dht.close();
  }
예제 #2
0
  @Test
  public void testCreatorAddressIsCorrect() throws Exception {
    Context root = (Context) MojitoFactory.createDHT("bootstrap");
    root.bind(new InetSocketAddress("localhost", 8081));
    root.start();

    Context dht = (Context) MojitoFactory.createDHT("dht");
    dht.bind(new InetSocketAddress("localhost", 8080));
    dht.start();
    dht.bootstrap(new InetSocketAddress("localhost", 8081)).get();
    assertTrue(dht.isBootstrapped());

    DHTValueImpl value =
        new DHTValueImpl(DHTValueType.TEXT, Version.ZERO, "hello world".getBytes());

    StoreResult store = dht.put(Keys.of("key"), value).get();

    assertEquals(2, store.getLocations().size());

    FindValueResult result =
        dht.get(EntityKey.createEntityKey(Keys.of("key"), DHTValueType.TEXT)).get();

    assertTrue(result.isSuccess());
    assertEquals(1, result.getEntities().size());
    DHTValueEntity entity = result.getEntities().iterator().next();
    assertEquals(
        InetSocketAddress.createUnresolved("localhost", 8080),
        entity.getCreator().getContactAddress());
    assertEquals(
        InetSocketAddress.createUnresolved("localhost", 8081),
        entity.getSender().getContactAddress());

    root.close();
    dht.close();
  }
  private boolean extractDataFromResponse(FindValueResponse response) {

    Contact sender = response.getContact();

    Collection<KUID> availableSecondaryKeys = response.getSecondaryKeys();
    Collection<? extends DHTValueEntity> entities = response.getDHTValueEntities();

    // No keys and no values? In other words the remote Node sent us
    // a FindValueResponse even though it doesn't have a value for
    // the given KUID!? Continue with the lookup if so...!
    if (availableSecondaryKeys.isEmpty() && entities.isEmpty()) {
      if (LOG.isWarnEnabled()) {
        LOG.warn(sender + " returned neither keys nor values for " + lookupId);
      }

      // Continue with the lookup...
      return false;
    }

    Collection<? extends DHTValueEntity> filtered =
        DatabaseUtils.filter(lookupKey.getDHTValueType(), entities);

    // The filtered Set is empty and the unfiltered isn't?
    // The remote Node send us unrequested Value(s)!
    // Continue with the lookup if so...!
    if (filtered.isEmpty() && !entities.isEmpty()) {
      if (LOG.isWarnEnabled()) {
        LOG.warn(sender + " returned unrequested types of values for " + lookupId);
      }

      // Continue with the lookup...
      return false;
    }

    this.entities.addAll(filtered);

    for (KUID secondaryKey : availableSecondaryKeys) {
      EntityKey key =
          EntityKey.createEntityKey(sender, lookupId, secondaryKey, lookupKey.getDHTValueType());

      this.entityKeys.add(key);
    }

    return true;
  }