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. |
| 4 | * |
| 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/>. |
| 18 | */ |
| 19 | |
| 20 | #ifndef NDNS_RR_HPP |
| 21 | #define NDNS_RR_HPP |
| 22 | |
| 23 | #include "ndns-tlv.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/encoding/block.hpp> |
| 26 | #include <ndn-cxx/interest.hpp> |
| 27 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 28 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
| 29 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace ndns { |
| 32 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 33 | |
| 34 | class RR { |
| 35 | public: |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 36 | |
| 37 | enum RRType |
| 38 | { |
| 39 | NS, |
| 40 | TXT, |
| 41 | UNKNOWN |
| 42 | }; |
| 43 | static std::string |
| 44 | toString(const RRType& type) |
| 45 | { |
| 46 | std::string str; |
| 47 | |
| 48 | switch (type) |
| 49 | { |
| 50 | case NS: |
| 51 | str = "NS"; |
| 52 | break; |
| 53 | case TXT: |
| 54 | str = "TXT"; |
| 55 | break; |
| 56 | default: |
| 57 | str = "UNKNOWN"; |
| 58 | break; |
| 59 | } |
| 60 | return str; |
| 61 | } |
| 62 | |
| 63 | static RRType |
| 64 | toRRType(const std::string& str) |
| 65 | { |
| 66 | RRType atype; |
| 67 | if (str == "NS"){ |
| 68 | atype = NS; |
| 69 | } |
| 70 | else if (str == "TXT") { |
| 71 | atype = TXT; |
| 72 | } |
| 73 | else { |
| 74 | atype = UNKNOWN; |
| 75 | } |
| 76 | return atype; |
| 77 | } |
| 78 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 79 | RR(); |
| 80 | virtual ~RR(); |
| 81 | |
| 82 | const std::string& |
| 83 | getRrdata() const |
| 84 | { |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 85 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 86 | return m_rrData; |
| 87 | } |
| 88 | |
| 89 | void setRrdata(const std::string& rrdata) |
| 90 | { |
| 91 | this->m_rrData = rrdata; |
| 92 | } |
| 93 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 94 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 95 | |
| 96 | public: |
| 97 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 98 | uint32_t getId() const { |
| 99 | return m_id; |
| 100 | } |
| 101 | |
| 102 | void setId(uint32_t id) { |
| 103 | m_id = id; |
| 104 | } |
| 105 | |
| 106 | const Block& getWire() const { |
| 107 | return m_wire; |
| 108 | } |
| 109 | |
| 110 | void setWire(const Block& wire) { |
| 111 | m_wire = wire; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | inline bool operator==(const RR& rr) const |
| 116 | { |
| 117 | if (this->getRrdata() == rr.getRrdata()) |
| 118 | return true; |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | template<bool T> |
| 125 | inline size_t |
| 126 | wireEncode(EncodingImpl<T> & block) const |
| 127 | { |
| 128 | size_t totalLength = 0; |
| 129 | const std::string& msg = this->getRrdata(); |
| 130 | totalLength += prependByteArrayBlock(block, |
| 131 | ndn::ndns::tlv::RRDataSub2, |
| 132 | reinterpret_cast<const uint8_t*>(msg.c_str()), |
| 133 | msg.size() |
| 134 | ); |
| 135 | |
| 136 | totalLength += prependNonNegativeIntegerBlock(block, |
| 137 | ndn::ndns::tlv::RRDataSub1, |
| 138 | this->getId()); |
| 139 | |
| 140 | totalLength += block.prependVarNumber(totalLength); |
| 141 | totalLength += block.prependVarNumber(ndn::ndns::tlv::RRData); |
| 142 | //std::cout<<"call rr.h wireEncode"<<std::endl; |
| 143 | return totalLength; |
| 144 | } |
| 145 | |
| 146 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 147 | const Block& |
| 148 | wireEncode() const; |
| 149 | |
| 150 | void |
| 151 | wireDecode(const Block& wire); |
| 152 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 153 | //inline std::ostream& operator<<(std::ostream& os, const RR& rr); |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 154 | |
| 155 | private: |
| 156 | uint32_t m_id; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 157 | //unsigned long m_id; |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 158 | std::string m_rrData; |
| 159 | |
| 160 | mutable Block m_wire; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 161 | };//class RR |
| 162 | |
| 163 | |
| 164 | inline std::ostream& |
| 165 | operator<<(std::ostream& os, const RR& rr) |
| 166 | { |
| 167 | os<<"RR: Id="<<rr.getId()<<" Data="<<rr.getRrdata(); |
| 168 | return os; |
| 169 | } |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 170 | |
| 171 | } // namespace ndns |
| 172 | } // namespace ndn |
| 173 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 174 | |
| 175 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 176 | #endif // NDNS_RR_HPP |