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