blob: ffbefe6042001a5a52b80de65f694105f5a6324f [file] [log] [blame]
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
shockjiange9c1ab92014-07-21 12:02:52 -07004 *
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -07005 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
shockjiange9c1ab92014-07-21 12:02:52 -070018 */
19
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070020#include "rr.hpp"
shockjiange9c1ab92014-07-21 12:02:52 -070021
22namespace ndn {
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070023namespace ndns {
shockjiange9c1ab92014-07-21 12:02:52 -070024
25RR::RR()
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070026 : m_id(0Lu)
27 , m_rrData("ex.com")
shockjiange9c1ab92014-07-21 12:02:52 -070028{
shockjiange9c1ab92014-07-21 12:02:52 -070029}
30
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070031RR::~RR()
32{
shockjiange9c1ab92014-07-21 12:02:52 -070033}
34
shockjiange9c1ab92014-07-21 12:02:52 -070035const Block&
36RR::wireEncode() const
37{
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070038 if (m_wire.hasWire())
39 return m_wire;
40 EncodingEstimator estimator;
shockjianga5ae48c2014-07-27 23:21:41 -070041
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070042 size_t estimatedSize = wireEncode(estimator);
shockjianga5ae48c2014-07-27 23:21:41 -070043 //std::cout<<"estmatedSize="<<estimatedSize<<std::endl;
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070044 EncodingBuffer buffer(estimatedSize, 0);
45 wireEncode(buffer);
46 m_wire = buffer.block();
47 return m_wire;
shockjiange9c1ab92014-07-21 12:02:52 -070048}
49
shockjiang99ad3892014-08-03 14:56:13 -070050void RR::wireDecode(const Block& wire)
shockjiange9c1ab92014-07-21 12:02:52 -070051{
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070052 if (!wire.hasWire()) {
53 throw Tlv::Error("The supplied block does not contain wire format");
54 }
shockjiange9c1ab92014-07-21 12:02:52 -070055
shockjianga5ae48c2014-07-27 23:21:41 -070056 if (wire.type() != ndn::ndns::tlv::RRData)
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070057 throw Tlv::Error("Unexpected TLV type when decoding Content");
shockjiange9c1ab92014-07-21 12:02:52 -070058
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070059 m_wire = wire;
60 m_wire.parse();
shockjiange9c1ab92014-07-21 12:02:52 -070061
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070062 Block::element_const_iterator it = m_wire.elements_begin();
shockjiange9c1ab92014-07-21 12:02:52 -070063
shockjiang99ad3892014-08-03 14:56:13 -070064 if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::RRDataSub1) {
65 m_id = readNonNegativeInteger(*it);
66 it++;
67 } else {
shockjianga5ae48c2014-07-27 23:21:41 -070068 throw Tlv::Error("not the RRDataSub1 Type");
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070069 }
shockjiange9c1ab92014-07-21 12:02:52 -070070
shockjiang99ad3892014-08-03 14:56:13 -070071 if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::RRDataSub2) {
shockjianga5ae48c2014-07-27 23:21:41 -070072
shockjiang99ad3892014-08-03 14:56:13 -070073 m_rrData = std::string(reinterpret_cast<const char*>(it->value()),
74 it->value_size());
75 it++;
76 } else {
shockjianga5ae48c2014-07-27 23:21:41 -070077 throw Tlv::Error("not the RRDataSub2 Type");
78 }
79
shockjiange9c1ab92014-07-21 12:02:52 -070080}
81
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070082} // namespace ndns
83} // namespace ndn