blob: 9853672dd6478ba0fb0c3a9b99d3ece874f20a3f [file] [log] [blame]
Shock Jiang01712f32014-10-01 14:34:16 -07001/* -*- 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 Jiang01712f32014-10-01 14:34:16 -07004 *
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
22namespace ndn {
23namespace ndns {
24
25Query::Query()
26 : m_interestLifetime(ndn::DEFAULT_INTEREST_LIFETIME)
27{
28}
29
Yumin Xia6343c5b2016-10-20 15:45:50 -070030Query::Query(const Name& zone, const name::Component& queryType)
31 : m_zone(zone)
Shock Jiang01712f32014-10-01 14:34:16 -070032 , m_queryType(queryType)
33 , m_interestLifetime(ndn::DEFAULT_INTEREST_LIFETIME)
34{
35}
36
37
38bool
Yumin Xia6343c5b2016-10-20 15:45:50 -070039Query::fromInterest(const Name& zone, const Interest& interest)
Shock Jiang01712f32014-10-01 14:34:16 -070040{
41 label::MatchResult re;
Yumin Xia6343c5b2016-10-20 15:45:50 -070042 if (!matchName(interest, zone, re))
Shock Jiang01712f32014-10-01 14:34:16 -070043 return false;
44
45 m_rrLabel = re.rrLabel;
46 m_rrType = re.rrType;
47
Shock Jiang01712f32014-10-01 14:34:16 -070048 m_zone = zone;
49
Yumin Xia4e561892016-10-21 10:48:01 -070050 if (interest.hasLink()) {
51 m_link = interest.getLink().wireEncode();
52 } else {
53 m_link = Block();
54 }
55
56
Shock Jiang01712f32014-10-01 14:34:16 -070057 size_t len = zone.size();
Shock Jiang01712f32014-10-01 14:34:16 -070058 m_queryType = interest.getName().get(len);
59
60 return true;
61}
62
63Interest
64Query::toInterest() const
65{
Yumin Xia6343c5b2016-10-20 15:45:50 -070066 // <zone> [<KEY>|<NDNS>|<NDNS-R>] <rrLabel> <rrType>
Shock Jiang01712f32014-10-01 14:34:16 -070067 Name name;
Shock Jiang01712f32014-10-01 14:34:16 -070068
69 name.append(this->m_zone)
70 .append(this->m_queryType)
71 .append(this->m_rrLabel)
72 .append(this->m_rrType);
73
Yumin Xia4e561892016-10-21 10:48:01 -070074 Interest interest;
75 interest.setName(name);
76 interest.setInterestLifetime(m_interestLifetime);
77 if (m_link.hasWire()) {
78 interest.setLink(m_link);
79 }
80
81 return interest;
Shock Jiang01712f32014-10-01 14:34:16 -070082}
83
84std::ostream&
85operator<<(std::ostream& os, const Query& query)
86{
87 os << "Query: zone=" << query.getZone()
Shock Jiang01712f32014-10-01 14:34:16 -070088 << " queryType=" << query.getQueryType()
89 << " rrLabel=" << query.getRrLabel()
90 << " rrType=" << query.getRrType()
91 << " Lifetime=" << query.getInterestLifetime()
92 ;
93 return os;
94}
95
96} // namespace ndns
97} // namespace ndn