Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
Fixed blockwise upload for Cf clients.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Kovatsch committed Mar 17, 2012
1 parent 808a9ec commit 85c9508
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
Binary file modified run/ExampleClient.jar
Binary file not shown.
Binary file modified run/ExampleServer.jar
Binary file not shown.
Binary file modified run/GETClient.jar
Binary file not shown.
Binary file modified run/HelloWorldServer.jar
Binary file not shown.
5 changes: 2 additions & 3 deletions src/ch/ethz/inf/vs/californium/layers/MatchingLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ protected void doSendMessage(Message msg) throws IOException {

if (msg instanceof Request) {

LOG.finer(String.format("Storing open request: %s", msg.sequenceKey()));
addOpenRequest((Request) msg);
}

Expand Down Expand Up @@ -139,11 +138,11 @@ private RequestResponsePair addOpenRequest(Request request) {
exchange.key = request.sequenceKey();
exchange.request = request;

LOG.finer(String.format("Storing open request: %s", exchange.key));

// associate token with Transaction
pairs.put(exchange.key, exchange);

LOG.finer(String.format("Stored new request: %s", exchange.key));

return exchange;
}

Expand Down
53 changes: 39 additions & 14 deletions src/ch/ethz/inf/vs/californium/layers/TransferLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
/**
* The class TransferLayer provides support for
* <a href="http://tools.ietf.org/html/draft-ietf-core-block">blockwise transfers</a>.
* <p>
* {@link #doSendMessage(Message)} and {@link #doReceiveMessage(Message)} do not
* distinguish between clients and server directly, but rather between incoming
* and outgoing transfers. This saves duplicate code, but introduces rather
* confusing Request/Response checks at various places.<br/>
* TODO: Explore alternative designs.
*
* @author Matthias Kovatsch
*/
Expand Down Expand Up @@ -193,6 +199,10 @@ protected void doReceiveMessage(Message msg) {
} else if (msg instanceof Response) {
blockIn = (BlockOption) msg.getFirstOption(OptionNumberRegistry.BLOCK2);
blockOut = (BlockOption) msg.getFirstOption(OptionNumberRegistry.BLOCK1);
if (blockOut!=null) {
// client must increase
blockOut.setNUM(blockOut.getNUM()+1);
}
} else {
LOG.warning(String.format("Unknown message type received: %s", msg.key()));
return;
Expand All @@ -217,33 +227,43 @@ protected void doReceiveMessage(Message msg) {
LOG.fine(String.format("Freed blockwise transfer by client token reuse: %s", msg.sequenceKey()));

} else {

if (msg instanceof Request) {
// update MID
transfer.cache.setMID(msg.getMID());
}

// use cached representation
Message resp = getBlock(transfer.cache, blockOut.getNUM(), blockOut.getSZX());
Message next = getBlock(transfer.cache, blockOut.getNUM(), blockOut.getSZX());

if (resp!=null) {

// update message ID
resp.setMID(msg.getMID());
if (next!=null) {

try {
LOG.finer(String.format("Sending next block: %s | %s", resp.sequenceKey(), blockOut));
sendMessageOverLowerLayer(resp);
LOG.finer(String.format("Sending next block: %s | %s", next.sequenceKey(), blockOut));
sendMessageOverLowerLayer(next);
} catch (IOException e) {
LOG.severe(String.format("Failed to send block response: %s", e.getMessage()));
}

BlockOption respBlock = (BlockOption) resp.getFirstOption(blockOut.getOptionNumber());
BlockOption respBlock = (BlockOption) next.getFirstOption(blockOut.getOptionNumber());

// remove transfer context if completed
if (!respBlock.getM()) {
if (!respBlock.getM() && msg instanceof Request) {
outgoing.remove(msg.sequenceKey());
LOG.fine(String.format("Freed blockwise transfer by completion: %s", resp.sequenceKey()));
LOG.fine(String.format("Freed blockwise download by completion: %s", next.sequenceKey()));
}
return;

} else if (msg instanceof Response && !blockOut.getM()) {

outgoing.remove(msg.sequenceKey());
LOG.fine(String.format("Freed blockwise upload by completion: %s", msg.sequenceKey()));

// restore original request with registered handlers
((Response)msg).setRequest((Request)transfer.cache);

} else {
LOG.warning(String.format("Rejecting out-of-scope request with cached response (freed): %s | %s, %d bytes available", msg.sequenceKey(), blockOut, transfer.cache.payloadSize()));
LOG.warning(String.format("Rejecting out-of-scope demand for cached transfer (freed): %s | %s, %d bytes available", msg.sequenceKey(), blockOut, transfer.cache.payloadSize()));
outgoing.remove(msg.sequenceKey());
handleOutOfScopeError(msg.newReply(true));
return;
Expand All @@ -252,7 +272,7 @@ protected void doReceiveMessage(Message msg) {
}
}

// get current representation
// get current representation/deliver response
deliverMessage(msg);
}

Expand Down Expand Up @@ -397,9 +417,14 @@ private static Message getBlock(Message msg, int num, int szx) {
int payloadLeft = msg.payloadSize() - payloadOffset;

if (payloadLeft > 0) {
Message block = new Message(msg.getType(), msg.getCode());
Message block = null;
if (msg instanceof Request) {
block = new Request(msg.getCode(), msg.isConfirmable());
} else {
block = new Response(msg.getCode(), !msg.isNonConfirmable());
block.setMID(msg.getMID());
}

block.setMID(msg.getMID());
block.setPeerAddress(msg.getPeerAddress());

// use same options
Expand Down

0 comments on commit 85c9508

Please sign in to comment.