blob: 5d658ee857b4dcfa06e3cf00feb0c24908a9ba44 [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 *
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 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 *
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 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 *
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 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 *
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 Brownc46c1602015-02-18 10:45:56 -080094
Andrew Brown211d2b62015-02-18 11:12:02 -080095 /**
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 Brownc46c1602015-02-18 10:45:56 -0800104
Andrew Brown211d2b62015-02-18 11:12:02 -0800105 // Encode backwards.
106 encoder.writeOptionalNonNegativeIntegerTlvFromDouble(Tlv.ControlParameters_ExpirationPeriod,
107 controlParameters.getExpirationPeriod());
Andrew Brownc46c1602015-02-18 10:45:56 -0800108
Andrew Brown211d2b62015-02-18 11:12:02 -0800109 // 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 Brownc46c1602015-02-18 10:45:56 -0800116
Andrew Brown211d2b62015-02-18 11:12:02 -0800117 // 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 Brownc46c1602015-02-18 10:45:56 -0800123
Andrew Brown211d2b62015-02-18 11:12:02 -0800124 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 Brownc46c1602015-02-18 10:45:56 -0800128
Andrew Brown211d2b62015-02-18 11:12:02 -0800129 // Encode URI
130 if (!controlParameters.getUri().isEmpty()) {
131 encoder.writeBlobTlv(Tlv.ControlParameters_Uri,
132 new Blob(controlParameters.getUri()).buf());
133 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800134
Andrew Brown211d2b62015-02-18 11:12:02 -0800135 encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_FaceId, controlParameters.getFaceId());
Andrew Brownc46c1602015-02-18 10:45:56 -0800136
Andrew Brown211d2b62015-02-18 11:12:02 -0800137 // Encode name
138 if (controlParameters.getName().size() != 0) {
139 encodeName(controlParameters.getName(), encoder);
140 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800141
Andrew Brown211d2b62015-02-18 11:12:02 -0800142 encoder.writeTypeAndLength(Tlv.ControlParameters_ControlParameters, encoder.getLength() - saveLength);
143 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800144}