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