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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 24 | #include "face.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 25 | #include "detail/face-impl.hpp" |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 26 | |
Junxiao Shi | 468abc3 | 2014-11-04 09:12:47 -0700 | [diff] [blame] | 27 | #include "encoding/tlv.hpp" |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 28 | #include "security/key-chain.hpp" |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 29 | #include "util/time.hpp" |
| 30 | #include "util/random.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()) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 38 | , m_isDirectNfdFibManagementRequested(false) |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 39 | , m_impl(new Impl(*this)) |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 40 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 41 | const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config); |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 42 | construct(make_shared<UnixTransport>(socketName), |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 43 | m_internalKeyChain); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 46 | Face::Face(boost::asio::io_service& ioService) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 47 | : m_ioService(ioService) |
| 48 | , m_internalKeyChain(new KeyChain()) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 49 | , m_isDirectNfdFibManagementRequested(false) |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 50 | , m_impl(new Impl(*this)) |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 51 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 52 | const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config); |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 53 | construct(make_shared<UnixTransport>(socketName), |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 54 | m_internalKeyChain); |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 57 | Face::Face(const std::string& host, const std::string& port/* = "6363"*/) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 58 | : m_internalIoService(new boost::asio::io_service()) |
| 59 | , m_ioService(*m_internalIoService) |
| 60 | , m_internalKeyChain(new KeyChain()) |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 61 | , m_impl(new Impl(*this)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 62 | { |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 63 | construct(make_shared<TcpTransport>(host, port), |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 64 | m_internalKeyChain); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 67 | Face::Face(const shared_ptr<Transport>& transport) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 68 | : m_internalIoService(new boost::asio::io_service()) |
| 69 | , m_ioService(*m_internalIoService) |
| 70 | , m_internalKeyChain(new KeyChain()) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 71 | , m_isDirectNfdFibManagementRequested(false) |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 72 | , m_impl(new Impl(*this)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 73 | { |
| 74 | construct(transport, |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 75 | m_internalKeyChain); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Face::Face(const shared_ptr<Transport>& transport, |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 79 | boost::asio::io_service& ioService) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 80 | : m_ioService(ioService) |
| 81 | , m_internalKeyChain(new KeyChain()) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 82 | , m_isDirectNfdFibManagementRequested(false) |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 83 | , m_impl(new Impl(*this)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 84 | { |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 85 | construct(transport, |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 86 | m_internalKeyChain); |
| 87 | } |
| 88 | |
| 89 | Face::Face(shared_ptr<Transport> transport, |
| 90 | boost::asio::io_service& ioService, |
| 91 | KeyChain& keyChain) |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 92 | : m_ioService(ioService) |
| 93 | , m_internalKeyChain(nullptr) |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 94 | , m_isDirectNfdFibManagementRequested(false) |
| 95 | , m_impl(new Impl(*this)) |
| 96 | { |
| 97 | construct(transport, |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 98 | &keyChain); |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 101 | void |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 102 | Face::construct(shared_ptr<Transport> transport, |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 103 | KeyChain* keyChain) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 104 | { |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 105 | m_nfdController = new nfd::Controller(*this, *keyChain); |
| 106 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 107 | m_impl->m_pitTimeoutCheckTimerActive = false; |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 108 | m_transport = transport; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 109 | |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 110 | m_impl->m_pitTimeoutCheckTimer = make_shared<monotonic_deadline_timer>(ref(m_ioService)); |
| 111 | m_impl->m_processEventsTimeoutTimer = make_shared<monotonic_deadline_timer>(ref(m_ioService)); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 112 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 113 | std::string protocol = "nrd-0.1"; |
| 114 | |
| 115 | try |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 116 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 117 | protocol = m_impl->m_config.getParsedConfiguration().get<std::string>("protocol"); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 118 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 119 | catch (boost::property_tree::ptree_bad_path& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 120 | { |
| 121 | // protocol not specified |
| 122 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 123 | catch (boost::property_tree::ptree_bad_data& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 124 | { |
| 125 | throw ConfigFile::Error(error.what()); |
| 126 | } |
| 127 | |
| 128 | if (isSupportedNrdProtocol(protocol)) |
| 129 | { |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 130 | // do nothing |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 131 | } |
Steve DiBenedetto | acab880 | 2014-03-24 11:15:57 -0600 | [diff] [blame] | 132 | else if (isSupportedNfdProtocol(protocol)) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 133 | { |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 134 | m_isDirectNfdFibManagementRequested = true; |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 135 | } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 136 | else |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 137 | { |
| 138 | throw Face::Error("Cannot create controller for unsupported protocol \"" + protocol + "\""); |
| 139 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 142 | Face::~Face() |
| 143 | { |
| 144 | if (m_internalKeyChain != nullptr) { |
| 145 | delete m_internalKeyChain; |
| 146 | } |
| 147 | |
| 148 | delete m_nfdController; |
| 149 | delete m_impl; |
| 150 | } |
| 151 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 152 | const PendingInterestId* |
| 153 | Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout) |
| 154 | { |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 155 | shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 156 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 157 | // Use `interestToExpress` to avoid wire format creation for the original Interest |
| 158 | if (interestToExpress->wireEncode().size() > MAX_NDN_PACKET_SIZE) |
| 159 | throw Error("Interest size exceeds maximum limit"); |
| 160 | |
Alexander Afanasyev | 4e50b97 | 2014-03-25 10:57:50 -0700 | [diff] [blame] | 161 | // If the same ioService thread, dispatch directly calls the method |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 162 | m_ioService.dispatch(bind(&Impl::asyncExpressInterest, m_impl, |
| 163 | interestToExpress, onData, onTimeout)); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 165 | return reinterpret_cast<const PendingInterestId*>(interestToExpress.get()); |
| 166 | } |
| 167 | |
| 168 | const PendingInterestId* |
| 169 | Face::expressInterest(const Name& name, |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 170 | const Interest& tmpl, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 171 | const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/) |
| 172 | { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 173 | return expressInterest(Interest(tmpl) |
| 174 | .setName(name) |
| 175 | .setNonce(0), |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 176 | onData, onTimeout); |
| 177 | } |
| 178 | |
| 179 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 180 | Face::put(const Data& data) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 181 | { |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 182 | // Use original `data`, since wire format should already exist for the original Data |
| 183 | if (data.wireEncode().size() > MAX_NDN_PACKET_SIZE) |
| 184 | throw Error("Data size exceeds maximum limit"); |
| 185 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 186 | shared_ptr<const Data> dataPtr; |
| 187 | try { |
| 188 | dataPtr = data.shared_from_this(); |
| 189 | } |
| 190 | catch (const bad_weak_ptr& e) { |
| 191 | std::cerr << "Face::put WARNING: the supplied Data should be created using make_shared<Data>()" |
| 192 | << std::endl; |
| 193 | dataPtr = make_shared<Data>(data); |
| 194 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 195 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 196 | // If the same ioService thread, dispatch directly calls the method |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 197 | m_ioService.dispatch(bind(&Impl::asyncPutData, m_impl, dataPtr)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 200 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 201 | Face::removePendingInterest(const PendingInterestId* pendingInterestId) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 202 | { |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 203 | m_ioService.post(bind(&Impl::asyncRemovePendingInterest, m_impl, pendingInterestId)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Alexander Afanasyev | 6fcdde2 | 2014-08-22 19:03:36 -0700 | [diff] [blame] | 206 | size_t |
| 207 | Face::getNPendingInterests() const |
| 208 | { |
| 209 | return m_impl->m_pendingInterestTable.size(); |
| 210 | } |
| 211 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 212 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 213 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 214 | const OnInterest& onInterest, |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 215 | const RegisterPrefixSuccessCallback& onSuccess, |
| 216 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 0866f51 | 2014-08-11 13:25:09 -0700 | [diff] [blame] | 217 | const IdentityCertificate& certificate, |
| 218 | uint64_t flags) |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 219 | { |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 220 | shared_ptr<InterestFilterRecord> filter = |
| 221 | make_shared<InterestFilterRecord>(interestFilter, onInterest); |
| 222 | |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 223 | nfd::CommandOptions options; |
| 224 | if (certificate.getName().empty()) { |
| 225 | options.setSigningDefault(); |
| 226 | } |
| 227 | else { |
| 228 | options.setSigningCertificate(certificate); |
| 229 | } |
| 230 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 231 | return m_impl->registerPrefix(interestFilter.getPrefix(), filter, |
| 232 | onSuccess, onFailure, |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 233 | flags, options); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 237 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 238 | const OnInterest& onInterest, |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 239 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 0866f51 | 2014-08-11 13:25:09 -0700 | [diff] [blame] | 240 | const IdentityCertificate& certificate, |
| 241 | uint64_t flags) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 242 | { |
| 243 | shared_ptr<InterestFilterRecord> filter = |
| 244 | make_shared<InterestFilterRecord>(interestFilter, onInterest); |
| 245 | |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 246 | nfd::CommandOptions options; |
| 247 | if (certificate.getName().empty()) { |
| 248 | options.setSigningDefault(); |
| 249 | } |
| 250 | else { |
| 251 | options.setSigningCertificate(certificate); |
| 252 | } |
| 253 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 254 | return m_impl->registerPrefix(interestFilter.getPrefix(), filter, |
| 255 | RegisterPrefixSuccessCallback(), onFailure, |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 256 | flags, options); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | const RegisteredPrefixId* |
| 260 | Face::setInterestFilter(const InterestFilter& interestFilter, |
| 261 | const OnInterest& onInterest, |
| 262 | const RegisterPrefixSuccessCallback& onSuccess, |
| 263 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 0866f51 | 2014-08-11 13:25:09 -0700 | [diff] [blame] | 264 | const Name& identity, |
| 265 | uint64_t flags) |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 266 | { |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 267 | shared_ptr<InterestFilterRecord> filter = |
| 268 | make_shared<InterestFilterRecord>(interestFilter, onInterest); |
| 269 | |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 270 | nfd::CommandOptions options; |
| 271 | options.setSigningIdentity(identity); |
| 272 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 273 | return m_impl->registerPrefix(interestFilter.getPrefix(), filter, |
| 274 | onSuccess, onFailure, |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 275 | flags, options); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | const RegisteredPrefixId* |
| 279 | Face::setInterestFilter(const InterestFilter& interestFilter, |
| 280 | const OnInterest& onInterest, |
| 281 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 0866f51 | 2014-08-11 13:25:09 -0700 | [diff] [blame] | 282 | const Name& identity, |
| 283 | uint64_t flags) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 284 | { |
| 285 | shared_ptr<InterestFilterRecord> filter = |
| 286 | make_shared<InterestFilterRecord>(interestFilter, onInterest); |
| 287 | |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 288 | nfd::CommandOptions options; |
| 289 | options.setSigningIdentity(identity); |
| 290 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 291 | return m_impl->registerPrefix(interestFilter.getPrefix(), filter, |
| 292 | RegisterPrefixSuccessCallback(), onFailure, |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 293 | flags, options); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
| 297 | const InterestFilterId* |
| 298 | Face::setInterestFilter(const InterestFilter& interestFilter, |
| 299 | const OnInterest& onInterest) |
| 300 | { |
| 301 | shared_ptr<InterestFilterRecord> filter = |
| 302 | make_shared<InterestFilterRecord>(interestFilter, onInterest); |
| 303 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 304 | getIoService().post(bind(&Impl::asyncSetInterestFilter, m_impl, filter)); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 305 | |
| 306 | return reinterpret_cast<const InterestFilterId*>(filter.get()); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 309 | const RegisteredPrefixId* |
| 310 | Face::registerPrefix(const Name& prefix, |
| 311 | const RegisterPrefixSuccessCallback& onSuccess, |
| 312 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 0866f51 | 2014-08-11 13:25:09 -0700 | [diff] [blame] | 313 | const IdentityCertificate& certificate, |
| 314 | uint64_t flags) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 315 | { |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 316 | nfd::CommandOptions options; |
| 317 | if (certificate.getName().empty()) { |
| 318 | options.setSigningDefault(); |
| 319 | } |
| 320 | else { |
| 321 | options.setSigningCertificate(certificate); |
| 322 | } |
| 323 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 324 | return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(), |
| 325 | onSuccess, onFailure, |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 326 | flags, options); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | const RegisteredPrefixId* |
| 330 | Face::registerPrefix(const Name& prefix, |
| 331 | const RegisterPrefixSuccessCallback& onSuccess, |
| 332 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 0866f51 | 2014-08-11 13:25:09 -0700 | [diff] [blame] | 333 | const Name& identity, |
| 334 | uint64_t flags) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 335 | { |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 336 | nfd::CommandOptions options; |
| 337 | options.setSigningIdentity(identity); |
| 338 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 339 | return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(), |
| 340 | onSuccess, onFailure, |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 341 | flags, options); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 344 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 345 | Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 346 | { |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 347 | m_ioService.post(bind(&Impl::asyncUnregisterPrefix, m_impl, registeredPrefixId, |
| 348 | UnregisterPrefixSuccessCallback(), UnregisterPrefixFailureCallback())); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 351 | void |
| 352 | Face::unsetInterestFilter(const InterestFilterId* interestFilterId) |
| 353 | { |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 354 | m_ioService.post(bind(&Impl::asyncUnsetInterestFilter, m_impl, interestFilterId)); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 355 | } |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 356 | |
| 357 | void |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 358 | Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId, |
| 359 | const UnregisterPrefixSuccessCallback& onSuccess, |
| 360 | const UnregisterPrefixFailureCallback& onFailure) |
| 361 | { |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 362 | m_ioService.post(bind(&Impl::asyncUnregisterPrefix, m_impl, registeredPrefixId, |
| 363 | onSuccess, onFailure)); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 366 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 367 | Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/, |
| 368 | bool keepThread/* = false*/) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 369 | { |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 370 | try { |
| 371 | if (timeout < time::milliseconds::zero()) |
| 372 | { |
| 373 | // do not block if timeout is negative, but process pending events |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 374 | m_ioService.poll(); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 375 | return; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 376 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 377 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 378 | if (timeout > time::milliseconds::zero()) |
| 379 | { |
| 380 | m_impl->m_processEventsTimeoutTimer->expires_from_now(time::milliseconds(timeout)); |
| 381 | m_impl->m_processEventsTimeoutTimer->async_wait(&fireProcessEventsTimeout); |
| 382 | } |
| 383 | |
| 384 | if (keepThread) { |
| 385 | // work will ensure that m_ioService is running until work object exists |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 386 | m_impl->m_ioServiceWork = make_shared<boost::asio::io_service::work>(ref(m_ioService)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 387 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 388 | |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 389 | m_ioService.run(); |
| 390 | m_ioService.reset(); // so it is possible to run processEvents again (if necessary) |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 391 | } |
| 392 | catch (Face::ProcessEventsTimeout&) { |
| 393 | // break |
| 394 | m_impl->m_ioServiceWork.reset(); |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 395 | m_ioService.reset(); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 396 | } |
| 397 | catch (...) { |
| 398 | m_impl->m_ioServiceWork.reset(); |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 399 | m_ioService.reset(); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 400 | m_impl->m_pendingInterestTable.clear(); |
| 401 | m_impl->m_registeredPrefixTable.clear(); |
| 402 | throw; |
| 403 | } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 406 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 407 | Face::shutdown() |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 408 | { |
Junxiao Shi | 2cced06 | 2014-11-02 21:27:38 -0700 | [diff] [blame^] | 409 | m_ioService.post(bind(&Face::asyncShutdown, this)); |
Alexander Afanasyev | 7dced46 | 2014-03-19 15:12:32 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void |
| 413 | Face::asyncShutdown() |
| 414 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 415 | m_impl->m_pendingInterestTable.clear(); |
| 416 | m_impl->m_registeredPrefixTable.clear(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 417 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 418 | if (m_transport->isConnected()) |
| 419 | m_transport->close(); |
| 420 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 421 | m_impl->m_pitTimeoutCheckTimer->cancel(); |
| 422 | m_impl->m_processEventsTimeoutTimer->cancel(); |
| 423 | m_impl->m_pitTimeoutCheckTimerActive = false; |
Alexander Afanasyev | 1f5486e | 2014-07-10 17:45:49 -0700 | [diff] [blame] | 424 | |
| 425 | m_impl->m_ioServiceWork.reset(); |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 428 | void |
| 429 | Face::fireProcessEventsTimeout(const boost::system::error_code& error) |
| 430 | { |
| 431 | if (!error) // can fire for some other reason, e.g., cancelled |
| 432 | throw Face::ProcessEventsTimeout(); |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 433 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 434 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 435 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 436 | void |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 437 | Face::onReceiveElement(const Block& blockFromDaemon) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 438 | { |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 439 | const Block& block = nfd::LocalControlHeader::getPayload(blockFromDaemon); |
| 440 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 441 | if (block.type() == tlv::Interest) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 442 | { |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 443 | shared_ptr<Interest> interest = make_shared<Interest>(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 444 | interest->wireDecode(block); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 445 | if (&block != &blockFromDaemon) |
| 446 | interest->getLocalControlHeader().wireDecode(blockFromDaemon); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 447 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 448 | m_impl->processInterestFilters(*interest); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 449 | } |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 450 | else if (block.type() == tlv::Data) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 451 | { |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 452 | shared_ptr<Data> data = make_shared<Data>(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 453 | data->wireDecode(block); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 454 | if (&block != &blockFromDaemon) |
| 455 | data->getLocalControlHeader().wireDecode(blockFromDaemon); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 456 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 457 | m_impl->satisfyPendingInterests(*data); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 458 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 459 | if (m_impl->m_pendingInterestTable.empty()) { |
| 460 | m_impl->m_pitTimeoutCheckTimer->cancel(); // this will cause checkPitExpire invocation |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 461 | } |
| 462 | } |
Yingdi Yu | f9fa52f | 2014-02-06 12:27:32 -0800 | [diff] [blame] | 463 | // ignore any other type |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 466 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 467 | |
| 468 | } // namespace ndn |