shockjiang | a5ae48c | 2014-07-27 23:21:41 -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 | #include "name-dig.hpp" |
| 20 | |
| 21 | namespace ndn { |
| 22 | namespace ndns { |
| 23 | |
| 24 | NameDig::NameDig(const char *programName, const char *prefix) |
| 25 | : NDNApp(programName, prefix) |
| 26 | , m_resolverName(Name("/")) |
| 27 | , m_dstLabel(Name(prefix)){ |
| 28 | //prefix in this app is the m_dstLabel |
| 29 | this->setInterestLifetime(time::milliseconds(10000)); |
| 30 | } |
| 31 | |
| 32 | NameDig::~NameDig() { |
| 33 | // TODO Auto-generated destructor stub |
| 34 | } |
| 35 | |
| 36 | void |
| 37 | NameDig::onData(const Interest& interest, Data& data) |
| 38 | { |
| 39 | Response re; |
| 40 | re.fromData(data); |
| 41 | cout<<"get data:->"<<data.getName()<<endl; |
| 42 | cout<<"get response:->"<<re<<endl; |
| 43 | |
| 44 | |
| 45 | |
| 46 | m_rrs = re.getRrs(); |
| 47 | |
| 48 | |
| 49 | |
| 50 | vector<RR>::iterator iter = m_rrs.begin(); |
| 51 | |
| 52 | while (iter != m_rrs.end()) |
| 53 | { |
| 54 | RR rr = *iter; |
| 55 | cout<<rr<<endl; |
| 56 | iter ++; |
| 57 | } |
| 58 | |
| 59 | this->stop(); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | NameDig::sendQuery() |
| 64 | { |
| 65 | Query q; |
| 66 | q.setAuthorityZone(this->m_resolverName); |
| 67 | q.setRrLabel(m_dstLabel); |
| 68 | q.setQueryType(Query::QUERY_DNS_R); |
| 69 | q.setRrType(m_rrType); |
| 70 | |
| 71 | Interest interest = q.toInterest(); |
| 72 | interest.setInterestLifetime(this->m_interestLifetime); |
| 73 | try { |
| 74 | m_face.expressInterest(interest, |
| 75 | boost::bind(&NameDig::onData, this, _1, _2), |
| 76 | boost::bind(&NameDig::onTimeout, this, _1) |
| 77 | ); |
| 78 | std::cout<<"[* <- *] send Interest: "<<interest.getName().toUri()<<std::endl; |
| 79 | }catch(std::exception& e) { |
| 80 | m_hasError = true; |
| 81 | m_error = e.what(); |
| 82 | } |
| 83 | m_interestTriedNum += 1; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | NameDig::onTimeout(const Interest& interest) |
| 88 | { |
| 89 | std::cout<<"[* !! *] timeout Interest"<<interest.getName()<<std::endl; |
| 90 | |
| 91 | if (m_interestTriedNum >= m_interestTriedMax) |
| 92 | { |
| 93 | m_error = "All Interests timeout"; |
| 94 | m_hasError = true; |
| 95 | this->stop(); |
| 96 | } else |
| 97 | { |
| 98 | sendQuery(); |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | NameDig::run() |
| 105 | { |
| 106 | |
| 107 | this->sendQuery(); |
| 108 | |
| 109 | try |
| 110 | { |
| 111 | m_face.processEvents(); |
| 112 | } |
| 113 | catch (std::exception& e) |
| 114 | { |
| 115 | m_error = e.what(); |
| 116 | m_hasError = true; |
| 117 | this->stop(); |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |
| 122 | } /* namespace ndns */ |
| 123 | } /* namespace ndn */ |