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