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 "conf.hpp" |
| 11 | |
| 12 | namespace chronochat { |
| 13 | |
| 14 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<Conf>)); |
| 15 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<Conf>)); |
| 16 | |
| 17 | Conf::Conf() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | Conf::Conf(const Block& confWire) |
| 22 | { |
| 23 | this->wireDecode(confWire); |
| 24 | } |
| 25 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 26 | template<ndn::encoding::Tag T> |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 27 | size_t |
| 28 | Conf::wireEncode(ndn::EncodingImpl<T>& block) const |
| 29 | { |
| 30 | size_t totalLength = 0; |
| 31 | |
| 32 | // Conf := CONF-TYPE TLV-LENGTH |
| 33 | // Name |
| 34 | // Nick |
| 35 | // |
| 36 | // Nick := NICK-TYPE TLV-LENGTH |
| 37 | // String |
| 38 | |
| 39 | // Nick |
| 40 | const uint8_t* nickWire = reinterpret_cast<const uint8_t*>(m_nick.c_str()); |
| 41 | totalLength += block.prependByteArrayBlock(tlv::Nick, nickWire, m_nick.length()); |
| 42 | |
| 43 | // Identity |
| 44 | totalLength += m_identity.wireEncode(block); |
| 45 | |
| 46 | // Conf |
| 47 | totalLength += block.prependVarNumber(totalLength); |
| 48 | totalLength += block.prependVarNumber(tlv::Conf); |
| 49 | |
| 50 | return totalLength; |
| 51 | } |
| 52 | |
| 53 | const Block& |
| 54 | Conf::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 | Conf::wireDecode(const Block& confWire) |
| 70 | { |
| 71 | m_wire = confWire; |
| 72 | m_wire.parse(); |
| 73 | |
| 74 | // Conf := CONF-TYPE TLV-LENGTH |
| 75 | // Name |
| 76 | // Nick |
| 77 | // |
| 78 | // Nick := NICK-TYPE TLV-LENGTH |
| 79 | // String |
| 80 | |
| 81 | if (m_wire.type() != tlv::Conf) |
| 82 | throw Error("Unexpected TLV number when decoding conf packet"); |
| 83 | |
| 84 | // Identity |
| 85 | Block::element_const_iterator i = m_wire.elements_begin(); |
| 86 | if (i == m_wire.elements_end()) |
| 87 | throw Error("Missing Identity"); |
| 88 | if (i->type() != tlv::Name) |
| 89 | throw Error("Expect Identity but get TLV Type " + std::to_string(i->type())); |
| 90 | |
| 91 | m_identity.wireDecode(*i); |
| 92 | ++i; |
| 93 | |
| 94 | // Nick |
| 95 | if (i == m_wire.elements_end()) |
| 96 | return; |
| 97 | if (i->type() != tlv::Nick) |
| 98 | throw Error("Expect Nick but get TLV Type " + std::to_string(i->type())); |
| 99 | |
| 100 | m_nick = std::string(reinterpret_cast<const char* >(i->value()), |
| 101 | i->value_size()); |
| 102 | ++i; |
| 103 | |
| 104 | if (i != m_wire.elements_end()) { |
| 105 | throw Error("Unexpected element"); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | Conf::setIdentity(const Name& identity) |
| 111 | { |
| 112 | m_wire.reset(); |
| 113 | m_identity = identity; |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | Conf::setNick(const std::string& nick) |
| 118 | { |
| 119 | m_wire.reset(); |
| 120 | m_nick = nick; |
| 121 | } |
| 122 | |
| 123 | } // namespace chronochat |