@Test
  public void testNonConfirmable() throws Exception {
    // send request
    Request req2acc = new Request(Code.POST);
    req2acc.setConfirmable(false);
    req2acc.setURI("localhost:" + serverPort + "/" + ACC_RESOURCE);
    req2acc.setPayload("client says hi");
    req2acc.send();

    // receive response and check
    Response response = req2acc.waitForResponse(100);
    assertNotNull(response);
    assertEquals(response.getPayloadString(), SERVER_RESPONSE);
    assertEquals(response.getType(), Type.NON);

    Request req2noacc = new Request(Code.POST);
    req2noacc.setConfirmable(false);
    req2noacc.setURI("coap://localhost:" + serverPort + "/" + NO_ACC_RESOURCE);
    req2noacc.setPayload("client says hi");
    req2noacc.send();

    // receive response and check
    response = req2noacc.waitForResponse(100);
    assertNotNull(response);
    assertEquals(response.getPayloadString(), SERVER_RESPONSE);
    assertEquals(response.getType(), Type.NON);
  }
Beispiel #2
0
  @Override
  protected synchronized void executeRequest(
      Request request, String serverURI, String resourceUri) {

    // defensive check for slash
    if (!serverURI.endsWith("/") && !resourceUri.startsWith("/")) {
      resourceUri = "/" + resourceUri;
    }

    URI uri = null;
    try {
      uri = new URI(serverURI + resourceUri);
    } catch (URISyntaxException use) {
      throw new IllegalArgumentException("Invalid URI: " + use.getMessage());
    }

    request.setURI(uri);

    // for observing
    int observeLoop = 2;

    // print request info
    if (verbose) {
      System.out.println("Request for test " + this.testName + " sent");
      Utils.prettyPrint(request);
    }

    // execute the request
    try {
      Response response = null;
      boolean success = true;
      long time = 5000;

      request.send();

      System.out.println();
      System.out.println("**** TEST: " + testName + " ****");
      System.out.println("**** BEGIN CHECK ****");

      response = request.waitForResponse(time);
      if (response != null) {
        success &= checkType(Type.ACK, response.getType());
        success &= checkInt(EXPECTED_RESPONSE_CODE.value, response.getCode().value, "code");
        success &= checkToken(request.getToken(), response.getToken());
        success &= hasContentType(response);
        success &= hasNonEmptyPalyoad(response);
        success &= hasObserve(response);

        if (success) {

          time = response.getOptions().getMaxAge() * 1000;
          System.out.println("+++++ Max-Age: " + time + " +++++");
          if (time == 0) time = 5000;

          for (int l = 0; success && (l < observeLoop); ++l) {

            response = request.waitForResponse(time + 1000);

            // checking the response
            if (response != null) {
              System.out.println("Received notification " + l);

              // print response info
              if (verbose) {
                System.out.println("Response received");
                System.out.println("Time elapsed (ms): " + response.getRTT());
                Utils.prettyPrint(response);
              }

              success &= checkResponse(request, response);

            } else {
              System.out.println("FAIL: Notifications stopped");
              success = false;
              break;
            } // response != null
          } // observeLoop

          if (response != null) {

            System.out.println("+++++++ Canceling +++++++");
            request.cancel(); // stack should send RST

            Thread.sleep(time + time / 2);

          } else {
            System.out.println("FAIL: Notifications stopped");
            success = false;
          }
        }
      } else {
        System.out.println("FAIL: No notification after registration");
        success = false;
      }

      if (success) {
        System.out.println("**** TEST PASSED ****");
        addSummaryEntry(testName + ": PASSED (conditionally)");
      } else {
        System.out.println("**** TEST FAILED ****");
        addSummaryEntry(testName + ": --FAILED--");
      }

      tickOffTest();

    } catch (InterruptedException e) {
      System.err.println("Interupted during receive: " + e.getMessage());
      System.exit(-1);
    }
  }