Shock Jiang | 01712f3 | 2014-10-01 14:34:16 -0700 | [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 | #include "query.hpp" |
| 21 | |
| 22 | namespace ndn { |
| 23 | namespace ndns { |
| 24 | |
| 25 | Query::Query() |
| 26 | : m_interestLifetime(ndn::DEFAULT_INTEREST_LIFETIME) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | Query::Query(const Name& hint, const Name& zone, const name::Component& queryType) |
| 31 | : m_hint(hint) |
| 32 | , m_zone(zone) |
| 33 | , m_queryType(queryType) |
| 34 | , m_interestLifetime(ndn::DEFAULT_INTEREST_LIFETIME) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | |
| 39 | bool |
| 40 | Query::fromInterest(const Name& hint, const Name& zone, const Interest& interest) |
| 41 | { |
| 42 | label::MatchResult re; |
| 43 | if (!matchName(interest, hint, zone, re)) |
| 44 | return false; |
| 45 | |
| 46 | m_rrLabel = re.rrLabel; |
| 47 | m_rrType = re.rrType; |
| 48 | |
| 49 | m_hint = hint; |
| 50 | m_zone = zone; |
| 51 | |
| 52 | size_t len = zone.size(); |
| 53 | if (!hint.empty()) |
| 54 | len += hint.size() + 1; |
| 55 | m_queryType = interest.getName().get(len); |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | Interest |
| 61 | Query::toInterest() const |
| 62 | { |
| 63 | // <hint> /xF0. <zone> [<KEY>|<NDNS>|<NDNS-R>] <rrLabel> <rrType> |
| 64 | Name name; |
| 65 | if (!m_hint.empty()) { |
| 66 | name.append(m_hint) |
| 67 | .append(ndn::ndns::label::FORWARDING_HINT_LABEL); |
| 68 | } |
| 69 | |
| 70 | name.append(this->m_zone) |
| 71 | .append(this->m_queryType) |
| 72 | .append(this->m_rrLabel) |
| 73 | .append(this->m_rrType); |
| 74 | |
| 75 | return Interest(name, m_interestLifetime); |
| 76 | } |
| 77 | |
| 78 | std::ostream& |
| 79 | operator<<(std::ostream& os, const Query& query) |
| 80 | { |
| 81 | os << "Query: zone=" << query.getZone() |
| 82 | << " hint=" << query.getHint() |
| 83 | << " queryType=" << query.getQueryType() |
| 84 | << " rrLabel=" << query.getRrLabel() |
| 85 | << " rrType=" << query.getRrType() |
| 86 | << " Lifetime=" << query.getInterestLifetime() |
| 87 | ; |
| 88 | return os; |
| 89 | } |
| 90 | |
| 91 | } // namespace ndns |
| 92 | } // namespace ndn |