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 | * |
| 39 | * @param input |
| 40 | * @return |
| 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 | * |
| 52 | * @param decoder |
| 53 | * @return |
| 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 | * |
| 71 | * @param name |
| 72 | * @return |
| 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 | * |
| 84 | * @param name |
| 85 | * @param encoder |
| 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 | /** |
| 96 | * Helper to encode control parameters using an existing encoding context; |
| 97 | * could be merged to Tlv0_1_1WireFormat.java. |
| 98 | * |
| 99 | * @param controlParameters |
| 100 | * @param encoder |
| 101 | */ |
| 102 | public static final void encodeControlParameters(ControlParameters controlParameters, TlvEncoder encoder) { |
| 103 | int saveLength = encoder.getLength(); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 104 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 105 | // Encode backwards. |
| 106 | encoder.writeOptionalNonNegativeIntegerTlvFromDouble(Tlv.ControlParameters_ExpirationPeriod, |
| 107 | controlParameters.getExpirationPeriod()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 108 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 109 | // Encode strategy |
| 110 | if (controlParameters.getStrategy().size() != 0) { |
| 111 | int strategySaveLength = encoder.getLength(); |
| 112 | encodeName(controlParameters.getStrategy(), encoder); |
| 113 | encoder.writeTypeAndLength(Tlv.ControlParameters_Strategy, |
| 114 | encoder.getLength() - strategySaveLength); |
| 115 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 116 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 117 | // Encode ForwardingFlags |
| 118 | int flags = controlParameters.getForwardingFlags().getNfdForwardingFlags(); |
| 119 | if (flags != new ForwardingFlags().getNfdForwardingFlags()) // The flags are not the default value. |
| 120 | { |
| 121 | encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_Flags, flags); |
| 122 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 123 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 124 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_Cost, controlParameters.getCost()); |
| 125 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_Origin, controlParameters.getOrigin()); |
| 126 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_LocalControlFeature, |
| 127 | controlParameters.getLocalControlFeature()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 128 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 129 | // Encode URI |
| 130 | if (!controlParameters.getUri().isEmpty()) { |
| 131 | encoder.writeBlobTlv(Tlv.ControlParameters_Uri, |
| 132 | new Blob(controlParameters.getUri()).buf()); |
| 133 | } |
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 | encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_FaceId, controlParameters.getFaceId()); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 136 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 137 | // Encode name |
| 138 | if (controlParameters.getName().size() != 0) { |
| 139 | encodeName(controlParameters.getName(), encoder); |
| 140 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 141 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 142 | encoder.writeTypeAndLength(Tlv.ControlParameters_ControlParameters, encoder.getLength() - saveLength); |
| 143 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 144 | } |