blob: aff9e43912067c06e130febfb04eaf80bd15d6c8 [file] [log] [blame]
shockjiang85312022014-07-27 23:22:16 -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#ifndef INTERATIVE_QUERY_HPP_
20#define INTERATIVE_QUERY_HPP_
21
22#include <ndn-cxx/data.hpp>
23#include <ndn-cxx/interest.hpp>
24#include <ndn-cxx/name.hpp>
25#include <sys/_types/_ssize_t.h>
26#include <cstdbool>
27#include <iostream>
28
shockjiang99ad3892014-08-03 14:56:13 -070029#include <boost/algorithm/string/predicate.hpp>
30#include <boost/lexical_cast.hpp>
31
shockjiang85312022014-07-27 23:22:16 -070032#include "query.hpp"
33#include "response.hpp"
34//#include "rr.hpp"
35
36namespace ndn {
37namespace ndns {
38
39class IterativeQuery
40{
41
42public:
shockjiang99ad3892014-08-03 14:56:13 -070043 enum QuerySteps
44 {
45 NSQuery=1,
shockjiang85312022014-07-27 23:22:16 -070046 NackNS,
47 RRQuery,
48 NackRR,
shockjiang99ad3892014-08-03 14:56:13 -070049 FHQuery,
shockjiang85312022014-07-27 23:22:16 -070050 AnswerStub,
51 Abort,
shockjiang99ad3892014-08-03 14:56:13 -070052 FinishedSuccessfully,
53 UNKNOWN
shockjiang85312022014-07-27 23:22:16 -070054 };
55
shockjiang85312022014-07-27 23:22:16 -070056 static std::string
57 toString(const QuerySteps& step)
58 {
59 std::string label;
shockjiang99ad3892014-08-03 14:56:13 -070060 switch (step) {
shockjiang85312022014-07-27 23:22:16 -070061 case NSQuery:
62 label = "NSQuery";
63 break;
64 case NackNS:
65 label = "NackNS";
66 break;
67 case RRQuery:
68 label = "RRQuery";
69 break;
70 case NackRR:
71 label = "NackRR";
72 break;
shockjiang99ad3892014-08-03 14:56:13 -070073 case FHQuery:
74 label = "FHQuery";
75 break;
shockjiang85312022014-07-27 23:22:16 -070076 case AnswerStub:
77 label = "AnswerStub";
78 break;
79 case Abort:
80 label = "Abort";
81 break;
shockjiang99ad3892014-08-03 14:56:13 -070082 case FinishedSuccessfully:
83 label = "FinishedSuccessfully";
84 break;
shockjiang85312022014-07-27 23:22:16 -070085 default:
86 label = "UNKNOW";
87 }
88
89 return label;
90 }
91
shockjiang99ad3892014-08-03 14:56:13 -070092 static QuerySteps
93 toQuerySteps(std::string str)
94 {
95 QuerySteps step = UNKNOWN;
96 if (str == "NSQuery") {
97 step = NSQuery;
98 } else if (str == "NackNS") {
99 step = NackNS;
100 } else if (str == "RRQuery") {
101 step = RRQuery;
102 } else if (str == "NackRR") {
103 step = NackRR;
104 } else if (str == "FHQuery")
105 step = FHQuery;
106 else if (str == "AnswerStub")
107 step = AnswerStub;
108 else if (str == "Abort")
109 step = Abort;
110 else if (str == "FinishedSuccessfully")
111 step = FinishedSuccessfully;
112 else
113 step = UNKNOWN;
shockjiang85312022014-07-27 23:22:16 -0700114
shockjiang99ad3892014-08-03 14:56:13 -0700115 return step;
116 }
117
118
119public:
120 IterativeQuery(const Query&);
121
122 virtual void
shockjiang85312022014-07-27 23:22:16 -0700123 doData(Data& data);
124
shockjiang99ad3892014-08-03 14:56:13 -0700125 virtual bool
shockjiang85312022014-07-27 23:22:16 -0700126 doTimeout();
127
shockjiang99ad3892014-08-03 14:56:13 -0700128 void abort();
shockjiang85312022014-07-27 23:22:16 -0700129
shockjiang99ad3892014-08-03 14:56:13 -0700130 virtual const Interest
shockjiang85312022014-07-27 23:22:16 -0700131 toLatestInterest();
132
shockjiang99ad3892014-08-03 14:56:13 -0700133 bool hasFinished()
134 {
shockjiang85312022014-07-27 23:22:16 -0700135 return m_query.getRrLabel().size() == m_finishedLabelNum;
136 }
137
shockjiang99ad3892014-08-03 14:56:13 -0700138 void addTryNum()
139 {
shockjiang85312022014-07-27 23:22:16 -0700140 m_tryNum += 1;
141 }
142
shockjiang99ad3892014-08-03 14:56:13 -0700143 ssize_t getFinishedLabelNum() const
144 {
shockjiang85312022014-07-27 23:22:16 -0700145 return m_finishedLabelNum;
146 }
147
shockjiang99ad3892014-08-03 14:56:13 -0700148 void setFinishedLabelNum(ssize_t finishedLabelNum)
149 {
shockjiang85312022014-07-27 23:22:16 -0700150 m_finishedLabelNum = finishedLabelNum;
151 }
152
shockjiang99ad3892014-08-03 14:56:13 -0700153 const Query& getQuery() const
154 {
shockjiang85312022014-07-27 23:22:16 -0700155 return m_query;
156 }
157
shockjiang99ad3892014-08-03 14:56:13 -0700158 const Response& getLastResponse() const
159 {
shockjiang85312022014-07-27 23:22:16 -0700160 return m_lastResponse;
161 }
162
shockjiang99ad3892014-08-03 14:56:13 -0700163 void setLastResponse(const Response& response)
164 {
shockjiang85312022014-07-27 23:22:16 -0700165 m_lastResponse = response;
166 }
167
shockjiang99ad3892014-08-03 14:56:13 -0700168 unsigned short getTryMax() const
169 {
shockjiang85312022014-07-27 23:22:16 -0700170 return m_tryMax;
171 }
172
shockjiang99ad3892014-08-03 14:56:13 -0700173 void setTryMax(unsigned short tryMax)
174 {
shockjiang85312022014-07-27 23:22:16 -0700175 m_tryMax = tryMax;
176 }
177
shockjiang99ad3892014-08-03 14:56:13 -0700178 unsigned short getTryNum() const
179 {
shockjiang85312022014-07-27 23:22:16 -0700180 return m_tryNum;
181 }
182
shockjiang99ad3892014-08-03 14:56:13 -0700183 void setTryNum(unsigned short tryNum)
184 {
shockjiang85312022014-07-27 23:22:16 -0700185 m_tryNum = tryNum;
186 }
187
shockjiang99ad3892014-08-03 14:56:13 -0700188 const Interest& getLastInterest() const
189 {
shockjiang85312022014-07-27 23:22:16 -0700190 return m_lastInterest;
191 }
192
shockjiang99ad3892014-08-03 14:56:13 -0700193 void setLastInterest(const Interest& lastInterest)
194 {
shockjiang85312022014-07-27 23:22:16 -0700195 m_lastInterest = lastInterest;
196
197 }
198
shockjiang99ad3892014-08-03 14:56:13 -0700199 QuerySteps getStep() const
200 {
shockjiang85312022014-07-27 23:22:16 -0700201 return m_step;
202 }
shockjiang99ad3892014-08-03 14:56:13 -0700203/*
204 void setStep(QuerySteps step)
205 {
shockjiang85312022014-07-27 23:22:16 -0700206 m_step = step;
207 }
shockjiang99ad3892014-08-03 14:56:13 -0700208*/
209 ssize_t getRrLabelLen() const
210 {
shockjiang85312022014-07-27 23:22:16 -0700211 return m_rrLabelLen;
212 }
213
shockjiang99ad3892014-08-03 14:56:13 -0700214 void setRrLabelLen(ssize_t rrLabelLen)
215 {
shockjiang85312022014-07-27 23:22:16 -0700216 m_rrLabelLen = rrLabelLen;
217 }
218
shockjiang99ad3892014-08-03 14:56:13 -0700219 unsigned int getAuthZoneIndex() const
220 {
shockjiang85312022014-07-27 23:22:16 -0700221 return m_authZoneIndex;
222 }
223
shockjiang99ad3892014-08-03 14:56:13 -0700224 void setRrAsNextAuthorityZoneToTry(unsigned int authZoneIndex)
225 {
shockjiang85312022014-07-27 23:22:16 -0700226 m_authZoneIndex = authZoneIndex;
227 }
228
shockjiang99ad3892014-08-03 14:56:13 -0700229 /**
230 * The function is neccesarry since the template of class CachingResolver call setForwardingHint
231
232 void setForwardingHint(const Name& lastForwardingHint)
233 {
234 }
235*/
236protected:
shockjiang85312022014-07-27 23:22:16 -0700237 QuerySteps m_step;
238
239 unsigned short m_tryNum;
240 unsigned short m_tryMax;
241 //Name m_dstLabel;
242
shockjiang85312022014-07-27 23:22:16 -0700243 /*
244 * the original query, not the intermediate query
245 */
246 const Query m_query;
247 /*
248 * the number of label that has been resolved successfully
249 * This also define the next AuthZone prefix
250 * AuthZone = m_query.getRrLabel().getPrefix(m_finishedLabelNum)
251 */
252 ssize_t m_finishedLabelNum;
253
shockjiang99ad3892014-08-03 14:56:13 -0700254
255 /*
256 * used when query the KSK (key signing key), e.g., /net/ndnsim/ksk-1
257 */
258 ssize_t m_lastFinishedLabelNum;
259
260
shockjiang85312022014-07-27 23:22:16 -0700261 /*
262 * the number used to generated the label
263 * query.getRrLabel().getSubName(m_finishedLabelNum, m_labelLength);
264 */
265 ssize_t m_rrLabelLen;
266
shockjiang85312022014-07-27 23:22:16 -0700267 /*
268 * m_lastResponse is changed once a new expecting Data is understood:
269 * response.from(data)
270 */
271 Response m_lastResponse;
272
273 /*
274 * next auth zone should be m_lastResponse.getRrs()[m_authZoneIndex]
275 */
276 unsigned int m_authZoneIndex;
277
shockjiang85312022014-07-27 23:22:16 -0700278 /*
279 * last interest that has sent
280 */
281 Interest m_lastInterest;
shockjiang99ad3892014-08-03 14:56:13 -0700282
283 //friend class IterativeQueryWithForwardingHint;
shockjiang85312022014-07-27 23:22:16 -0700284};
285
286inline std::ostream&
287operator<<(std::ostream& os, const IterativeQuery iq)
288{
shockjiang99ad3892014-08-03 14:56:13 -0700289 os << "InterativeQuery: dstLabel=" << iq.getQuery().getRrLabel().toUri()
290 << " currentStep=" << IterativeQuery::toString(iq.getStep())
291 << " finishedLabel=" << iq.getFinishedLabelNum() << " rrLabelen="
292 << iq.getRrLabelLen() << " NextAuZoneIndex=" << iq.getAuthZoneIndex()
293 << " [OriginalQuery: " << iq.getQuery() << "]" << " [LastReponse: "
294 << iq.getLastResponse() << "]" << " [LastInterest: "
295 << iq.getLastInterest() << "]";
shockjiang85312022014-07-27 23:22:16 -0700296
297 return os;
298}
299
300} /* namespace ndns */
301} /* namespace ndn */
302
303#endif /* INTERATIVE_QUERY_HPP_ */