blob: 714a504b9bbff6ebd7f431a43d2e7a0fad4a8a69 [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#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>
35
36namespace ndn {
37namespace ndns {
38
39/**
40 * @brief controller which iteratively query a target label
41 */
42class IterativeQueryController : public QueryController
43{
44public:
45 /**
46 * @brief indicates a step in an iterative query process.
47 * Iterative Query contains multiple steps which are listed here
48 */
49 enum QueryStep {
50 QUERY_STEP_QUERY_NS = 1, ///< to query the naming server, before querying NS & waiting for Data
51 QUERY_STEP_QUERY_RR, ///< to query RR, before querying RR & waiting for it Data
52 QUERY_STEP_ANSWER_STUB, ///< to answer to stub resolver, after getting final Response,
53 ///< or NACK or timeout
54 QUERY_STEP_ABORT, ///< to abort the resolver process, if unexpected behavior happens
55 QUERY_STEP_UNKNOWN = 255
56 };
57
58public:
59 explicit
60 IterativeQueryController(const Name& dstLabel, const name::Component& rrType,
61 const time::milliseconds& interestLifetime,
62 const QuerySucceedCallback& onSucceed, const QueryFailCallback& onFail,
Shock Jiang5d5928c2014-12-03 13:41:22 -080063 Face& face, Validator* validator = nullptr);
Shock Jiang698e6ed2014-11-09 11:22:24 -080064
65 virtual void
66 start();
67
68 /**
69 * @brief return false if the current status is not QUEYR_STEP_QUERY_NS
70 * or QUERY_STEP_QUERY_RR
71 */
72 virtual bool
73 hasEnded();
74
75NDNS_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
76 void
77 onData(const ndn::Interest& interest, const Data& data);
78
Shock Jiang5d5928c2014-12-03 13:41:22 -080079 void
80 onDataValidated(const shared_ptr<const Data>& data, NdnsType ndnsType);
81
Shock Jiang698e6ed2014-11-09 11:22:24 -080082 /**
83 * @brief change the Controller state according to timeout. For current,
84 * abort the query when timeout
85 */
86 void
87 onTimeout(const Interest& interest);
88
89 void
90 abort();
91
92 /**
93 * @brief get the Interest according to current Controller state.
94 * Only be valid on State QueryNS & QueryRR, or throw exception
95 */
96 const Interest
97 makeLatestInterest();
98
99 const Response
100 parseFinalResponse(const Data& data);
101
102 void
103 express(const Interest& interest);
104
Shock Jiang5d5928c2014-12-03 13:41:22 -0800105public:
Shock Jiang698e6ed2014-11-09 11:22:24 -0800106 void
Shock Jiang5d5928c2014-12-03 13:41:22 -0800107 setStartComponentIndex(size_t finished)
Shock Jiang698e6ed2014-11-09 11:22:24 -0800108 {
109 m_nFinishedComps = finished;
110 }
111
Shock Jiang698e6ed2014-11-09 11:22:24 -0800112 QueryStep
113 getStep() const
114 {
115 return m_step;
116 }
117
118 size_t
119 getNFinishedComps() const
120 {
121 return m_nFinishedComps;
122 }
123
124 size_t
125 getNTryComps() const
126 {
127 return m_nTryComps;
128 }
129
130protected:
Shock Jiang5d5928c2014-12-03 13:41:22 -0800131 Validator* m_validator;
Shock Jiang698e6ed2014-11-09 11:22:24 -0800132 /**
133 * @brief current query step
134 */
135 QueryStep m_step;
136
137 /*
138 * the number of label that has been resolved successfully
139 * This also define the next AuthZone prefix
140 * AuthZone = m_query.getRrLabel().getPrefix(m_nFinishedComps)
141 */
142 size_t m_nFinishedComps;
143
144 /*
145 * used when query the KSK (key signing key), e.g., /net/ndnsim/ksk-1
146 */
147 size_t m_nTryComps;
148};
149
150std::ostream&
151operator<<(std::ostream& os, const IterativeQueryController& iq);
152
153std::ostream&
154operator<<(std::ostream& os, const IterativeQueryController::QueryStep step);
155
156} // namespace ndns
157} // namespace ndn
158
159#endif // NDNS_CLIENTS_ITERATIVE_QUERY_HPP