blob: db44604c9c0336304cd727b4de75d4b7ec19206d [file] [log] [blame]
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08004 *
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)
Varun Patila24bd3e2020-11-24 10:08:33 +053082 NDN_THROW(Error("Unexpected TLV number when decoding conf packet"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080083
84 // Identity
85 Block::element_const_iterator i = m_wire.elements_begin();
86 if (i == m_wire.elements_end())
Varun Patila24bd3e2020-11-24 10:08:33 +053087 NDN_THROW(Error("Missing Identity"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080088 if (i->type() != tlv::Name)
Varun Patila24bd3e2020-11-24 10:08:33 +053089 NDN_THROW(Error("Expect Identity but get TLV Type " + std::to_string(i->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080090
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 Patila24bd3e2020-11-24 10:08:33 +053098 NDN_THROW(Error("Expect Nick but get TLV Type " + std::to_string(i->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080099
100 m_nick = std::string(reinterpret_cast<const char* >(i->value()),
101 i->value_size());
102 ++i;
103
Varun Patila24bd3e2020-11-24 10:08:33 +0530104 if (i != m_wire.elements_end())
105 NDN_THROW(Error("Unexpected element"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800106}
107
108void
109Conf::setIdentity(const Name& identity)
110{
111 m_wire.reset();
112 m_identity = identity;
113}
114
115void
116Conf::setNick(const std::string& nick)
117{
118 m_wire.reset();
119 m_nick = nick;
120}
121
122} // namespace chronochat