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