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

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


    return request;
  }
 
  public Response parseResponse() {
    assert(isResponse());
    Response response = new Response(ResponseCode.valueOf(code));
    parseMessage(response);
    return response;
  }
View Full Code Here


     
      // The decision whether to postpone this notification or not and the
      // decision which notification is the youngest to send next must be
      // synchronized
      synchronized (exchange) {
        Response current = relation.getCurrentControlNotification();
        if (current != null && isInTransit(current)) {
          LOGGER.fine("A former notification is still in transit. Postpone " + response);
          relation.setNextControlNotification(response);
          return;
        } else {
View Full Code Here

   
    @Override
    public void onAcknowledgement() {
      synchronized (exchange) {
        ObserveRelation relation = exchange.getRelation();
        Response next = relation.getNextControlNotification();
        relation.setCurrentControlNotification(next); // next may be null
        relation.setNextControlNotification(null);
        if (next != null) {
          LOGGER.fine("Notification has been acknowledged, send the next one");
          ObserveLayer.super.sendResponse(exchange, next); // TODO: make this as new task?
View Full Code Here

   
    @Override
    public void onRetransmission() {
      synchronized (exchange) {
        final ObserveRelation relation = exchange.getRelation();
        final Response next = relation.getNextControlNotification();
        if (next != null) {
          LOGGER.fine("The notification has timed out and there is a younger notification. Send the younger one");
          relation.setNextControlNotification(null);
          // Send the next notification
          response.cancel();
          Type nt = next.getType();
          if (nt != Type.CON); {
            LOGGER.finer("The next notification's type was "+nt+". Since it replaces a CON control notification, it becomes a CON as well");
            prepareSelfReplacement(exchange, next);
            next.setType(Type.CON); // Force the next to be a Confirmable as well
          }
          relation.setCurrentControlNotification(next);
          // Create a new task for sending next response so that we can leave the sync-block
          executor.execute(new Runnable() {
            public void run() {
View Full Code Here

   * @param request the request
   * @return the CoAP response
   */
  private CoapResponse synchronous(Request request) {
    try {
      Response response = send(request).waitForResponse(getTimeout());
      if (response == null) return null;
      else return new CoapResponse(response);
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
View Full Code Here

      } else {
        resource.handleRequest(exchange);
      }
    } else {
      LOGGER.info("Did not find resource " + path.toString());
      exchange.sendResponse(new Response(ResponseCode.NOT_FOUND));
    }
  }
View Full Code Here

          ongoingExchanges.remove(uriKey);
        }
        // TODO: What if the request is only a block?
        // TODO: This should only happen if the transfer was blockwise

        Response response = exchange.getResponse();
        if (response != null) {
          // only response MIDs are stored for ACK and RST, no reponse Tokens
          KeyMID midKey = new KeyMID(response.getMID(),
              response.getDestination().getAddress(), response.getDestinationPort());
//          LOGGER.warning("++++++++++++++++++Remote ongoing completed, cleaning up "+midKey);
          exchangesByMID.remove(midKey);
        }
      }
    }
View Full Code Here

      if (block1.getNum() == status.getCurrentNum()) {
       
        if (request.getOptions().getContentFormat()==status.getContentFormat()) {
          status.addBlock(request.getPayload());
        } else {
          Response error = Response.createPiggybackedResponse(request, ResponseCode.REQUEST_ENTITY_INCOMPLETE);
          error.getOptions().setBlock1(block1.getSzx(), block1.isM(), block1.getNum());
          error.setPayload("Changed Content-Format");
          request.setAcknowledged(true);
          exchange.setCurrentResponse(error);
          super.sendResponse(exchange, error);
          return;
        }
       
        status.setCurrentNum(status.getCurrentNum() + 1);
        if ( block1.isM() ) {
          LOGGER.finest("There are more blocks to come. Acknowledge this block.");
         
          if (request.isConfirmable()) {
            Response piggybacked = Response.createPiggybackedResponse(request, ResponseCode.CONTINUE);
            piggybacked.getOptions().setBlock1(block1.getSzx(), true, block1.getNum());
            piggybacked.setLast(false);
            request.setAcknowledged(true);
            exchange.setCurrentResponse(piggybacked);
            super.sendResponse(exchange, piggybacked);
          }
          // do not assemble and deliver the request yet
         
        } else {
          LOGGER.finer("This was the last block. Deliver request");
         
          // Remember block to acknowledge. TODO: We might make this a boolean flag in status.
          exchange.setBlock1ToAck(block1);
         
          // Block2 early negotiation
          earlyBlock2Negotiation(exchange, request);
         
          // Assemble and deliver
          Request assembled = new Request(request.getCode()); // getAssembledRequest(status, request);
          assembleMessage(status, assembled, request);
//          assembled.setAcknowledged(true); // TODO: prevents accept from sending ACK. Maybe the resource uses separate...
          exchange.setRequest(assembled);
          super.receiveRequest(exchange, assembled);
        }
       
      } else {
        // ERROR, wrong number, Incomplete
        LOGGER.warning("Wrong block number. Expected "+status.getCurrentNum()+" but received "+block1.getNum()+". Respond with 4.08 (Request Entity Incomplete)");
        Response error = Response.createPiggybackedResponse(request, ResponseCode.REQUEST_ENTITY_INCOMPLETE);
        error.getOptions().setBlock1(block1.getSzx(), block1.isM(), block1.getNum());
        error.setPayload("Wrong block number");
        request.setAcknowledged(true);
        exchange.setCurrentResponse(error);
        super.sendResponse(exchange, error);
      }
     
    } else if (exchange.getResponse()!=null && request.getOptions().hasBlock2()) {
      // The response has already been generated and the client just wants
      // the next block of it
      BlockOption block2 = request.getOptions().getBlock2();
      Response response = exchange.getResponse();
      BlockwiseStatus status = findResponseBlockStatus(exchange, response);
      status.setCurrentNum(block2.getNum());
      status.setCurrentSzx(block2.getSzx());
     
      Response block = getNextResponseBlock(response, status);
      block.setToken(request.getToken());
      block.getOptions().removeObserve();
     
      if (status.isComplete()) {
        // clean up blockwise status
        LOGGER.severe("Ongoing is complete "+status);
        exchange.setResponseBlockStatus(null);
View Full Code Here

      // This must be a large response to a GET or POST request (PUT?)
      LOGGER.fine("Response payload "+response.getPayloadSize()+"/"+maxMsgSize+" requires Blockwise");
     
      BlockwiseStatus status = findResponseBlockStatus(exchange, response);
     
      Response block = getNextResponseBlock(response, status);
      block.setType(response.getType()); // This is only true for the first block
      if (block1 != null) // in case we still have to ack the last block1
        block.getOptions().setBlock1(block1);
      if (block.getToken() == null)
        block.setToken(exchange.getRequest().getToken());
     
      if (response.getOptions().hasObserve()) {
        // the ACK for the first block should acknowledge the whole notification
        exchange.setCurrentResponse(response);
      } else {
View Full Code Here

          exchange.setCurrentRequest(block);
          super.sendRequest(exchange, block);
         
        } else {
          LOGGER.finer("We have received all "+status.getBlockCount()+" blocks of the response. Assemble and deliver");
          Response assembled = new Response(response.getCode());
          assembleMessage(status, assembled, response);
          assembled.setType(response.getType());
         
          // Check if this response is a notification
          int observe = status.getObserve();
          if (observe != BlockwiseStatus.NO_OBSERVE) {
            assembled.getOptions().setObserve(observe);
            // This is necessary for notifications that are sent blockwise:
            // Reset block number AND container with all blocks
            exchange.setResponseBlockStatus(null);
          }
         
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.