blob: 49a775f35517251a44bb070cb052ffe1f26c434b [file] [log] [blame]
Andrew Brownc46c1602015-02-18 10:45:56 -08001/*
andrewsbrown7e6b9e82015-03-03 16:11:11 -08002 * 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 Brownc46c1602015-02-18 10:45:56 -080013 */
14package com.intel.jndn.management;
15
16import java.nio.ByteBuffer;
17import net.named_data.jndn.ControlParameters;
18import net.named_data.jndn.ForwardingFlags;
19import net.named_data.jndn.Name;
20import net.named_data.jndn.encoding.EncodingException;
21import net.named_data.jndn.encoding.tlv.Tlv;
22import net.named_data.jndn.encoding.tlv.TlvDecoder;
23import net.named_data.jndn.encoding.tlv.TlvEncoder;
24import 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 */
33public class EncodingHelper {
34
Andrew Brown211d2b62015-02-18 11:12:02 -080035 /**
36 * Helper to decode names since Tlv0_1_1WireFormat.java uses its own internal,
37 * protected implementation.
38 *
Andrew Brown37cec242015-05-11 14:24:13 -070039 * @param input the bytes to decode
40 * @return a decoded {@link Name}
Andrew Brown211d2b62015-02-18 11:12:02 -080041 * @throws EncodingException
42 */
43 public static Name decodeName(ByteBuffer input) throws EncodingException {
44 TlvDecoder decoder = new TlvDecoder(input);
45 return decodeName(decoder);
46 }
Andrew Brownc46c1602015-02-18 10:45:56 -080047
Andrew Brown211d2b62015-02-18 11:12:02 -080048 /**
49 * Helper to decode names using an existing decoding context; could be merged
50 * to Tlv0_1_1WireFormat.java.
51 *
Andrew Brown37cec242015-05-11 14:24:13 -070052 * @param decoder a current decoder context to use for decoding
53 * @return a decoded {@link Name}
Andrew Brown211d2b62015-02-18 11:12:02 -080054 * @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 Brownc46c1602015-02-18 10:45:56 -080062
Andrew Brown211d2b62015-02-18 11:12:02 -080063 decoder.finishNestedTlvs(endOffset);
64 return name;
65 }
Andrew Brownc46c1602015-02-18 10:45:56 -080066
Andrew Brown211d2b62015-02-18 11:12:02 -080067 /**
68 * Helper to encode names since Tlv0_1_1WireFormat.java uses its own internal,
69 * protected implementation.
70 *
Andrew Brown37cec242015-05-11 14:24:13 -070071 * @param name the {@link Name} to encode
72 * @return an encoded {@link Blob}
Andrew Brown211d2b62015-02-18 11:12:02 -080073 */
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 Brownc46c1602015-02-18 10:45:56 -080079
Andrew Brown211d2b62015-02-18 11:12:02 -080080 /**
81 * Helper to encode names using an existing encoding context; could be merged
82 * to Tlv0_1_1WireFormat.java.
83 *
Andrew Brown37cec242015-05-11 14:24:13 -070084 * @param name the {@link Name} to encode
85 * @param encoder the current {@link TlvEncoder} context to encode with
Andrew Brown211d2b62015-02-18 11:12:02 -080086 */
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 Brownc46c1602015-02-18 10:45:56 -080094
Andrew Brown211d2b62015-02-18 11:12:02 -080095 /**
Andrew Brown37cec242015-05-11 14:24:13 -070096 * 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 Brown211d2b62015-02-18 11:12:02 -0800151 * 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 Brownc46c1602015-02-18 10:45:56 -0800159
Andrew Brown211d2b62015-02-18 11:12:02 -0800160 // Encode backwards.
161 encoder.writeOptionalNonNegativeIntegerTlvFromDouble(Tlv.ControlParameters_ExpirationPeriod,
162 controlParameters.getExpirationPeriod());
Andrew Brownc46c1602015-02-18 10:45:56 -0800163
Andrew Brown211d2b62015-02-18 11:12:02 -0800164 // 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 Brownc46c1602015-02-18 10:45:56 -0800171
Andrew Brown211d2b62015-02-18 11:12:02 -0800172 // 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 Brownc46c1602015-02-18 10:45:56 -0800178
Andrew Brown211d2b62015-02-18 11:12:02 -0800179 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 Brownc46c1602015-02-18 10:45:56 -0800183
Andrew Brown211d2b62015-02-18 11:12:02 -0800184 // Encode URI
185 if (!controlParameters.getUri().isEmpty()) {
186 encoder.writeBlobTlv(Tlv.ControlParameters_Uri,
187 new Blob(controlParameters.getUri()).buf());
188 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800189
Andrew Brown211d2b62015-02-18 11:12:02 -0800190 encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_FaceId, controlParameters.getFaceId());
Andrew Brownc46c1602015-02-18 10:45:56 -0800191
Andrew Brown211d2b62015-02-18 11:12:02 -0800192 // Encode name
193 if (controlParameters.getName().size() != 0) {
194 encodeName(controlParameters.getName(), encoder);
195 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800196
Andrew Brown211d2b62015-02-18 11:12:02 -0800197 encoder.writeTypeAndLength(Tlv.ControlParameters_ControlParameters, encoder.getLength() - saveLength);
198 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800199}