blob: a33e42a36cb78ccec55d6ffda315b723b2a81e98 [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/*
3 * Copyright (c) 2014-2017, 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 */
43const time::seconds DEFAULT_RR_FRESHNESS_PERIOD(3600);
44
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:
53 Response();
54
55 Response(const Name& zone, const name::Component& queryType);
56
57 /**
58 * @brief fill the attributes from Data packet.
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080059 *
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080060 * @param zone NDNS zone name
Yumin Xia6343c5b2016-10-20 15:45:50 -070061 * @param data Data.getName() must the same zone as its prefix,
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080062 * otherwise it's undefined behavior
Shock Jiang895bc1b2014-10-01 20:00:58 -070063 * @return false if Data.getName() does not follow the structure of NDNS Response without
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080064 * changing any attributes, otherwise return true and fill the attributes
Shock Jiang895bc1b2014-10-01 20:00:58 -070065 */
66 bool
Yumin Xia6343c5b2016-10-20 15:45:50 -070067 fromData(const Name& zone, const Data& data);
Shock Jiang895bc1b2014-10-01 20:00:58 -070068
69 shared_ptr<Data>
70 toData();
71
72 Response&
73 addRr(const Block& rr);
74
75 /**
76 * @brief add Block which contains string information and its tlv type is ndns::tlv::RrData
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080077 *
78 * @return Response that is service level information, the encoding level abstraction,
79 * i.e., Block is not very convenient.
Shock Jiang895bc1b2014-10-01 20:00:58 -070080 */
81 Response&
82 addRr(const std::string& rr);
83
84 bool
85 removeRr(const Block& rr);
86
87 bool
88 operator==(const Response& other) const;
89
90 bool
91 operator!=(const Response& other) const
92 {
93 return !(*this == other);
94 }
95
96 /**
97 * @brief encode the app-level data
98 */
99 const Block
100 wireEncode() const;
101
102 /**
103 * @brief decode the app-level data
104 */
105 void
106 wireDecode(const Block& wire);
107
108 /**
109 * @brief encode app-level data
110 */
Yumin Xia2c509c22017-02-09 14:37:36 -0800111 template<encoding::Tag T>
Shock Jiang895bc1b2014-10-01 20:00:58 -0700112 size_t
Yumin Xia2c509c22017-02-09 14:37:36 -0800113 wireEncode(EncodingImpl<T>& block) const;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700114
115public:
116 ///////////////////////////////////////////////
117 // getter and setter
118
119 const Name&
120 getZone() const
121 {
122 return m_zone;
123 }
124
125 void
126 setZone(const Name& zone)
127 {
128 m_zone = zone;
129 }
130
131 const name::Component&
132 getQueryType() const
133 {
134 return m_queryType;
135 }
136
137 void
138 setQueryType(const name::Component& queryType)
139 {
140 m_queryType = queryType;
141 }
142
143 const Name&
144 getRrLabel() const
145 {
146 return m_rrLabel;
147 }
148
149 void
150 setRrLabel(const Name& rrLabel)
151 {
152 m_rrLabel = rrLabel;
153 }
154
155 void
156 setRrType(const name::Component& rrType)
157 {
158 m_rrType = rrType;
159 }
160
161 const name::Component&
162 getRrType() const
163 {
164 return m_rrType;
165 }
166
167 void
168 setVersion(const name::Component& version)
169 {
170 m_version = version;
171 }
172
173 const name::Component&
174 getVersion() const
175 {
176 return m_version;
177 }
178
179 void
Yumin Xiaa484ba72016-11-10 20:40:12 -0800180 setContentType(NdnsContentType contentType)
Shock Jiang895bc1b2014-10-01 20:00:58 -0700181 {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800182 m_contentType = contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700183 }
184
Yumin Xiaa484ba72016-11-10 20:40:12 -0800185 NdnsContentType
186 getContentType() const
Shock Jiang895bc1b2014-10-01 20:00:58 -0700187 {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800188 return m_contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700189 }
190
191 const Block&
192 getAppContent() const
193 {
194 return m_appContent;
195 }
196
197 void
198 setAppContent(const Block& block);
199
200 const std::vector<Block>&
201 getRrs() const
202 {
203 return m_rrs;
204 }
205
206 void
207 setRrs(const std::vector<Block>& rrs)
208 {
209 m_rrs = rrs;
210 }
211
212 time::seconds
213 getFreshnessPeriod() const
214 {
215 return m_freshnessPeriod;
216 }
217
218 void
219 setFreshnessPeriod(time::seconds freshnessPeriod)
220 {
221 m_freshnessPeriod = freshnessPeriod;
222 }
223
224private:
225 Name m_zone;
226 name::Component m_queryType;
227 Name m_rrLabel;
228 name::Component m_rrType;
229 name::Component m_version;
230
Yumin Xiaa484ba72016-11-10 20:40:12 -0800231 NdnsContentType m_contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700232 time::seconds m_freshnessPeriod;
233
234 /**
235 * @brief App content. Be valid only for NDNS-NULL Response
236 */
237 Block m_appContent;
238
239 /**
240 * @brief Content of Resource Record. Be valid only when this is not a NDNS-NULL Response
241 */
242 std::vector<Block> m_rrs;
243};
244
245std::ostream&
246operator<<(std::ostream& os, const Response& response);
247
248} // namespace ndns
249} // namespace ndn
250
251#endif // NDNS_CLIENTS_RESPONSE_HPP