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

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


    } else {
      int maxNUM = response.getOptions().getBlock2().getNum();
      success &= checkType(Type.ACK, response.getType());
      success &= checkInt(EXPECTED_RESPONSE_CODE.value,
          response.getCode().value, "code");
      success &= checkOption(new BlockOption(PlugtestChecker.PLUGTEST_BLOCK_SZX,
          false, maxNUM), response.getOptions().getBlock2(),
          "Block2");
      success &= hasNonEmptyPalyoad(response);
      success &= hasContentType(response);
    }
View Full Code Here


      System.out.println("FAIL: no Block1 option");
    } else {
      int maxNUM = response.getOptions().getBlock1().getNum();
      success &= checkInt(EXPECTED_RESPONSE_CODE.value,
          response.getCode().value, "code");
      success &= checkOption(new BlockOption(PlugtestChecker.PLUGTEST_BLOCK_SZX,
          false, maxNUM), response.getOptions().getBlock1(),
          "Block1");
      success &= hasLocation(response);
    }
View Full Code Here

    } else {
      int maxNUM = response.getOptions().getBlock2().getNum();
      success &= checkType(Type.ACK, response.getType());
      success &= checkInt(EXPECTED_RESPONSE_CODE.value,
          response.getCode().value, "code");
      success &= checkOption(new BlockOption(EXPECTED_BLOCK_SIZE,
          false, maxNUM), response.getOptions().getBlock2(),
          "Block2");
      success &= hasNonEmptyPalyoad(response);
      success &= hasContentType(response);
    }
View Full Code Here

   
    // use the specified message type
    request.setType(this.type);

    if (blockwise!=0) {
      request.getOptions().setBlock2(new BlockOption(BlockOption.size2Szx(this.blockwise), false, 0));
    }
   
    if (endpoint != null)
      endpoint.sendRequest(request);
    else request.send();
View Full Code Here

  @Override
  public void receiveRequest(Exchange exchange, Request request) {
    if (request.getOptions().hasBlock1()) {
      // This must be a large POST or PUT request
      BlockOption block1 = request.getOptions().getBlock1();
      LOGGER.fine("Request contains block1 option "+block1);
     
      BlockwiseStatus status = findRequestBlockStatus(exchange, request);
      if (block1.getNum() == 0 && status.getCurrentNum() > 0) {
        // reset the blockwise transfer
        LOGGER.finer("Block1 num is 0, the client has restarted the blockwise transfer. Reset status.");
        status = new BlockwiseStatus(request.getOptions().getContentFormat());
        exchange.setRequestBlockStatus(status);
      }
     
      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();
     
View Full Code Here

    }
  }

  @Override
  public void sendResponse(Exchange exchange, Response response) {
    BlockOption block1 = exchange.getBlock1ToAck();
    if (block1 != null)
      exchange.setBlock1ToAck(null);
   
    if (requireBlockwise(exchange, response)) {
      // This must be a large response to a GET or POST request (PUT?)
View Full Code Here

      return;
    }
   
    if (response.getOptions().hasBlock1()) {
      // TODO: What if request has not been sent blockwise (server error)
      BlockOption block1 = response.getOptions().getBlock1();
      LOGGER.finer("Response acknowledges block "+block1);
     
      BlockwiseStatus status = exchange.getRequestBlockStatus();
      if (! status.isComplete()) {
        // TODO: the response code should be CONTINUE. Otherwise deliver
        // Send next block
        int currentSize = 1 << (4 + status.getCurrentSzx());
        int nextNum = status.getCurrentNum() + currentSize / block1.getSize();
        LOGGER.finer("Send next block num = "+nextNum);
        status.setCurrentNum(nextNum);
        status.setCurrentSzx(block1.getSzx());
        Request nextBlock = getNextRequestBlock(exchange.getRequest(), status);
        if (nextBlock.getToken() == null)
          nextBlock.setToken(response.getToken()); // reuse same token
        exchange.setCurrentRequest(nextBlock);
        super.sendRequest(exchange, nextBlock);
        // do not deliver response
       
      } else if (!response.getOptions().hasBlock2()) {
        // All request block have been acknowledged and we receive a piggy-backed
        // response that needs no blockwise transfer. Thus, deliver it.
        super.receiveResponse(exchange, response);
      } else {
        LOGGER.fine("Response has Block2 option and is therefore sent blockwise");
      }
    }
   
    if (response.getOptions().hasBlock2()) {
      BlockOption block2 = response.getOptions().getBlock2();
      BlockwiseStatus status = findResponseBlockStatus(exchange, response);
     
      if (block2.getNum() == status.getCurrentNum()) {
        // We got the block we expected :-)
        status.addBlock(response.getPayload());
        if (response.getOptions().hasObserve())
          status.setObserve(response.getOptions().getObserve());
       
        if (block2.isM()) {
          LOGGER.finer("Request the next response block");
          // TODO: If this is a notification, do we have to use
          // another token now?

          Request request = exchange.getRequest();
          int num = block2.getNum() + 1;
          int szx = block2.getSzx();
          boolean m = false;
          Request block = new Request(request.getCode());
          block.setOptions(new OptionSet(request.getOptions()));
          block.setDestination(request.getDestination());
          block.setDestinationPort(request.getDestinationPort());
         
          block.setType(request.getType()); // NON could make sense over SMS or similar transports
          block.getOptions().setBlock2(szx, m, num);
          status.setCurrentNum(num);
         
          // to make it easier for Observe, we do not re-use the Token
//          if (!response.getOptions().hasObserve()) {
//            block.setToken(request.getToken());
//          }
         
          // make sure not to use Observe for block retrieval
          block.getOptions().removeObserve();
         
          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);
          }
         
          LOGGER.fine("Assembled response: "+assembled);
          exchange.setResponse(assembled);
          super.receiveResponse(exchange, assembled);
        }
       
      } else {
        // ERROR, wrong block number (server error)
        // TODO: This scenario is not specified in the draft.
        // Currently, we reject it and cancel the request.
        LOGGER.warning("Wrong block number. Expected "+status.getCurrentNum()+" but received "+block2.getNum()+". Reject response; exchange has failed.");
        if (response.getType()==Type.CON) {
          EmptyMessage rst = EmptyMessage.newRST(response);
          super.sendEmptyMessage(exchange, rst);
        }
        exchange.getRequest().cancel();
View Full Code Here

 
  private void earlyBlock2Negotiation(Exchange exchange, Request request) {
    // Call this method when a request has completely arrived (might have
    // been sent in one piece without blockwise).
    if (request.getOptions().hasBlock2()) {
      BlockOption block2 = request.getOptions().getBlock2();
      LOGGER.fine("Request demands blockwise transfer of response with option "+block2+". Create and set new block2 status");
      BlockwiseStatus status2 = new BlockwiseStatus(request.getOptions().getContentFormat(), block2.getNum(), block2.getSzx());
      exchange.setResponseBlockStatus(status2);
    }
  }
View Full Code Here

    } else {
      int maxNUM = response.getOptions().getBlock1().getNum();
      success &= checkType(Type.ACK, response.getType());
      success &= checkInt(EXPECTED_RESPONSE_CODE.value,
          response.getCode().value, "code");
      success &= checkOption(new BlockOption(PlugtestChecker.PLUGTEST_BLOCK_SZX,
          false, maxNUM), response.getOptions().getBlock1(),
          "Block1");
    }

    return success;
View Full Code Here

TOP

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

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.