summaryrefslogtreecommitdiffstats
path: root/libtransport/src/hicn/transport/core/manifest_format_json_libparc_deprecated.cc
blob: d0365d2fe89ddcd063a7493bbb12a6ae759803ee (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * 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/manifest_format_json_libparc_deprecated.h>
#include <hicn/transport/core/packet.h>
#include <hicn/transport/errors/errors.h>
#include <hicn/transport/portability/transport_portability.h>

extern "C" {
#include <parc/algol/parc_Memory.h>
}

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(PARCJSON* root, EnumType value) {
  parcJSON_AddInteger(root, JSONKey<EnumType>::key,
                      static_cast<int64_t>(value));
}

template <typename EnumType>
TRANSPORT_ALWAYS_INLINE EnumType getValueFromJson(PARCJSON* root) {
  checkPointer(root);

  PARCJSONValue* value = parcJSON_GetValueByName(root, JSONKey<EnumType>::key);

  EnumType ret = static_cast<EnumType>(parcJSONValue_GetInteger(value));
  // parcJSONValue_Release(&value);

  return ret;
};

}  // namespace

JSONManifestEncoder::JSONManifestEncoder() : root_(parcJSON_Create()) {
  parcJSON_Acquire(root_);
}

JSONManifestEncoder::~JSONManifestEncoder() {
  if (root_) {
    parcJSON_Release(&root_);
  }
}

