blob: 4f55e214fb8c076df4f624b431a662dbeb7b62c4 [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
35template<bool T>
36size_t
37RR::wireEncode(EncodingImpl<T> & block) const
38{
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070039 size_t totalLength = 0;
40 const std::string& msg = this->getRrdata();
41 totalLength += prependByteArrayBlock(block,
42 tlv::ndns::RRData,
43 reinterpret_cast<const uint8_t*>(msg.c_str()),
44 msg.size()
45 );
shockjiange9c1ab92014-07-21 12:02:52 -070046
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070047 return totalLength;
shockjiange9c1ab92014-07-21 12:02:52 -070048}
49
50const Block&
51RR::wireEncode() const
52{
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070053 if (m_wire.hasWire())
54 return m_wire;
55 EncodingEstimator estimator;
56 size_t estimatedSize = wireEncode(estimator);
57 EncodingBuffer buffer(estimatedSize, 0);
58 wireEncode(buffer);
59 m_wire = buffer.block();
60 return m_wire;
shockjiange9c1ab92014-07-21 12:02:52 -070061}
62
63void
64RR::wireDecode(const Block& wire)
65{
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070066 if (!wire.hasWire()) {
67 throw Tlv::Error("The supplied block does not contain wire format");
68 }
shockjiange9c1ab92014-07-21 12:02:52 -070069
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070070 if (wire.type() != tlv::ndns::RRData)
71 throw Tlv::Error("Unexpected TLV type when decoding Content");
shockjiange9c1ab92014-07-21 12:02:52 -070072
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070073 m_wire = wire;
74 m_wire.parse();
shockjiange9c1ab92014-07-21 12:02:52 -070075
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070076 Block::element_const_iterator it = m_wire.elements_begin();
shockjiange9c1ab92014-07-21 12:02:52 -070077
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070078 if (it != m_wire.elements_end() && it->type() == tlv::ndns::RRData)
79 {
80 m_rrData = std::string(reinterpret_cast<const char*>(it->value()), it->value_size());
81 it ++;
82 } else {
83 throw Tlv::Error("not the RRData Type");
84 }
shockjiange9c1ab92014-07-21 12:02:52 -070085
86}
87
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070088} // namespace ndns
89} // namespace ndn