Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 5 | * 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/>. |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 20 | #include "rr.hpp" |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 21 | |
| 22 | namespace ndn { |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 23 | namespace ndns { |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 24 | |
| 25 | RR::RR() |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 26 | : m_id(0Lu) |
| 27 | , m_rrData("ex.com") |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 28 | { |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 31 | RR::~RR() |
| 32 | { |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 33 | } |
| 34 | |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 35 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 36 | |
| 37 | |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 38 | |
| 39 | const Block& |
| 40 | RR::wireEncode() const |
| 41 | { |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 42 | if (m_wire.hasWire()) |
| 43 | return m_wire; |
| 44 | EncodingEstimator estimator; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 45 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 46 | size_t estimatedSize = wireEncode(estimator); |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 47 | //std::cout<<"estmatedSize="<<estimatedSize<<std::endl; |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 48 | EncodingBuffer buffer(estimatedSize, 0); |
| 49 | wireEncode(buffer); |
| 50 | m_wire = buffer.block(); |
| 51 | return m_wire; |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void |
| 55 | RR::wireDecode(const Block& wire) |
| 56 | { |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 57 | if (!wire.hasWire()) { |
| 58 | throw Tlv::Error("The supplied block does not contain wire format"); |
| 59 | } |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 60 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 61 | if (wire.type() != ndn::ndns::tlv::RRData) |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 62 | throw Tlv::Error("Unexpected TLV type when decoding Content"); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 63 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 64 | m_wire = wire; |
| 65 | m_wire.parse(); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 66 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 67 | Block::element_const_iterator it = m_wire.elements_begin(); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 68 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 69 | if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::RRDataSub1) |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 70 | { |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 71 | m_id = readNonNegativeInteger(*it); |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 72 | it ++; |
| 73 | } else { |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 74 | throw Tlv::Error("not the RRDataSub1 Type"); |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 75 | } |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 76 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 77 | 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 | |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 91 | } // namespace ndns |
| 92 | } // namespace ndn |