blob: a95a0d026fed783cf0d982541e0ac5d32d64226d [file] [log] [blame]
shockjianga5ae48c2014-07-27 23:21:41 -07001/* -*- 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
21namespace ndn {
22namespace ndns {
23
24NameDig::NameDig(const char *programName, const char *prefix)
shockjiang99ad3892014-08-03 14:56:13 -070025 : NDNApp(programName, prefix)
26 , m_resolverName(Name("/"))
27 , m_dstLabel(Name(prefix))
28 , m_rrType(RR::TXT)
29{
shockjianga5ae48c2014-07-27 23:21:41 -070030 //prefix in this app is the m_dstLabel
31 this->setInterestLifetime(time::milliseconds(10000));
32}
33
shockjiang99ad3892014-08-03 14:56:13 -070034NameDig::~NameDig()
35{
shockjianga5ae48c2014-07-27 23:21:41 -070036 // TODO Auto-generated destructor stub
37}
38
shockjiang99ad3892014-08-03 14:56:13 -070039void NameDig::onData(const Interest& interest, Data& data)
shockjianga5ae48c2014-07-27 23:21:41 -070040{
41 Response re;
42 re.fromData(data);
shockjiang99ad3892014-08-03 14:56:13 -070043 cout << "get data:->" << data.getName() << endl;
44 cout << "get response:->" << re << endl;
shockjianga5ae48c2014-07-27 23:21:41 -070045
shockjiang99ad3892014-08-03 14:56:13 -070046 response = re;
shockjianga5ae48c2014-07-27 23:21:41 -070047
48 m_rrs = re.getRrs();
49
shockjianga5ae48c2014-07-27 23:21:41 -070050 vector<RR>::iterator iter = m_rrs.begin();
51
shockjiang99ad3892014-08-03 14:56:13 -070052 while (iter != m_rrs.end()) {
shockjianga5ae48c2014-07-27 23:21:41 -070053 RR rr = *iter;
shockjiang99ad3892014-08-03 14:56:13 -070054 cout << rr << endl;
55 iter++;
shockjianga5ae48c2014-07-27 23:21:41 -070056 }
57
58 this->stop();
59}
60
shockjiang99ad3892014-08-03 14:56:13 -070061void NameDig::sendQuery()
shockjianga5ae48c2014-07-27 23:21:41 -070062{
63 Query q;
64 q.setAuthorityZone(this->m_resolverName);
65 q.setRrLabel(m_dstLabel);
66 q.setQueryType(Query::QUERY_DNS_R);
67 q.setRrType(m_rrType);
68
69 Interest interest = q.toInterest();
70 interest.setInterestLifetime(this->m_interestLifetime);
71 try {
72 m_face.expressInterest(interest,
shockjiang99ad3892014-08-03 14:56:13 -070073 boost::bind(&NameDig::onData, this, _1, _2),
74 boost::bind(&NameDig::onTimeout, this, _1));
75 std::cout << "[* <- *] send Interest: " << interest.getName().toUri()
76 << std::endl;
77 } catch (std::exception& e) {
shockjianga5ae48c2014-07-27 23:21:41 -070078 m_hasError = true;
79 m_error = e.what();
80 }
81 m_interestTriedNum += 1;
82}
83
shockjiang99ad3892014-08-03 14:56:13 -070084void NameDig::onTimeout(const Interest& interest)
shockjianga5ae48c2014-07-27 23:21:41 -070085{
shockjiang99ad3892014-08-03 14:56:13 -070086 std::cout << "[* !! *] timeout Interest" << interest.getName() << std::endl;
shockjianga5ae48c2014-07-27 23:21:41 -070087
shockjiang99ad3892014-08-03 14:56:13 -070088 if (m_interestTriedNum >= m_interestTriedMax) {
shockjianga5ae48c2014-07-27 23:21:41 -070089 m_error = "All Interests timeout";
90 m_hasError = true;
91 this->stop();
shockjiang99ad3892014-08-03 14:56:13 -070092 } else {
shockjianga5ae48c2014-07-27 23:21:41 -070093 sendQuery();
94 }
95
96}
97
shockjiang99ad3892014-08-03 14:56:13 -070098void NameDig::run()
shockjianga5ae48c2014-07-27 23:21:41 -070099{
100
101 this->sendQuery();
102
shockjiang99ad3892014-08-03 14:56:13 -0700103 try {
shockjianga5ae48c2014-07-27 23:21:41 -0700104 m_face.processEvents();
shockjiang99ad3892014-08-03 14:56:13 -0700105 } catch (std::exception& e) {
shockjianga5ae48c2014-07-27 23:21:41 -0700106 m_error = e.what();
107 m_hasError = true;
108 this->stop();
109 }
110
111}
112
113} /* namespace ndns */
114} /* namespace ndn */