blob: 4e8f33525149af3d0bb2a4a4c031c49f1cb04984 [file] [log] [blame]
Shock Jiang895bc1b2014-10-01 20:00:58 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Davide Pesavento98026122022-03-14 22:00:03 -04003 * Copyright (c) 2014-2022, Regents of the University of California.
Shock Jiang895bc1b2014-10-01 20:00:58 -07004 *
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_CLIENTS_RESPONSE_HPP
21#define NDNS_CLIENTS_RESPONSE_HPP
22
23#include "ndns-tlv.hpp"
24#include "ndns-enum.hpp"
25#include "ndns-label.hpp"
26
Shock Jiang895bc1b2014-10-01 20:00:58 -070027#include <ndn-cxx/data.hpp>
Shock Jiang895bc1b2014-10-01 20:00:58 -070028#include <ndn-cxx/meta-info.hpp>
29
Shock Jiang895bc1b2014-10-01 20:00:58 -070030namespace ndn {
31namespace ndns {
32
33/**
Shock Jiangcde28712014-10-19 21:17:20 -070034 * @brief Default life time of resource record
Shock Jiang895bc1b2014-10-01 20:00:58 -070035 */
Yumin Xia55a7cc42017-05-14 18:43:34 -070036const time::seconds DEFAULT_RR_FRESHNESS_PERIOD = 3600_s;
Shock Jiang895bc1b2014-10-01 20:00:58 -070037
38
39/**
40 * @brief NDNS Response abstraction. Response is used on client side,
41 * while Rrset is used on server side, and Data packet is used during transmission in network.
42 */
43class Response
44{
45public:
Yumin Xia55a7cc42017-05-14 18:43:34 -070046 class Error : public ndn::tlv::Error
47 {
48 public:
49 using ndn::tlv::Error::Error;
50 };
51
Shock Jiang895bc1b2014-10-01 20:00:58 -070052 Response();
53
54 Response(const Name& zone, const name::Component& queryType);
55
56 /**
57 * @brief fill the attributes from Data packet.
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080058 *
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080059 * @param zone NDNS zone name
Yumin Xia6343c5b2016-10-20 15:45:50 -070060 * @param data Data.getName() must the same zone as its prefix,
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080061 * otherwise it's undefined behavior
Shock Jiang895bc1b2014-10-01 20:00:58 -070062 * @return false if Data.getName() does not follow the structure of NDNS Response without
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080063 * changing any attributes, otherwise return true and fill the attributes
Shock Jiang895bc1b2014-10-01 20:00:58 -070064 */
65 bool
Yumin Xia6343c5b2016-10-20 15:45:50 -070066 fromData(const Name& zone, const Data& data);
Shock Jiang895bc1b2014-10-01 20:00:58 -070067
68 shared_ptr<Data>
69 toData();
70
71 Response&
72 addRr(const Block& rr);
73
74 /**
75 * @brief add Block which contains string information and its tlv type is ndns::tlv::RrData
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080076 *
77 * @return Response that is service level information, the encoding level abstraction,
78 * i.e., Block is not very convenient.
Shock Jiang895bc1b2014-10-01 20:00:58 -070079 */
80 Response&
81 addRr(const std::string& rr);
82
83 bool
84 removeRr(const Block& rr);
85
86 bool
87 operator==(const Response& other) const;
88
89 bool
90 operator!=(const Response& other) const
91 {
92 return !(*this == other);
93 }
94
95 /**
96 * @brief encode the app-level data
97 */
Davide Pesavento98026122022-03-14 22:00:03 -040098 Block
Shock Jiang895bc1b2014-10-01 20:00:58 -070099 wireEncode() const;
100
101 /**
102 * @brief decode the app-level data
103 */
104 void
105 wireDecode(const Block& wire);
106
107 /**
108 * @brief encode app-level data
109 */
Yumin Xia2c509c22017-02-09 14:37:36 -0800110 template<encoding::Tag T>
Shock Jiang895bc1b2014-10-01 20:00:58 -0700111 size_t
Davide Pesavento98026122022-03-14 22:00:03 -0400112 wireEncode(EncodingImpl<T>& encoder) const;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700113
Yumin Xia55a7cc42017-05-14 18:43:34 -0700114 static std::pair<Name, Name>
115 wireDecodeDoe(const Block& wire);
116
Davide Pesavento98026122022-03-14 22:00:03 -0400117public: // getter and setter
Shock Jiang895bc1b2014-10-01 20:00:58 -0700118 const Name&
119 getZone() const
120 {
121 return m_zone;
122 }
123
124 void
125 setZone(const Name& zone)
126 {
127 m_zone = zone;
128 }
129
130 const name::Component&
131 getQueryType() const
132 {
133 return m_queryType;
134 }
135
136 void
137 setQueryType(const name::Component& queryType)
138 {
139 m_queryType = queryType;
140 }
141
142 const Name&
143 getRrLabel() const
144 {
145 return m_rrLabel;
146 }
147
148 void
149 setRrLabel(const Name& rrLabel)
150 {
151 m_rrLabel = rrLabel;
152 }
153
154 void
155 setRrType(const name::Component& rrType)
156 {
157 m_rrType = rrType;
158 }
159
160 const name::Component&
161 getRrType() const
162 {
163 return m_rrType;
164 }
165
166 void
167 setVersion(const name::Component& version)
168 {
169 m_version = version;
170 }
171
172 const name::Component&
173 getVersion() const
174 {
175 return m_version;
176 }
177
178 void
Yumin Xiaa484ba72016-11-10 20:40:12 -0800179 setContentType(NdnsContentType contentType)
Shock Jiang895bc1b2014-10-01 20:00:58 -0700180 {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800181 m_contentType = contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700182 }
183
Yumin Xiaa484ba72016-11-10 20:40:12 -0800184 NdnsContentType
185 getContentType() const
Shock Jiang895bc1b2014-10-01 20:00:58 -0700186 {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800187 return m_contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700188 }
189
190 const Block&
191 getAppContent() const
192 {
193 return m_appContent;
194 }
195
196 void
197 setAppContent(const Block& block);
198
199 const std::vector<Block>&
200 getRrs() const
201 {
202 return m_rrs;
203 }
204
205 void
206 setRrs(const std::vector<Block>& rrs)
207 {
208 m_rrs = rrs;
209 }
210
211 time::seconds
212 getFreshnessPeriod() const
213 {
214 return m_freshnessPeriod;
215 }
216
217 void
218 setFreshnessPeriod(time::seconds freshnessPeriod)
219 {
220 m_freshnessPeriod = freshnessPeriod;
221 }
222
223private:
224 Name m_zone;
225 name::Component m_queryType;
226 Name m_rrLabel;
227 name::Component m_rrType;
228 name::Component m_version;
229
Yumin Xiaa484ba72016-11-10 20:40:12 -0800230 NdnsContentType m_contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700231 time::seconds m_freshnessPeriod;
232
233 /**
234 * @brief App content. Be valid only for NDNS-NULL Response
235 */
236 Block m_appContent;
237
238 /**
239 * @brief Content of Resource Record. Be valid only when this is not a NDNS-NULL Response
240 */
241 std::vector<Block> m_rrs;
242};
243
244std::ostream&
245operator<<(std::ostream& os, const Response& response);
246
247} // namespace ndns
248} // namespace ndn
249
250#endif // NDNS_CLIENTS_RESPONSE_HPP