blob: 86945ba61349043283f9cce7f3e738e2a444a48f [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi4460e822017-08-07 22:02:45 +00002/*
Alexander Afanasyev1013fd02017-01-03 13:19:03 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyev258ec2b2014-05-14 16:15:37 -070020 */
21
22#ifndef NDN_DETAIL_FACE_IMPL_HPP
23#define NDN_DETAIL_FACE_IMPL_HPP
24
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070025#include "../face.hpp"
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080026#include "container-with-on-empty-signal.hpp"
Junxiao Shib6e276f2017-08-14 20:10:04 +000027#include "lp-field-tag.hpp"
Junxiao Shi4460e822017-08-07 22:02:45 +000028#include "pending-interest.hpp"
29#include "registered-prefix.hpp"
Eric Newberry83872fd2015-08-06 17:01:24 -070030#include "../lp/packet.hpp"
Junxiao Shi4b469982015-12-03 18:20:19 +000031#include "../lp/tags.hpp"
Junxiao Shi4460e822017-08-07 22:02:45 +000032#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
41NDN_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 Afanasyev258ec2b2014-05-14 16:15:37 -070054
55namespace ndn {
56
Junxiao Shib6e276f2017-08-14 20:10:04 +000057/** @brief implementation detail of Face
Junxiao Shi103d8ed2016-08-07 20:34:10 +000058 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070059class Face::Impl : noncopyable
60{
61public:
Junxiao Shib6e276f2017-08-14 20:10:04 +000062 using PendingInterestTable = ContainerWithOnEmptySignal<shared_ptr<PendingInterest>>;
63 using InterestFilterTable = std::list<shared_ptr<InterestFilterRecord>>;
64 using RegisteredPrefixTable = ContainerWithOnEmptySignal<shared_ptr<RegisteredPrefix>>;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070065
66 explicit
67 Impl(Face& face)
68 : m_face(face)
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080069 , m_scheduler(m_face.getIoService())
70 , m_processEventsTimeoutEvent(m_scheduler)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070071 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080072 auto postOnEmptyPitOrNoRegisteredPrefixes = [this] {
Junxiao Shi103d8ed2016-08-07 20:34:10 +000073 this->m_face.getIoService().post([this] { this->onEmptyPitOrNoRegisteredPrefixes(); });
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080074 // 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 Afanasyev258ec2b2014-05-14 16:15:37 -070082 }
83
Junxiao Shi103d8ed2016-08-07 20:34:10 +000084public: // consumer
Junxiao Shia1ea5062014-12-27 22:33:39 -070085 void
Eric Newberry83872fd2015-08-06 17:01:24 -070086 asyncExpressInterest(shared_ptr<const Interest> interest,
87 const DataCallback& afterSatisfied,
88 const NackCallback& afterNacked,
89 const TimeoutCallback& afterTimeout)
Junxiao Shia1ea5062014-12-27 22:33:39 -070090 {
Junxiao Shib6828912017-11-20 14:06:32 +000091 NDN_LOG_DEBUG("<I " << *interest);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080092 this->ensureConnected(true);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070093
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +000094 const Interest& interest2 = *interest;
Junxiao Shi79a7a162017-09-09 08:33:57 +000095 auto i = m_pendingInterestTable.insert(make_shared<PendingInterest>(
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +000096 std::move(interest), afterSatisfied, afterNacked, afterTimeout, ref(m_scheduler))).first;
Junxiao Shib6828912017-11-20 14:06:32 +000097 // In dispatchInterest, an InterestCallback may respond with Data right away and delete
98 // the PendingInterestTable entry. shared_ptr is retained to ensure PendingInterest instance
99 // remains valid in this case.
100 shared_ptr<PendingInterest> entry = *i;
101 entry->setDeleter([this, i] { m_pendingInterestTable.erase(i); });
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700102
Junxiao Shib6e276f2017-08-14 20:10:04 +0000103 lp::Packet lpPacket;
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000104 addFieldFromTag<lp::NextHopFaceIdField, lp::NextHopFaceIdTag>(lpPacket, interest2);
105 addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, interest2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700106
Junxiao Shib6828912017-11-20 14:06:32 +0000107 entry->recordForwarding();
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000108 m_face.m_transport->send(finishEncoding(std::move(lpPacket), interest2.wireEncode(),
109 'I', interest2.getName()));
Junxiao Shib6828912017-11-20 14:06:32 +0000110 dispatchInterest(*entry, interest2);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700111 }
112
113 void
114 asyncRemovePendingInterest(const PendingInterestId* pendingInterestId)
115 {
116 m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId));
117 }
118
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700119 void
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500120 asyncRemoveAllPendingInterests()
121 {
122 m_pendingInterestTable.clear();
123 }
124
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000125 /** @return whether the Data should be sent to the forwarder, if it does not come from the forwarder
126 */
127 bool
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000128 satisfyPendingInterests(const Data& data)
129 {
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000130 bool hasAppMatch = false, hasForwarderMatch = false;
Junxiao Shi79a7a162017-09-09 08:33:57 +0000131 for (auto i = m_pendingInterestTable.begin(); i != m_pendingInterestTable.end(); ) {
132 shared_ptr<PendingInterest> entry = *i;
133 if (!entry->getInterest()->matchesData(data)) {
134 ++i;
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000135 continue;
136 }
137
Junxiao Shi79a7a162017-09-09 08:33:57 +0000138 NDN_LOG_DEBUG(" satisfying " << *entry->getInterest() << " from " << entry->getOrigin());
139 i = m_pendingInterestTable.erase(i);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000140
Junxiao Shi79a7a162017-09-09 08:33:57 +0000141 if (entry->getOrigin() == PendingInterestOrigin::APP) {
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000142 hasAppMatch = true;
Junxiao Shi79a7a162017-09-09 08:33:57 +0000143 entry->invokeDataCallback(data);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000144 }
145 else {
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000146 hasForwarderMatch = true;
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000147 }
148 }
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000149 // if Data matches no pending Interest record, it is sent to the forwarder as unsolicited Data
150 return hasForwarderMatch || !hasAppMatch;
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000151 }
152
Junxiao Shi79a7a162017-09-09 08:33:57 +0000153 /** @return a Nack to be sent to the forwarder, or nullopt if no Nack should be sent
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000154 */
Junxiao Shi79a7a162017-09-09 08:33:57 +0000155 optional<lp::Nack>
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000156 nackPendingInterests(const lp::Nack& nack)
157 {
Junxiao Shi79a7a162017-09-09 08:33:57 +0000158 optional<lp::Nack> outNack;
159 for (auto i = m_pendingInterestTable.begin(); i != m_pendingInterestTable.end(); ) {
160 shared_ptr<PendingInterest> entry = *i;
161 if (!nack.getInterest().matchesInterest(*entry->getInterest())) {
162 ++i;
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000163 continue;
164 }
165
Junxiao Shi79a7a162017-09-09 08:33:57 +0000166 NDN_LOG_DEBUG(" nacking " << *entry->getInterest() << " from " << entry->getOrigin());
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000167
Junxiao Shi79a7a162017-09-09 08:33:57 +0000168 optional<lp::Nack> outNack1 = entry->recordNack(nack);
169 if (!outNack1) {
170 ++i;
171 continue;
172 }
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000173
Junxiao Shi79a7a162017-09-09 08:33:57 +0000174 if (entry->getOrigin() == PendingInterestOrigin::APP) {
175 entry->invokeNackCallback(*outNack1);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000176 }
177 else {
Junxiao Shi79a7a162017-09-09 08:33:57 +0000178 outNack = outNack1;
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000179 }
Junxiao Shi79a7a162017-09-09 08:33:57 +0000180 i = m_pendingInterestTable.erase(i);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000181 }
Junxiao Shi79a7a162017-09-09 08:33:57 +0000182 // send "least severe" Nack from any PendingInterest record originated from forwarder, because
183 // it is unimportant to consider Nack reason for the unlikely case when forwarder sends multiple
184 // Interests to an app in a short while
185 return outNack;
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000186 }
187
188public: // producer
189 void
190 asyncSetInterestFilter(shared_ptr<InterestFilterRecord> interestFilterRecord)
191 {
Junxiao Shi4460e822017-08-07 22:02:45 +0000192 NDN_LOG_INFO("setting InterestFilter: " << interestFilterRecord->getFilter());
193 m_interestFilterTable.push_back(std::move(interestFilterRecord));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000194 }
195
196 void
197 asyncUnsetInterestFilter(const InterestFilterId* interestFilterId)
198 {
199 InterestFilterTable::iterator i = std::find_if(m_interestFilterTable.begin(),
200 m_interestFilterTable.end(),
201 MatchInterestFilterId(interestFilterId));
202 if (i != m_interestFilterTable.end()) {
Junxiao Shi4460e822017-08-07 22:02:45 +0000203 NDN_LOG_INFO("unsetting InterestFilter: " << (*i)->getFilter());
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000204 m_interestFilterTable.erase(i);
205 }
206 }
207
208 void
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000209 processIncomingInterest(shared_ptr<const Interest> interest)
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000210 {
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000211 const Interest& interest2 = *interest;
Junxiao Shi79a7a162017-09-09 08:33:57 +0000212 auto i = m_pendingInterestTable.insert(make_shared<PendingInterest>(
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000213 std::move(interest), ref(m_scheduler))).first;
Junxiao Shib6828912017-11-20 14:06:32 +0000214 // In dispatchInterest, an InterestCallback may respond with Data right away and delete
215 // the PendingInterestTable entry. shared_ptr is retained to ensure PendingInterest instance
216 // remains valid in this case.
Junxiao Shi859888f2017-09-12 14:29:16 +0000217 shared_ptr<PendingInterest> entry = *i;
218 entry->setDeleter([this, i] { m_pendingInterestTable.erase(i); });
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000219
Junxiao Shib6828912017-11-20 14:06:32 +0000220 this->dispatchInterest(*entry, interest2);
221 }
222
223 void
224 dispatchInterest(PendingInterest& entry, const Interest& interest)
225 {
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000226 for (const auto& filter : m_interestFilterTable) {
Junxiao Shib6828912017-11-20 14:06:32 +0000227 if (filter->doesMatch(entry)) {
Junxiao Shi4460e822017-08-07 22:02:45 +0000228 NDN_LOG_DEBUG(" matches " << filter->getFilter());
Junxiao Shib6828912017-11-20 14:06:32 +0000229 entry.recordForwarding();
230 filter->invokeInterestCallback(interest);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000231 }
232 }
233 }
234
235 void
Junxiao Shib6e276f2017-08-14 20:10:04 +0000236 asyncPutData(const Data& data)
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700237 {
Junxiao Shib6828912017-11-20 14:06:32 +0000238 NDN_LOG_DEBUG("<D " << data.getName());
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000239 bool shouldSendToForwarder = satisfyPendingInterests(data);
240 if (!shouldSendToForwarder) {
241 return;
242 }
243
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800244 this->ensureConnected(true);
Junxiao Shib6e276f2017-08-14 20:10:04 +0000245
246 lp::Packet lpPacket;
247 addFieldFromTag<lp::CachePolicyField, lp::CachePolicyTag>(lpPacket, data);
248 addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, data);
249
250 m_face.m_transport->send(finishEncoding(std::move(lpPacket), data.wireEncode(),
251 'D', data.getName()));
252 }
253
254 void
255 asyncPutNack(const lp::Nack& nack)
256 {
Junxiao Shib6828912017-11-20 14:06:32 +0000257 NDN_LOG_DEBUG("<N " << nack.getInterest() << '~' << nack.getHeader().getReason());
Junxiao Shi79a7a162017-09-09 08:33:57 +0000258 optional<lp::Nack> outNack = nackPendingInterests(nack);
259 if (!outNack) {
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000260 return;
261 }
262
Junxiao Shib6e276f2017-08-14 20:10:04 +0000263 this->ensureConnected(true);
264
265 lp::Packet lpPacket;
Junxiao Shi79a7a162017-09-09 08:33:57 +0000266 lpPacket.add<lp::NackField>(outNack->getHeader());
267 addFieldFromTag<lp::CongestionMarkField, lp::CongestionMarkTag>(lpPacket, *outNack);
Junxiao Shib6e276f2017-08-14 20:10:04 +0000268
Junxiao Shi79a7a162017-09-09 08:33:57 +0000269 const Interest& interest = outNack->getInterest();
Junxiao Shib6e276f2017-08-14 20:10:04 +0000270 m_face.m_transport->send(finishEncoding(std::move(lpPacket), interest.wireEncode(),
271 'N', interest.getName()));
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700272 }
273
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000274public: // prefix registration
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700275 const RegisteredPrefixId*
276 registerPrefix(const Name& prefix,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000277 shared_ptr<InterestFilterRecord> filter,
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700278 const RegisterPrefixSuccessCallback& onSuccess,
279 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700280 uint64_t flags,
Junxiao Shi388ec252014-11-02 15:19:57 -0700281 const nfd::CommandOptions& options)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700282 {
Junxiao Shi4460e822017-08-07 22:02:45 +0000283 NDN_LOG_INFO("registering prefix: " << prefix);
284 auto record = make_shared<RegisteredPrefix>(prefix, filter, options);
285
Junxiao Shie7c7f152016-08-20 22:36:22 +0000286 nfd::ControlParameters params;
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400287 params.setName(prefix);
288 params.setFlags(flags);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000289 m_face.m_nfdController->start<nfd::RibRegisterCommand>(
290 params,
Junxiao Shi4460e822017-08-07 22:02:45 +0000291 [=] (const nfd::ControlParameters&) { this->afterPrefixRegistered(record, onSuccess); },
292 [=] (const nfd::ControlResponse& resp) {
293 NDN_LOG_INFO("register prefix failed: " << record->getPrefix());
294 onFailure(record->getPrefix(), resp.getText());
295 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000296 options);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700297
Junxiao Shi4460e822017-08-07 22:02:45 +0000298 return reinterpret_cast<const RegisteredPrefixId*>(record.get());
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700299 }
300
301 void
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000302 afterPrefixRegistered(shared_ptr<RegisteredPrefix> registeredPrefix,
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700303 const RegisterPrefixSuccessCallback& onSuccess)
304 {
Junxiao Shi4460e822017-08-07 22:02:45 +0000305 NDN_LOG_INFO("registered prefix: " << registeredPrefix->getPrefix());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800306 m_registeredPrefixTable.insert(registeredPrefix);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700307
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000308 if (registeredPrefix->getFilter() != nullptr) {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700309 // it was a combined operation
310 m_interestFilterTable.push_back(registeredPrefix->getFilter());
311 }
312
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000313 if (onSuccess != nullptr) {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700314 onSuccess(registeredPrefix->getPrefix());
315 }
316 }
317
318 void
319 asyncUnregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
320 const UnregisterPrefixSuccessCallback& onSuccess,
321 const UnregisterPrefixFailureCallback& onFailure)
322 {
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400323 auto i = std::find_if(m_registeredPrefixTable.begin(),
324 m_registeredPrefixTable.end(),
325 MatchRegisteredPrefixId(registeredPrefixId));
326 if (i != m_registeredPrefixTable.end()) {
327 RegisteredPrefix& record = **i;
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400328 const shared_ptr<InterestFilterRecord>& filter = record.getFilter();
329
330 if (filter != nullptr) {
331 // it was a combined operation
332 m_interestFilterTable.remove(filter);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700333 }
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400334
Junxiao Shi4460e822017-08-07 22:02:45 +0000335 NDN_LOG_INFO("unregistering prefix: " << record.getPrefix());
336
Junxiao Shie7c7f152016-08-20 22:36:22 +0000337 nfd::ControlParameters params;
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400338 params.setName(record.getPrefix());
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000339 m_face.m_nfdController->start<nfd::RibUnregisterCommand>(
340 params,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000341 [=] (const nfd::ControlParameters&) { this->finalizeUnregisterPrefix(i, onSuccess); },
Junxiao Shi4460e822017-08-07 22:02:45 +0000342 [=] (const nfd::ControlResponse& resp) {
343 NDN_LOG_INFO("unregister prefix failed: " << params.getName());
344 onFailure(resp.getText());
345 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000346 record.getCommandOptions());
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400347 }
Alexander Afanasyev851228a2014-10-20 15:55:28 -0400348 else {
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400349 if (onFailure != nullptr) {
Alexander Afanasyev851228a2014-10-20 15:55:28 -0400350 onFailure("Unrecognized PrefixId");
351 }
352 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700353
354 // there cannot be two registered prefixes with the same id
355 }
356
357 void
358 finalizeUnregisterPrefix(RegisteredPrefixTable::iterator item,
359 const UnregisterPrefixSuccessCallback& onSuccess)
360 {
Junxiao Shi4460e822017-08-07 22:02:45 +0000361 NDN_LOG_INFO("unregistered prefix: " << (*item)->getPrefix());
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700362 m_registeredPrefixTable.erase(item);
363
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000364 if (onSuccess != nullptr) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700365 onSuccess();
366 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700367 }
368
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000369public: // IO routine
370 void
371 ensureConnected(bool wantResume)
372 {
373 if (!m_face.m_transport->isConnected())
374 m_face.m_transport->connect(m_face.m_ioService,
375 [=] (const Block& wire) { m_face.onReceiveElement(wire); });
376
377 if (wantResume && !m_face.m_transport->isReceiving()) {
378 m_face.m_transport->resume();
379 }
380 }
381
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700382 void
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800383 onEmptyPitOrNoRegisteredPrefixes()
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700384 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800385 if (m_pendingInterestTable.empty() && m_registeredPrefixTable.empty()) {
386 m_face.m_transport->pause();
387 if (!m_ioServiceWork) {
388 m_processEventsTimeoutEvent.cancel();
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700389 }
390 }
391 }
392
393private:
Junxiao Shib6e276f2017-08-14 20:10:04 +0000394 /** @brief Finish packet encoding
395 * @param lpPacket NDNLP packet without FragmentField
396 * @param wire wire encoding of Interest or Data
397 * @param pktType packet type, 'I' for Interest, 'D' for Data, 'N' for Nack
398 * @param name packet name
399 * @return wire encoding of either NDNLP or bare network packet
400 * @throw Face::OversizedPacketError wire encoding exceeds limit
401 */
402 Block
403 finishEncoding(lp::Packet&& lpPacket, Block wire, char pktType, const Name& name)
404 {
405 if (!lpPacket.empty()) {
406 lpPacket.add<lp::FragmentField>(std::make_pair(wire.begin(), wire.end()));
407 wire = lpPacket.wireEncode();
408 }
409
410 if (wire.size() > MAX_NDN_PACKET_SIZE) {
411 BOOST_THROW_EXCEPTION(Face::OversizedPacketError(pktType, name, wire.size()));
412 }
413
414 return wire;
415 }
416
417private:
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700418 Face& m_face;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800419 util::Scheduler m_scheduler;
420 util::scheduler::ScopedEventId m_processEventsTimeoutEvent;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700421
422 PendingInterestTable m_pendingInterestTable;
423 InterestFilterTable m_interestFilterTable;
424 RegisteredPrefixTable m_registeredPrefixTable;
425
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800426 unique_ptr<boost::asio::io_service::work> m_ioServiceWork; // if thread needs to be preserved
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700427
428 friend class Face;
429};
430
431} // namespace ndn
432
433#endif // NDN_DETAIL_FACE_IMPL_HPP