Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 3 | * Copyright (c) 2020, Regents of the University of California |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 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) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 82 | NDN_THROW(Error("Unexpected TLV number when decoding conf packet")); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 83 | |
| 84 | // Identity |
| 85 | Block::element_const_iterator i = m_wire.elements_begin(); |
| 86 | if (i == m_wire.elements_end()) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 87 | NDN_THROW(Error("Missing Identity")); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 88 | if (i->type() != tlv::Name) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 89 | NDN_THROW(Error("Expect Identity but get TLV Type " + std::to_string(i->type()))); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 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) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 98 | NDN_THROW(Error("Expect Nick but get TLV Type " + std::to_string(i->type()))); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 99 | |
| 100 | m_nick = std::string(reinterpret_cast<const char* >(i->value()), |
| 101 | i->value_size()); |
| 102 | ++i; |
| 103 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 104 | if (i != m_wire.elements_end()) |
| 105 | NDN_THROW(Error("Unexpected element")); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
| 109 | Conf::setIdentity(const Name& identity) |
| 110 | { |
| 111 | m_wire.reset(); |
| 112 | m_identity = identity; |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | Conf::setNick(const std::string& nick) |
| 117 | { |
| 118 | m_wire.reset(); |
| 119 | m_nick = nick; |
| 120 | } |
| 121 | |
| 122 | } // namespace chronochat |