blob: a97fb7677a1315212c2d5eb52fe18b561f7d32a3 [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/*
Yumin Xia55a7cc42017-05-14 18:43:34 -07003 * Copyright (c) 2014-2018, 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
27
28#include <ndn-cxx/face.hpp>
29#include <ndn-cxx/name.hpp>
30#include <ndn-cxx/data.hpp>
31#include <ndn-cxx/encoding/block-helpers.hpp>
32#include <ndn-cxx/encoding/block.hpp>
33#include <ndn-cxx/meta-info.hpp>
34
35#include <vector>
36
37namespace ndn {
38namespace ndns {
39
40/**
Shock Jiangcde28712014-10-19 21:17:20 -070041 * @brief Default life time of resource record
Shock Jiang895bc1b2014-10-01 20:00:58 -070042 */
Yumin Xia55a7cc42017-05-14 18:43:34 -070043const time::seconds DEFAULT_RR_FRESHNESS_PERIOD = 3600_s;
Shock Jiang895bc1b2014-10-01 20:00:58 -070044
45
46/**
47 * @brief NDNS Response abstraction. Response is used on client side,
48 * while Rrset is used on server side, and Data packet is used during transmission in network.
49 */
50class Response
51{
52public:
Yumin Xia55a7cc42017-05-14 18:43:34 -070053 class Error : public ndn::tlv::Error
54 {
55 public:
56 using ndn::tlv::Error::Error;
57 };
58
Shock Jiang895bc1b2014-10-01 20:00:58 -070059 Response();
60
61 Response(const Name& zone, const name::Component& queryType);
62
63 /**
64 * @brief fill the attributes from Data packet.
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080065 *
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080066 * @param zone NDNS zone name
Yumin Xia6343c5b2016-10-20 15:45:50 -070067 * @param data Data.getName() must the same zone as its prefix,
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080068 * otherwise it's undefined behavior
Shock Jiang895bc1b2014-10-01 20:00:58 -070069 * @return false if Data.getName() does not follow the structure of NDNS Response without
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080070 * changing any attributes, otherwise return true and fill the attributes
Shock Jiang895bc1b2014-10-01 20:00:58 -070071 */
72 bool
Yumin Xia6343c5b2016-10-20 15:45:50 -070073 fromData(const Name& zone, const Data& data);
Shock Jiang895bc1b2014-10-01 20:00:58 -070074
75 shared_ptr<Data>
76 toData();
77
78 Response&
79 addRr(const Block& rr);
80
81 /**
82 * @brief add Block which contains string information and its tlv type is ndns::tlv::RrData
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080083 *
84 * @return Response that is service level information, the encoding level abstraction,
85 * i.e., Block is not very convenient.
Shock Jiang895bc1b2014-10-01 20:00:58 -070086 */
87 Response&
88 addRr(const std::string& rr);
89
90 bool
91 removeRr(const Block& rr);
92
93 bool
94 operator==(const Response& other) const;
95
96 bool
97 operator!=(const Response& other) const
98 {
99 return !(*this == other);
100 }
101
102 /**
103 * @brief encode the app-level data
104 */
105 const Block
106 wireEncode() const;
107
108 /**
109 * @brief decode the app-level data
110 */
111 void
112 wireDecode(const Block& wire);
113
114 /**
115 * @brief encode app-level data
116 */
Yumin Xia2c509c22017-02-09 14:37:36 -0800117 template<encoding::Tag T>
Shock Jiang895bc1b2014-10-01 20:00:58 -0700118 size_t
Yumin Xia2c509c22017-02-09 14:37:36 -0800119 wireEncode(EncodingImpl<T>& block) const;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700120
Yumin Xia55a7cc42017-05-14 18:43:34 -0700121 static std::pair<Name, Name>
122 wireDecodeDoe(const Block& wire);
123
Shock Jiang895bc1b2014-10-01 20:00:58 -0700124public:
125 ///////////////////////////////////////////////
126 // getter and setter
127
128 const Name&
129 getZone() const
130 {
131 return m_zone;
132 }
133
134 void
135 setZone(const Name& zone)
136 {
137 m_zone = zone;
138 }
139
140 const name::Component&
141 getQueryType() const
142 {
143 return m_queryType;
144 }
145
146 void
147 setQueryType(const name::Component& queryType)
148 {
149 m_queryType = queryType;
150 }
151
152 const Name&
153 getRrLabel() const
154 {
155 return m_rrLabel;
156 }
157
158 void
159 setRrLabel(const Name& rrLabel)
160 {
161 m_rrLabel = rrLabel;
162 }
163
164 void
165 setRrType(const name::Component& rrType)
166 {
167 m_rrType = rrType;
168 }
169
170 const name::Component&
171 getRrType() const
172 {
173 return m_rrType;
174 }
175
176 void
177 setVersion(const name::Component& version)
178 {
179 m_version = version;
180 }
181
182 const name::Component&
183 getVersion() const
184 {
185 return m_version;
186 }
187
188 void
Yumin Xiaa484ba72016-11-10 20:40:12 -0800189 setContentType(NdnsContentType contentType)
Shock Jiang895bc1b2014-10-01 20:00:58 -0700190 {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800191 m_contentType = contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700192 }
193
Yumin Xiaa484ba72016-11-10 20:40:12 -0800194 NdnsContentType
195 getContentType() const
Shock Jiang895bc1b2014-10-01 20:00:58 -0700196 {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800197 return m_contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700198 }
199
200 const Block&
201 getAppContent() const
202 {
203 return m_appContent;
204 }
205
206 void
207 setAppContent(const Block& block);
208
209 const std::vector<Block>&
210 getRrs() const
211 {
212 return m_rrs;
213 }
214
215 void
216 setRrs(const std::vector<Block>& rrs)
217 {
218 m_rrs = rrs;
219 }
220
221 time::seconds
222 getFreshnessPeriod() const
223 {
224 return m_freshnessPeriod;
225 }
226
227 void
228 setFreshnessPeriod(time::seconds freshnessPeriod)
229 {
230 m_freshnessPeriod = freshnessPeriod;
231 }
232
233private:
234 Name m_zone;
235 name::Component m_queryType;
236 Name m_rrLabel;
237 name::Component m_rrType;
238 name::Component m_version;
239
Yumin Xiaa484ba72016-11-10 20:40:12 -0800240 NdnsContentType m_contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700241 time::seconds m_freshnessPeriod;
242
243 /**
244 * @brief App content. Be valid only for NDNS-NULL Response
245 */
246 Block m_appContent;
247
248 /**
249 * @brief Content of Resource Record. Be valid only when this is not a NDNS-NULL Response
250 */
251 std::vector<Block> m_rrs;
252};
253
254std::ostream&
255operator<<(std::ostream& os, const Response& response);
256
257} // namespace ndns
258} // namespace ndn
259
260#endif // NDNS_CLIENTS_RESPONSE_HPP