TRANSPORT_ALWAYS_INLINE SONManifestEncoder& JSONManifestEncoder::encodeImpl(
    Packet& packet) {
  char* json_string = parcJSON_ToString(root_);
  packet.setPayload(reinterpret_cast<uint8_t*>(json_string),
                    std::strlen(json_string));
  parcMemory_Deallocate(&json_string);

  return *this;
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder& JSONManifestEncoder::clearImpl() {
  if (root_) {
    parcJSON_Release(&root_);
  }

  root_ = parcJSON_Create();

  return *this;
}

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

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

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

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

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder&
JSONManifestEncoder::addSuffixAndHashImpl(uint32_t suffix,
                                          utils::CryptoHash& hash) {
  throw errors::NotImplementedException();
  //  PARCJSONValue *value = parcJSON_GetValueByName(root_,
  //                                                 JSONKey<SuffixHashList>::key);
  //
  //  // Create the pair to store in the array.
  //  // It will be segment number + Hash of the segment
  //  PARCJSONArray * pair = parcJSONArray_Create();
  //
  //  PARCJSONValue *v = parcJSONValue_CreateFromInteger(suffix);
  //  parcJSONArray_AddValue(pair, v);
  //  parcJSONValue_Release(&v);
  //
  //  v = parcJSONValue_CreateFromInteger(hash);
  //  parcJSONArray_AddValue(pair, v);
  //  parcJSONValue_Release(&v);
  //
  //  if (value == nullptr /* || !parcJSONValue_IsArray(value) */) {
  //    // Create the array
  //    PARCJSONArray *array = parcJSONArray_Create();
  //    parcJSON_AddArray(root_,
  //                      JSONKey<SuffixHashList>::key,
  //                      array);
  //    parcJSONArray_Release(&array);
  //
  //    value = parcJSON_GetValueByName(root_, JSONKey<SuffixHashList>::key);
  //  }
  //
  //  v = parcJSONValue_CreateFromJSONArray(pair);
  //  parcJSONArray_AddValue(parcJSONValue_GetArray(value), v);
  //  parcJSONValue_Release(&v);
  //
  //  parcJSONArray_Release(&pair);
  //  // parcJSONValue_Release(&value);

  return *this;
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder&
JSONManifestEncoder::setIsFinalManifestImpl(bool is_last) {
  parcJSON_AddBoolean(root_, JSONKey<bool>::final_manifest, is_last);

  return *this;
}

TRANSPORT_ALWAYS_INLINE JSONManifestEncoder&
JSONManifestEncoder::setSuffixHashListImpl(
    const SuffixHashList& name_hash_list) {
  for (auto& suffix : name_hash_list) {
    addSuffixAndHashImpl(suffix.first, suffix.second);
  }

  return *this;
}

TRANSPORT_ALWAYS_INLINE JSONManifestDecoder::JSONManifestDecoder()
    : root_(nullptr) {}

TRANSPORT_ALWAYS_INLINE JSONManifestDecoder::~JSONManifestDecoder() {
  if (root_) {
    parcJSON_Release(&root_);
  }
}

TRANSPORT_ALWAYS_INLINE void JSONManifestDecoder::decodeImpl(
    const uint8_t* payload, std::size_t payload_size) {
  PARCBuffer* b = parcBuffer_Wrap(const_cast<uint8_t*>(payload), payload_size,
                                  0, payload_size);
  clearImpl();

  root_ = parcJSON_ParseBuffer(b);
  parcBuffer_Release(&b);

  char* str = parcJSON_ToString(root_);
}

TRANSPORT_ALWAYS_INLINE JSONManifestDecoder& JSONManifestDecoder::clearImpl() {
  if (root_) {
    parcJSON_Release(&root_);
  }

  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 SuffixHashList
JSONManifestDecoder::getSuffixHashListImpl() {
  throw errors::NotImplementedException();
  //  SuffixHashList hash_list;
  //
  //  char * str = parcJSON_ToString(root_);
  //
  //  PARCJSONValue *value = parcJSON_GetValueByName(root_,
  //                                                 JSONKey<SuffixHashList>::key);
  //
  //  if (value == nullptr || !parcJSONValue_IsArray(value)) {
  //    throw errors::RuntimeException("Manifest does not contain suffix-hash
  //    list");
  //  }
  //
  //  PARCJSONArray *array = parcJSONValue_GetArray(value);
  //  std::size_t array_size = parcJSONArray_GetLength(array);
  //
  //  for (std::size_t i = 0; i < array_size; i++) {
  //    PARCJSONValue *v = parcJSONArray_GetValue(array, i);
  //    checkPointer(v);
  //    PARCJSONArray *a = parcJSONValue_GetArray(v);
  //    PARCJSONValue *_suffix = parcJSONArray_GetValue(a, 0);
  //    PARCJSONValue *_hash = parcJSONArray_GetValue(a, 1);
  //
  //    uint32_t value1 =
  //    static_cast<uint32_t>(parcJSONValue_GetInteger(_suffix)); uint64_t
  //    value2 = static_cast<uint64_t>(parcJSONValue_GetInteger(_hash));
  //
  //    hash_list[static_cast<uint32_t>(parcJSONValue_GetInteger(_suffix))] =
  //        static_cast<uint64_t>(parcJSONValue_GetInteger(_hash));
  //
  ////    parcJSONValue_Release(&_hash);
  ////    parcJSONValue_Release(&_suffix);
  ////    parcJSONArray_Release(&a);
  ////    parcJSONValue_Release(&v);
  //  }
  //
  ////  parcJSONArray_Release(&array);
  ////  parcJSONValue_Release(&value);
  //
  //  char * str2 = parcJSON_ToString(root_);
  //
  //  return hash_list;
}

TRANSPORT_ALWAYS_INLINE core::Name JSONManifestDecoder::getBaseNameImpl()
    const {
  checkPointer(root_);
  PARCJSONValue* value =
      parcJSON_GetValueByName(root_, JSONKey<core::Name>::key);

  PARCBuffer* b = parcJSONValue_GetString(value);
  char* string = parcBuffer_ToString(b);

  core::Name ret(string);

  // parcJSONValue_Release(&value);
  parcMemory_Deallocate(&string);

  return ret;
}

TRANSPORT_ALWAYS_INLINE bool JSONManifestDecoder::getIsFinalManifestImpl() {
  checkPointer(root_);
  PARCJSONValue* value =
      parcJSON_GetValueByName(root_, JSONKey<bool>::final_manifest);

  bool ret = parcJSONValue_GetBoolean(value);

  // parcJSONValue_Release(&value);

  return ret;
}

TRANSPORT_ALWAYS_INLINE std::size_t
JSONManifestDecoder::estimateSerializedLengthImpl(
    std::size_t number_of_entries) {
  return 0;
}

}  // end namespace core

}  // end namespace transport