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