Package ch.ethz.inf.vs.californium.coap

Examples of ch.ethz.inf.vs.californium.coap.Response


  }
 
  private Response getNextResponseBlock(Response response, BlockwiseStatus status) {
    int szx = status.getCurrentSzx();
    int num = status.getCurrentNum();
    Response block = new Response(response.getCode());
//    block.setType(response.getType()); // NO! First block has type from origin response, all other depend on current request
    block.setDestination(response.getDestination());
    block.setDestinationPort(response.getDestinationPort());
    block.setToken(response.getToken());
    block.setOptions(new OptionSet(response.getOptions()));
    block.addMessageObserver(new TimeoutForwarder(response));
   
    if (response.getPayloadSize() > 0) {
      int currentSize = 1 << (4 + szx);
      int from = num * currentSize;
      int to = Math.min((num + 1) * currentSize, response.getPayloadSize());
      int length = to - from;
      byte[] blockPayload = new byte[length];
      System.arraycopy(response.getPayload(), from, blockPayload, 0, length);
      block.setPayload(blockPayload);
     
      boolean m = (to < response.getPayloadSize());
      block.getOptions().setBlock2(szx, m, num);
      block.setLast(!m);
     
      status.setComplete(!m);
    } else {
      block.getOptions().setBlock2(szx, false, 0);
      block.setLast(true);
      status.setComplete(true);
    }
    return block;
  }
View Full Code Here


      Utils.prettyPrint(request);
    }

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

      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 &= hasContentType(response);
        success &= checkToken(request.getToken(), response.getToken());
        success &= hasObserve(response);

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

      // receive multiple responses
      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);
         
          if (l==1) {
                  System.out.println("+++++ Unrelated GET +++++");
            // GET with different Token
            Request asyncRequest = Request.newGet();
            asyncRequest.setURI(uri);
            asyncRequest.send();
            response = asyncRequest.waitForResponse(time/2);
            if (response!=null) {
              success &= checkToken(asyncRequest.getToken(), response.getToken());
              success &= hasObserve(response, true); // inverted
                      System.out.println("+++++ OK +++++");
            } else {
                      System.out.println("FAIL: No response to unrelated GET");
              success = false;
View Full Code Here

      Utils.prettyPrint(request);
    }

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

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

      // Part A
      request.send();
      response = request.waitForResponse(6000);

      // checking the response
      if (response != null) {

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

        success &= checkType(Type.ACK, response.getType());
        success &= checkInt(EXPECTED_RESPONSE_CODE_A.value,
            response.getCode().value, "code");

        // Part B
        request = new Request(Code.PUT, Type.CON);
        // request.setIfNoneMatch();
        request.getOptions().setIfNoneMatch(true);
        request.setPayload("TD_COAP_CORE_23 Part B",
            MediaTypeRegistry.TEXT_PLAIN);

        request.setURI(uri);

        request.send();
        response = request.waitForResponse(6000);

        // checking the response
        if (response != null) {

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

          success &= checkType(Type.ACK, response.getType());
          success &= checkInt(EXPECTED_RESPONSE_CODE_B.value,
              response.getCode().value, "code");

        }
      }

      if (success) {
View Full Code Here

      Utils.prettyPrint(request);
    }

    // execute the request
    try {
      Response response = null;
      boolean success = true;
     
      request.send();
     
      response = request.waitForResponse(wait);
View Full Code Here

     
      request.send();
     
      // receive response
      try {
        Response response = request.waitForResponse(1000);
       
        if (response != null) {
          // response received, output a pretty-print
          System.out.println(response);
        } else {
View Full Code Here

   
    // get request to read out details
    Request request = exchange.advanced().getRequest();

    // successively create response
    Response response;
   
    if (exchange.getRequestOptions().containsETag(etag)) {
     
      response = new Response(VALID);
      response.getOptions().addETag(etag.clone());
     
      // automatically change now
      storeData(null, UNDEFINED);
    } else {
      response = new Response(CONTENT);

      if (data==null) {
        etag = ByteBuffer.allocate(2).putShort( (short) (Math.random()*0x10000) ).array();
       
        StringBuilder payload = new StringBuilder();
        payload.append(
            String.format(
                "Type: %d (%s)\nCode: %d (%s)\nMID: %d",
                request.getType().value,
                request.getType(),
                request.getCode().value,
                request.getCode(),
                request.getMID()));
   
        if (request.getToken().length > 0) {
          payload.append("\nToken: ");
          payload.append(request.getTokenString());
        }
       
        if (payload.length() > 64) {
          payload.delete(63, payload.length());
          payload.append('»');
        }
        response.setPayload(payload.toString());
        response.getOptions().setContentFormat(TEXT_PLAIN);
      } else {
        response.setPayload(data);
        response.getOptions().setContentFormat(dataCf);
      }
      response.getOptions().addETag(etag.clone());
    }
    exchange.respond(response);
  }
View Full Code Here

   
    // get request to read out details
    Request request = exchange.advanced().getRequest();
   
    // successively create response
    Response response = new Response(CONTENT);

    String format = "";
    switch (exchange.getRequestOptions().getAccept()) {
      case UNDEFINED:
      case TEXT_PLAIN:
        response.getOptions().setContentFormat(TEXT_PLAIN);
        format = "Status type: \"%s\"\nCode: \"%s\"\nMID: \"%s\"\nAccept: \"%s\"";
        break;
 
      case APPLICATION_XML:
        response.getOptions().setContentFormat(APPLICATION_XML);
        format = "<msg type=\"%s\" code=\"%s\" mid=%s accept=\"%s\"/>"; // should fit 64 bytes
        break;
 
      default:
        response = new Response(NOT_ACCEPTABLE);
        format = "text/plain or application/xml only";
        break;
    }
   
    response.setPayload(
        String.format(format,
            request.getType(),
            request.getCode(),
            request.getMID(),
            MediaTypeRegistry.toString(request.getOptions().getAccept()))
View Full Code Here

    super(name);
  }
 
  @Override
  public void handleRequest(Exchange exchange) {
    Response response = new Response(ResponseCode.CONTENT);
    response.setPayload("hello world");
    exchange.sendResponse(response);
  }
View Full Code Here

  public static void selfTest() {
    try {
      Request request = Request.newGet();
      request.setURI("localhost:5683/benchmark");
      request.send();
      Response response = request.waitForResponse(1000);
      System.out.println("received "+response);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
View Full Code Here

  public static void selfTest() {
    try {
      Request request = Request.newGet();
      request.setURI("localhost:5683/hello");
      request.send();
      Response response = request.waitForResponse(1000);
      System.out.println("received "+response);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
View Full Code Here

TOP

Related Classes of ch.ethz.inf.vs.californium.coap.Response

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.