Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 22 | #include "face.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 23 | #include "detail/face-impl.hpp" |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 24 | |
Junxiao Shi | 468abc3 | 2014-11-04 09:12:47 -0700 | [diff] [blame] | 25 | #include "encoding/tlv.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 26 | #include "net/face-uri.hpp" |
Junxiao Shi | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 27 | #include "security/signing-helpers.hpp" |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 28 | #include "util/random.hpp" |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 29 | #include "util/time.hpp" |
| 30 | |
| 31 | // NDN_LOG_INIT(ndn.Face) is declared in face-impl.hpp |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 32 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 33 | // A callback scheduled through io.post and io.dispatch may be invoked after the face |
| 34 | // is destructed. To prevent this situation, these macros captures Face::m_impl as weak_ptr, |
| 35 | // and skips callback execution if the face has been destructed. |
| 36 | #define IO_CAPTURE_WEAK_IMPL(OP) \ |
| 37 | { \ |
| 38 | weak_ptr<Impl> implWeak(m_impl); \ |
| 39 | m_ioService.OP([=] { \ |
| 40 | auto impl = implWeak.lock(); \ |
| 41 | if (impl != nullptr) { |
| 42 | #define IO_CAPTURE_WEAK_IMPL_END \ |
| 43 | } \ |
| 44 | }); \ |
| 45 | } |
| 46 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 47 | namespace ndn { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 48 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 49 | Face::Face(shared_ptr<Transport> transport) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 50 | : m_internalIoService(new boost::asio::io_service()) |
| 51 | , m_ioService(*m_internalIoService) |
| 52 | , m_internalKeyChain(new KeyChain()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 53 | , m_impl(make_shared<Impl>(*this)) |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 54 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 55 | construct(transport, *m_internalKeyChain); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 58 | Face::Face(boost::asio::io_service& ioService) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 59 | : m_ioService(ioService) |
| 60 | , m_internalKeyChain(new KeyChain()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 61 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 62 | { |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 63 | construct(nullptr, *m_internalKeyChain); |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 66 | Face::Face(const std::string& host, const std::string& port) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 67 | : m_internalIoService(new boost::asio::io_service()) |
| 68 | , m_ioService(*m_internalIoService) |
| 69 | , m_internalKeyChain(new KeyChain()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 70 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 71 | { |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 72 | construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 75 | Face::Face(shared_ptr<Transport> transport, KeyChain& keyChain) |
| 76 | : m_internalIoService(new boost::asio::io_service()) |
| 77 | , m_ioService(*m_internalIoService) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 78 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 79 | { |
| 80 | construct(transport, keyChain); |
| 81 | } |
| 82 | |
| 83 | Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 84 | : m_ioService(ioService) |
| 85 | , m_internalKeyChain(new KeyChain()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 86 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 87 | { |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 88 | construct(transport, *m_internalKeyChain); |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 91 | Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 92 | : m_ioService(ioService) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 93 | , m_impl(make_shared<Impl>(*this)) |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 94 | { |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 95 | construct(transport, keyChain); |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 98 | shared_ptr<Transport> |
| 99 | Face::makeDefaultTransport() |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 100 | { |
| 101 | // transport=unix:///var/run/nfd.sock |
| 102 | // transport=tcp://localhost:6363 |
| 103 | |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 104 | std::string transportUri; |
| 105 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 106 | const char* transportEnviron = getenv("NDN_CLIENT_TRANSPORT"); |
| 107 | if (transportEnviron != nullptr) { |
| 108 | transportUri = transportEnviron; |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 109 | } |
| 110 | else { |
| 111 | ConfigFile config; |
| 112 | transportUri = config.getParsedConfiguration().get<std::string>("transport", ""); |
| 113 | } |
| 114 | |
| 115 | if (transportUri.empty()) { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 116 | // transport not specified, use default Unix transport. |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 117 | return UnixTransport::create(""); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 118 | } |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 120 | std::string protocol; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 121 | try { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 122 | FaceUri uri(transportUri); |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 123 | protocol = uri.getScheme(); |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 124 | |
| 125 | if (protocol == "unix") { |
| 126 | return UnixTransport::create(transportUri); |
| 127 | } |
| 128 | else if (protocol == "tcp" || protocol == "tcp4" || protocol == "tcp6") { |
| 129 | return TcpTransport::create(transportUri); |
| 130 | } |
| 131 | else { |
| 132 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unsupported transport protocol \"" + protocol + "\"")); |
| 133 | } |
| 134 | } |
| 135 | catch (const Transport::Error& error) { |
| 136 | BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what())); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 137 | } |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 138 | catch (const FaceUri::Error& error) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 139 | BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what())); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 140 | } |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 144 | Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 145 | { |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 146 | if (transport == nullptr) { |
| 147 | transport = makeDefaultTransport(); |
| 148 | } |
| 149 | BOOST_ASSERT(transport != nullptr); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 150 | m_transport = transport; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 152 | m_nfdController.reset(new nfd::Controller(*this, keyChain)); |
| 153 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 154 | IO_CAPTURE_WEAK_IMPL(post) { |
| 155 | impl->ensureConnected(false); |
| 156 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 159 | Face::~Face() = default; |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 160 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 161 | shared_ptr<Transport> |
| 162 | Face::getTransport() |
| 163 | { |
| 164 | return m_transport; |
| 165 | } |
| 166 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 167 | const PendingInterestId* |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 168 | Face::expressInterest(const Interest& interest, |
| 169 | const DataCallback& afterSatisfied, |
| 170 | const NackCallback& afterNacked, |
| 171 | const TimeoutCallback& afterTimeout) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 172 | { |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 173 | shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 174 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 175 | // Use `interestToExpress` to avoid wire format creation for the original Interest |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 176 | if (interestToExpress->wireEncode().size() > MAX_NDN_PACKET_SIZE) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 177 | BOOST_THROW_EXCEPTION(Error("Interest size exceeds maximum limit")); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 178 | } |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 179 | NDN_LOG_DEBUG("<I " << *interestToExpress); // interestToExpress is guaranteed to have nonce |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 180 | |
Alexander Afanasyev | 4e50b97 | 2014-03-25 10:57:50 -0700 | [diff] [blame] | 181 | // If the same ioService thread, dispatch directly calls the method |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 182 | IO_CAPTURE_WEAK_IMPL(dispatch) { |
| 183 | impl->asyncExpressInterest(interestToExpress, afterSatisfied, afterNacked, afterTimeout); |
| 184 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 185 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 186 | return reinterpret_cast<const PendingInterestId*>(interestToExpress.get()); |
| 187 | } |
| 188 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 189 | void |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 190 | Face::removePendingInterest(const PendingInterestId* pendingInterestId) |
| 191 | { |
| 192 | IO_CAPTURE_WEAK_IMPL(post) { |
| 193 | impl->asyncRemovePendingInterest(pendingInterestId); |
| 194 | } IO_CAPTURE_WEAK_IMPL_END |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | Face::removeAllPendingInterests() |
| 199 | { |
| 200 | IO_CAPTURE_WEAK_IMPL(post) { |
| 201 | impl->asyncRemoveAllPendingInterests(); |
| 202 | } IO_CAPTURE_WEAK_IMPL_END |
| 203 | } |
| 204 | |
| 205 | size_t |
| 206 | Face::getNPendingInterests() const |
| 207 | { |
| 208 | return m_impl->m_pendingInterestTable.size(); |
| 209 | } |
| 210 | |
| 211 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 212 | Face::put(const Data& data) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 213 | { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 214 | Block wire = data.wireEncode(); |
| 215 | |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 216 | lp::Packet packet; |
| 217 | bool hasLpFields = false; |
| 218 | |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 219 | shared_ptr<lp::CachePolicyTag> cachePolicyTag = data.getTag<lp::CachePolicyTag>(); |
| 220 | if (cachePolicyTag != nullptr) { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 221 | packet.add<lp::CachePolicyField>(*cachePolicyTag); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 222 | hasLpFields = true; |
| 223 | } |
| 224 | |
| 225 | shared_ptr<lp::CongestionMarkTag> congestionMarkTag = data.getTag<lp::CongestionMarkTag>(); |
| 226 | if (congestionMarkTag != nullptr) { |
| 227 | packet.add<lp::CongestionMarkField>(*congestionMarkTag); |
| 228 | hasLpFields = true; |
| 229 | } |
| 230 | |
| 231 | if (hasLpFields) { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 232 | packet.add<lp::FragmentField>(std::make_pair(wire.begin(), wire.end())); |
| 233 | wire = packet.wireEncode(); |
| 234 | } |
| 235 | |
| 236 | if (wire.size() > MAX_NDN_PACKET_SIZE) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 237 | BOOST_THROW_EXCEPTION(Error("Data size exceeds maximum limit")); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 238 | |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 239 | NDN_LOG_DEBUG("<D " << data.getName()); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 240 | IO_CAPTURE_WEAK_IMPL(dispatch) { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 241 | impl->asyncSend(wire); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 242 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 245 | void |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 246 | Face::put(const lp::Nack& nack) |
| 247 | { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 248 | lp::Packet packet; |
| 249 | packet.add<lp::NackField>(nack.getHeader()); |
| 250 | const Block& interestWire = nack.getInterest().wireEncode(); |
| 251 | packet.add<lp::FragmentField>(std::make_pair(interestWire.begin(), interestWire.end())); |
| 252 | |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 253 | shared_ptr<lp::CongestionMarkTag> congestionMarkTag = nack.getTag<lp::CongestionMarkTag>(); |
| 254 | if (congestionMarkTag != nullptr) { |
| 255 | packet.add<lp::CongestionMarkField>(*congestionMarkTag); |
| 256 | } |
| 257 | |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 258 | Block wire = packet.wireEncode(); |
| 259 | |
| 260 | if (wire.size() > MAX_NDN_PACKET_SIZE) |
| 261 | BOOST_THROW_EXCEPTION(Error("Nack size exceeds maximum limit")); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 262 | |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 263 | NDN_LOG_DEBUG("<N " << nack.getInterest() << '~' << nack.getHeader().getReason()); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 264 | IO_CAPTURE_WEAK_IMPL(dispatch) { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 265 | impl->asyncSend(wire); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 266 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 6fcdde2 | 2014-08-22 19:03:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 269 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 270 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 271 | const InterestCallback& onInterest, |
| 272 | const RegisterPrefixFailureCallback& onFailure, |
| 273 | const security::SigningInfo& signingInfo, |
| 274 | uint64_t flags) |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 275 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 276 | return setInterestFilter(interestFilter, onInterest, nullptr, onFailure, signingInfo, flags); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 280 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 281 | const InterestCallback& onInterest, |
| 282 | const RegisterPrefixSuccessCallback& onSuccess, |
| 283 | const RegisterPrefixFailureCallback& onFailure, |
| 284 | const security::SigningInfo& signingInfo, |
| 285 | uint64_t flags) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 286 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 287 | auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 288 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 289 | nfd::CommandOptions options; |
| 290 | options.setSigningInfo(signingInfo); |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 291 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 292 | return m_impl->registerPrefix(interestFilter.getPrefix(), filter, |
| 293 | onSuccess, onFailure, flags, options); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 296 | const InterestFilterId* |
| 297 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 298 | const InterestCallback& onInterest) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 299 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 300 | auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 301 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 302 | IO_CAPTURE_WEAK_IMPL(post) { |
| 303 | impl->asyncSetInterestFilter(filter); |
| 304 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 305 | |
| 306 | return reinterpret_cast<const InterestFilterId*>(filter.get()); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 309 | const RegisteredPrefixId* |
| 310 | Face::registerPrefix(const Name& prefix, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 311 | const RegisterPrefixSuccessCallback& onSuccess, |
| 312 | const RegisterPrefixFailureCallback& onFailure, |
| 313 | const security::SigningInfo& signingInfo, |
| 314 | uint64_t flags) |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 315 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 316 | nfd::CommandOptions options; |
| 317 | options.setSigningInfo(signingInfo); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 318 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 319 | return m_impl->registerPrefix(prefix, nullptr, onSuccess, onFailure, flags, options); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 320 | } |
| 321 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 322 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 323 | Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 324 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 325 | IO_CAPTURE_WEAK_IMPL(post) { |
| 326 | impl->asyncUnregisterPrefix(registeredPrefixId, nullptr, nullptr); |
| 327 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 330 | void |
| 331 | Face::unsetInterestFilter(const InterestFilterId* interestFilterId) |
| 332 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 333 | IO_CAPTURE_WEAK_IMPL(post) { |
| 334 | impl->asyncUnsetInterestFilter(interestFilterId); |
| 335 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 336 | } |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 337 | |
| 338 | void |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 339 | Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId, |
| 340 | const UnregisterPrefixSuccessCallback& onSuccess, |
| 341 | const UnregisterPrefixFailureCallback& onFailure) |
| 342 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 343 | IO_CAPTURE_WEAK_IMPL(post) { |
| 344 | impl->asyncUnregisterPrefix(registeredPrefixId, onSuccess, onFailure); |
| 345 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 348 | void |
Junxiao Shi | 2ed9e07 | 2017-08-13 16:45:48 +0000 | [diff] [blame^] | 349 | Face::doProcessEvents(time::milliseconds timeout, bool keepThread) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 350 | { |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 351 | if (m_ioService.stopped()) { |
| 352 | m_ioService.reset(); // ensure that run()/poll() will do some work |
| 353 | } |
| 354 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 355 | try { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 356 | if (timeout < time::milliseconds::zero()) { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 357 | // do not block if timeout is negative, but process pending events |
| 358 | m_ioService.poll(); |
| 359 | return; |
| 360 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 361 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 362 | if (timeout > time::milliseconds::zero()) { |
| 363 | boost::asio::io_service& ioService = m_ioService; |
| 364 | unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork; |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 365 | m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.scheduleEvent(timeout, |
| 366 | [&ioService, &work] { |
| 367 | ioService.stop(); |
| 368 | work.reset(); |
| 369 | }); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 370 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 371 | |
| 372 | if (keepThread) { |
| 373 | // work will ensure that m_ioService is running until work object exists |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 374 | m_impl->m_ioServiceWork.reset(new boost::asio::io_service::work(m_ioService)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 375 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 376 | |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 377 | m_ioService.run(); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 378 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 379 | catch (...) { |
| 380 | m_impl->m_ioServiceWork.reset(); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 381 | m_impl->m_pendingInterestTable.clear(); |
| 382 | m_impl->m_registeredPrefixTable.clear(); |
| 383 | throw; |
| 384 | } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 387 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 388 | Face::shutdown() |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 389 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 390 | IO_CAPTURE_WEAK_IMPL(post) { |
| 391 | this->asyncShutdown(); |
| 392 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 7dced46 | 2014-03-19 15:12:32 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void |
| 396 | Face::asyncShutdown() |
| 397 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 398 | m_impl->m_pendingInterestTable.clear(); |
| 399 | m_impl->m_registeredPrefixTable.clear(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 400 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 401 | if (m_transport->isConnected()) |
| 402 | m_transport->close(); |
| 403 | |
Alexander Afanasyev | 1f5486e | 2014-07-10 17:45:49 -0700 | [diff] [blame] | 404 | m_impl->m_ioServiceWork.reset(); |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 407 | /** |
| 408 | * @brief extract local fields from NDNLPv2 packet and tag onto a network layer packet |
| 409 | */ |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 410 | template<typename NetPkt> |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 411 | static void |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 412 | extractLpLocalFields(NetPkt& netPacket, const lp::Packet& lpPacket) |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 413 | { |
| 414 | if (lpPacket.has<lp::IncomingFaceIdField>()) { |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 415 | netPacket.setTag(make_shared<lp::IncomingFaceIdTag>(lpPacket.get<lp::IncomingFaceIdField>())); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 416 | } |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 417 | |
| 418 | if (lpPacket.has<lp::CongestionMarkField>()) { |
| 419 | netPacket.setTag(make_shared<lp::CongestionMarkTag>(lpPacket.get<lp::CongestionMarkField>())); |
| 420 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 423 | void |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 424 | Face::onReceiveElement(const Block& blockFromDaemon) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 425 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 426 | lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet, |
| 427 | // no need to distinguish |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 428 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 429 | Buffer::const_iterator begin, end; |
| 430 | std::tie(begin, end) = lpPacket.get<lp::FragmentField>(); |
| 431 | Block netPacket(&*begin, std::distance(begin, end)); |
| 432 | switch (netPacket.type()) { |
| 433 | case tlv::Interest: { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 434 | auto interest = make_shared<Interest>(netPacket); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 435 | if (lpPacket.has<lp::NackField>()) { |
| 436 | auto nack = make_shared<lp::Nack>(std::move(*interest)); |
| 437 | nack->setHeader(lpPacket.get<lp::NackField>()); |
| 438 | extractLpLocalFields(*nack, lpPacket); |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 439 | NDN_LOG_DEBUG(">N " << nack->getInterest() << '~' << nack->getHeader().getReason()); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 440 | m_impl->nackPendingInterests(*nack); |
| 441 | } |
| 442 | else { |
| 443 | extractLpLocalFields(*interest, lpPacket); |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 444 | NDN_LOG_DEBUG(">I " << *interest); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 445 | m_impl->processInterestFilters(*interest); |
| 446 | } |
| 447 | break; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 448 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 449 | case tlv::Data: { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 450 | auto data = make_shared<Data>(netPacket); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 451 | extractLpLocalFields(*data, lpPacket); |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 452 | NDN_LOG_DEBUG(">D " << data->getName()); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 453 | m_impl->satisfyPendingInterests(*data); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 454 | break; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 455 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 456 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 459 | } // namespace ndn |