NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
dns.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "dns.hpp"
23 
24 #include "scheduler.hpp"
25 
26 namespace ndn {
27 namespace dns {
28 
29 typedef boost::asio::ip::udp::endpoint EndPoint;
30 typedef boost::asio::ip::basic_resolver<boost::asio::ip::udp> BoostResolver;
31 
32 class Resolver : noncopyable
33 {
34 public:
35  Resolver(const SuccessCallback& onSuccess,
36  const ErrorCallback& onError,
37  const ndn::dns::AddressSelector& addressSelector,
38  boost::asio::io_service& ioService)
39  : m_resolver(ioService)
40  , m_addressSelector(addressSelector)
41  , m_onSuccess(onSuccess)
42  , m_onError(onError)
43  , m_scheduler(ioService)
44  {
45  }
46 
47  void
48  asyncResolve(const std::string& host,
49  const time::nanoseconds& timeout,
50  const shared_ptr<Resolver>& self)
51  {
52  BoostResolver::query query(host, NULL_PORT);
53 
54  m_resolver.async_resolve(query, bind(&Resolver::onResolveSuccess, this, _1, _2, self));
55 
56  m_resolveTimeout = m_scheduler.scheduleEvent(timeout,
57  bind(&Resolver::onResolveError, this,
58  "Timeout", self));
59  }
60 
62  syncResolve(BoostResolver::query query)
63  {
64  return m_resolver.resolve(query);
65  }
66 
67  void
68  onResolveSuccess(const boost::system::error_code& error,
69  BoostResolver::iterator remoteEndpoint,
70  const shared_ptr<Resolver>& self)
71  {
72  m_scheduler.cancelEvent(m_resolveTimeout);
73 
74  if (error)
75  {
76  if (error == boost::system::errc::operation_canceled)
77  {
78  return;
79  }
80 
81  return m_onError("Remote endpoint hostname or port cannot be resolved: " +
82  error.category().message(error.value()));
83  }
84 
86  for (; remoteEndpoint != end; ++remoteEndpoint)
87  {
88  IpAddress address(EndPoint(*remoteEndpoint).address());
89 
90  if (m_addressSelector(address))
91  {
92  return m_onSuccess(address);
93  }
94  }
95 
96  m_onError("No endpoint matching the specified address selector found");
97  }
98 
99  void
100  onResolveError(const std::string& errorInfo, const shared_ptr<Resolver>& self)
101  {
102  m_resolver.cancel();
103  m_onError(errorInfo);
104  }
105 
106 public:
107  static const std::string NULL_PORT;
108 
109 private:
110  BoostResolver m_resolver;
111  EventId m_resolveTimeout;
112 
113  ndn::dns::AddressSelector m_addressSelector;
114  SuccessCallback m_onSuccess;
115  ErrorCallback m_onError;
116 
117  Scheduler m_scheduler;
118 };
119 
120 const std::string Resolver::NULL_PORT = "";
121 
122 void
123 asyncResolve(const std::string& host,
124  const SuccessCallback& onSuccess,
125  const ErrorCallback& onError,
126  boost::asio::io_service& ioService,
127  const ndn::dns::AddressSelector& addressSelector,
128  const time::nanoseconds& timeout)
129 {
130  shared_ptr<Resolver> resolver = make_shared<Resolver>(onSuccess, onError,
131  addressSelector, ndn::ref(ioService));
132  resolver->asyncResolve(host, timeout, resolver);
133  // resolver will be destroyed when async operation finishes or global IO service stops
134 }
135 
136 IpAddress
137 syncResolve(const std::string& host, boost::asio::io_service& ioService,
138  const ndn::dns::AddressSelector& addressSelector)
139 {
140  Resolver resolver(SuccessCallback(), ErrorCallback(), addressSelector, ioService);
141 
142  BoostResolver::query query(host, Resolver::NULL_PORT);
143 
144  BoostResolver::iterator remoteEndpoint = resolver.syncResolve(query);
145 
147  for (; remoteEndpoint != end; ++remoteEndpoint)
148  {
149  if (addressSelector(EndPoint(*remoteEndpoint).address()))
150  {
151  return EndPoint(*remoteEndpoint).address();
152  }
153  }
154  BOOST_THROW_EXCEPTION(Error("No endpoint matching the specified address selector found"));
155 }
156 
157 } // namespace dns
158 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
static const std::string NULL_PORT
Definition: dns.cpp:107
Resolver(const SuccessCallback &onSuccess, const ErrorCallback &onError, const ndn::dns::AddressSelector &addressSelector, boost::asio::io_service &ioService)
Definition: dns.cpp:35
function< bool(const boost::asio::ip::address &address)> AddressSelector
Definition: dns.hpp:32
boost::asio::ip::udp::endpoint EndPoint
Definition: dns.cpp:29
function< void(const IpAddress &address)> SuccessCallback
Definition: dns.hpp:71
std::shared_ptr< ns3::EventId > EventId
Definition: scheduler.hpp:39
function< void(const std::string &reason)> ErrorCallback
Definition: dns.hpp:72
void asyncResolve(const std::string &host, const time::nanoseconds &timeout, const shared_ptr< Resolver > &self)
Definition: dns.cpp:48
void onResolveError(const std::string &errorInfo, const shared_ptr< Resolver > &self)
Definition: dns.cpp:100
Table::const_iterator iterator
Definition: cs-internal.hpp:41
boost::asio::ip::basic_resolver< boost::asio::ip::udp > BoostResolver
Definition: dns.cpp:30
boost::asio::ip::address IpAddress
Definition: dns.hpp:69
void onResolveSuccess(const boost::system::error_code &error, BoostResolver::iterator remoteEndpoint, const shared_ptr< Resolver > &self)
Definition: dns.cpp:68
BoostResolver::iterator syncResolve(BoostResolver::query query)
Definition: dns.cpp:62