/** * Converts a BlockOption with the specified parameters to a byte array and * back and checks that the result is the same as the original. */ private static void testCombined(int szx, boolean m, int num) { BlockOption block = new BlockOption(szx, m, num); BlockOption copy = new BlockOption(block.getValue()); assertEquals(block.getSzx(), copy.getSzx()); assertEquals(block.isM(), copy.isM()); assertEquals(block.getNum(), copy.getNum()); System.out.println(Utils.toHexString(block.getValue()) +" == " + "(szx="+block.getSzx()+", m="+block.isM()+", num="+block.getNum()+")"); }
public String toString() { BlockOption option = new BlockOption(BlockOption.size2Szx(size), m, num); return "Expected Block2 option: " + option; } });
/** * Gets the offset into a body this block option represents. * * @return The offset calculated as num * size. */ public int getOffset() { return num * szx2Size(szx); }
/** * Instantiates a new block option with the same values as the specified * block option. * * @param origin the origin * @throws NullPointerException if the specified block option is null */ public BlockOption(BlockOption origin) { if (origin == null) throw new NullPointerException(); this.setSzx(origin.getSzx()); this.setM(origin.isM()); this.setNum(origin.getNum()); }
/** * Instantiates a new block option with the same values as the specified * block option. * * @param origin the origin * @throws NullPointerException if the specified block option is null */ public BlockOption(final BlockOption origin) { if (origin == null) { throw new NullPointerException(); } else { this.szx = origin.getSzx(); this.m = origin.isM(); this.num = origin.getNum(); } }
@Override protected boolean checkResponse(Request request, Response response) { boolean success = response.getOptions().hasBlock1(); if (!success) { 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); } return success; } }
private static void earlyBlock2Negotiation(final Exchange exchange, final Request request) { // Call this method when a request has completely arrived (might have // been sent in one piece without blockwise). BlockOption block2 = request.getOptions().getBlock2(); if (block2 != null) { BlockwiseStatus status2 = new BlockwiseStatus(request.getOptions().getContentFormat(), block2.getNum(), block2.getSzx()); LOGGER.log(Level.FINE, "Request with early block negotiation {0}. Create and set new Block2 status: {1}", new Object[]{block2, status2}); exchange.setResponseBlockStatus(status2); } }
/** * Sets the Block1 option based on its encoded blob. * Returns the current OptionSet object for a fluent API. * * @param value the encoded value * @return this OptionSet */ public OptionSet setBlock2(byte[] value) { this.block2 = new BlockOption(value); return this; }
int nextNum = status.getCurrentNum() + currentSize / block1.getSize(); LOGGER.finer("Sending next Block1 num="+nextNum); status.setCurrentNum(nextNum); status.setCurrentSzx(block1.getSzx()); Request nextBlock = getNextRequestBlock(exchange.getRequest(), status); if (response.getOptions().hasObserve() && block2.getNum()==0 && status.getCurrentNum()!=0) { if (block2.getNum() == status.getCurrentNum() && (block2.getNum()==0 || Arrays.equals(response.getToken(), exchange.getCurrentRequest().getToken()))) { super.receiveResponse(exchange, response); } else if (block2.isM()) { int num = block2.getNum() + 1; int szx = block2.getSzx(); boolean m = false; LOGGER.warning("Wrong block number. Expected "+status.getCurrentNum()+" but received "+block2.getNum()+": "+response); if (response.getType()==Type.CON) { EmptyMessage rst = EmptyMessage.newRST(response);
public CB01(String serverURI) { super(CB01.class.getSimpleName()); Request request = Request.newGet(); request.getOptions().setBlock2(BlockOption.size2Szx(64), false, 0); // set the parameters and execute the request executeRequest(request, serverURI, "/large"); }
System.out.println("Request block number " + num); int szx = BlockOption.size2Szx(16); Request request = Request.newGet(); request.setURI(uri); Assert.assertEquals(expectations[i], response.getPayloadString()); Assert.assertTrue(response.getOptions().hasBlock2()); Assert.assertEquals(num, response.getOptions().getBlock2().getNum()); Assert.assertEquals(szx, response.getOptions().getBlock2().getSzx());
/** * Helper function that creates a BlockOption with the specified parameters * and serializes them to a byte array. */ private static byte[] toBytes(int szx, boolean m, int num) { byte[] bytes = new BlockOption(szx, m, num).getValue(); System.out.println("(szx="+szx+", m="+m+", num="+num+") => " + Utils.toHexString(bytes)); return bytes; }
private void sendNextBlock(final Exchange exchange, final Response response, final BlockOption block1, final BlockwiseStatus requestStatus) { // Send next block int currentSize = 1 << (4 + requestStatus.getCurrentSzx()); // Define new size of the block depending of preferred size block int newSize, newSzx; if (block1.getSize() < currentSize) { newSize = block1.getSize(); newSzx = block1.getSzx(); } else { newSize = currentSize; newSzx = requestStatus.getCurrentSzx(); } int nextNum = requestStatus.getCurrentNum() + currentSize / newSize; LOGGER.log(Level.FINER, "Sending next Block1 num={0}", nextNum); requestStatus.setCurrentNum(nextNum); requestStatus.setCurrentSzx(newSzx); Request nextBlock = getNextRequestBlock(exchange.getRequest(), requestStatus); // indicate overall body size to peer nextBlock.getOptions().setSize1(exchange.getRequest().getPayloadSize()); // we use the same token to ease traceability nextBlock.setToken(response.getToken()); exchange.setCurrentRequest(nextBlock); lower().sendRequest(exchange, nextBlock); // do not deliver response }
if (block1.isM()) {
protected boolean checkOption(BlockOption expected, BlockOption actual, String optionName) { boolean success = expected == null ? actual == null : expected .equals(actual); if (!success) { System.out.println("FAIL: option " + optionName + ": expected " + expected + " but was " + actual); } else { System.out.println("PASS: Correct option " + actual); } return success; }
if (block1.getNum() == 0 && status.getCurrentNum() > 0) { if (block1.getNum() == status.getCurrentNum()) { if ( block1.isM() ) { LOGGER.finest("There are more blocks to come. Acknowledge this block."); piggybacked.getOptions().setBlock1(block1.getSzx(), true, block1.getNum()); piggybacked.setLast(false); error.getOptions().setBlock1(block1.getSzx(), block1.isM(), block1.getNum()); error.setPayload("unexpected Content-Format"); new Object[]{status.getCurrentNum(), block1.getNum()}); Response error = Response.createResponse(request, ResponseCode.REQUEST_ENTITY_INCOMPLETE); error.getOptions().setBlock1(block1.getSzx(), block1.isM(), block1.getNum()); error.setPayload("Wrong block number"); exchange.setCurrentResponse(error);
public void check(Message message) { assertTrue("No Block2 option:", message.getOptions().hasBlock2()); BlockOption block2 = message.getOptions().getBlock2(); assertEquals("Wrong Block2 num:", num, block2.getNum()); assertEquals("Wrong Block2 m:", m, block2.isM()); assertEquals("Wrong Block2 size:", size, block2.getSize()); print("Correct Block2 option: " + block2); }
@Override protected boolean checkResponse(Request request, Response response) { boolean success = response.getOptions().hasBlock1(); if (!success) { System.out.println("FAIL: no Block1 option"); } 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; } }