blob: ca2275be97125ad0e7b6979cabbb8c5e306d963d [file] [log] [blame]
Andrew Brownc46c1602015-02-18 10:45:56 -08001/*
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 */
12package com.intel.jndn.management;
13
14import java.nio.ByteBuffer;
15import net.named_data.jndn.ControlParameters;
16import net.named_data.jndn.ForwardingFlags;
17import net.named_data.jndn.Name;
18import net.named_data.jndn.encoding.EncodingException;
19import net.named_data.jndn.encoding.tlv.Tlv;
20import net.named_data.jndn.encoding.tlv.TlvDecoder;
21import net.named_data.jndn.encoding.tlv.TlvEncoder;
22import 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 */
31public class EncodingHelper {
32
Andrew Brown211d2b62015-02-18 11:12:02 -080033 /**
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 Brownc46c1602015-02-18 10:45:56 -080045
Andrew Brown211d2b62015-02-18 11:12:02 -080046 /**
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 Brownc46c1602015-02-18 10:45:56 -080060
Andrew Brown211d2b62015-02-18 11:12:02 -080061 decoder.finishNestedTlvs(endOffset);
62 return name;
63 }
Andrew Brownc46c1602015-02-18 10:45:56 -080064
Andrew Brown211d2b62015-02-18 11:12:02 -080065 /**
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 Brownc46c1602015-02-18 10:45:56 -080077
Andrew Brown211d2b62015-02-18 11:12:02 -080078 /**
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 Brownc46c1602015-02-18 10:45:56 -080092
Andrew Brown211d2b62015-02-18 11:12:02 -080093 /**
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 Brownc46c1602015-02-18 10:45:56 -0800102
Andrew Brown211d2b62015-02-18 11:12:02 -0800103 // Encode backwards.
104 encoder.writeOptionalNonNegativeIntegerTlvFromDouble(Tlv.ControlParameters_ExpirationPeriod,
105 controlParameters.getExpirationPeriod());
Andrew Brownc46c1602015-02-18 10:45:56 -0800106
Andrew Brown211d2b62015-02-18 11:12:02 -0800107 // 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 Brownc46c1602015-02-18 10:45:56 -0800114
Andrew Brown211d2b62015-02-18 11:12:02 -0800115 // 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 Brownc46c1602015-02-18 10:45:56 -0800121
Andrew Brown211d2b62015-02-18 11:12:02 -0800122 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 Brownc46c1602015-02-18 10:45:56 -0800126
Andrew Brown211d2b62015-02-18 11:12:02 -0800127 // Encode URI
128 if (!controlParameters.getUri().isEmpty()) {
129 encoder.writeBlobTlv(Tlv.ControlParameters_Uri,
130 new Blob(controlParameters.getUri()).buf());
131 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800132
Andrew Brown211d2b62015-02-18 11:12:02 -0800133 encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_FaceId, controlParameters.getFaceId());
Andrew Brownc46c1602015-02-18 10:45:56 -0800134
Andrew Brown211d2b62015-02-18 11:12:02 -0800135 // Encode name
136 if (controlParameters.getName().size() != 0) {
137 encodeName(controlParameters.getName(), encoder);
138 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800139
Andrew Brown211d2b62015-02-18 11:12:02 -0800140 encoder.writeTypeAndLength(Tlv.ControlParameters_ControlParameters, encoder.getLength() - saveLength);
141 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800142}