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 | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 33 | // A callback scheduled through io.post and io.dispatch may be invoked after the face is destructed. |
| 34 | // To prevent this situation, use these macros to capture Face::m_impl as weak_ptr and skip callback |
| 35 | // execution if the face has been destructed. |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 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 | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 49 | Face::OversizedPacketError::OversizedPacketError(char pktType, const Name& name, size_t wireSize) |
| 50 | : Error((pktType == 'I' ? "Interest " : pktType == 'D' ? "Data " : "Nack ") + |
| 51 | name.toUri() + " encodes into " + to_string(wireSize) + " octets, " |
| 52 | "exceeding the implementation limit of " + to_string(MAX_NDN_PACKET_SIZE) + " octets") |
| 53 | , pktType(pktType) |
| 54 | , name(name) |
| 55 | , wireSize(wireSize) |
| 56 | { |
| 57 | } |
| 58 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 59 | Face::Face(shared_ptr<Transport> transport) |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 60 | : m_internalIoService(make_unique<boost::asio::io_service>()) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 61 | , m_ioService(*m_internalIoService) |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 62 | , m_internalKeyChain(make_unique<KeyChain>()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 63 | , m_impl(make_shared<Impl>(*this)) |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 64 | { |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 65 | construct(std::move(transport), *m_internalKeyChain); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 68 | Face::Face(boost::asio::io_service& ioService) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 69 | : m_ioService(ioService) |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 70 | , m_internalKeyChain(make_unique<KeyChain>()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 71 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 72 | { |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 73 | construct(nullptr, *m_internalKeyChain); |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 76 | Face::Face(const std::string& host, const std::string& port) |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 77 | : m_internalIoService(make_unique<boost::asio::io_service>()) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 78 | , m_ioService(*m_internalIoService) |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 79 | , m_internalKeyChain(make_unique<KeyChain>()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 80 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 81 | { |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 82 | construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 85 | Face::Face(shared_ptr<Transport> transport, KeyChain& keyChain) |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 86 | : m_internalIoService(make_unique<boost::asio::io_service>()) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 87 | , m_ioService(*m_internalIoService) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 88 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 89 | { |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 90 | construct(std::move(transport), keyChain); |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 94 | : m_ioService(ioService) |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 95 | , m_internalKeyChain(make_unique<KeyChain>()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 96 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 97 | { |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 98 | construct(std::move(transport), *m_internalKeyChain); |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 101 | 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] | 102 | : m_ioService(ioService) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 103 | , m_impl(make_shared<Impl>(*this)) |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 104 | { |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 105 | construct(std::move(transport), keyChain); |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 108 | shared_ptr<Transport> |
| 109 | Face::makeDefaultTransport() |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 110 | { |
| 111 | // transport=unix:///var/run/nfd.sock |
| 112 | // transport=tcp://localhost:6363 |
| 113 | |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 114 | std::string transportUri; |
| 115 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 116 | const char* transportEnviron = getenv("NDN_CLIENT_TRANSPORT"); |
| 117 | if (transportEnviron != nullptr) { |
| 118 | transportUri = transportEnviron; |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 119 | } |
| 120 | else { |
| 121 | ConfigFile config; |
| 122 | transportUri = config.getParsedConfiguration().get<std::string>("transport", ""); |
| 123 | } |
| 124 | |
| 125 | if (transportUri.empty()) { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 126 | // transport not specified, use default Unix transport. |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 127 | return UnixTransport::create(""); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 128 | } |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 130 | std::string protocol; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 131 | try { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 132 | FaceUri uri(transportUri); |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 133 | protocol = uri.getScheme(); |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 134 | |
| 135 | if (protocol == "unix") { |
| 136 | return UnixTransport::create(transportUri); |
| 137 | } |
| 138 | else if (protocol == "tcp" || protocol == "tcp4" || protocol == "tcp6") { |
| 139 | return TcpTransport::create(transportUri); |
| 140 | } |
| 141 | else { |
| 142 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unsupported transport protocol \"" + protocol + "\"")); |
| 143 | } |
| 144 | } |
| 145 | catch (const Transport::Error& error) { |
| 146 | BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what())); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 147 | } |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 148 | catch (const FaceUri::Error& error) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 149 | BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what())); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 150 | } |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 154 | Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 155 | { |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 156 | if (transport == nullptr) { |
| 157 | transport = makeDefaultTransport(); |
| 158 | } |
| 159 | BOOST_ASSERT(transport != nullptr); |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 160 | m_transport = std::move(transport); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 161 | |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 162 | m_nfdController = make_unique<nfd::Controller>(*this, keyChain); |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 163 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 164 | IO_CAPTURE_WEAK_IMPL(post) { |
| 165 | impl->ensureConnected(false); |
| 166 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 169 | Face::~Face() = default; |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 170 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 171 | shared_ptr<Transport> |
| 172 | Face::getTransport() |
| 173 | { |
| 174 | return m_transport; |
| 175 | } |
| 176 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 177 | const PendingInterestId* |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 178 | Face::expressInterest(const Interest& interest, |
| 179 | const DataCallback& afterSatisfied, |
| 180 | const NackCallback& afterNacked, |
| 181 | const TimeoutCallback& afterTimeout) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 182 | { |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 183 | shared_ptr<Interest> interest2 = make_shared<Interest>(interest); |
| 184 | interest2->getNonce(); |
| 185 | NDN_LOG_DEBUG("<I " << *interest2); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 186 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 187 | IO_CAPTURE_WEAK_IMPL(dispatch) { |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 188 | impl->asyncExpressInterest(interest2, afterSatisfied, afterNacked, afterTimeout); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 189 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 190 | |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 191 | return reinterpret_cast<const PendingInterestId*>(interest2.get()); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 194 | void |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 195 | Face::removePendingInterest(const PendingInterestId* pendingInterestId) |
| 196 | { |
| 197 | IO_CAPTURE_WEAK_IMPL(post) { |
| 198 | impl->asyncRemovePendingInterest(pendingInterestId); |
| 199 | } IO_CAPTURE_WEAK_IMPL_END |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | Face::removeAllPendingInterests() |
| 204 | { |
| 205 | IO_CAPTURE_WEAK_IMPL(post) { |
| 206 | impl->asyncRemoveAllPendingInterests(); |
| 207 | } IO_CAPTURE_WEAK_IMPL_END |
| 208 | } |
| 209 | |
| 210 | size_t |
| 211 | Face::getNPendingInterests() const |
| 212 | { |
| 213 | return m_impl->m_pendingInterestTable.size(); |
| 214 | } |
| 215 | |
| 216 | void |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 217 | Face::put(Data data) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 218 | { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 219 | NDN_LOG_DEBUG("<D " << data.getName()); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 220 | IO_CAPTURE_WEAK_IMPL(dispatch) { |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 221 | impl->asyncPutData(data); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 222 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 225 | void |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 226 | Face::put(lp::Nack nack) |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 227 | { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 228 | NDN_LOG_DEBUG("<N " << nack.getInterest() << '~' << nack.getHeader().getReason()); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 229 | IO_CAPTURE_WEAK_IMPL(dispatch) { |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 230 | impl->asyncPutNack(nack); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 231 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 6fcdde2 | 2014-08-22 19:03:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 234 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 235 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 236 | const InterestCallback& onInterest, |
| 237 | const RegisterPrefixFailureCallback& onFailure, |
| 238 | const security::SigningInfo& signingInfo, |
| 239 | uint64_t flags) |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 240 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 241 | return setInterestFilter(interestFilter, onInterest, nullptr, onFailure, signingInfo, flags); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 245 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 246 | const InterestCallback& onInterest, |
| 247 | const RegisterPrefixSuccessCallback& onSuccess, |
| 248 | const RegisterPrefixFailureCallback& onFailure, |
| 249 | const security::SigningInfo& signingInfo, |
| 250 | uint64_t flags) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 251 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 252 | auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 253 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 254 | nfd::CommandOptions options; |
| 255 | options.setSigningInfo(signingInfo); |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 256 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 257 | return m_impl->registerPrefix(interestFilter.getPrefix(), filter, |
| 258 | onSuccess, onFailure, flags, options); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 261 | const InterestFilterId* |
| 262 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 263 | const InterestCallback& onInterest) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 264 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 265 | auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 266 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 267 | IO_CAPTURE_WEAK_IMPL(post) { |
| 268 | impl->asyncSetInterestFilter(filter); |
| 269 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 270 | |
| 271 | return reinterpret_cast<const InterestFilterId*>(filter.get()); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 274 | const RegisteredPrefixId* |
| 275 | Face::registerPrefix(const Name& prefix, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 276 | const RegisterPrefixSuccessCallback& onSuccess, |
| 277 | const RegisterPrefixFailureCallback& onFailure, |
| 278 | const security::SigningInfo& signingInfo, |
| 279 | uint64_t flags) |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 280 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 281 | nfd::CommandOptions options; |
| 282 | options.setSigningInfo(signingInfo); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 283 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 284 | return m_impl->registerPrefix(prefix, nullptr, onSuccess, onFailure, flags, options); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 287 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 288 | Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 289 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 290 | IO_CAPTURE_WEAK_IMPL(post) { |
| 291 | impl->asyncUnregisterPrefix(registeredPrefixId, nullptr, nullptr); |
| 292 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 295 | void |
| 296 | Face::unsetInterestFilter(const InterestFilterId* interestFilterId) |
| 297 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 298 | IO_CAPTURE_WEAK_IMPL(post) { |
| 299 | impl->asyncUnsetInterestFilter(interestFilterId); |
| 300 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 301 | } |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 302 | |
| 303 | void |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 304 | Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId, |
| 305 | const UnregisterPrefixSuccessCallback& onSuccess, |
| 306 | const UnregisterPrefixFailureCallback& onFailure) |
| 307 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 308 | IO_CAPTURE_WEAK_IMPL(post) { |
| 309 | impl->asyncUnregisterPrefix(registeredPrefixId, onSuccess, onFailure); |
| 310 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 313 | void |
Junxiao Shi | 2ed9e07 | 2017-08-13 16:45:48 +0000 | [diff] [blame] | 314 | Face::doProcessEvents(time::milliseconds timeout, bool keepThread) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 315 | { |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 316 | if (m_ioService.stopped()) { |
| 317 | m_ioService.reset(); // ensure that run()/poll() will do some work |
| 318 | } |
| 319 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 320 | try { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 321 | if (timeout < time::milliseconds::zero()) { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 322 | // do not block if timeout is negative, but process pending events |
| 323 | m_ioService.poll(); |
| 324 | return; |
| 325 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 326 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 327 | if (timeout > time::milliseconds::zero()) { |
| 328 | boost::asio::io_service& ioService = m_ioService; |
| 329 | unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork; |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 330 | m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.scheduleEvent(timeout, |
| 331 | [&ioService, &work] { |
| 332 | ioService.stop(); |
| 333 | work.reset(); |
| 334 | }); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 335 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 336 | |
| 337 | if (keepThread) { |
| 338 | // work will ensure that m_ioService is running until work object exists |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 339 | 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] | 340 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 341 | |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 342 | m_ioService.run(); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 343 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 344 | catch (...) { |
| 345 | m_impl->m_ioServiceWork.reset(); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 346 | m_impl->m_pendingInterestTable.clear(); |
| 347 | m_impl->m_registeredPrefixTable.clear(); |
| 348 | throw; |
| 349 | } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 352 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 353 | Face::shutdown() |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 354 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 355 | IO_CAPTURE_WEAK_IMPL(post) { |
| 356 | this->asyncShutdown(); |
| 357 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 7dced46 | 2014-03-19 15:12:32 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void |
| 361 | Face::asyncShutdown() |
| 362 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 363 | m_impl->m_pendingInterestTable.clear(); |
| 364 | m_impl->m_registeredPrefixTable.clear(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 365 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 366 | if (m_transport->isConnected()) |
| 367 | m_transport->close(); |
| 368 | |
Alexander Afanasyev | 1f5486e | 2014-07-10 17:45:49 -0700 | [diff] [blame] | 369 | m_impl->m_ioServiceWork.reset(); |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 372 | /** |
| 373 | * @brief extract local fields from NDNLPv2 packet and tag onto a network layer packet |
| 374 | */ |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 375 | template<typename NetPkt> |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 376 | static void |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 377 | extractLpLocalFields(NetPkt& netPacket, const lp::Packet& lpPacket) |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 378 | { |
| 379 | if (lpPacket.has<lp::IncomingFaceIdField>()) { |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 380 | netPacket.setTag(make_shared<lp::IncomingFaceIdTag>(lpPacket.get<lp::IncomingFaceIdField>())); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 381 | } |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 382 | |
| 383 | if (lpPacket.has<lp::CongestionMarkField>()) { |
| 384 | netPacket.setTag(make_shared<lp::CongestionMarkTag>(lpPacket.get<lp::CongestionMarkField>())); |
| 385 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 388 | void |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 389 | Face::onReceiveElement(const Block& blockFromDaemon) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 390 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 391 | lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet, |
| 392 | // no need to distinguish |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 393 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 394 | Buffer::const_iterator begin, end; |
| 395 | std::tie(begin, end) = lpPacket.get<lp::FragmentField>(); |
| 396 | Block netPacket(&*begin, std::distance(begin, end)); |
| 397 | switch (netPacket.type()) { |
| 398 | case tlv::Interest: { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 399 | auto interest = make_shared<Interest>(netPacket); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 400 | if (lpPacket.has<lp::NackField>()) { |
| 401 | auto nack = make_shared<lp::Nack>(std::move(*interest)); |
| 402 | nack->setHeader(lpPacket.get<lp::NackField>()); |
| 403 | extractLpLocalFields(*nack, lpPacket); |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 404 | NDN_LOG_DEBUG(">N " << nack->getInterest() << '~' << nack->getHeader().getReason()); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 405 | m_impl->nackPendingInterests(*nack); |
| 406 | } |
| 407 | else { |
| 408 | extractLpLocalFields(*interest, lpPacket); |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 409 | NDN_LOG_DEBUG(">I " << *interest); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 410 | m_impl->processInterestFilters(*interest); |
| 411 | } |
| 412 | break; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 413 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 414 | case tlv::Data: { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 415 | auto data = make_shared<Data>(netPacket); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 416 | extractLpLocalFields(*data, lpPacket); |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 417 | NDN_LOG_DEBUG(">D " << data->getName()); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 418 | m_impl->satisfyPendingInterests(*data); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 419 | break; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 420 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 421 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 424 | } // namespace ndn |