Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * File name: EncodingHelper.java |
| 3 | * |
| 4 | * Purpose: Provide helper methods to cover areas too protected in Tlv0_1_1WireFormat; |
| 5 | * this class can be deprecated if WireFormats allow passing in an existing |
| 6 | * TlvEncoder/TlvDecoder (currently these methods are protected). |
| 7 | * |
| 8 | * © Copyright Intel Corporation. All rights reserved. |
| 9 | * Intel Corporation, 2200 Mission College Boulevard, |
| 10 | * Santa Clara, CA 95052-8119, USA |
| 11 | */ |
| 12 | package com.intel.jndn.management; |
| 13 | |
| 14 | import java.nio.ByteBuffer; |
| 15 | import net.named_data.jndn.ControlParameters; |
| 16 | import net.named_data.jndn.ForwardingFlags; |
| 17 | import net.named_data.jndn.Name; |
| 18 | import net.named_data.jndn.encoding.EncodingException; |
| 19 | import net.named_data.jndn.encoding.tlv.Tlv; |
| 20 | import net.named_data.jndn.encoding.tlv.TlvDecoder; |
| 21 | import net.named_data.jndn.encoding.tlv.TlvEncoder; |
| 22 | import net.named_data.jndn.util.Blob; |
| 23 | |
| 24 | /** |
| 25 | * Provide helper methods to cover areas too protected in Tlv0_1_1WireFormat; |
| 26 | * this class can be deprecated if WireFormats allow passing in an existing |
| 27 | * TlvEncoder/TlvDecoder (currently these methods are protected). |
| 28 | * |
| 29 | * @author Andrew Brown <andrew.brown@intel.com> |
| 30 | */ |
| 31 | public class EncodingHelper { |
| 32 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Helper to decode names since Tlv0_1_1WireFormat.java uses its own internal, |
| 35 | * protected implementation. |
| 36 | * |
| 37 | * @param input |
| 38 | * @return |
| 39 | * @throws EncodingException |
| 40 | */ |
| 41 | public static Name decodeName(ByteBuffer input) throws EncodingException { |
| 42 | TlvDecoder decoder = new TlvDecoder(input); |
| 43 | return decodeName(decoder); |
| 44 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 45 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 46 | /** |
| 47 | * Helper to decode names using an existing decoding context; could be merged |
| 48 | * to Tlv0_1_1WireFormat.java. |
| 49 | * |
| 50 | * @param decoder |
| 51 | * @return |
| 52 | * @throws EncodingException |
| 53 | */ |
| 54 | public static Name decodeName(TlvDecoder decoder) throws EncodingException { |
| 55 | Name name = new Name(); |
| 56 | int endOffset = decoder.readNestedTlvsStart(Tlv.Name); |
| 57 | while (decoder.getOffset() < endOffset) { |
| 58 | name.append(new Blob(decoder.readBlobTlv(Tlv.NameComponent), true)); |
| 59 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 60 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 61 | decoder.finishNestedTlvs(endOffset); |
| 62 | return name; |
| 63 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 64 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Helper to encode names since Tlv0_1_1WireFormat.java uses its own internal, |
| 67 | * protected implementation. |
| 68 | * |
| 69 | * @param name |
| 70 | * @return |
| 71 | */ |
| 72 | public static Blob encodeName(Name name) { |
| 73 | TlvEncoder encoder = new TlvEncoder(); |
| 74 | encodeName(name, encoder); |
| 75 | return new Blob(encoder.getOutput(), false); |
| 76 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 77 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 78 | /** |
| 79 | * Helper to encode names using an existing encoding context; could be merged |
| 80 | * to Tlv0_1_1WireFormat.java. |
| 81 | * |
| 82 | * @param name |
| 83 | * @param encoder |
| 84 | */ |
| 85 | public static final void encodeName(Name name, TlvEncoder encoder) { |
| 86 | int saveLength = encoder.getLength(); |
| 87 | for (int i = name.size() - 1; i >= 0; --i) { |
| 88 | encoder.writeBlobTlv(Tlv.NameComponent, name.get(i).getValue().buf()); |
| 89 | } |
| 90 | encoder.writeTypeAndLength(Tlv.Name, encoder.getLength() - saveLength); |
| 91 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 92 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 93 | /** |
| 94 | * Helper to encode control parameters using an existing encoding context; |
| 95 | * could be merged to Tlv0_1_1WireFormat.java. |
| 96 | * |
| 97 | * @param controlParameters |
| 98 | * @param encoder |
| 99 | */ |
| 100 | public static final void encodeControlParameters(ControlParameters controlParameters, TlvEncoder encoder) { |
| 101 | int saveLength = encoder.getLength(); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 102 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 103 | // Encode backwards. |
| 104 | encoder.writeOptionalNonNegativeIntegerTlvFromDouble(Tlv.ControlParameters_ExpirationPeriod, |
| 105 | controlParameters.getExpirationPeriod()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 106 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 107 | // Encode strategy |
| 108 | if (controlParameters.getStrategy().size() != 0) { |
| 109 | int strategySaveLength = encoder.getLength(); |
| 110 | encodeName(controlParameters.getStrategy(), encoder); |
| 111 | encoder.writeTypeAndLength(Tlv.ControlParameters_Strategy, |
| 112 | encoder.getLength() - strategySaveLength); |
| 113 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 114 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 115 | // Encode ForwardingFlags |
| 116 | int flags = controlParameters.getForwardingFlags().getNfdForwardingFlags(); |
| 117 | if (flags != new ForwardingFlags().getNfdForwardingFlags()) // The flags are not the default value. |
| 118 | { |
| 119 | encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_Flags, flags); |
| 120 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 121 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 122 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_Cost, controlParameters.getCost()); |
| 123 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_Origin, controlParameters.getOrigin()); |
| 124 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_LocalControlFeature, |
| 125 | controlParameters.getLocalControlFeature()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 126 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 127 | // Encode URI |
| 128 | if (!controlParameters.getUri().isEmpty()) { |
| 129 | encoder.writeBlobTlv(Tlv.ControlParameters_Uri, |
| 130 | new Blob(controlParameters.getUri()).buf()); |
| 131 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 132 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 133 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_FaceId, controlParameters.getFaceId()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 134 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 135 | // Encode name |
| 136 | if (controlParameters.getName().size() != 0) { |
| 137 | encodeName(controlParameters.getName(), encoder); |
| 138 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 139 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 140 | encoder.writeTypeAndLength(Tlv.ControlParameters_ControlParameters, encoder.getLength() - saveLength); |
| 141 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 142 | } |