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-extension.hpp" |
| 11 | |
| 12 | namespace chronochat { |
| 13 | |
| 14 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<EndorseExtension>)); |
| 15 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<EndorseExtension>)); |
| 16 | |
| 17 | EndorseExtension::EndorseExtension() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | EndorseExtension::EndorseExtension(const Block& endorseWire) |
| 22 | { |
| 23 | this->wireDecode(endorseWire); |
| 24 | } |
| 25 | |
| 26 | template<bool T> |
| 27 | size_t |
| 28 | EndorseExtension::wireEncode(ndn::EncodingImpl<T>& block) const |
| 29 | { |
| 30 | size_t totalLength = 0; |
| 31 | |
| 32 | // EndorseExtension := ENDORSE-EXTENSION-TYPE TLV-LENGTH |
| 33 | // EntryData+ |
| 34 | // |
| 35 | // EntryData := ENTRYDATA-TYPE TLV-LENGTH |
| 36 | // String |
| 37 | // |
| 38 | |
| 39 | // EntryData |
| 40 | for (std::list<std::string>::const_reverse_iterator it = m_entries.rbegin(); |
| 41 | it != m_entries.rend(); it++) { |
| 42 | const uint8_t *entryWire = reinterpret_cast<const uint8_t*>(it->c_str()); |
| 43 | totalLength += block.prependByteArrayBlock(tlv::EntryData, entryWire, it->length()); |
| 44 | } |
| 45 | |
| 46 | // EndorseExtension |
| 47 | totalLength += block.prependVarNumber(totalLength); |
| 48 | totalLength += block.prependVarNumber(tlv::EndorseExtension); |
| 49 | |
| 50 | return totalLength; |
| 51 | } |
| 52 | |
| 53 | const Block& |
| 54 | EndorseExtension::wireEncode() const |
| 55 | { |
| 56 | ndn::EncodingEstimator estimator; |
| 57 | size_t estimatedSize = wireEncode(estimator); |
| 58 | |
| 59 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 60 | wireEncode(buffer); |
| 61 | |
| 62 | m_wire = buffer.block(); |
| 63 | m_wire.parse(); |
| 64 | |
| 65 | return m_wire; |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | EndorseExtension::wireDecode(const Block& endorseWire) |
| 70 | { |
| 71 | m_wire = endorseWire; |
| 72 | m_wire.parse(); |
| 73 | |
| 74 | // EndorseExtension := ENDORSE-EXTENSION-TYPE TLV-LENGTH |
| 75 | // EntryData+ |
| 76 | // |
| 77 | // EntryData := ENTRYDATA-TYPE TLV-LENGTH |
| 78 | // String |
| 79 | // |
| 80 | |
| 81 | if (m_wire.type() != tlv::EndorseExtension) |
| 82 | throw Error("Unexpected TLV number when decoding endorse extension packet"); |
| 83 | |
| 84 | // EntryData |
| 85 | Block::element_const_iterator i = m_wire.elements_begin(); |
| 86 | if (i == m_wire.elements_end()) |
| 87 | throw Error("Missing Entry Data"); |
| 88 | if (i->type() != tlv::EntryData) |
| 89 | throw Error("Expect Entry Data but get TLV Type " + std::to_string(i->type())); |
| 90 | |
| 91 | while (i != m_wire.elements_end() && i->type() == tlv::EntryData) { |
| 92 | m_entries.push_back(std::string(reinterpret_cast<const char* >(i->value()), |
| 93 | i->value_size())); |
| 94 | ++i; |
| 95 | } |
| 96 | |
| 97 | if (i != m_wire.elements_end()) { |
| 98 | throw Error("Unexpected element"); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | EndorseExtension::addEntry(const std::string& entry) |
| 104 | { |
| 105 | if (find(m_entries.begin(), m_entries.end(), entry) == m_entries.end()) { |
| 106 | m_entries.push_back(entry); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | EndorseExtension::removeEntry(const std::string& entry) |
| 112 | { |
| 113 | m_entries.remove(entry); |
| 114 | } |
| 115 | |
| 116 | } // namespace chronochat |