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