blob: 62e88ed647073b97dd1fa234c6fe72bda11698fc [file] [log] [blame]
Shock Jiang895bc1b2014-10-01 20:00:58 -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_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 *
60 * @param hint Forwarding hint
61 * @param zone NDNS zone name
62 * @param data Data.getName() must the same hint (if has) and zone as its prefix,
63 * otherwise it's undefined behavior
Shock Jiang895bc1b2014-10-01 20:00:58 -070064 * @return false if Data.getName() does not follow the structure of NDNS Response without
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080065 * changing any attributes, otherwise return true and fill the attributes
Shock Jiang895bc1b2014-10-01 20:00:58 -070066 */
67 bool
68 fromData(const Name& hint, const Name& zone, const Data& data);
69
70 shared_ptr<Data>
71 toData();
72
73 Response&
74 addRr(const Block& rr);
75
76 /**
77 * @brief add Block which contains string information and its tlv type is ndns::tlv::RrData
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080078 *
79 * @return Response that is service level information, the encoding level abstraction,
80 * i.e., Block is not very convenient.
Shock Jiang895bc1b2014-10-01 20:00:58 -070081 */
82 Response&
83 addRr(const std::string& rr);
84
85 bool
86 removeRr(const Block& rr);
87
88 bool
89 operator==(const Response& other) const;
90
91 bool
92 operator!=(const Response& other) const
93 {
94 return !(*this == other);
95 }
96
97 /**
98 * @brief encode the app-level data
99 */
100 const Block
101 wireEncode() const;
102
103 /**
104 * @brief decode the app-level data
105 */
106 void
107 wireDecode(const Block& wire);
108
109 /**
110 * @brief encode app-level data
111 */
112 template<bool T>
113 size_t
114 wireEncode(EncodingImpl<T> & block) const;
115
116public:
117 ///////////////////////////////////////////////
118 // getter and setter
119
120 const Name&
121 getZone() const
122 {
123 return m_zone;
124 }
125
126 void
127 setZone(const Name& zone)
128 {
129 m_zone = zone;
130 }
131
132 const name::Component&
133 getQueryType() const
134 {
135 return m_queryType;
136 }
137
138 void
139 setQueryType(const name::Component& queryType)
140 {
141 m_queryType = queryType;
142 }
143
144 const Name&
145 getRrLabel() const
146 {
147 return m_rrLabel;
148 }
149
150 void
151 setRrLabel(const Name& rrLabel)
152 {
153 m_rrLabel = rrLabel;
154 }
155
156 void
157 setRrType(const name::Component& rrType)
158 {
159 m_rrType = rrType;
160 }
161
162 const name::Component&
163 getRrType() const
164 {
165 return m_rrType;
166 }
167
168 void
169 setVersion(const name::Component& version)
170 {
171 m_version = version;
172 }
173
174 const name::Component&
175 getVersion() const
176 {
177 return m_version;
178 }
179
180 void
181 setNdnsType(NdnsType ndnsType)
182 {
183 m_ndnsType = ndnsType;
184 }
185
186 const NdnsType
187 getNdnsType() const
188 {
189 return m_ndnsType;
190 }
191
192 const Block&
193 getAppContent() const
194 {
195 return m_appContent;
196 }
197
198 void
199 setAppContent(const Block& block);
200
201 const std::vector<Block>&
202 getRrs() const
203 {
204 return m_rrs;
205 }
206
207 void
208 setRrs(const std::vector<Block>& rrs)
209 {
210 m_rrs = rrs;
211 }
212
213 time::seconds
214 getFreshnessPeriod() const
215 {
216 return m_freshnessPeriod;
217 }
218
219 void
220 setFreshnessPeriod(time::seconds freshnessPeriod)
221 {
222 m_freshnessPeriod = freshnessPeriod;
223 }
224
225private:
226 Name m_zone;
227 name::Component m_queryType;
228 Name m_rrLabel;
229 name::Component m_rrType;
230 name::Component m_version;
231
232 NdnsType m_ndnsType;
233 time::seconds m_freshnessPeriod;
234
235 /**
236 * @brief App content. Be valid only for NDNS-NULL Response
237 */
238 Block m_appContent;
239
240 /**
241 * @brief Content of Resource Record. Be valid only when this is not a NDNS-NULL Response
242 */
243 std::vector<Block> m_rrs;
244};
245
246std::ostream&
247operator<<(std::ostream& os, const Response& response);
248
249} // namespace ndns
250} // namespace ndn
251
252#endif // NDNS_CLIENTS_RESPONSE_HPP