Fix name shortening bug; SegmentedClient always removed the last component in reassembled packet, should only remove the last component if it is a segment marker
diff --git a/src/main/java/com/intel/jndn/utils/SegmentedClient.java b/src/main/java/com/intel/jndn/utils/SegmentedClient.java
index 40ef7bb..f469b90 100644
--- a/src/main/java/com/intel/jndn/utils/SegmentedClient.java
+++ b/src/main/java/com/intel/jndn/utils/SegmentedClient.java
@@ -14,7 +14,6 @@
package com.intel.jndn.utils;
import com.intel.jndn.utils.client.SegmentedFutureData;
-import com.intel.jndn.utils.client.FutureData;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -63,23 +62,26 @@
*
* @param face
* @param interest should include either a ChildSelector or an initial segment
- * number
+ * number; the initial segment number will be cut off in the de-segmented
+ * packet.
* @return a list of FutureData packets; if the first segment fails, the list
* will contain one FutureData with the failure exception
*/
@Override
public Future<Data> getAsync(Face face, Interest interest) {
List<Future<Data>> segments = getAsyncList(face, interest);
- return new SegmentedFutureData(interest.getName().getPrefix(-1), segments);
+ Name name = hasSegment(interest.getName()) ? interest.getName().getPrefix(-1) : interest.getName();
+ return new SegmentedFutureData(name, segments);
}
- /**
+ /**
* Asynchronously send Interest packets for a segmented result; will block
* until the first packet is received and then send remaining interests until
* the specified FinalBlockId.
*
* @param face
- * @param name
+ * @param name the {@link Name} of the packet to retrieve using a default
+ * interest
* @return an aggregated data packet from all received segments
*/
public Future<Data> getAsync(Face face, Name name) {
@@ -168,7 +170,7 @@
* @throws java.io.IOException
*/
@Override
- public Data getSync(Face face, Interest interest) throws IOException {
+ public Data getSync(Face face, Interest interest) throws IOException {
try {
return getAsync(face, interest).get();
} catch (ExecutionException | InterruptedException e) {
diff --git a/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java b/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java
index e6773ec..a08f3af 100644
--- a/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java
+++ b/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java
@@ -15,6 +15,7 @@
import com.intel.jndn.mock.MockFace;
import com.intel.jndn.mock.MockTransport;
+import com.intel.jndn.utils.client.SegmentedFutureData;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
@@ -86,7 +87,7 @@
// retrieve non-existent data, should timeout
logger.info("Client expressing interest asynchronously: /test/no-data");
List<Future<Data>> futureSegments = SegmentedClient.getDefault().getAsyncList(face, new Name("/test/no-data"));
-
+
// the list of future packets should be initialized
assertEquals(1, futureSegments.size());
assertTrue(futureSegments.get(0).isDone());
@@ -94,4 +95,24 @@
// should throw error
futureSegments.get(0).get();
}
+
+ /**
+ * Ensure Name of the returned Data is the same as was requested; identifies
+ * bug where the last Name.Component was always cut off.
+ *
+ * @throws InterruptedException
+ * @throws ExecutionException
+ */
+ @Test
+ public void testNameShorteningLogic() throws InterruptedException, ExecutionException {
+ MockFace face = new MockFace();
+ Name name = new Name("/test/123");
+ Data data = new Data(name);
+ data.setContent(new Blob("...."));
+ face.addResponse(name, data);
+
+ SegmentedFutureData future = (SegmentedFutureData) SegmentedClient.getDefault().getAsync(face, name);
+ assertEquals(name.toUri(), future.getName().toUri());
+ assertEquals(name.toUri(), future.get().getName().toUri());
+ }
}