Exemple #1
0
  @Test
  public void testInterestFilters()
      throws IOException, SecurityException, EncodingException, InterruptedException {
    class State {
      boolean regFailed = false;
      boolean regSucceed = false;
    }
    final State state = new State();

    // connect transport
    face.registerPrefix(
        new Name("/fake/prefix"),
        (OnInterestCallback) null,
        new OnRegisterFailed() {
          @Override
          public void onRegisterFailed(final Name prefix) {
            state.regFailed = true;
            counter++;
          }
        },
        new OnRegisterSuccess() {
          @Override
          public void onRegisterSuccess(final Name prefix, final long registeredPrefixId) {
            state.regSucceed = true;
            counter++;
          }
        });

    // set filter
    face.setInterestFilter(
        new InterestFilter("/a/b"),
        new OnInterestCallback() {
          @Override
          public void onInterest(
              final Name prefix,
              final Interest interest,
              final Face face,
              final long interestFilterId,
              final InterestFilter filter) {
            counter++;
          }
        });

    face.receive(new Interest(new Name("/a/b")).setInterestLifetimeMilliseconds(100));

    run(10, 2);

    assertEquals(2, counter);
    assertTrue(state.regSucceed);
    assertFalse(state.regFailed);
  }
Exemple #2
0
  @Test
  public void testExpressingAnInterestAfterConfiguration()
      throws IOException, EncodingException, InterruptedException {
    // add response (before face is connected)
    Data response = new Data(new Name("/test/with/responses"));
    response.setContent(new Blob("..."));
    face.receive(response);

    // make request
    expressInterest("/test/with/responses");

    run(20);

    assertNotNull(recvData);
    assertEquals(isTimeout, false);
    assertEquals(recvData.getName().toString(), "/test/with/responses");
    assertEquals(recvData.getContent().buf(), new Blob("...").buf());
  }
Exemple #3
0
  @Test
  public void testPrefixRegistration()
      throws IOException, SecurityException, EncodingException, InterruptedException {
    class State {
      boolean regFailed = false;
      boolean regSucceed = false;
    }
    final State state = new State();

    LOG.info("Register prefix: /test/with/handlers");
    face.registerPrefix(
        new Name("/test/with/handlers"),
        new OnInterestCallback() {
          @Override
          public void onInterest(
              final Name prefix,
              final Interest interest,
              final Face face,
              final long interestFilterId,
              final InterestFilter filter) {
            LOG.info("Received interest, responding: " + interest.getName().toUri());
            Data response = new Data(new Name("/test/with/handlers"));
            response.setContent(new Blob("..."));
            try {
              face.putData(response);
            } catch (IOException e) {
              exception = e;
            }
            counter++;
          }
        },
        new OnRegisterFailed() {
          @Override
          public void onRegisterFailed(final Name prefix) {
            LOG.info("Prefix registration fails: " + prefix);
            state.regFailed = true;
            counter++;
          }
        },
        new OnRegisterSuccess() {
          @Override
          public void onRegisterSuccess(final Name prefix, final long registeredPrefixId) {
            LOG.info("Prefix registration succeed: " + prefix);
            state.regSucceed = true;
            counter++;
          }
        });

    run(100, 1);
    assertTrue(state.regSucceed);
    assertFalse(state.regFailed);

    // make request
    face.receive(new Interest(new Name("/test/with/handlers")));

    run(100, 2);

    assertNull(exception);

    assertEquals(face.sentData.size(), 1);
    assertFalse(isTimeout);
    assertEquals("/test/with/handlers", face.sentData.get(0).getName().toString());
    assertEquals(new Blob("...").buf(), face.sentData.get(0).getContent().buf());
  }