Fix strategy encoding; NFD returns a dataset with the ControlParameters encoding of a strategy
diff --git a/src/main/java/com/intel/jndn/management/EncodingHelper.java b/src/main/java/com/intel/jndn/management/EncodingHelper.java
index 5d658ee..49a775f 100644
--- a/src/main/java/com/intel/jndn/management/EncodingHelper.java
+++ b/src/main/java/com/intel/jndn/management/EncodingHelper.java
@@ -36,8 +36,8 @@
* Helper to decode names since Tlv0_1_1WireFormat.java uses its own internal,
* protected implementation.
*
- * @param input
- * @return
+ * @param input the bytes to decode
+ * @return a decoded {@link Name}
* @throws EncodingException
*/
public static Name decodeName(ByteBuffer input) throws EncodingException {
@@ -49,8 +49,8 @@
* Helper to decode names using an existing decoding context; could be merged
* to Tlv0_1_1WireFormat.java.
*
- * @param decoder
- * @return
+ * @param decoder a current decoder context to use for decoding
+ * @return a decoded {@link Name}
* @throws EncodingException
*/
public static Name decodeName(TlvDecoder decoder) throws EncodingException {
@@ -68,8 +68,8 @@
* Helper to encode names since Tlv0_1_1WireFormat.java uses its own internal,
* protected implementation.
*
- * @param name
- * @return
+ * @param name the {@link Name} to encode
+ * @return an encoded {@link Blob}
*/
public static Blob encodeName(Name name) {
TlvEncoder encoder = new TlvEncoder();
@@ -81,8 +81,8 @@
* Helper to encode names using an existing encoding context; could be merged
* to Tlv0_1_1WireFormat.java.
*
- * @param name
- * @param encoder
+ * @param name the {@link Name} to encode
+ * @param encoder the current {@link TlvEncoder} context to encode with
*/
public static final void encodeName(Name name, TlvEncoder encoder) {
int saveLength = encoder.getLength();
@@ -93,6 +93,61 @@
}
/**
+ * Helper to decode strategies since Tlv0_1_1WireFormat.java uses its own
+ * internal, protected implementation.
+ *
+ * @param input the bytes to decode
+ * @return a decoded {@link Name}
+ * @throws EncodingException
+ */
+ public static Name decodeStrategy(ByteBuffer input) throws EncodingException {
+ TlvDecoder decoder = new TlvDecoder(input);
+ return decodeStrategy(decoder);
+ }
+
+ /**
+ * Helper to decode strategies using an existing decoding context; could be
+ * merged to Tlv0_1_1WireFormat.java.
+ *
+ * @param decoder the current {@link TlvDecoder} context to decode with
+ * @return a decoded strategy (e.g. {@link Name})
+ * @throws EncodingException
+ */
+ public static Name decodeStrategy(TlvDecoder decoder) throws EncodingException {
+ int strategyEndOffset = decoder.readNestedTlvsStart(Tlv.ControlParameters_Strategy);
+ Name strategy = decodeName(decoder);
+ decoder.finishNestedTlvs(strategyEndOffset);
+ return strategy;
+ }
+
+ /**
+ * Helper to encode strategies since Tlv0_1_1WireFormat.java uses its own
+ * internal, protected implementation.
+ *
+ * @param strategy the {@link Name} to encode
+ * @return an encoded {@link Blob}
+ */
+ public static Blob encodeStrategy(Name strategy) {
+ TlvEncoder encoder = new TlvEncoder();
+ encodeName(strategy, encoder);
+ return new Blob(encoder.getOutput(), false);
+ }
+
+ /**
+ * Helper to encode strategies using an existing decoding context; could be
+ * merged to Tlv0_1_1WireFormat.java.
+ *
+ * @param strategy the {@link Name} to encode
+ * @param encoder the current {@link TlvEncoder} context to use
+ */
+ public static final void encodeStrategy(Name strategy, TlvEncoder encoder) {
+ int strategySaveLength = encoder.getLength();
+ encodeName(strategy, encoder);
+ encoder.writeTypeAndLength(Tlv.ControlParameters_Strategy,
+ encoder.getLength() - strategySaveLength);
+ }
+
+ /**
* Helper to encode control parameters using an existing encoding context;
* could be merged to Tlv0_1_1WireFormat.java.
*
diff --git a/src/main/java/com/intel/jndn/management/NFD.java b/src/main/java/com/intel/jndn/management/NFD.java
index d1f0450..1a15a9d 100644
--- a/src/main/java/com/intel/jndn/management/NFD.java
+++ b/src/main/java/com/intel/jndn/management/NFD.java
@@ -487,7 +487,7 @@
*/
public static void unsetStrategy(Face forwarder, Name prefix) throws Exception {
// build command name
- Name command = new Name("/localhost/nfd/strategy-choice/unsetset");
+ Name command = new Name("/localhost/nfd/strategy-choice/unset");
ControlParameters parameters = new ControlParameters();
parameters.setName(prefix);
command.append(parameters.wireEncode());
diff --git a/src/main/java/com/intel/jndn/management/types/StrategyChoice.java b/src/main/java/com/intel/jndn/management/types/StrategyChoice.java
index 962b699..7718a75 100644
--- a/src/main/java/com/intel/jndn/management/types/StrategyChoice.java
+++ b/src/main/java/com/intel/jndn/management/types/StrategyChoice.java
@@ -14,7 +14,6 @@
package com.intel.jndn.management.types;
import com.intel.jndn.management.EncodingHelper;
-import static com.intel.jndn.management.types.RibEntry.TLV_RIB_ENTRY;
import java.nio.ByteBuffer;
import net.named_data.jndn.Name;
import net.named_data.jndn.encoding.EncodingException;
@@ -54,8 +53,8 @@
*/
public final void wireEncode(TlvEncoder encoder) {
int saveLength = encoder.getLength();
+ EncodingHelper.encodeStrategy(strategy, encoder);
EncodingHelper.encodeName(name, encoder);
- EncodingHelper.encodeName(strategy, encoder);
encoder.writeTypeAndLength(TLV_STRATEGY_CHOICE, encoder.getLength() - saveLength);
}
@@ -79,9 +78,9 @@
*/
@Override
public final void wireDecode(TlvDecoder decoder) throws EncodingException {
- int endOffset = decoder.readNestedTlvsStart(TLV_RIB_ENTRY);
- strategy = EncodingHelper.decodeName(decoder);
+ int endOffset = decoder.readNestedTlvsStart(TLV_STRATEGY_CHOICE);
name = EncodingHelper.decodeName(decoder);
+ strategy = EncodingHelper.decodeStrategy(decoder);
decoder.finishNestedTlvs(endOffset);
}
diff --git a/src/test/java/com/intel/jndn/management/StrategyTestIT.java b/src/test/java/com/intel/jndn/management/StrategyTestIT.java
index 3c63dd7..c0abd14 100644
--- a/src/test/java/com/intel/jndn/management/StrategyTestIT.java
+++ b/src/test/java/com/intel/jndn/management/StrategyTestIT.java
@@ -15,8 +15,8 @@
import com.intel.jndn.management.types.StrategyChoice;
import com.intel.jndn.mock.MockKeyChain;
-import com.intel.jndn.utils.SegmentedServer;
import java.util.List;
+import java.util.Random;
import java.util.logging.Logger;
import static junit.framework.Assert.assertEquals;
import net.named_data.jndn.Face;
@@ -36,7 +36,7 @@
Face face;
public StrategyTestIT() throws net.named_data.jndn.security.SecurityException {
- this.prefix = new Name("/test/strategy");
+ this.prefix = new Name("/test/strategy").append("random:" + new Random().nextInt());
this.face = new Face("localhost"); // strategy commands only available on localhost
KeyChain mockKeyChain = MockKeyChain.configure(new Name("/test/server"));
face.setCommandSigningInfo(mockKeyChain, mockKeyChain.getDefaultCertificateName());
@@ -48,6 +48,8 @@
int oldSize = choices.size();
NFD.setStrategy(face, prefix, Strategies.CLIENT_CONTROL);
+ Thread.sleep(1000); // strategy takes a while to register
+
choices = NFD.getStrategyList(face);
assertEquals(oldSize + 1, choices.size());
diff --git a/src/test/java/com/intel/jndn/management/types/StrategyChoiceTest.java b/src/test/java/com/intel/jndn/management/types/StrategyChoiceTest.java
index a7323a0..1b2ab64 100644
--- a/src/test/java/com/intel/jndn/management/types/StrategyChoiceTest.java
+++ b/src/test/java/com/intel/jndn/management/types/StrategyChoiceTest.java
@@ -21,16 +21,15 @@
import org.junit.Test;
/**
+ * Test StrategyCHoice encoding/decoding
*
* @author Andrew Brown <andrew.brown@intel.com>
*/
public class StrategyChoiceTest {
- public StrategyChoiceTest() {
- }
-
/**
* Test of wireEncode method, of class StrategyChoice.
+ * @throws net.named_data.jndn.encoding.EncodingException
*/
@Test
public void testEncodeDecode() throws EncodingException {