Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [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 | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 26 | #include "security/signing-helpers.hpp" |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 27 | #include "util/time.hpp" |
| 28 | #include "util/random.hpp" |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 29 | #include "util/face-uri.hpp" |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 30 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 31 | // A callback scheduled through io.post and io.dispatch may be invoked after the face |
| 32 | // is destructed. To prevent this situation, these macros captures Face::m_impl as weak_ptr, |
| 33 | // and skips callback execution if the face has been destructed. |
| 34 | #define IO_CAPTURE_WEAK_IMPL(OP) \ |
| 35 | { \ |
| 36 | weak_ptr<Impl> implWeak(m_impl); \ |
| 37 | m_ioService.OP([=] { \ |
| 38 | auto impl = implWeak.lock(); \ |
| 39 | if (impl != nullptr) { |
| 40 | #define IO_CAPTURE_WEAK_IMPL_END \ |
| 41 | } \ |
| 42 | }); \ |
| 43 | } |
| 44 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 45 | namespace ndn { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 46 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 47 | Face::Face(shared_ptr<Transport> transport) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 48 | : m_internalIoService(new boost::asio::io_service()) |
| 49 | , m_ioService(*m_internalIoService) |
| 50 | , m_internalKeyChain(new KeyChain()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 51 | , m_impl(make_shared<Impl>(*this)) |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 52 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 53 | construct(transport, *m_internalKeyChain); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 56 | Face::Face(boost::asio::io_service& ioService) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 57 | : m_ioService(ioService) |
| 58 | , m_internalKeyChain(new KeyChain()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 59 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 60 | { |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 61 | construct(nullptr, *m_internalKeyChain); |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 64 | Face::Face(const std::string& host, const std::string& port) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 65 | : m_internalIoService(new boost::asio::io_service()) |
| 66 | , m_ioService(*m_internalIoService) |
| 67 | , m_internalKeyChain(new KeyChain()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 68 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 69 | { |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 70 | construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 73 | Face::Face(shared_ptr<Transport> transport, KeyChain& keyChain) |
| 74 | : m_internalIoService(new boost::asio::io_service()) |
| 75 | , m_ioService(*m_internalIoService) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 76 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 77 | { |
| 78 | construct(transport, keyChain); |
| 79 | } |
| 80 | |
| 81 | Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 82 | : m_ioService(ioService) |
| 83 | , m_internalKeyChain(new KeyChain()) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 84 | , m_impl(make_shared<Impl>(*this)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 85 | { |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 86 | construct(transport, *m_internalKeyChain); |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Alexander Afanasyev | 8cf1c56 | 2016-06-23 16:01:55 -0700 | [diff] [blame] | 89 | 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] | 90 | : m_ioService(ioService) |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 91 | , m_impl(make_shared<Impl>(*this)) |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 92 | { |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 93 | construct(transport, keyChain); |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 96 | shared_ptr<Transport> |
| 97 | Face::makeDefaultTransport() |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 98 | { |
| 99 | // transport=unix:///var/run/nfd.sock |
| 100 | // transport=tcp://localhost:6363 |
| 101 | |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 102 | std::string transportUri; |
| 103 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 104 | const char* transportEnviron = getenv("NDN_CLIENT_TRANSPORT"); |
| 105 | if (transportEnviron != nullptr) { |
| 106 | transportUri = transportEnviron; |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 107 | } |
| 108 | else { |
| 109 | ConfigFile config; |
| 110 | transportUri = config.getParsedConfiguration().get<std::string>("transport", ""); |
| 111 | } |
| 112 | |
| 113 | if (transportUri.empty()) { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 114 | // transport not specified, use default Unix transport. |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 115 | return UnixTransport::create(""); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 116 | } |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 117 | |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 118 | std::string protocol; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 119 | try { |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 120 | util::FaceUri uri(transportUri); |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 121 | protocol = uri.getScheme(); |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 122 | |
| 123 | if (protocol == "unix") { |
| 124 | return UnixTransport::create(transportUri); |
| 125 | } |
| 126 | else if (protocol == "tcp" || protocol == "tcp4" || protocol == "tcp6") { |
| 127 | return TcpTransport::create(transportUri); |
| 128 | } |
| 129 | else { |
| 130 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unsupported transport protocol \"" + protocol + "\"")); |
| 131 | } |
| 132 | } |
| 133 | catch (const Transport::Error& error) { |
| 134 | BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what())); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 135 | } |
| 136 | catch (const util::FaceUri::Error& error) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 137 | BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what())); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 138 | } |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 142 | Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 143 | { |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 144 | if (transport == nullptr) { |
| 145 | transport = makeDefaultTransport(); |
| 146 | } |
| 147 | BOOST_ASSERT(transport != nullptr); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 148 | m_transport = transport; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 150 | m_nfdController.reset(new nfd::Controller(*this, keyChain)); |
| 151 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 152 | IO_CAPTURE_WEAK_IMPL(post) { |
| 153 | impl->ensureConnected(false); |
| 154 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Joao Pereira | 68c0d88 | 2015-05-19 14:27:55 -0400 | [diff] [blame] | 157 | Face::~Face() = default; |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 158 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 159 | shared_ptr<Transport> |
| 160 | Face::getTransport() |
| 161 | { |
| 162 | return m_transport; |
| 163 | } |
| 164 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 165 | const PendingInterestId* |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 166 | Face::expressInterest(const Interest& interest, |
| 167 | const DataCallback& afterSatisfied, |
| 168 | const NackCallback& afterNacked, |
| 169 | const TimeoutCallback& afterTimeout) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 170 | { |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 171 | shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 172 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 173 | // Use `interestToExpress` to avoid wire format creation for the original Interest |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 174 | if (interestToExpress->wireEncode().size() > MAX_NDN_PACKET_SIZE) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 175 | BOOST_THROW_EXCEPTION(Error("Interest size exceeds maximum limit")); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 176 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | 4e50b97 | 2014-03-25 10:57:50 -0700 | [diff] [blame] | 178 | // If the same ioService thread, dispatch directly calls the method |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 179 | IO_CAPTURE_WEAK_IMPL(dispatch) { |
| 180 | impl->asyncExpressInterest(interestToExpress, afterSatisfied, afterNacked, afterTimeout); |
| 181 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 182 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 183 | return reinterpret_cast<const PendingInterestId*>(interestToExpress.get()); |
| 184 | } |
| 185 | |
| 186 | const PendingInterestId* |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 187 | Face::expressInterest(const Interest& interest, |
| 188 | const OnData& onData, |
| 189 | const OnTimeout& onTimeout) |
| 190 | { |
| 191 | return this->expressInterest( |
| 192 | interest, |
| 193 | [onData] (const Interest& interest, const Data& data) { |
| 194 | if (onData != nullptr) { |
| 195 | onData(interest, const_cast<Data&>(data)); |
| 196 | } |
| 197 | }, |
| 198 | [onTimeout] (const Interest& interest, const lp::Nack& nack) { |
| 199 | if (onTimeout != nullptr) { |
| 200 | onTimeout(interest); |
| 201 | } |
| 202 | }, |
| 203 | onTimeout |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | const PendingInterestId* |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 208 | Face::expressInterest(const Name& name, const Interest& tmpl, |
| 209 | const OnData& onData, const OnTimeout& onTimeout) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 210 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 211 | return expressInterest(Interest(tmpl).setName(name).setNonce(0), |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 212 | onData, onTimeout); |
| 213 | } |
| 214 | |
| 215 | void |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 216 | Face::removePendingInterest(const PendingInterestId* pendingInterestId) |
| 217 | { |
| 218 | IO_CAPTURE_WEAK_IMPL(post) { |
| 219 | impl->asyncRemovePendingInterest(pendingInterestId); |
| 220 | } IO_CAPTURE_WEAK_IMPL_END |
| 221 | } |
| 222 | |
| 223 | void |
| 224 | Face::removeAllPendingInterests() |
| 225 | { |
| 226 | IO_CAPTURE_WEAK_IMPL(post) { |
| 227 | impl->asyncRemoveAllPendingInterests(); |
| 228 | } IO_CAPTURE_WEAK_IMPL_END |
| 229 | } |
| 230 | |
| 231 | size_t |
| 232 | Face::getNPendingInterests() const |
| 233 | { |
| 234 | return m_impl->m_pendingInterestTable.size(); |
| 235 | } |
| 236 | |
| 237 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 238 | Face::put(const Data& data) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 239 | { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 240 | Block wire = data.wireEncode(); |
| 241 | |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 242 | lp::Packet packet; |
| 243 | bool hasLpFields = false; |
| 244 | |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 245 | shared_ptr<lp::CachePolicyTag> cachePolicyTag = data.getTag<lp::CachePolicyTag>(); |
| 246 | if (cachePolicyTag != nullptr) { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 247 | packet.add<lp::CachePolicyField>(*cachePolicyTag); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 248 | hasLpFields = true; |
| 249 | } |
| 250 | |
| 251 | shared_ptr<lp::CongestionMarkTag> congestionMarkTag = data.getTag<lp::CongestionMarkTag>(); |
| 252 | if (congestionMarkTag != nullptr) { |
| 253 | packet.add<lp::CongestionMarkField>(*congestionMarkTag); |
| 254 | hasLpFields = true; |
| 255 | } |
| 256 | |
| 257 | if (hasLpFields) { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 258 | packet.add<lp::FragmentField>(std::make_pair(wire.begin(), wire.end())); |
| 259 | wire = packet.wireEncode(); |
| 260 | } |
| 261 | |
| 262 | if (wire.size() > MAX_NDN_PACKET_SIZE) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 263 | BOOST_THROW_EXCEPTION(Error("Data size exceeds maximum limit")); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 264 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 265 | IO_CAPTURE_WEAK_IMPL(dispatch) { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 266 | impl->asyncSend(wire); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 267 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 270 | void |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 271 | Face::put(const lp::Nack& nack) |
| 272 | { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 273 | lp::Packet packet; |
| 274 | packet.add<lp::NackField>(nack.getHeader()); |
| 275 | const Block& interestWire = nack.getInterest().wireEncode(); |
| 276 | packet.add<lp::FragmentField>(std::make_pair(interestWire.begin(), interestWire.end())); |
| 277 | |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 278 | shared_ptr<lp::CongestionMarkTag> congestionMarkTag = nack.getTag<lp::CongestionMarkTag>(); |
| 279 | if (congestionMarkTag != nullptr) { |
| 280 | packet.add<lp::CongestionMarkField>(*congestionMarkTag); |
| 281 | } |
| 282 | |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 283 | Block wire = packet.wireEncode(); |
| 284 | |
| 285 | if (wire.size() > MAX_NDN_PACKET_SIZE) |
| 286 | BOOST_THROW_EXCEPTION(Error("Nack size exceeds maximum limit")); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 287 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 288 | IO_CAPTURE_WEAK_IMPL(dispatch) { |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 289 | impl->asyncSend(wire); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 290 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 6fcdde2 | 2014-08-22 19:03:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 293 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 294 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 295 | const InterestCallback& onInterest, |
| 296 | const RegisterPrefixFailureCallback& onFailure, |
| 297 | const security::SigningInfo& signingInfo, |
| 298 | uint64_t flags) |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 299 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 300 | return setInterestFilter(interestFilter, onInterest, nullptr, onFailure, signingInfo, flags); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 304 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 305 | const InterestCallback& onInterest, |
| 306 | const RegisterPrefixSuccessCallback& onSuccess, |
| 307 | const RegisterPrefixFailureCallback& onFailure, |
| 308 | const security::SigningInfo& signingInfo, |
| 309 | uint64_t flags) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 310 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 311 | auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 312 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 313 | nfd::CommandOptions options; |
| 314 | options.setSigningInfo(signingInfo); |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 315 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 316 | return m_impl->registerPrefix(interestFilter.getPrefix(), filter, |
| 317 | onSuccess, onFailure, flags, options); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 320 | const InterestFilterId* |
| 321 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 322 | const InterestCallback& onInterest) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 323 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 324 | auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 325 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 326 | IO_CAPTURE_WEAK_IMPL(post) { |
| 327 | impl->asyncSetInterestFilter(filter); |
| 328 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 329 | |
| 330 | return reinterpret_cast<const InterestFilterId*>(filter.get()); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 333 | #ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING |
| 334 | |
| 335 | const RegisteredPrefixId* |
| 336 | Face::setInterestFilter(const InterestFilter& interestFilter, |
| 337 | const OnInterest& onInterest, |
| 338 | const RegisterPrefixSuccessCallback& onSuccess, |
| 339 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 340 | const security::v1::IdentityCertificate& certificate, |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 341 | uint64_t flags) |
| 342 | { |
| 343 | security::SigningInfo signingInfo; |
| 344 | if (!certificate.getName().empty()) { |
| 345 | signingInfo = signingByCertificate(certificate.getName()); |
| 346 | } |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 347 | return setInterestFilter(interestFilter, onInterest, onSuccess, onFailure, signingInfo, flags); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | const RegisteredPrefixId* |
| 351 | Face::setInterestFilter(const InterestFilter& interestFilter, |
| 352 | const OnInterest& onInterest, |
| 353 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 354 | const security::v1::IdentityCertificate& certificate, |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 355 | uint64_t flags) |
| 356 | { |
| 357 | security::SigningInfo signingInfo; |
| 358 | if (!certificate.getName().empty()) { |
| 359 | signingInfo = signingByCertificate(certificate.getName()); |
| 360 | } |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 361 | return setInterestFilter(interestFilter, onInterest, onFailure, signingInfo, flags); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | const RegisteredPrefixId* |
| 365 | Face::setInterestFilter(const InterestFilter& interestFilter, |
| 366 | const OnInterest& onInterest, |
| 367 | const RegisterPrefixSuccessCallback& onSuccess, |
| 368 | const RegisterPrefixFailureCallback& onFailure, |
| 369 | const Name& identity, |
| 370 | uint64_t flags) |
| 371 | { |
| 372 | security::SigningInfo signingInfo = signingByIdentity(identity); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 373 | return setInterestFilter(interestFilter, onInterest, |
| 374 | onSuccess, onFailure, |
| 375 | signingInfo, flags); |
| 376 | } |
| 377 | |
| 378 | const RegisteredPrefixId* |
| 379 | Face::setInterestFilter(const InterestFilter& interestFilter, |
| 380 | const OnInterest& onInterest, |
| 381 | const RegisterPrefixFailureCallback& onFailure, |
| 382 | const Name& identity, |
| 383 | uint64_t flags) |
| 384 | { |
| 385 | security::SigningInfo signingInfo = signingByIdentity(identity); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 386 | return setInterestFilter(interestFilter, onInterest, onFailure, signingInfo, flags); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | #endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING |
| 390 | |
| 391 | const RegisteredPrefixId* |
| 392 | Face::registerPrefix(const Name& prefix, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 393 | const RegisterPrefixSuccessCallback& onSuccess, |
| 394 | const RegisterPrefixFailureCallback& onFailure, |
| 395 | const security::SigningInfo& signingInfo, |
| 396 | uint64_t flags) |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 397 | { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 398 | nfd::CommandOptions options; |
| 399 | options.setSigningInfo(signingInfo); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 400 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 401 | return m_impl->registerPrefix(prefix, nullptr, onSuccess, onFailure, flags, options); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | #ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 405 | const RegisteredPrefixId* |
| 406 | Face::registerPrefix(const Name& prefix, |
| 407 | const RegisterPrefixSuccessCallback& onSuccess, |
| 408 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 409 | const security::v1::IdentityCertificate& certificate, |
Alexander Afanasyev | 0866f51 | 2014-08-11 13:25:09 -0700 | [diff] [blame] | 410 | uint64_t flags) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 411 | { |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 412 | security::SigningInfo signingInfo; |
Junxiao Shi | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 413 | if (!certificate.getName().empty()) { |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 414 | signingInfo = signingByCertificate(certificate.getName()); |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 415 | } |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 416 | return registerPrefix(prefix, onSuccess, onFailure, signingInfo, flags); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | const RegisteredPrefixId* |
| 420 | Face::registerPrefix(const Name& prefix, |
| 421 | const RegisterPrefixSuccessCallback& onSuccess, |
| 422 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 0866f51 | 2014-08-11 13:25:09 -0700 | [diff] [blame] | 423 | const Name& identity, |
| 424 | uint64_t flags) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 425 | { |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 426 | security::SigningInfo signingInfo = signingByIdentity(identity); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 427 | return registerPrefix(prefix, onSuccess, onFailure, signingInfo, flags); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 428 | } |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 429 | #endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 430 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 431 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 432 | Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 433 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 434 | IO_CAPTURE_WEAK_IMPL(post) { |
| 435 | impl->asyncUnregisterPrefix(registeredPrefixId, nullptr, nullptr); |
| 436 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 439 | void |
| 440 | Face::unsetInterestFilter(const InterestFilterId* interestFilterId) |
| 441 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 442 | IO_CAPTURE_WEAK_IMPL(post) { |
| 443 | impl->asyncUnsetInterestFilter(interestFilterId); |
| 444 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 445 | } |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 446 | |
| 447 | void |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 448 | Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId, |
| 449 | const UnregisterPrefixSuccessCallback& onSuccess, |
| 450 | const UnregisterPrefixFailureCallback& onFailure) |
| 451 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 452 | IO_CAPTURE_WEAK_IMPL(post) { |
| 453 | impl->asyncUnregisterPrefix(registeredPrefixId, onSuccess, onFailure); |
| 454 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 457 | void |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 458 | Face::doProcessEvents(const time::milliseconds& timeout, bool keepThread) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 459 | { |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 460 | if (m_ioService.stopped()) { |
| 461 | m_ioService.reset(); // ensure that run()/poll() will do some work |
| 462 | } |
| 463 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 464 | try { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 465 | if (timeout < time::milliseconds::zero()) { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 466 | // do not block if timeout is negative, but process pending events |
| 467 | m_ioService.poll(); |
| 468 | return; |
| 469 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 470 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 471 | if (timeout > time::milliseconds::zero()) { |
| 472 | boost::asio::io_service& ioService = m_ioService; |
| 473 | unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork; |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 474 | m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.scheduleEvent(timeout, |
| 475 | [&ioService, &work] { |
| 476 | ioService.stop(); |
| 477 | work.reset(); |
| 478 | }); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 479 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 480 | |
| 481 | if (keepThread) { |
| 482 | // work will ensure that m_ioService is running until work object exists |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 483 | 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] | 484 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 485 | |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame] | 486 | m_ioService.run(); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 487 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 488 | catch (...) { |
| 489 | m_impl->m_ioServiceWork.reset(); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 490 | m_impl->m_pendingInterestTable.clear(); |
| 491 | m_impl->m_registeredPrefixTable.clear(); |
| 492 | throw; |
| 493 | } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 496 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 497 | Face::shutdown() |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 498 | { |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 499 | IO_CAPTURE_WEAK_IMPL(post) { |
| 500 | this->asyncShutdown(); |
| 501 | } IO_CAPTURE_WEAK_IMPL_END |
Alexander Afanasyev | 7dced46 | 2014-03-19 15:12:32 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | void |
| 505 | Face::asyncShutdown() |
| 506 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 507 | m_impl->m_pendingInterestTable.clear(); |
| 508 | m_impl->m_registeredPrefixTable.clear(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 509 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 510 | if (m_transport->isConnected()) |
| 511 | m_transport->close(); |
| 512 | |
Alexander Afanasyev | 1f5486e | 2014-07-10 17:45:49 -0700 | [diff] [blame] | 513 | m_impl->m_ioServiceWork.reset(); |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 516 | /** |
| 517 | * @brief extract local fields from NDNLPv2 packet and tag onto a network layer packet |
| 518 | */ |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 519 | template<typename NetPkt> |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 520 | static void |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 521 | extractLpLocalFields(NetPkt& netPacket, const lp::Packet& lpPacket) |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 522 | { |
| 523 | if (lpPacket.has<lp::IncomingFaceIdField>()) { |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 524 | netPacket.setTag(make_shared<lp::IncomingFaceIdTag>(lpPacket.get<lp::IncomingFaceIdField>())); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 525 | } |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 526 | |
| 527 | if (lpPacket.has<lp::CongestionMarkField>()) { |
| 528 | netPacket.setTag(make_shared<lp::CongestionMarkTag>(lpPacket.get<lp::CongestionMarkField>())); |
| 529 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 532 | void |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 533 | Face::onReceiveElement(const Block& blockFromDaemon) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 534 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 535 | lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet, |
| 536 | // no need to distinguish |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 537 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 538 | Buffer::const_iterator begin, end; |
| 539 | std::tie(begin, end) = lpPacket.get<lp::FragmentField>(); |
| 540 | Block netPacket(&*begin, std::distance(begin, end)); |
| 541 | switch (netPacket.type()) { |
| 542 | case tlv::Interest: { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 543 | auto interest = make_shared<Interest>(netPacket); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 544 | if (lpPacket.has<lp::NackField>()) { |
| 545 | auto nack = make_shared<lp::Nack>(std::move(*interest)); |
| 546 | nack->setHeader(lpPacket.get<lp::NackField>()); |
| 547 | extractLpLocalFields(*nack, lpPacket); |
| 548 | m_impl->nackPendingInterests(*nack); |
| 549 | } |
| 550 | else { |
| 551 | extractLpLocalFields(*interest, lpPacket); |
| 552 | m_impl->processInterestFilters(*interest); |
| 553 | } |
| 554 | break; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 555 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 556 | case tlv::Data: { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 557 | auto data = make_shared<Data>(netPacket); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 558 | extractLpLocalFields(*data, lpPacket); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 559 | m_impl->satisfyPendingInterests(*data); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 560 | break; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 561 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 562 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 563 | } |
| 564 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 565 | } // namespace ndn |