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);