blob: c5e510462cb22b942332d343791faf8d1c9b9a69 [file] [log] [blame]
Shock Jiang698e6ed2014-11-09 11:22:24 -08001/* -*- 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#include "iterative-query-controller.hpp"
21#include "logger.hpp"
22#include <iostream>
23
24namespace ndn {
25namespace ndns {
26NDNS_LOG_INIT("IterQueryCtr")
27
28IterativeQueryController::IterativeQueryController(const Name& dstLabel,
29 const name::Component& rrType,
30 const time::milliseconds& interestLifetime,
31 const QuerySucceedCallback& onSucceed,
32 const QueryFailCallback& onFail,
33 Face& face)
34 : QueryController(dstLabel, rrType, interestLifetime, onSucceed, onFail, face)
35 , m_step(QUERY_STEP_QUERY_NS)
36 , m_nFinishedComps(0)
37 , m_nTryComps(1)
38{
39 if (m_dstLabel.size() == 1) // the first one is to Query RR directly
40 m_step = QUERY_STEP_QUERY_RR;
41}
42
43void
44IterativeQueryController::onTimeout(const Interest& interest)
45{
46 NDNS_LOG_INFO("[* !! *] timeout happens: " << interest.getName());
47 NDNS_LOG_TRACE(*this);
48 this->abort();
49}
50
51void
52IterativeQueryController::abort()
53{
54 NDNS_LOG_DEBUG("abort iterative query");
55 if (m_onFail != nullptr)
56 m_onFail(0, "abort");
57 else
58 NDNS_LOG_TRACE("m_onFail is 0");
59
60}
61
62void
63IterativeQueryController::onData(const ndn::Interest& interest, const Data& data)
64{
65 NdnsType ndnsType = NDNS_RAW;
66 const Block* block = data.getMetaInfo().findAppMetaInfo(ndns::tlv::NdnsType);
67 if (block != nullptr) {
68 ndnsType = static_cast<NdnsType>(readNonNegativeInteger(*block));
69 }
70
71 NDNS_LOG_TRACE("[* -> *] get a " << ndnsType
72 << " Response: " << data.getName());
73
74 switch (m_step) {
75 case QUERY_STEP_QUERY_NS:
76 if (ndnsType == NDNS_NACK) {
Shock Jiang06cd2142014-11-23 17:36:02 -080077 m_step = QUERY_STEP_QUERY_RR;
Shock Jiang698e6ed2014-11-09 11:22:24 -080078 }
79 else if (ndnsType == NDNS_RESP) {
80 if (m_nFinishedComps + m_nTryComps == m_dstLabel.size() && m_rrType == label::NS_RR_TYPE) {
81 // NS_RR_TYPE is different, since its record is stored at higher level
82 m_step = QUERY_STEP_ANSWER_STUB;
83 }
84 else {
85 m_nFinishedComps += m_nTryComps;
86 m_nTryComps = 1;
87 }
88 }
89 else if (ndnsType == NDNS_AUTH) {
90 m_nTryComps += 1;
91 }
92 else if (ndnsType == NDNS_RAW) {
93 std::ostringstream oss;
94 oss << *this;
95 NDNS_LOG_WARN("get unexpected Response: NDNS_RAW for QUERY_NS: " << oss.str());
96 }
97 //
98 if (m_nFinishedComps + m_nTryComps > m_dstLabel.size()) {
99 if (m_rrType == label::NS_RR_TYPE) {
100 m_step = QUERY_STEP_ANSWER_STUB;
101 }
102 else
103 m_step = QUERY_STEP_QUERY_RR;
104 }
105 break;
106 case QUERY_STEP_QUERY_RR:
107 m_step = QUERY_STEP_ANSWER_STUB;
108 break;
109 default:
110 NDNS_LOG_WARN("get unexpected Response at State " << *this);
111 // throw std::runtime_error("call makeLatestInterest() unexpected: " << *this);
112 // do not throw except since it may be duplicated Data
113 m_step = QUERY_STEP_ABORT;
114 break;
115 }
116
117 if (!hasEnded())
118 this->express(this->makeLatestInterest()); // express new Expres
119 else if (m_step == QUERY_STEP_ANSWER_STUB) {
120 NDNS_LOG_TRACE("query ends: " << *this);
121 Response re = this->parseFinalResponse(data);
122 if (m_onSucceed != nullptr)
123 m_onSucceed(data, re);
124 else
125 NDNS_LOG_TRACE("succeed callback is nullptr");
126 }
127 else if (m_step == QUERY_STEP_ABORT)
128 this->abort();
129}
130
131bool
132IterativeQueryController::hasEnded()
133{
134 return (m_step != QUERY_STEP_QUERY_NS && m_step != QUERY_STEP_QUERY_RR);
135}
136
137void
138IterativeQueryController::start()
139{
140 Interest interest = this->makeLatestInterest();
141 express(interest);
142}
143
144
145void
146IterativeQueryController::express(const Interest& interest)
147{
148 NDNS_LOG_DEBUG("[* <- *] send a Query: " << interest.getName());
149 m_face.expressInterest(interest,
150 bind(&IterativeQueryController::onData, this, _1, _2),
151 bind(&IterativeQueryController::onTimeout, this, _1)
152 );
153}
154
155
156const Response
157IterativeQueryController::parseFinalResponse(const Data& data)
158{
159 Response re;
160 Name zone = m_dstLabel.getPrefix(m_nFinishedComps);
161 re.fromData("", zone, data);
162 return re;
163}
164
165const Interest
166IterativeQueryController::makeLatestInterest()
167{
168 // NDNS_LOG_TRACE("get latest Interest");
169 Query query;
170 //const Name& dstLabel = m_query.getRrLabel();
171
172 query.setZone(m_dstLabel.getPrefix(m_nFinishedComps));
173 query.setInterestLifetime(m_interestLifetime);
174 switch (m_step) {
175 case QUERY_STEP_QUERY_NS:
176 query.setQueryType(label::NDNS_ITERATIVE_QUERY);
177 query.setRrLabel(m_dstLabel.getSubName(m_nFinishedComps, m_nTryComps));
178 query.setRrType(label::NS_RR_TYPE);
179 break;
180 case QUERY_STEP_QUERY_RR:
181 if (m_rrType == label::CERT_RR_TYPE) {
182 // this only works for dsk, and ksk needs different mechanism
183 query.setQueryType(label::NDNS_CERT_QUERY);
184 }
185 else {
186 query.setQueryType(label::NDNS_ITERATIVE_QUERY);
187 }
188 query.setRrLabel(m_dstLabel.getSubName(m_nFinishedComps));
189 query.setRrType(m_rrType);
190 break;
191 default:
192 std::ostringstream oss;
193 oss << *this;
194 NDNS_LOG_WARN("unexpected state: " << oss.str());
195 throw std::runtime_error("call makeLatestInterest() unexpected: " + oss.str());
196 }
197
198
199 Interest interest = query.toInterest();
200 return interest;
201}
202
203std::ostream&
204operator<<(std::ostream& os, const IterativeQueryController::QueryStep step)
205{
206 switch (step) {
207 case IterativeQueryController::QUERY_STEP_QUERY_NS:
208 os << "QueryNS";
209 break;
210 case IterativeQueryController::QUERY_STEP_QUERY_RR:
211 os << "QueryRR";
212 break;
213 case IterativeQueryController::QUERY_STEP_ANSWER_STUB:
214 os << "AnswerStub";
215 break;
216 case IterativeQueryController::QUERY_STEP_ABORT:
217 os << "Abort";
218 break;
219 default:
220 os << "UNKNOW";
221 break;
222 }
223 return os;
224}
225
226std::ostream&
227operator<<(std::ostream& os, const IterativeQueryController& ctr)
228{
229 os << "InterativeQueryController: dstLabel=" << ctr.getDstLabel()
230 << " rrType=" << ctr.getRrType()
231 << " currentStep=" << ctr.getStep()
232 << " nFinishedComps=" << ctr.getNFinishedComps()
233 << " nTryComp=" << ctr.getNTryComps()
234 ;
235
236 return os;
237}
238
239} // namespace ndns
240} // namespace ndn