Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * |
| 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 | |
| 12 | namespace chronochat { |
| 13 | |
| 14 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<EndorseInfo>)); |
| 15 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<EndorseInfo>)); |
| 16 | |
| 17 | |
| 18 | EndorseInfo::EndorseInfo() |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | EndorseInfo::EndorseInfo(const Block& endorseWire) |
| 23 | { |
| 24 | this->wireDecode(endorseWire); |
| 25 | } |
| 26 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 27 | template<ndn::encoding::Tag T> |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 28 | size_t |
| 29 | EndorseInfo::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 | |
| 81 | const Block& |
| 82 | EndorseInfo::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 | |
| 96 | void |
| 97 | EndorseInfo::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) |
| 104 | throw Error("Unexpected TLV number when decoding endorse info packet"); |
| 105 | |
| 106 | // Endorsement |
| 107 | Block::element_const_iterator i = m_wire.elements_begin(); |
| 108 | if (i == m_wire.elements_end()) |
| 109 | throw Error("Missing Endorsement"); |
| 110 | if (i->type() != tlv::Endorsement) |
| 111 | throw Error("Expect Endorsement but get TLV Type " + std::to_string(i->type())); |
| 112 | |
| 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()) |
| 121 | throw Error("Missing Endorse Type"); |
| 122 | if (j->type() != tlv::EndorseType) |
| 123 | throw Error("Expect Endorse Type but get TLV Type " + std::to_string(j->type())); |
| 124 | |
| 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()) |
| 131 | throw Error("Missing Endorse Value"); |
| 132 | if (j->type() != tlv::EndorseValue) |
| 133 | throw Error("Expect Endorse Value but get TLV Type " + std::to_string(j->type())); |
| 134 | |
| 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()) |
| 141 | throw Error("Missing Endorse Count"); |
| 142 | if (j->type() != tlv::EndorseCount) |
| 143 | throw Error("Expect Endorse Count but get TLV Type " + std::to_string(j->type())); |
| 144 | |
| 145 | endorsement.count = std::string(reinterpret_cast<const char* >(j->value()), |
| 146 | j->value_size()); |
| 147 | ++j; |
| 148 | if (j != temp.elements_end()) { |
| 149 | throw Error("Unexpected element"); |
| 150 | } |
| 151 | m_endorsements.push_back(endorsement); |
| 152 | ++i; |
| 153 | } |
| 154 | |
| 155 | if (i != m_wire.elements_end()) { |
| 156 | throw Error("Unexpected element"); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | EndorseInfo::addEndorsement(const std::string& type, const std::string& value, |
| 162 | const std::string& count) { |
| 163 | Endorsement endorsement; |
| 164 | endorsement.type = type; |
| 165 | endorsement.value = value; |
| 166 | endorsement.count = count; |
| 167 | m_endorsements.push_back(endorsement); |
| 168 | } |
| 169 | |
| 170 | } // namespace chronochat |