blob: 3f0ab9808818064fc1f461b377f037999ade1d95 [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
29#include "query.hpp"
30#include "response.hpp"
31//#include "rr.hpp"
32
33namespace ndn {
34namespace ndns {
35
36class IterativeQuery
37{
38
39public:
40 enum QuerySteps{
41 NSQuery,
42 NackNS,
43 RRQuery,
44 NackRR,
45 AnswerStub,
46 Abort,
47 FinishedSuccessfully
48 };
49
50
51 static std::string
52 toString(const QuerySteps& step)
53 {
54 std::string label;
55 switch(step)
56 {
57 case NSQuery:
58 label = "NSQuery";
59 break;
60 case NackNS:
61 label = "NackNS";
62 break;
63 case RRQuery:
64 label = "RRQuery";
65 break;
66 case NackRR:
67 label = "NackRR";
68 break;
69 case AnswerStub:
70 label = "AnswerStub";
71 break;
72 case Abort:
73 label = "Abort";
74 break;
75 default:
76 label = "UNKNOW";
77 }
78
79 return label;
80 }
81
82public:
83 IterativeQuery(const Query& );
84
85 void
86 doData(Data& data);
87
88 bool
89 doTimeout();
90
91
92 void
93 abort();
94
95
96 const Interest
97 toLatestInterest();
98
99
100 bool hasFinished() {
101 return m_query.getRrLabel().size() == m_finishedLabelNum;
102 }
103
104 void addTryNum() {
105 m_tryNum += 1;
106 }
107
108 ssize_t getFinishedLabelNum() const {
109 return m_finishedLabelNum;
110 }
111
112 void setFinishedLabelNum(ssize_t finishedLabelNum) {
113 m_finishedLabelNum = finishedLabelNum;
114 }
115
116 const Query& getQuery() const {
117 return m_query;
118 }
119
120 const Response& getLastResponse() const {
121 return m_lastResponse;
122 }
123
124 void setLastResponse(const Response& response) {
125 m_lastResponse = response;
126 }
127
128 unsigned short getTryMax() const {
129 return m_tryMax;
130 }
131
132 void setTryMax(unsigned short tryMax) {
133 m_tryMax = tryMax;
134 }
135
136 unsigned short getTryNum() const {
137 return m_tryNum;
138 }
139
140 void setTryNum(unsigned short tryNum) {
141 m_tryNum = tryNum;
142 }
143
144 const Interest& getLastInterest() const {
145 return m_lastInterest;
146 }
147
148 void setLastInterest(const Interest& lastInterest) {
149 m_lastInterest = lastInterest;
150
151 }
152
153 QuerySteps getStep() const {
154 return m_step;
155 }
156
157 void setStep(QuerySteps step) {
158 m_step = step;
159 }
160
161 ssize_t getRrLabelLen() const {
162 return m_rrLabelLen;
163 }
164
165 void setRrLabelLen(ssize_t rrLabelLen) {
166 m_rrLabelLen = rrLabelLen;
167 }
168
169 unsigned int getAuthZoneIndex() const {
170 return m_authZoneIndex;
171 }
172
173 void setRrAsNextAuthorityZoneToTry(unsigned int authZoneIndex) {
174 m_authZoneIndex = authZoneIndex;
175 }
176
177private:
178 QuerySteps m_step;
179
180 unsigned short m_tryNum;
181 unsigned short m_tryMax;
182 //Name m_dstLabel;
183
184
185 /*
186 * the original query, not the intermediate query
187 */
188 const Query m_query;
189 /*
190 * the number of label that has been resolved successfully
191 * This also define the next AuthZone prefix
192 * AuthZone = m_query.getRrLabel().getPrefix(m_finishedLabelNum)
193 */
194 ssize_t m_finishedLabelNum;
195
196 /*
197 * the number used to generated the label
198 * query.getRrLabel().getSubName(m_finishedLabelNum, m_labelLength);
199 */
200 ssize_t m_rrLabelLen;
201
202
203 /*
204 * m_lastResponse is changed once a new expecting Data is understood:
205 * response.from(data)
206 */
207 Response m_lastResponse;
208
209 /*
210 * next auth zone should be m_lastResponse.getRrs()[m_authZoneIndex]
211 */
212 unsigned int m_authZoneIndex;
213
214
215
216
217 /*
218 * last interest that has sent
219 */
220 Interest m_lastInterest;
221};
222
223inline std::ostream&
224operator<<(std::ostream& os, const IterativeQuery iq)
225{
226 os<<"InterativeQuery: dstLabel="<<iq.getQuery().getRrLabel().toUri()
227 <<" currentStep="<<IterativeQuery::toString( iq.getStep())
228 <<" finishedLabel="<<iq.getFinishedLabelNum()
229 <<" rrLabelen="<<iq.getRrLabelLen()
230 <<" NextAuZoneIndex="<<iq.getAuthZoneIndex()
231 << " [OriginalQuery: " <<iq.getQuery() << "]"
232 <<" [LastReponse: "<<iq.getLastResponse()<<"]"
233 <<" [LastInterest: " <<iq.getLastInterest()<<"]";
234
235 return os;
236}
237
238} /* namespace ndns */
239} /* namespace ndn */
240
241#endif /* INTERATIVE_QUERY_HPP_ */