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