blob: d02fe0831cf83452629c28e42954b39ce8c861b0 [file] [log] [blame]
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08001/* -*- 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
12namespace chronochat {
13
14BOOST_CONCEPT_ASSERT((ndn::WireEncodable<Conf>));
15BOOST_CONCEPT_ASSERT((ndn::WireDecodable<Conf>));
16
17Conf::Conf()
18{
19}
20
21Conf::Conf(const Block& confWire)
22{
23 this->wireDecode(confWire);
24}
25
Varun Patil3d850902020-11-23 12:19:14 +053026template<ndn::encoding::Tag T>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080027size_t
28Conf::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
53const Block&
54Conf::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
68void
69Conf::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
109void
110Conf::setIdentity(const Name& identity)
111{
112 m_wire.reset();
113 m_identity = identity;
114}
115
116void
117Conf::setNick(const std::string& nick)
118{
119 m_wire.reset();
120 m_nick = nick;
121}
122
123} // namespace chronochat