aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/hicn/transport/core/manifest_format_json_jsoncpp.cc
blob: 0ffca39f6815f0a87e856cc506757ace46d4d7b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <hicn/transport/core/packet.h>
#include <hicn/transport/portability/transport_portability.h>

#include <array>

namespace transport {

namespace core {

namespace {

template <typename T>
TRANSPORT_ALWAYS_INLINE void checkPointer(T *pointer) {
  if (pointer == nullptr) {
    throw errors::NullPointerException();
  }
}

template <typename EnumType>
TRANSPORT_ALWAYS_INLINE void setValueToJson(Json::Value &root, EnumType value) {
  root[JSONKey<EnumType>::key] = static_cast<uint8_t>(value);
}

template <typename EnumType>
TRANSPORT_ALWAYS_INLINE EnumType getValueFromJson(const Json::Value &root) {
  return static_cast<EnumType>(root[JSONKey<EnumType>::key].asUInt());
};

}  // namespace

JSONManifestEncoder::JSONManifestEncoder(Packet &packet) : packet_(packet) {}

JSONManifestEncoder::~JSONManifestEncoder() {}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &JSONManifestEncoder::encodeImpl() {
  Json::StreamWriterBuilder writer_builder;
  Json::StreamWriter *fast_writer = writer_builder.newStreamWriter();

  asio::streambuf strbuf;
  strbuf.prepare(1500);
  std::ostream stream(&strbuf);
  fast_writer->write(root_, &stream);

  const uint8_t *buffer = asio::buffer_cast<const uint8_t *>(strbuf.data());

  packet_.setPayload(buffer, strbuf.size());

  delete fast_writer;

  return *this;
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &JSONManifestEncoder::clearImpl() {
  root_.clear();
  return *this;
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &
JSONManifestEncoder::setHashAlgorithmImpl(HashAlgorithm algorithm) {
  setValueToJson(root_, algorithm);
  return *this;
}

JSONManifestEncoder &JSONManifestEncoder::setManifestTypeImpl(
    ManifestType manifest_type) {
  setValueToJson(root_, manifest_type);
  return *this;
}

JSONManifestEncoder &JSONManifestEncoder::setNextSegmentCalculationStrategyImpl(
    NextSegmentCalculationStrategy strategy) {
  setValueToJson(root_, strategy);
  return *this;
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &
JSONManifestEncoder::setBaseNameImpl(const core::Name &base_name) {
  root_[JSONKey<core::Name>::key] = base_name.toString().c_str();
  return *this;
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &
JSONManifestEncoder::addSuffixAndHashImpl(uint32_t suffix,
                                          const utils::CryptoHash &hash) {
  throw errors::NotImplementedException();
  //  Json::Value value(Json::arrayValue);
  //  value.append(Json::Value(suffix));
  //  value.append(Json::Value(Json::Value::UInt64 (hash)));
  //  root_[JSONKey<SuffixHashList>::key].append(value);

  return *this;
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &
JSONManifestEncoder::setIsFinalManifestImpl(bool is_last) {
  root_[JSONKey<bool>::final_manifest] = is_last;
  return *this;
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &
JSONManifestEncoder::setVersionImpl(ManifestVersion version) {
  setValueToJson(root_, version);
  return *this;
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &
JSONManifestEncoder::setSuffixHashListImpl(
    const typename JSON::SuffixList &name_hash_list) {
  throw errors::NotImplementedException();
  //  for (auto &suffix : name_hash_list) {
  //    addSuffixAndHashImpl(suffix.first, suffix.second);
  //  }
  //
  //  return *this;
}

TRANSPORT_ALWAYS_INLINE std::size_t
JSONManifestEncoder::estimateSerializedLengthImpl(
    std::size_t number_of_entries) {
  Json::StreamWriterBuilder writer_builder;
  Json::StreamWriter *fast_writer = writer_builder.newStreamWriter();

  asio::streambuf strbuf;
  strbuf.prepare(1500);
  std::ostream stream(&strbuf);
  fast_writer->write(root_, &stream);

  return strbuf.size();
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &JSONManifestEncoder::updateImpl() {
  throw errors::NotImplementedException();
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &
JSONManifestEncoder::setFinalBlockNumberImpl(std::uint32_t final_block_number) {
  throw errors::NotImplementedException();
}

TRANSPORT_ALWAYS_INLINE std::size_t
JSONManifestEncoder::getManifestHeaderSizeImpl() {
  return 0;
}

JSONManifestDecoder::JSONManifestDecoder(Packet &packet) : packet_(packet) {}

JSONManifestDecoder::~JSONManifestDecoder() {}

TRANSPORT_ALWAYS_INLINE void JSONManifestDecoder::decodeImpl() {
  auto array = packet_.getPayload();
  auto payload = array.data();
  auto payload_size = array.length();

  Json::CharReaderBuilder reader_builder;
  Json::CharReader *reader = reader_builder.newCharReader();
  std::string errors;

  if (!reader->parse((char *)payload, (char *)payload + payload_size, &root_,
                     &errors)) {
    TRANSPORT_LOGE("Error parsing manifest!");
    TRANSPORT_LOGE("%s", errors.c_str());

    delete reader;

    throw errors::MalformedPacketException();
  }

  delete reader;
}

TRANSPORT_ALWAYS_INLINE JSONManifestDecoder &JSONManifestDecoder::clearImpl() {
  root_.clear();
  return *this;
}

TRANSPORT_ALWAYS_INLINE ManifestType
JSONManifestDecoder::getManifestTypeImpl() const {
  return getValueFromJson<ManifestType>(root_);
}

TRANSPORT_ALWAYS_INLINE HashAlgorithm
JSONManifestDecoder::getHashAlgorithmImpl() const {
  return getValueFromJson<HashAlgorithm>(root_);
}

TRANSPORT_ALWAYS_INLINE NextSegmentCalculationStrategy
JSONManifestDecoder::getNextSegmentCalculationStrategyImpl() const {
  return getValueFromJson<NextSegmentCalculationStrategy>(root_);
}

TRANSPORT_ALWAYS_INLINE typename JSON::SuffixList
JSONManifestDecoder::getSuffixHashListImpl() {
  throw errors::NotImplementedException();
  //  SuffixHashList hash_list;
  //
  //  Json::Value &array = root_[JSONKey<SuffixHashList>::key];
  //
  //  for (Json::Value::ArrayIndex i = 0;
  //       i != array.size();
  //       i++) {
  //    hash_list[array[i][0].asUInt()] = array[i][1].asUInt64();
  //  }
  //
  //  return hash_list;
}

TRANSPORT_ALWAYS_INLINE core::Name JSONManifestDecoder::getBaseNameImpl()
    const {
  return core::Name(root_[JSONKey<core::Name>::key].asCString());
}

TRANSPORT_ALWAYS_INLINE bool JSONManifestDecoder::getIsFinalManifestImpl()
    const {
  return root_[JSONKey<bool>::final_manifest].asBool();
}

TRANSPORT_ALWAYS_INLINE ManifestVersion
JSONManifestDecoder::getVersionImpl() const {
  return getValueFromJson<ManifestVersion>(root_);
}

TRANSPORT_ALWAYS_INLINE uint32_t
JSONManifestDecoder::getFinalBlockNumberImpl() const {
  return 0;
}

}  // end namespace core

}  // end namespace transport