blob: 7f975da9e491aad539371a9b5c86bd9e43d0fb21 [file] [log] [blame]
Shock Jiang698e6ed2014-11-09 11:22:24 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yumin Xia6343c5b2016-10-20 15:45:50 -07003 * Copyright (c) 2014-2016, Regents of the University of California.
Shock Jiang698e6ed2014-11-09 11:22:24 -08004 *
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_ITERATIVE_QUERY_CONTROLLER_HPP
21#define NDNS_CLIENTS_ITERATIVE_QUERY_CONTROLLER_HPP
22
23#include "ndns-enum.hpp"
24#include "query.hpp"
25#include "response.hpp"
26#include "query-controller.hpp"
27#include "config.hpp"
28#include "common.hpp"
Shock Jiang5d5928c2014-12-03 13:41:22 -080029#include "validator.hpp"
Shock Jiang698e6ed2014-11-09 11:22:24 -080030
31#include <ndn-cxx/common.hpp>
32#include <ndn-cxx/data.hpp>
33#include <ndn-cxx/interest.hpp>
34#include <ndn-cxx/name.hpp>
Yumin Xia4e561892016-10-21 10:48:01 -070035#include <ndn-cxx/link.hpp>
Shock Jiang698e6ed2014-11-09 11:22:24 -080036
37namespace ndn {
38namespace ndns {
39
40/**
41 * @brief controller which iteratively query a target label
42 */
43class IterativeQueryController : public QueryController
44{
45public:
46 /**
47 * @brief indicates a step in an iterative query process.
48 * Iterative Query contains multiple steps which are listed here
49 */
50 enum QueryStep {
51 QUERY_STEP_QUERY_NS = 1, ///< to query the naming server, before querying NS & waiting for Data
52 QUERY_STEP_QUERY_RR, ///< to query RR, before querying RR & waiting for it Data
53 QUERY_STEP_ANSWER_STUB, ///< to answer to stub resolver, after getting final Response,
54 ///< or NACK or timeout
55 QUERY_STEP_ABORT, ///< to abort the resolver process, if unexpected behavior happens
56 QUERY_STEP_UNKNOWN = 255
57 };
58
59public:
60 explicit
61 IterativeQueryController(const Name& dstLabel, const name::Component& rrType,
62 const time::milliseconds& interestLifetime,
63 const QuerySucceedCallback& onSucceed, const QueryFailCallback& onFail,
Shock Jiang5d5928c2014-12-03 13:41:22 -080064 Face& face, Validator* validator = nullptr);
Shock Jiang698e6ed2014-11-09 11:22:24 -080065
66 virtual void
67 start();
68
69 /**
70 * @brief return false if the current status is not QUEYR_STEP_QUERY_NS
71 * or QUERY_STEP_QUERY_RR
72 */
73 virtual bool
74 hasEnded();
75
76NDNS_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
77 void
78 onData(const ndn::Interest& interest, const Data& data);
79
Shock Jiang5d5928c2014-12-03 13:41:22 -080080 void
Yumin Xiaa484ba72016-11-10 20:40:12 -080081 onDataValidated(const shared_ptr<const Data>& data, NdnsContentType contentType);
Shock Jiang5d5928c2014-12-03 13:41:22 -080082
Shock Jiang698e6ed2014-11-09 11:22:24 -080083 /**
84 * @brief change the Controller state according to timeout. For current,
85 * abort the query when timeout
86 */
87 void
88 onTimeout(const Interest& interest);
89
90 void
91 abort();
92
93 /**
94 * @brief get the Interest according to current Controller state.
95 * Only be valid on State QueryNS & QueryRR, or throw exception
96 */
97 const Interest
98 makeLatestInterest();
99
100 const Response
101 parseFinalResponse(const Data& data);
102
103 void
104 express(const Interest& interest);
105
Shock Jiang5d5928c2014-12-03 13:41:22 -0800106public:
Shock Jiang698e6ed2014-11-09 11:22:24 -0800107 void
Shock Jiang5d5928c2014-12-03 13:41:22 -0800108 setStartComponentIndex(size_t finished)
Shock Jiang698e6ed2014-11-09 11:22:24 -0800109 {
110 m_nFinishedComps = finished;
111 }
112
Shock Jiang698e6ed2014-11-09 11:22:24 -0800113 QueryStep
114 getStep() const
115 {
116 return m_step;
117 }
118
119 size_t
120 getNFinishedComps() const
121 {
122 return m_nFinishedComps;
123 }
124
125 size_t
126 getNTryComps() const
127 {
128 return m_nTryComps;
129 }
130
131protected:
Shock Jiang5d5928c2014-12-03 13:41:22 -0800132 Validator* m_validator;
Shock Jiang698e6ed2014-11-09 11:22:24 -0800133 /**
134 * @brief current query step
135 */
136 QueryStep m_step;
137
138 /*
139 * the number of label that has been resolved successfully
140 * This also define the next AuthZone prefix
141 * AuthZone = m_query.getRrLabel().getPrefix(m_nFinishedComps)
142 */
143 size_t m_nFinishedComps;
144
145 /*
146 * used when query the KSK (key signing key), e.g., /net/ndnsim/ksk-1
147 */
148 size_t m_nTryComps;
Yumin Xia4e561892016-10-21 10:48:01 -0700149
150private:
151 Block m_lastLink;
Shock Jiang698e6ed2014-11-09 11:22:24 -0800152};
153
154std::ostream&
155operator<<(std::ostream& os, const IterativeQueryController& iq);
156
157std::ostream&
158operator<<(std::ostream& os, const IterativeQueryController::QueryStep step);
159
160} // namespace ndns
161} // namespace ndn
162
163#endif // NDNS_CLIENTS_ITERATIVE_QUERY_HPP