Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -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 | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_DETAIL_FACE_IMPL_HPP |
| 23 | #define NDN_DETAIL_FACE_IMPL_HPP |
| 24 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 25 | #include "../face.hpp" |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 26 | #include "container-with-on-empty-signal.hpp" |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 27 | #include "pending-interest.hpp" |
| 28 | #include "registered-prefix.hpp" |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 29 | #include "../lp/packet.hpp" |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 30 | #include "../lp/tags.hpp" |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 31 | #include "../mgmt/nfd/command-options.hpp" |
| 32 | #include "../mgmt/nfd/controller.hpp" |
| 33 | #include "../transport/tcp-transport.hpp" |
| 34 | #include "../transport/unix-transport.hpp" |
| 35 | #include "../util/config-file.hpp" |
| 36 | #include "../util/logger.hpp" |
| 37 | #include "../util/scheduler.hpp" |
| 38 | #include "../util/signal.hpp" |
| 39 | |
| 40 | NDN_LOG_INIT(ndn.Face); |
| 41 | // INFO level: prefix registration, etc. |
| 42 | // |
| 43 | // DEBUG level: packet logging. |
| 44 | // Each log entry starts with a direction symbol ('<' denotes an outgoing packet, '>' denotes an |
| 45 | // incoming packet) and a packet type symbol ('I' denotes an Interest, 'D' denotes a Data, 'N' |
| 46 | // denotes a Nack). Interest is printed as its string representation, Data is printed as name only, |
| 47 | // Nack is printed as the Interest followed by the Nack reason and delimited by a '~' symbol. A |
| 48 | // log line about an incoming packet may be followed by zero or more lines about Interest matching |
| 49 | // InterestFilter, Data satisfying Interest, or Nack rejecting Interest, which are also written at |
| 50 | // DEBUG level. |
| 51 | // |
| 52 | // TRACE level: more detailed unstructured messages. |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 53 | |
| 54 | namespace ndn { |
| 55 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 56 | /** |
| 57 | * @brief implementation detail of Face |
| 58 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 59 | class Face::Impl : noncopyable |
| 60 | { |
| 61 | public: |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 62 | typedef ContainerWithOnEmptySignal<shared_ptr<PendingInterest>> PendingInterestTable; |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 63 | typedef std::list<shared_ptr<InterestFilterRecord>> InterestFilterTable; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 64 | typedef ContainerWithOnEmptySignal<shared_ptr<RegisteredPrefix>> RegisteredPrefixTable; |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 65 | |
| 66 | explicit |
| 67 | Impl(Face& face) |
| 68 | : m_face(face) |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 69 | , m_scheduler(m_face.getIoService()) |
| 70 | , m_processEventsTimeoutEvent(m_scheduler) |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 71 | { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 72 | auto postOnEmptyPitOrNoRegisteredPrefixes = [this] { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 73 | this->m_face.getIoService().post([this] { this->onEmptyPitOrNoRegisteredPrefixes(); }); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 74 | // without this extra "post", transport can get paused (-async_read) and then resumed |
| 75 | // (+async_read) from within onInterest/onData callback. After onInterest/onData |
| 76 | // finishes, there is another +async_read with the same memory block. A few of such |
| 77 | // async_read duplications can cause various effects and result in segfault. |
| 78 | }; |
| 79 | |
| 80 | m_pendingInterestTable.onEmpty.connect(postOnEmptyPitOrNoRegisteredPrefixes); |
| 81 | m_registeredPrefixTable.onEmpty.connect(postOnEmptyPitOrNoRegisteredPrefixes); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 84 | public: // consumer |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 85 | void |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 86 | asyncExpressInterest(shared_ptr<const Interest> interest, |
| 87 | const DataCallback& afterSatisfied, |
| 88 | const NackCallback& afterNacked, |
| 89 | const TimeoutCallback& afterTimeout) |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 90 | { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 91 | this->ensureConnected(true); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 93 | auto entry = m_pendingInterestTable.insert(make_shared<PendingInterest>( |
| 94 | interest, afterSatisfied, afterNacked, afterTimeout, ref(m_scheduler))).first; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 95 | (*entry)->setDeleter([this, entry] { m_pendingInterestTable.erase(entry); }); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 96 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 97 | lp::Packet packet; |
| 98 | |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 99 | shared_ptr<lp::NextHopFaceIdTag> nextHopFaceIdTag = interest->getTag<lp::NextHopFaceIdTag>(); |
| 100 | if (nextHopFaceIdTag != nullptr) { |
| 101 | packet.add<lp::NextHopFaceIdField>(*nextHopFaceIdTag); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 102 | } |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 103 | |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 104 | shared_ptr<lp::CongestionMarkTag> congestionMarkTag = interest->getTag<lp::CongestionMarkTag>(); |
| 105 | if (congestionMarkTag != nullptr) { |
| 106 | packet.add<lp::CongestionMarkField>(*congestionMarkTag); |
| 107 | } |
| 108 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 109 | packet.add<lp::FragmentField>(std::make_pair(interest->wireEncode().begin(), |
| 110 | interest->wireEncode().end())); |
| 111 | |
| 112 | m_face.m_transport->send(packet.wireEncode()); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
| 116 | asyncRemovePendingInterest(const PendingInterestId* pendingInterestId) |
| 117 | { |
| 118 | m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId)); |
| 119 | } |
| 120 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 121 | void |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 122 | asyncRemoveAllPendingInterests() |
| 123 | { |
| 124 | m_pendingInterestTable.clear(); |
| 125 | } |
| 126 | |
| 127 | void |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 128 | satisfyPendingInterests(const Data& data) |
| 129 | { |
| 130 | for (auto entry = m_pendingInterestTable.begin(); entry != m_pendingInterestTable.end(); ) { |
| 131 | if ((*entry)->getInterest()->matchesData(data)) { |
| 132 | shared_ptr<PendingInterest> matchedEntry = *entry; |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 133 | NDN_LOG_DEBUG(" satisfying " << *matchedEntry->getInterest()); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 134 | entry = m_pendingInterestTable.erase(entry); |
| 135 | matchedEntry->invokeDataCallback(data); |
| 136 | } |
| 137 | else { |
| 138 | ++entry; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | nackPendingInterests(const lp::Nack& nack) |
| 145 | { |
| 146 | for (auto entry = m_pendingInterestTable.begin(); entry != m_pendingInterestTable.end(); ) { |
| 147 | const Interest& pendingInterest = *(*entry)->getInterest(); |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 148 | if (nack.getInterest().matchesInterest(pendingInterest)) { |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 149 | shared_ptr<PendingInterest> matchedEntry = *entry; |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 150 | NDN_LOG_DEBUG(" nacking " << *matchedEntry->getInterest()); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 151 | entry = m_pendingInterestTable.erase(entry); |
| 152 | matchedEntry->invokeNackCallback(nack); |
| 153 | } |
| 154 | else { |
| 155 | ++entry; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public: // producer |
| 161 | void |
| 162 | asyncSetInterestFilter(shared_ptr<InterestFilterRecord> interestFilterRecord) |
| 163 | { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 164 | NDN_LOG_INFO("setting InterestFilter: " << interestFilterRecord->getFilter()); |
| 165 | m_interestFilterTable.push_back(std::move(interestFilterRecord)); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void |
| 169 | asyncUnsetInterestFilter(const InterestFilterId* interestFilterId) |
| 170 | { |
| 171 | InterestFilterTable::iterator i = std::find_if(m_interestFilterTable.begin(), |
| 172 | m_interestFilterTable.end(), |
| 173 | MatchInterestFilterId(interestFilterId)); |
| 174 | if (i != m_interestFilterTable.end()) { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 175 | NDN_LOG_INFO("unsetting InterestFilter: " << (*i)->getFilter()); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 176 | m_interestFilterTable.erase(i); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 181 | processInterestFilters(const Interest& interest) |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 182 | { |
| 183 | for (const auto& filter : m_interestFilterTable) { |
| 184 | if (filter->doesMatch(interest.getName())) { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 185 | NDN_LOG_DEBUG(" matches " << filter->getFilter()); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 186 | filter->invokeInterestCallback(interest); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 192 | asyncSend(const Block& wire) |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 193 | { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 194 | this->ensureConnected(true); |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 195 | m_face.m_transport->send(wire); |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 198 | public: // prefix registration |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 199 | const RegisteredPrefixId* |
| 200 | registerPrefix(const Name& prefix, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 201 | shared_ptr<InterestFilterRecord> filter, |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 202 | const RegisterPrefixSuccessCallback& onSuccess, |
| 203 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 0866f51 | 2014-08-11 13:25:09 -0700 | [diff] [blame] | 204 | uint64_t flags, |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 205 | const nfd::CommandOptions& options) |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 206 | { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 207 | NDN_LOG_INFO("registering prefix: " << prefix); |
| 208 | auto record = make_shared<RegisteredPrefix>(prefix, filter, options); |
| 209 | |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 210 | nfd::ControlParameters params; |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 211 | params.setName(prefix); |
| 212 | params.setFlags(flags); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 213 | m_face.m_nfdController->start<nfd::RibRegisterCommand>( |
| 214 | params, |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 215 | [=] (const nfd::ControlParameters&) { this->afterPrefixRegistered(record, onSuccess); }, |
| 216 | [=] (const nfd::ControlResponse& resp) { |
| 217 | NDN_LOG_INFO("register prefix failed: " << record->getPrefix()); |
| 218 | onFailure(record->getPrefix(), resp.getText()); |
| 219 | }, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 220 | options); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 221 | |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 222 | return reinterpret_cast<const RegisteredPrefixId*>(record.get()); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 226 | afterPrefixRegistered(shared_ptr<RegisteredPrefix> registeredPrefix, |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 227 | const RegisterPrefixSuccessCallback& onSuccess) |
| 228 | { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 229 | NDN_LOG_INFO("registered prefix: " << registeredPrefix->getPrefix()); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 230 | m_registeredPrefixTable.insert(registeredPrefix); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 231 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 232 | if (registeredPrefix->getFilter() != nullptr) { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 233 | // it was a combined operation |
| 234 | m_interestFilterTable.push_back(registeredPrefix->getFilter()); |
| 235 | } |
| 236 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 237 | if (onSuccess != nullptr) { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 238 | onSuccess(registeredPrefix->getPrefix()); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void |
| 243 | asyncUnregisterPrefix(const RegisteredPrefixId* registeredPrefixId, |
| 244 | const UnregisterPrefixSuccessCallback& onSuccess, |
| 245 | const UnregisterPrefixFailureCallback& onFailure) |
| 246 | { |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 247 | auto i = std::find_if(m_registeredPrefixTable.begin(), |
| 248 | m_registeredPrefixTable.end(), |
| 249 | MatchRegisteredPrefixId(registeredPrefixId)); |
| 250 | if (i != m_registeredPrefixTable.end()) { |
| 251 | RegisteredPrefix& record = **i; |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 252 | const shared_ptr<InterestFilterRecord>& filter = record.getFilter(); |
| 253 | |
| 254 | if (filter != nullptr) { |
| 255 | // it was a combined operation |
| 256 | m_interestFilterTable.remove(filter); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 257 | } |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 258 | |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 259 | NDN_LOG_INFO("unregistering prefix: " << record.getPrefix()); |
| 260 | |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 261 | nfd::ControlParameters params; |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 262 | params.setName(record.getPrefix()); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 263 | m_face.m_nfdController->start<nfd::RibUnregisterCommand>( |
| 264 | params, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 265 | [=] (const nfd::ControlParameters&) { this->finalizeUnregisterPrefix(i, onSuccess); }, |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 266 | [=] (const nfd::ControlResponse& resp) { |
| 267 | NDN_LOG_INFO("unregister prefix failed: " << params.getName()); |
| 268 | onFailure(resp.getText()); |
| 269 | }, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 270 | record.getCommandOptions()); |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 271 | } |
Alexander Afanasyev | 851228a | 2014-10-20 15:55:28 -0400 | [diff] [blame] | 272 | else { |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 273 | if (onFailure != nullptr) { |
Alexander Afanasyev | 851228a | 2014-10-20 15:55:28 -0400 | [diff] [blame] | 274 | onFailure("Unrecognized PrefixId"); |
| 275 | } |
| 276 | } |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 277 | |
| 278 | // there cannot be two registered prefixes with the same id |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | finalizeUnregisterPrefix(RegisteredPrefixTable::iterator item, |
| 283 | const UnregisterPrefixSuccessCallback& onSuccess) |
| 284 | { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 285 | NDN_LOG_INFO("unregistered prefix: " << (*item)->getPrefix()); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 286 | m_registeredPrefixTable.erase(item); |
| 287 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 288 | if (onSuccess != nullptr) { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 289 | onSuccess(); |
| 290 | } |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 293 | public: // IO routine |
| 294 | void |
| 295 | ensureConnected(bool wantResume) |
| 296 | { |
| 297 | if (!m_face.m_transport->isConnected()) |
| 298 | m_face.m_transport->connect(m_face.m_ioService, |
| 299 | [=] (const Block& wire) { m_face.onReceiveElement(wire); }); |
| 300 | |
| 301 | if (wantResume && !m_face.m_transport->isReceiving()) { |
| 302 | m_face.m_transport->resume(); |
| 303 | } |
| 304 | } |
| 305 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 306 | void |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 307 | onEmptyPitOrNoRegisteredPrefixes() |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 308 | { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 309 | if (m_pendingInterestTable.empty() && m_registeredPrefixTable.empty()) { |
| 310 | m_face.m_transport->pause(); |
| 311 | if (!m_ioServiceWork) { |
| 312 | m_processEventsTimeoutEvent.cancel(); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | private: |
| 318 | Face& m_face; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 319 | util::Scheduler m_scheduler; |
| 320 | util::scheduler::ScopedEventId m_processEventsTimeoutEvent; |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 321 | |
| 322 | PendingInterestTable m_pendingInterestTable; |
| 323 | InterestFilterTable m_interestFilterTable; |
| 324 | RegisteredPrefixTable m_registeredPrefixTable; |
| 325 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 326 | unique_ptr<boost::asio::io_service::work> m_ioServiceWork; // if thread needs to be preserved |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 327 | |
| 328 | friend class Face; |
| 329 | }; |
| 330 | |
| 331 | } // namespace ndn |
| 332 | |
| 333 | #endif // NDN_DETAIL_FACE_IMPL_HPP |