Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 7e6b9e8 | 2015-03-03 16:11:11 -0800 | [diff] [blame] | 2 | * jndn-management |
| 3 | * Copyright (c) 2015, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU Lesser General Public License, |
| 7 | * version 3, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 12 | * more details. |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.management; |
| 15 | |
| 16 | import java.nio.ByteBuffer; |
| 17 | import net.named_data.jndn.ControlParameters; |
| 18 | import net.named_data.jndn.ForwardingFlags; |
| 19 | import net.named_data.jndn.Name; |
| 20 | import net.named_data.jndn.encoding.EncodingException; |
| 21 | import net.named_data.jndn.encoding.tlv.Tlv; |
| 22 | import net.named_data.jndn.encoding.tlv.TlvDecoder; |
| 23 | import net.named_data.jndn.encoding.tlv.TlvEncoder; |
| 24 | import net.named_data.jndn.util.Blob; |
| 25 | |
| 26 | /** |
| 27 | * Provide helper methods to cover areas too protected in Tlv0_1_1WireFormat; |
| 28 | * this class can be deprecated if WireFormats allow passing in an existing |
| 29 | * TlvEncoder/TlvDecoder (currently these methods are protected). |
| 30 | * |
| 31 | * @author Andrew Brown <andrew.brown@intel.com> |
| 32 | */ |
| 33 | public class EncodingHelper { |
| 34 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Helper to decode names since Tlv0_1_1WireFormat.java uses its own internal, |
| 37 | * protected implementation. |
| 38 | * |
Andrew Brown | 37cec24 | 2015-05-11 14:24:13 -0700 | [diff] [blame] | 39 | * @param input the bytes to decode |
| 40 | * @return a decoded {@link Name} |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 41 | * @throws EncodingException |
| 42 | */ |
| 43 | public static Name decodeName(ByteBuffer input) throws EncodingException { |
| 44 | TlvDecoder decoder = new TlvDecoder(input); |
| 45 | return decodeName(decoder); |
| 46 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 47 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 48 | /** |
| 49 | * Helper to decode names using an existing decoding context; could be merged |
| 50 | * to Tlv0_1_1WireFormat.java. |
| 51 | * |
Andrew Brown | 37cec24 | 2015-05-11 14:24:13 -0700 | [diff] [blame] | 52 | * @param decoder a current decoder context to use for decoding |
| 53 | * @return a decoded {@link Name} |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 54 | * @throws EncodingException |
| 55 | */ |
| 56 | public static Name decodeName(TlvDecoder decoder) throws EncodingException { |
| 57 | Name name = new Name(); |
| 58 | int endOffset = decoder.readNestedTlvsStart(Tlv.Name); |
| 59 | while (decoder.getOffset() < endOffset) { |
| 60 | name.append(new Blob(decoder.readBlobTlv(Tlv.NameComponent), true)); |
| 61 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 62 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 63 | decoder.finishNestedTlvs(endOffset); |
| 64 | return name; |
| 65 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 66 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Helper to encode names since Tlv0_1_1WireFormat.java uses its own internal, |
| 69 | * protected implementation. |
| 70 | * |
Andrew Brown | 37cec24 | 2015-05-11 14:24:13 -0700 | [diff] [blame] | 71 | * @param name the {@link Name} to encode |
| 72 | * @return an encoded {@link Blob} |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 73 | */ |
| 74 | public static Blob encodeName(Name name) { |
| 75 | TlvEncoder encoder = new TlvEncoder(); |
| 76 | encodeName(name, encoder); |
| 77 | return new Blob(encoder.getOutput(), false); |
| 78 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 79 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 80 | /** |
| 81 | * Helper to encode names using an existing encoding context; could be merged |
| 82 | * to Tlv0_1_1WireFormat.java. |
| 83 | * |
Andrew Brown | 37cec24 | 2015-05-11 14:24:13 -0700 | [diff] [blame] | 84 | * @param name the {@link Name} to encode |
| 85 | * @param encoder the current {@link TlvEncoder} context to encode with |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 86 | */ |
| 87 | public static final void encodeName(Name name, TlvEncoder encoder) { |
| 88 | int saveLength = encoder.getLength(); |
| 89 | for (int i = name.size() - 1; i >= 0; --i) { |
| 90 | encoder.writeBlobTlv(Tlv.NameComponent, name.get(i).getValue().buf()); |
| 91 | } |
| 92 | encoder.writeTypeAndLength(Tlv.Name, encoder.getLength() - saveLength); |
| 93 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 94 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 95 | /** |
Andrew Brown | 37cec24 | 2015-05-11 14:24:13 -0700 | [diff] [blame] | 96 | * Helper to decode strategies since Tlv0_1_1WireFormat.java uses its own |
| 97 | * internal, protected implementation. |
| 98 | * |
| 99 | * @param input the bytes to decode |
| 100 | * @return a decoded {@link Name} |
| 101 | * @throws EncodingException |
| 102 | */ |
| 103 | public static Name decodeStrategy(ByteBuffer input) throws EncodingException { |
| 104 | TlvDecoder decoder = new TlvDecoder(input); |
| 105 | return decodeStrategy(decoder); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Helper to decode strategies using an existing decoding context; could be |
| 110 | * merged to Tlv0_1_1WireFormat.java. |
| 111 | * |
| 112 | * @param decoder the current {@link TlvDecoder} context to decode with |
| 113 | * @return a decoded strategy (e.g. {@link Name}) |
| 114 | * @throws EncodingException |
| 115 | */ |
| 116 | public static Name decodeStrategy(TlvDecoder decoder) throws EncodingException { |
| 117 | int strategyEndOffset = decoder.readNestedTlvsStart(Tlv.ControlParameters_Strategy); |
| 118 | Name strategy = decodeName(decoder); |
| 119 | decoder.finishNestedTlvs(strategyEndOffset); |
| 120 | return strategy; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Helper to encode strategies since Tlv0_1_1WireFormat.java uses its own |
| 125 | * internal, protected implementation. |
| 126 | * |
| 127 | * @param strategy the {@link Name} to encode |
| 128 | * @return an encoded {@link Blob} |
| 129 | */ |
| 130 | public static Blob encodeStrategy(Name strategy) { |
| 131 | TlvEncoder encoder = new TlvEncoder(); |
| 132 | encodeName(strategy, encoder); |
| 133 | return new Blob(encoder.getOutput(), false); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Helper to encode strategies using an existing decoding context; could be |
| 138 | * merged to Tlv0_1_1WireFormat.java. |
| 139 | * |
| 140 | * @param strategy the {@link Name} to encode |
| 141 | * @param encoder the current {@link TlvEncoder} context to use |
| 142 | */ |
| 143 | public static final void encodeStrategy(Name strategy, TlvEncoder encoder) { |
| 144 | int strategySaveLength = encoder.getLength(); |
| 145 | encodeName(strategy, encoder); |
| 146 | encoder.writeTypeAndLength(Tlv.ControlParameters_Strategy, |
| 147 | encoder.getLength() - strategySaveLength); |
| 148 | } |
| 149 | |
| 150 | /** |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 151 | * Helper to encode control parameters using an existing encoding context; |
| 152 | * could be merged to Tlv0_1_1WireFormat.java. |
| 153 | * |
| 154 | * @param controlParameters |
| 155 | * @param encoder |
| 156 | */ |
| 157 | public static final void encodeControlParameters(ControlParameters controlParameters, TlvEncoder encoder) { |
| 158 | int saveLength = encoder.getLength(); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 159 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 160 | // Encode backwards. |
| 161 | encoder.writeOptionalNonNegativeIntegerTlvFromDouble(Tlv.ControlParameters_ExpirationPeriod, |
| 162 | controlParameters.getExpirationPeriod()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 163 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 164 | // Encode strategy |
| 165 | if (controlParameters.getStrategy().size() != 0) { |
| 166 | int strategySaveLength = encoder.getLength(); |
| 167 | encodeName(controlParameters.getStrategy(), encoder); |
| 168 | encoder.writeTypeAndLength(Tlv.ControlParameters_Strategy, |
| 169 | encoder.getLength() - strategySaveLength); |
| 170 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 171 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 172 | // Encode ForwardingFlags |
| 173 | int flags = controlParameters.getForwardingFlags().getNfdForwardingFlags(); |
| 174 | if (flags != new ForwardingFlags().getNfdForwardingFlags()) // The flags are not the default value. |
| 175 | { |
| 176 | encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_Flags, flags); |
| 177 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 178 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 179 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_Cost, controlParameters.getCost()); |
| 180 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_Origin, controlParameters.getOrigin()); |
| 181 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_LocalControlFeature, |
| 182 | controlParameters.getLocalControlFeature()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 183 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 184 | // Encode URI |
| 185 | if (!controlParameters.getUri().isEmpty()) { |
| 186 | encoder.writeBlobTlv(Tlv.ControlParameters_Uri, |
| 187 | new Blob(controlParameters.getUri()).buf()); |
| 188 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 189 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 190 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_FaceId, controlParameters.getFaceId()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 191 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 192 | // Encode name |
| 193 | if (controlParameters.getName().size() != 0) { |
| 194 | encodeName(controlParameters.getName(), encoder); |
| 195 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 196 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 197 | encoder.writeTypeAndLength(Tlv.ControlParameters_ControlParameters, encoder.getLength() - saveLength); |
| 198 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 199 | } |