NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
stream-transport-with-resolver-impl.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_TRANSPORT_STREAM_TRANSPORT_WITH_RESOLVER_IMPL_HPP
23 #define NDN_TRANSPORT_STREAM_TRANSPORT_WITH_RESOLVER_IMPL_HPP
24 
26 
27 namespace ndn {
28 
32 template<typename BaseTransport, typename Protocol>
33 class StreamTransportWithResolverImpl : public StreamTransportImpl<BaseTransport, Protocol>
34 {
35 public:
37 
38  StreamTransportWithResolverImpl(BaseTransport& transport, boost::asio::io_service& ioService)
39  : StreamTransportImpl<BaseTransport, Protocol>(transport, ioService)
40  {
41  }
42 
43  void
44  connect(const typename Protocol::resolver::query& query)
45  {
46  if (this->m_isConnecting) {
47  return;
48  }
49 
50  this->m_isConnecting = true;
51 
52  // Wait at most 4 seconds to connect
54  this->m_connectTimer.expires_from_now(boost::posix_time::seconds(4));
55  this->m_connectTimer.async_wait(bind(&Impl::connectTimeoutHandler, this->shared_from_this(), _1));
56 
57  // typename boost::asio::ip::basic_resolver< Protocol > resolver;
58  auto resolver = make_shared<typename Protocol::resolver>(ref(this->m_socket.get_io_service()));
59  resolver->async_resolve(query, bind(&Impl::resolveHandler, this->shared_from_base(), _1, _2, resolver));
60  }
61 
62 protected:
63  void
64  resolveHandler(const boost::system::error_code& error,
65  typename Protocol::resolver::iterator endpoint,
66  const shared_ptr<typename Protocol::resolver>&)
67  {
68  if (error) {
69  if (error == boost::system::errc::operation_canceled)
70  return;
71 
72  BOOST_THROW_EXCEPTION(Transport::Error(error, "Error during resolution of host or port"));
73  }
74 
75  typename Protocol::resolver::iterator end;
76  if (endpoint == end) {
77  this->m_transport.close();
78  BOOST_THROW_EXCEPTION(Transport::Error(error, "Unable to resolve because host or port"));
79  }
80 
81  this->m_socket.async_connect(*endpoint, bind(&Impl::connectHandler, this->shared_from_this(), _1));
82  }
83 
84 private:
85  shared_ptr<Impl>
86  shared_from_base()
87  {
88  return static_pointer_cast<Impl>(this->shared_from_this());
89  }
90 };
91 
92 
93 } // namespace ndn
94 
95 #endif // NDN_TRANSPORT_STREAM_TRANSPORT_WITH_RESOLVER_IMPL_HPP
void connectTimeoutHandler(const boost::system::error_code &error)
Copyright (c) 2011-2015 Regents of the University of California.
implementation detail of a Boost.Asio-based stream-oriented transport
Table::const_iterator iterator
Definition: cs-internal.hpp:41
void connect(const typename Protocol::resolver::query &query)
implementation detail of a Boost.Asio-based stream-oriented transport with resolver support ...
void resolveHandler(const boost::system::error_code &error, typename Protocol::resolver::iterator endpoint, const shared_ptr< typename Protocol::resolver > &)
StreamTransportWithResolverImpl< BaseTransport, Protocol > Impl
boost::asio::deadline_timer m_connectTimer
void connectHandler(const boost::system::error_code &error)
StreamTransportWithResolverImpl(BaseTransport &transport, boost::asio::io_service &ioService)