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