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