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 | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 27 | #include "lp-field-tag.hpp" |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 28 | #include "pending-interest.hpp" |
| 29 | #include "registered-prefix.hpp" |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 30 | #include "../lp/packet.hpp" |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 31 | #include "../lp/tags.hpp" |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 32 | #include "../mgmt/nfd/command-options.hpp" |
| 33 | #include "../mgmt/nfd/controller.hpp" |
| 34 | #include "../transport/tcp-transport.hpp" |
| 35 | #include "../transport/unix-transport.hpp" |
| 36 | #include "../util/config-file.hpp" |
| 37 | #include "../util/logger.hpp" |
| 38 | #include "../util/scheduler.hpp" |
| 39 | #include "../util/signal.hpp" |
| 40 | |
| 41 | NDN_LOG_INIT(ndn.Face); |
| 42 | // INFO level: prefix registration, etc. |
| 43 | // |
| 44 | // DEBUG level: packet logging. |
| 45 | // Each log entry starts with a direction symbol ('<' denotes an outgoing packet, '>' denotes an |
| 46 | // incoming packet) and a packet type symbol ('I' denotes an Interest, 'D' denotes a Data, 'N' |
| 47 | // denotes a Nack). Interest is printed as its string representation, Data is printed as name only, |
| 48 | // Nack is printed as the Interest followed by the Nack reason and delimited by a '~' symbol. A |
| 49 | // log line about an incoming packet may be followed by zero or more lines about Interest matching |
| 50 | // InterestFilter, Data satisfying Interest, or Nack rejecting Interest, which are also written at |
| 51 | // DEBUG level. |
| 52 | // |
| 53 | // TRACE level: more detailed unstructured messages. |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 54 | |
| 55 | namespace ndn { |
| 56 | |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 57 | /** @brief implementation detail of Face |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 58 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 59 | class Face::Impl : noncopyable |
| 60 | { |
| 61 | public: |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 62 | using PendingInterestTable = ContainerWithOnEmptySignal<shared_ptr<PendingInterest>>; |
| 63 | using InterestFilterTable = std::list<shared_ptr<InterestFilterRecord>>; |
| 64 | using RegisteredPrefixTable = ContainerWithOnEmptySignal<shared_ptr<RegisteredPrefix>>; |
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 | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 93 | const Interest& interest2 = *interest; |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 94 | auto i = m_pendingInterestTable.insert(make_shared<PendingInterest>( |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 95 | std::move(interest), afterSatisfied, afterNacked, afterTimeout, ref(m_scheduler))).first; |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 96 | PendingInterest& entry = **i; |
| 97 | entry.setDeleter([this, i] { m_pendingInterestTable.erase(i); }); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 98 | |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 99 | lp::Packet lpPacket; |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 100 | addFieldFromTag<lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, interest2); |
| 101 | addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, interest2); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 102 | |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 103 | m_face.m_transport->send(finishEncoding(std::move(lpPacket), interest2.wireEncode(), |
| 104 | 'I', interest2.getName())); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 105 | entry.recordForwarding(); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
| 109 | asyncRemovePendingInterest(const PendingInterestId* pendingInterestId) |
| 110 | { |
| 111 | m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId)); |
| 112 | } |
| 113 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 114 | void |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 115 | asyncRemoveAllPendingInterests() |
| 116 | { |
| 117 | m_pendingInterestTable.clear(); |
| 118 | } |
| 119 | |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 120 | /** @return whether the Data should be sent to the forwarder, if it does not come from the forwarder |
| 121 | */ |
| 122 | bool |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 123 | satisfyPendingInterests(const Data& data) |
| 124 | { |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 125 | bool hasAppMatch = false, hasForwarderMatch = false; |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 126 | for (auto i = m_pendingInterestTable.begin(); i != m_pendingInterestTable.end(); ) { |
| 127 | shared_ptr<PendingInterest> entry = *i; |
| 128 | if (!entry->getInterest()->matchesData(data)) { |
| 129 | ++i; |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 130 | continue; |
| 131 | } |
| 132 | |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 133 | NDN_LOG_DEBUG(" satisfying " << *entry->getInterest() << " from " << entry->getOrigin()); |
| 134 | i = m_pendingInterestTable.erase(i); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 135 | |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 136 | if (entry->getOrigin() == PendingInterestOrigin::APP) { |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 137 | hasAppMatch = true; |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 138 | entry->invokeDataCallback(data); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 139 | } |
| 140 | else { |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 141 | hasForwarderMatch = true; |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 144 | // if Data matches no pending Interest record, it is sent to the forwarder as unsolicited Data |
| 145 | return hasForwarderMatch || !hasAppMatch; |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 148 | /** @return a Nack to be sent to the forwarder, or nullopt if no Nack should be sent |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 149 | */ |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 150 | optional<lp::Nack> |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 151 | nackPendingInterests(const lp::Nack& nack) |
| 152 | { |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 153 | optional<lp::Nack> outNack; |
| 154 | for (auto i = m_pendingInterestTable.begin(); i != m_pendingInterestTable.end(); ) { |
| 155 | shared_ptr<PendingInterest> entry = *i; |
| 156 | if (!nack.getInterest().matchesInterest(*entry->getInterest())) { |
| 157 | ++i; |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 158 | continue; |
| 159 | } |
| 160 | |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 161 | NDN_LOG_DEBUG(" nacking " << *entry->getInterest() << " from " << entry->getOrigin()); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 162 | |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 163 | optional<lp::Nack> outNack1 = entry->recordNack(nack); |
| 164 | if (!outNack1) { |
| 165 | ++i; |
| 166 | continue; |
| 167 | } |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 168 | |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 169 | if (entry->getOrigin() == PendingInterestOrigin::APP) { |
| 170 | entry->invokeNackCallback(*outNack1); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 171 | } |
| 172 | else { |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 173 | outNack = outNack1; |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 174 | } |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 175 | i = m_pendingInterestTable.erase(i); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 176 | } |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 177 | // send "least severe" Nack from any PendingInterest record originated from forwarder, because |
| 178 | // it is unimportant to consider Nack reason for the unlikely case when forwarder sends multiple |
| 179 | // Interests to an app in a short while |
| 180 | return outNack; |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | public: // producer |
| 184 | void |
| 185 | asyncSetInterestFilter(shared_ptr<InterestFilterRecord> interestFilterRecord) |
| 186 | { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 187 | NDN_LOG_INFO("setting InterestFilter: " << interestFilterRecord->getFilter()); |
| 188 | m_interestFilterTable.push_back(std::move(interestFilterRecord)); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void |
| 192 | asyncUnsetInterestFilter(const InterestFilterId* interestFilterId) |
| 193 | { |
| 194 | InterestFilterTable::iterator i = std::find_if(m_interestFilterTable.begin(), |
| 195 | m_interestFilterTable.end(), |
| 196 | MatchInterestFilterId(interestFilterId)); |
| 197 | if (i != m_interestFilterTable.end()) { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 198 | NDN_LOG_INFO("unsetting InterestFilter: " << (*i)->getFilter()); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 199 | m_interestFilterTable.erase(i); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 204 | processIncomingInterest(shared_ptr<const Interest> interest) |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 205 | { |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 206 | const Interest& interest2 = *interest; |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 207 | auto i = m_pendingInterestTable.insert(make_shared<PendingInterest>( |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 208 | std::move(interest), ref(m_scheduler))).first; |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 209 | PendingInterest& entry = **i; |
| 210 | entry.setDeleter([this, i] { m_pendingInterestTable.erase(i); }); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 211 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 212 | for (const auto& filter : m_interestFilterTable) { |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 213 | if (filter->doesMatch(interest2.getName())) { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 214 | NDN_LOG_DEBUG(" matches " << filter->getFilter()); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 215 | filter->invokeInterestCallback(interest2); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 216 | entry.recordForwarding(); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 222 | asyncPutData(const Data& data) |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 223 | { |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 224 | bool shouldSendToForwarder = satisfyPendingInterests(data); |
| 225 | if (!shouldSendToForwarder) { |
| 226 | return; |
| 227 | } |
| 228 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 229 | this->ensureConnected(true); |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 230 | |
| 231 | lp::Packet lpPacket; |
| 232 | addFieldFromTag<lp::CachePolicyField, lp::CachePolicyTag>(lpPacket, data); |
| 233 | addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, data); |
| 234 | |
| 235 | m_face.m_transport->send(finishEncoding(std::move(lpPacket), data.wireEncode(), |
| 236 | 'D', data.getName())); |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | asyncPutNack(const lp::Nack& nack) |
| 241 | { |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 242 | optional<lp::Nack> outNack = nackPendingInterests(nack); |
| 243 | if (!outNack) { |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 247 | this->ensureConnected(true); |
| 248 | |
| 249 | lp::Packet lpPacket; |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 250 | lpPacket.add<lp::NackField>(outNack->getHeader()); |
| 251 | addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, *outNack); |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 252 | |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame^] | 253 | const Interest& interest = outNack->getInterest(); |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 254 | m_face.m_transport->send(finishEncoding(std::move(lpPacket), interest.wireEncode(), |
| 255 | 'N', interest.getName())); |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 258 | public: // prefix registration |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 259 | const RegisteredPrefixId* |
| 260 | registerPrefix(const Name& prefix, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 261 | shared_ptr<InterestFilterRecord> filter, |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 262 | const RegisterPrefixSuccessCallback& onSuccess, |
| 263 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 0866f51 | 2014-08-11 13:25:09 -0700 | [diff] [blame] | 264 | uint64_t flags, |
Junxiao Shi | 388ec25 | 2014-11-02 15:19:57 -0700 | [diff] [blame] | 265 | const nfd::CommandOptions& options) |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 266 | { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 267 | NDN_LOG_INFO("registering prefix: " << prefix); |
| 268 | auto record = make_shared<RegisteredPrefix>(prefix, filter, options); |
| 269 | |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 270 | nfd::ControlParameters params; |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 271 | params.setName(prefix); |
| 272 | params.setFlags(flags); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 273 | m_face.m_nfdController->start<nfd::RibRegisterCommand>( |
| 274 | params, |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 275 | [=] (const nfd::ControlParameters&) { this->afterPrefixRegistered(record, onSuccess); }, |
| 276 | [=] (const nfd::ControlResponse& resp) { |
| 277 | NDN_LOG_INFO("register prefix failed: " << record->getPrefix()); |
| 278 | onFailure(record->getPrefix(), resp.getText()); |
| 279 | }, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 280 | options); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 281 | |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 282 | return reinterpret_cast<const RegisteredPrefixId*>(record.get()); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 286 | afterPrefixRegistered(shared_ptr<RegisteredPrefix> registeredPrefix, |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 287 | const RegisterPrefixSuccessCallback& onSuccess) |
| 288 | { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 289 | NDN_LOG_INFO("registered prefix: " << registeredPrefix->getPrefix()); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 290 | m_registeredPrefixTable.insert(registeredPrefix); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 291 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 292 | if (registeredPrefix->getFilter() != nullptr) { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 293 | // it was a combined operation |
| 294 | m_interestFilterTable.push_back(registeredPrefix->getFilter()); |
| 295 | } |
| 296 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 297 | if (onSuccess != nullptr) { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 298 | onSuccess(registeredPrefix->getPrefix()); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | asyncUnregisterPrefix(const RegisteredPrefixId* registeredPrefixId, |
| 304 | const UnregisterPrefixSuccessCallback& onSuccess, |
| 305 | const UnregisterPrefixFailureCallback& onFailure) |
| 306 | { |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 307 | auto i = std::find_if(m_registeredPrefixTable.begin(), |
| 308 | m_registeredPrefixTable.end(), |
| 309 | MatchRegisteredPrefixId(registeredPrefixId)); |
| 310 | if (i != m_registeredPrefixTable.end()) { |
| 311 | RegisteredPrefix& record = **i; |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 312 | const shared_ptr<InterestFilterRecord>& filter = record.getFilter(); |
| 313 | |
| 314 | if (filter != nullptr) { |
| 315 | // it was a combined operation |
| 316 | m_interestFilterTable.remove(filter); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 317 | } |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 318 | |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 319 | NDN_LOG_INFO("unregistering prefix: " << record.getPrefix()); |
| 320 | |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 321 | nfd::ControlParameters params; |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 322 | params.setName(record.getPrefix()); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 323 | m_face.m_nfdController->start<nfd::RibUnregisterCommand>( |
| 324 | params, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 325 | [=] (const nfd::ControlParameters&) { this->finalizeUnregisterPrefix(i, onSuccess); }, |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 326 | [=] (const nfd::ControlResponse& resp) { |
| 327 | NDN_LOG_INFO("unregister prefix failed: " << params.getName()); |
| 328 | onFailure(resp.getText()); |
| 329 | }, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 330 | record.getCommandOptions()); |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 331 | } |
Alexander Afanasyev | 851228a | 2014-10-20 15:55:28 -0400 | [diff] [blame] | 332 | else { |
Joao Pereira | ba1e3b9 | 2015-06-01 17:50:37 -0400 | [diff] [blame] | 333 | if (onFailure != nullptr) { |
Alexander Afanasyev | 851228a | 2014-10-20 15:55:28 -0400 | [diff] [blame] | 334 | onFailure("Unrecognized PrefixId"); |
| 335 | } |
| 336 | } |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 337 | |
| 338 | // there cannot be two registered prefixes with the same id |
| 339 | } |
| 340 | |
| 341 | void |
| 342 | finalizeUnregisterPrefix(RegisteredPrefixTable::iterator item, |
| 343 | const UnregisterPrefixSuccessCallback& onSuccess) |
| 344 | { |
Junxiao Shi | 4460e82 | 2017-08-07 22:02:45 +0000 | [diff] [blame] | 345 | NDN_LOG_INFO("unregistered prefix: " << (*item)->getPrefix()); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 346 | m_registeredPrefixTable.erase(item); |
| 347 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 348 | if (onSuccess != nullptr) { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 349 | onSuccess(); |
| 350 | } |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 353 | public: // IO routine |
| 354 | void |
| 355 | ensureConnected(bool wantResume) |
| 356 | { |
| 357 | if (!m_face.m_transport->isConnected()) |
| 358 | m_face.m_transport->connect(m_face.m_ioService, |
| 359 | [=] (const Block& wire) { m_face.onReceiveElement(wire); }); |
| 360 | |
| 361 | if (wantResume && !m_face.m_transport->isReceiving()) { |
| 362 | m_face.m_transport->resume(); |
| 363 | } |
| 364 | } |
| 365 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 366 | void |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 367 | onEmptyPitOrNoRegisteredPrefixes() |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 368 | { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 369 | if (m_pendingInterestTable.empty() && m_registeredPrefixTable.empty()) { |
| 370 | m_face.m_transport->pause(); |
| 371 | if (!m_ioServiceWork) { |
| 372 | m_processEventsTimeoutEvent.cancel(); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | private: |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 378 | /** @brief Finish packet encoding |
| 379 | * @param lpPacket NDNLP packet without FragmentField |
| 380 | * @param wire wire encoding of Interest or Data |
| 381 | * @param pktType packet type, 'I' for Interest, 'D' for Data, 'N' for Nack |
| 382 | * @param name packet name |
| 383 | * @return wire encoding of either NDNLP or bare network packet |
| 384 | * @throw Face::OversizedPacketError wire encoding exceeds limit |
| 385 | */ |
| 386 | Block |
| 387 | finishEncoding(lp::Packet&& lpPacket, Block wire, char pktType, const Name& name) |
| 388 | { |
| 389 | if (!lpPacket.empty()) { |
| 390 | lpPacket.add<lp::FragmentField>(std::make_pair(wire.begin(), wire.end())); |
| 391 | wire = lpPacket.wireEncode(); |
| 392 | } |
| 393 | |
| 394 | if (wire.size() > MAX_NDN_PACKET_SIZE) { |
| 395 | BOOST_THROW_EXCEPTION(Face::OversizedPacketError(pktType, name, wire.size())); |
| 396 | } |
| 397 | |
| 398 | return wire; |
| 399 | } |
| 400 | |
| 401 | private: |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 402 | Face& m_face; |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 403 | util::Scheduler m_scheduler; |
| 404 | util::scheduler::ScopedEventId m_processEventsTimeoutEvent; |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 405 | |
| 406 | PendingInterestTable m_pendingInterestTable; |
| 407 | InterestFilterTable m_interestFilterTable; |
| 408 | RegisteredPrefixTable m_registeredPrefixTable; |
| 409 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 410 | 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] | 411 | |
| 412 | friend class Face; |
| 413 | }; |
| 414 | |
| 415 | } // namespace ndn |
| 416 | |
| 417 | #endif // NDN_DETAIL_FACE_IMPL_HPP |