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-collection.hpp" |
| 11 | |
| 12 | namespace chronochat { |
| 13 | |
| 14 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<EndorseCollection>)); |
| 15 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<EndorseCollection>)); |
| 16 | |
| 17 | EndorseCollection::EndorseCollection() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | EndorseCollection::EndorseCollection(const Block& endorseWire) |
| 22 | { |
| 23 | this->wireDecode(endorseWire); |
| 24 | } |
| 25 | |
| 26 | template<bool T> |
| 27 | size_t |
| 28 | EndorseCollection::wireEncode(ndn::EncodingImpl<T>& block) const |
| 29 | { |
| 30 | size_t totalLength = 0; |
| 31 | |
| 32 | // EndorseCollection := ENDORSE-COLLECTION-TYPE TLV-LENGTH |
| 33 | // EndorseCollectionEntry+ |
| 34 | // |
| 35 | // EndorseCollectionEntry := ENDORSE-COLLECTION-ENTRY-TYPE TLV-LENGTH |
| 36 | // Name |
| 37 | // Hash |
| 38 | // |
| 39 | // Hash := HASH-TYPE TLV-LENGTH |
| 40 | // String |
| 41 | |
| 42 | // Entries |
| 43 | size_t entryLength = 0; |
| 44 | for (std::vector<CollectionEntry>::const_reverse_iterator it = m_entries.rbegin(); |
| 45 | it != m_entries.rend(); it++) { |
| 46 | // Hash |
| 47 | const uint8_t* dataWire = reinterpret_cast<const uint8_t*>(it->hash.c_str()); |
| 48 | entryLength += block.prependByteArrayBlock(tlv::Hash, dataWire, it->hash.length()); |
| 49 | // CertName |
| 50 | entryLength += it->certName.wireEncode(block); |
| 51 | // Entry |
| 52 | entryLength += block.prependVarNumber(entryLength); |
| 53 | entryLength += block.prependVarNumber(tlv::EndorseCollectionEntry); |
| 54 | totalLength += entryLength; |
| 55 | entryLength = 0; |
| 56 | } |
| 57 | |
| 58 | // Profile |
| 59 | totalLength += block.prependVarNumber(totalLength); |
| 60 | totalLength += block.prependVarNumber(tlv::EndorseCollection); |
| 61 | |
| 62 | return totalLength; |
| 63 | |
| 64 | } |
| 65 | |
| 66 | const Block& |
| 67 | EndorseCollection::wireEncode() const |
| 68 | { |
| 69 | ndn::EncodingEstimator estimator; |
| 70 | size_t estimatedSize = wireEncode(estimator); |
| 71 | |
| 72 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 73 | wireEncode(buffer); |
| 74 | |
| 75 | m_wire = buffer.block(); |
| 76 | m_wire.parse(); |
| 77 | |
| 78 | return m_wire; |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | EndorseCollection::wireDecode(const Block& endorseWire) |
| 83 | { |
| 84 | m_wire = endorseWire; |
| 85 | m_wire.parse(); |
| 86 | m_entries.clear(); |
| 87 | |
| 88 | if (m_wire.type() != tlv::EndorseCollection) |
| 89 | throw Error("Unexpected TLV number when decoding endorse collection packet"); |
| 90 | |
| 91 | Block::element_const_iterator i = m_wire.elements_begin(); |
| 92 | if (i == m_wire.elements_end()) |
| 93 | throw Error("Missing Endorse Collection Entry"); |
| 94 | if (i->type() != tlv::EndorseCollectionEntry) |
| 95 | throw Error("Expect Endorse Collection Entry but get TLV Type " + std::to_string(i->type())); |
| 96 | |
| 97 | while (i != m_wire.elements_end() && i->type() == tlv::EndorseCollectionEntry) { |
| 98 | CollectionEntry entry; |
| 99 | Block temp = *i; |
| 100 | temp.parse(); |
| 101 | Block::element_const_iterator j = temp.elements_begin(); |
| 102 | if (j == temp.elements_end()) |
| 103 | throw Error("Missing Cert Name"); |
| 104 | if (j->type() != tlv::Name) |
| 105 | throw Error("Expect Cert Name but get TLV Type " + std::to_string(j->type())); |
| 106 | |
| 107 | entry.certName.wireDecode(*j); |
| 108 | |
| 109 | ++j; |
| 110 | if (j == temp.elements_end()) |
| 111 | throw Error("Missing Hash"); |
| 112 | if (j->type() != tlv::Hash) |
| 113 | throw Error("Expect Hash but get TLV Type " + std::to_string(j->type())); |
| 114 | |
| 115 | entry.hash = std::string(reinterpret_cast<const char* >(j->value()), |
| 116 | j->value_size()); |
| 117 | ++j; |
| 118 | if (j != temp.elements_end()) { |
| 119 | throw Error("Unexpected element"); |
| 120 | } |
| 121 | m_entries.push_back(entry); |
| 122 | ++i; |
| 123 | } |
| 124 | |
| 125 | if (i != m_wire.elements_end()) { |
| 126 | throw Error("Unexpected element"); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | EndorseCollection::addCollectionEntry(const Name& certName, const std::string& hash) { |
| 132 | CollectionEntry entry; |
| 133 | entry.certName = certName; |
| 134 | entry.hash = hash; |
| 135 | m_entries.push_back(entry); |
| 136 | } |
| 137 | |
| 138 | } // namespace chronochat |