blob: 106bb397b5516fd4bef8d49ed8cc0ad882bf39d6 [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.
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
shockjianga5ae48c2014-07-27 23:21:41 -070028#include <ndn-cxx/encoding/encoding-buffer.hpp>
29
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070030namespace ndn {
31namespace ndns {
32
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070033
34class RR {
35public:
shockjianga5ae48c2014-07-27 23:21:41 -070036
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 Afanasyev6e3d1ac2014-07-21 12:47:49 -070079 RR();
80 virtual ~RR();
81
82 const std::string&
83 getRrdata() const
84 {
shockjianga5ae48c2014-07-27 23:21:41 -070085
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070086 return m_rrData;
87 }
88
89 void setRrdata(const std::string& rrdata)
90 {
91 this->m_rrData = rrdata;
92 }
93
shockjianga5ae48c2014-07-27 23:21:41 -070094
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070095
96public:
97
shockjianga5ae48c2014-07-27 23:21:41 -070098 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 Afanasyev6e3d1ac2014-07-21 12:47:49 -0700147 const Block&
148 wireEncode() const;
149
150 void
151 wireDecode(const Block& wire);
152
shockjianga5ae48c2014-07-27 23:21:41 -0700153 //inline std::ostream& operator<<(std::ostream& os, const RR& rr);
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700154
155private:
156 uint32_t m_id;
shockjianga5ae48c2014-07-27 23:21:41 -0700157 //unsigned long m_id;
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700158 std::string m_rrData;
159
160 mutable Block m_wire;
shockjianga5ae48c2014-07-27 23:21:41 -0700161};//class RR
162
163
164inline std::ostream&
165operator<<(std::ostream& os, const RR& rr)
166{
167 os<<"RR: Id="<<rr.getId()<<" Data="<<rr.getRrdata();
168 return os;
169}
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700170
171} // namespace ndns
172} // namespace ndn
173
shockjianga5ae48c2014-07-27 23:21:41 -0700174
175
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700176#endif // NDNS_RR_HPP