blob: 7efdfcef574d0b103b50e2a8ee6f966f909c1d86 [file] [log] [blame]
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08004 *
5 * BSD license, See the LICENSE file for more information
6 *
7 * Author: Qiuhan Ding <qiuhanding@cs.ucla.edu>
8 */
9
10#include "endorse-info.hpp"
11
12namespace chronochat {
13
14BOOST_CONCEPT_ASSERT((ndn::WireEncodable<EndorseInfo>));
15BOOST_CONCEPT_ASSERT((ndn::WireDecodable<EndorseInfo>));
16
17
18EndorseInfo::EndorseInfo()
19{
20}
21
22EndorseInfo::EndorseInfo(const Block& endorseWire)
23{
24 this->wireDecode(endorseWire);
25}
26
Varun Patil3d850902020-11-23 12:19:14 +053027template<ndn::encoding::Tag T>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080028size_t
29EndorseInfo::wireEncode(ndn::EncodingImpl<T>& block) const
30{
31 size_t totalLength = 0;
32
33 // EndorseInfo := ENDORSE-INFO-TYPE TLV-LENGTH
34 // ENDORSEMENT+
35 //
36 // Endorsement := ENDORSEMENT-TYPE TLV-LENGTH
37 // EndorseType
38 // EndorseValue
39 // EndorseCount
40 //
41 // EndorseType := ENDORSETYPE-TYPE TLV-LENGTH
42 // String
43 //
44 // EndorseValue := ENDORSEVALUE-TYPE TLV-LENGTH
45 // String
46 // EndorseCount := ENDORSECOUNT-TYPE TLV-LENGTH
47 // String
48
49 // Entries
50 size_t entryLength = 0;
51 for (std::vector<Endorsement>::const_reverse_iterator it = m_endorsements.rbegin();
52 it != m_endorsements.rend(); it++) {
53
54 // Endorse Count
55 const uint8_t *countWire = reinterpret_cast<const uint8_t*>(it->count.c_str());
56 entryLength += block.prependByteArrayBlock(tlv::EndorseCount, countWire, it->count.length());
57
58 // Endorse Value
59 const uint8_t *valueWire = reinterpret_cast<const uint8_t*>(it->value.c_str());
60 entryLength += block.prependByteArrayBlock(tlv::EndorseValue, valueWire, it->value.length());
61
62 // Endorse Type
63 const uint8_t *typeWire = reinterpret_cast<const uint8_t*>(it->type.c_str());
64 entryLength += block.prependByteArrayBlock(tlv::EndorseType, typeWire, it->type.length());
65
66 // Endorsement
67 entryLength += block.prependVarNumber(entryLength);
68 entryLength += block.prependVarNumber(tlv::Endorsement);
69 totalLength += entryLength;
70 entryLength = 0;
71 }
72
73 // Profile
74 totalLength += block.prependVarNumber(totalLength);
75 totalLength += block.prependVarNumber(tlv::EndorseInfo);
76
77 return totalLength;
78
79}
80
81const Block&
82EndorseInfo::wireEncode() const
83{
84 ndn::EncodingEstimator estimator;
85 size_t estimatedSize = wireEncode(estimator);
86
87 ndn::EncodingBuffer buffer(estimatedSize, 0);
88 wireEncode(buffer);
89
90 m_wire = buffer.block();
91 m_wire.parse();
92
93 return m_wire;
94}
95
96void
97EndorseInfo::wireDecode(const Block& endorseWire)
98{
99 m_wire = endorseWire;
100 m_wire.parse();
101 m_endorsements.clear();
102
103 if (m_wire.type() != tlv::EndorseInfo)
Varun Patila24bd3e2020-11-24 10:08:33 +0530104 NDN_THROW(Error("Unexpected TLV number when decoding endorse info packet"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800105
106 // Endorsement
107 Block::element_const_iterator i = m_wire.elements_begin();
108 if (i == m_wire.elements_end())
Varun Patila24bd3e2020-11-24 10:08:33 +0530109 NDN_THROW(Error("Missing Endorsement"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800110 if (i->type() != tlv::Endorsement)
Varun Patila24bd3e2020-11-24 10:08:33 +0530111 NDN_THROW(Error("Expect Endorsement but get TLV Type " + std::to_string(i->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800112
113 while (i != m_wire.elements_end() && i->type() == tlv::Endorsement) {
114 Endorsement endorsement;
115 Block temp = *i;
116 temp.parse();
117 Block::element_const_iterator j = temp.elements_begin();
118
119 // Endorse Type
120 if (j == temp.elements_end())
Varun Patila24bd3e2020-11-24 10:08:33 +0530121 NDN_THROW(Error("Missing Endorse Type"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800122 if (j->type() != tlv::EndorseType)
Varun Patila24bd3e2020-11-24 10:08:33 +0530123 NDN_THROW(Error("Expect Endorse Type but get TLV Type " + std::to_string(j->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800124
125 endorsement.type = std::string(reinterpret_cast<const char* >(j->value()),
126 j->value_size());;
127 ++j;
128
129 // Endorse Value
130 if (j == temp.elements_end())
Varun Patila24bd3e2020-11-24 10:08:33 +0530131 NDN_THROW(Error("Missing Endorse Value"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800132 if (j->type() != tlv::EndorseValue)
Varun Patila24bd3e2020-11-24 10:08:33 +0530133 NDN_THROW(Error("Expect Endorse Value but get TLV Type " + std::to_string(j->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800134
135 endorsement.value = std::string(reinterpret_cast<const char* >(j->value()),
136 j->value_size());
137 ++j;
138
139 // Endorse Count
140 if (j == temp.elements_end())
Varun Patila24bd3e2020-11-24 10:08:33 +0530141 NDN_THROW(Error("Missing Endorse Count"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800142 if (j->type() != tlv::EndorseCount)
Varun Patila24bd3e2020-11-24 10:08:33 +0530143 NDN_THROW(Error("Expect Endorse Count but get TLV Type " + std::to_string(j->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800144
145 endorsement.count = std::string(reinterpret_cast<const char* >(j->value()),
146 j->value_size());
147 ++j;
Varun Patila24bd3e2020-11-24 10:08:33 +0530148 if (j != temp.elements_end())
149 NDN_THROW(Error("Unexpected element"));
150
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800151 m_endorsements.push_back(endorsement);
152 ++i;
153 }
154
Varun Patila24bd3e2020-11-24 10:08:33 +0530155 if (i != m_wire.elements_end())
156 NDN_THROW(Error("Unexpected element"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800157}
158
159void
160EndorseInfo::addEndorsement(const std::string& type, const std::string& value,
161 const std::string& count) {
162 Endorsement endorsement;
163 endorsement.type = type;
164 endorsement.value = value;
165 endorsement.count = count;
166 m_endorsements.push_back(endorsement);
167}
168
169} // namespace chronochat