blob: c2700ba32e17be04f03170d0d5cc4f5ee7ac8c40 [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 -070035
shockjianga5ae48c2014-07-27 23:21:41 -070036
37
shockjiange9c1ab92014-07-21 12:02:52 -070038
39const Block&
40RR::wireEncode() const
41{
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070042 if (m_wire.hasWire())
43 return m_wire;
44 EncodingEstimator estimator;
shockjianga5ae48c2014-07-27 23:21:41 -070045
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070046 size_t estimatedSize = wireEncode(estimator);
shockjianga5ae48c2014-07-27 23:21:41 -070047 //std::cout<<"estmatedSize="<<estimatedSize<<std::endl;
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070048 EncodingBuffer buffer(estimatedSize, 0);
49 wireEncode(buffer);
50 m_wire = buffer.block();
51 return m_wire;
shockjiange9c1ab92014-07-21 12:02:52 -070052}
53
54void
55RR::wireDecode(const Block& wire)
56{
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070057 if (!wire.hasWire()) {
58 throw Tlv::Error("The supplied block does not contain wire format");
59 }
shockjiange9c1ab92014-07-21 12:02:52 -070060
shockjianga5ae48c2014-07-27 23:21:41 -070061 if (wire.type() != ndn::ndns::tlv::RRData)
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070062 throw Tlv::Error("Unexpected TLV type when decoding Content");
shockjiange9c1ab92014-07-21 12:02:52 -070063
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070064 m_wire = wire;
65 m_wire.parse();
shockjiange9c1ab92014-07-21 12:02:52 -070066
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070067 Block::element_const_iterator it = m_wire.elements_begin();
shockjiange9c1ab92014-07-21 12:02:52 -070068
shockjianga5ae48c2014-07-27 23:21:41 -070069 if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::RRDataSub1)
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070070 {
shockjianga5ae48c2014-07-27 23:21:41 -070071 m_id = readNonNegativeInteger(*it);
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070072 it ++;
73 } else {
shockjianga5ae48c2014-07-27 23:21:41 -070074 throw Tlv::Error("not the RRDataSub1 Type");
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070075 }
shockjiange9c1ab92014-07-21 12:02:52 -070076
shockjianga5ae48c2014-07-27 23:21:41 -070077 if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::RRDataSub2)
78 {
79
80 m_rrData = std::string(reinterpret_cast<const char*>(it->value()),
81 it->value_size());
82 it ++;
83 } else {
84 throw Tlv::Error("not the RRDataSub2 Type");
85 }
86
87
88
shockjiange9c1ab92014-07-21 12:02:52 -070089}
90
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070091} // namespace ndns
92} // namespace ndn