blob: 92f102ff77056cb327640c4d4100ce0d08b873ad [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -07002/**
Junxiao Shi446de3c2016-07-25 22:38:16 +00003 * Copyright (c) 2013-2016 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
25#include "../common.hpp"
26#include "../face.hpp"
27
28#include "registered-prefix.hpp"
29#include "pending-interest.hpp"
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080030#include "container-with-on-empty-signal.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070031
32#include "../util/scheduler.hpp"
33#include "../util/config-file.hpp"
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080034#include "../util/signal.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070035
Junxiao Shi0fa28452014-12-13 22:02:36 -070036#include "../transport/transport.hpp"
37#include "../transport/unix-transport.hpp"
38#include "../transport/tcp-transport.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070039
Junxiao Shi0fa28452014-12-13 22:02:36 -070040#include "../management/nfd-controller.hpp"
41#include "../management/nfd-command-options.hpp"
Eric Newberry83872fd2015-08-06 17:01:24 -070042
43#include "../lp/packet.hpp"
Junxiao Shi4b469982015-12-03 18:20:19 +000044#include "../lp/tags.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070045
46namespace ndn {
47
Junxiao Shi103d8ed2016-08-07 20:34:10 +000048using ndn::nfd::ControlParameters;
49
50/**
51 * @brief implementation detail of Face
52 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070053class Face::Impl : noncopyable
54{
55public:
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080056 typedef ContainerWithOnEmptySignal<shared_ptr<PendingInterest>> PendingInterestTable;
Junxiao Shi103d8ed2016-08-07 20:34:10 +000057 typedef std::list<shared_ptr<InterestFilterRecord>> InterestFilterTable;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080058 typedef ContainerWithOnEmptySignal<shared_ptr<RegisteredPrefix>> RegisteredPrefixTable;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070059
60 explicit
61 Impl(Face& face)
62 : m_face(face)
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080063 , m_scheduler(m_face.getIoService())
64 , m_processEventsTimeoutEvent(m_scheduler)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070065 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080066 auto postOnEmptyPitOrNoRegisteredPrefixes = [this] {
Junxiao Shi103d8ed2016-08-07 20:34:10 +000067 this->m_face.getIoService().post([this] { this->onEmptyPitOrNoRegisteredPrefixes(); });
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080068 // without this extra "post", transport can get paused (-async_read) and then resumed
69 // (+async_read) from within onInterest/onData callback. After onInterest/onData
70 // finishes, there is another +async_read with the same memory block. A few of such
71 // async_read duplications can cause various effects and result in segfault.
72 };
73
74 m_pendingInterestTable.onEmpty.connect(postOnEmptyPitOrNoRegisteredPrefixes);
75 m_registeredPrefixTable.onEmpty.connect(postOnEmptyPitOrNoRegisteredPrefixes);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070076 }
77
Junxiao Shi103d8ed2016-08-07 20:34:10 +000078public: // consumer
Junxiao Shia1ea5062014-12-27 22:33:39 -070079 void
Eric Newberry83872fd2015-08-06 17:01:24 -070080 asyncExpressInterest(shared_ptr<const Interest> interest,
81 const DataCallback& afterSatisfied,
82 const NackCallback& afterNacked,
83 const TimeoutCallback& afterTimeout)
Junxiao Shia1ea5062014-12-27 22:33:39 -070084 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080085 this->ensureConnected(true);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070086
Junxiao Shi103d8ed2016-08-07 20:34:10 +000087 auto entry = m_pendingInterestTable.insert(make_shared<PendingInterest>(
88 interest, afterSatisfied, afterNacked, afterTimeout, ref(m_scheduler))).first;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080089 (*entry)->setDeleter([this, entry] { m_pendingInterestTable.erase(entry); });
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070090
Eric Newberry83872fd2015-08-06 17:01:24 -070091 lp::Packet packet;
92
Junxiao Shi4b469982015-12-03 18:20:19 +000093 shared_ptr<lp::NextHopFaceIdTag> nextHopFaceIdTag = interest->getTag<lp::NextHopFaceIdTag>();
94 if (nextHopFaceIdTag != nullptr) {
95 packet.add<lp::NextHopFaceIdField>(*nextHopFaceIdTag);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080096 }
Eric Newberry83872fd2015-08-06 17:01:24 -070097
98 packet.add<lp::FragmentField>(std::make_pair(interest->wireEncode().begin(),
99 interest->wireEncode().end()));
100
101 m_face.m_transport->send(packet.wireEncode());
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700102 }
103
104 void
105 asyncRemovePendingInterest(const PendingInterestId* pendingInterestId)
106 {
107 m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId));
108 }
109
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700110 void
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500111 asyncRemoveAllPendingInterests()
112 {
113 m_pendingInterestTable.clear();
114 }
115
116 void
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000117 satisfyPendingInterests(const Data& data)
118 {
119 for (auto entry = m_pendingInterestTable.begin(); entry != m_pendingInterestTable.end(); ) {
120 if ((*entry)->getInterest()->matchesData(data)) {
121 shared_ptr<PendingInterest> matchedEntry = *entry;
122 entry = m_pendingInterestTable.erase(entry);
123 matchedEntry->invokeDataCallback(data);
124 }
125 else {
126 ++entry;
127 }
128 }
129 }
130
131 void
132 nackPendingInterests(const lp::Nack& nack)
133 {
134 for (auto entry = m_pendingInterestTable.begin(); entry != m_pendingInterestTable.end(); ) {
135 const Interest& pendingInterest = *(*entry)->getInterest();
136 if (pendingInterest == nack.getInterest()) {
137 shared_ptr<PendingInterest> matchedEntry = *entry;
138 entry = m_pendingInterestTable.erase(entry);
139 matchedEntry->invokeNackCallback(nack);
140 }
141 else {
142 ++entry;
143 }
144 }
145 }
146
147public: // producer
148 void
149 asyncSetInterestFilter(shared_ptr<InterestFilterRecord> interestFilterRecord)
150 {
151 m_interestFilterTable.push_back(interestFilterRecord);
152 }
153
154 void
155 asyncUnsetInterestFilter(const InterestFilterId* interestFilterId)
156 {
157 InterestFilterTable::iterator i = std::find_if(m_interestFilterTable.begin(),
158 m_interestFilterTable.end(),
159 MatchInterestFilterId(interestFilterId));
160 if (i != m_interestFilterTable.end()) {
161 m_interestFilterTable.erase(i);
162 }
163 }
164
165 void
166 processInterestFilters(Interest& interest)
167 {
168 for (const auto& filter : m_interestFilterTable) {
169 if (filter->doesMatch(interest.getName())) {
170 filter->invokeInterestCallback(interest);
171 }
172 }
173 }
174
175 void
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000176 asyncSend(const Block& wire)
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700177 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800178 this->ensureConnected(true);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000179 m_face.m_transport->send(wire);
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700180 }
181
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000182public: // prefix registration
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700183 const RegisteredPrefixId*
184 registerPrefix(const Name& prefix,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000185 shared_ptr<InterestFilterRecord> filter,
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700186 const RegisterPrefixSuccessCallback& onSuccess,
187 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700188 uint64_t flags,
Junxiao Shi388ec252014-11-02 15:19:57 -0700189 const nfd::CommandOptions& options)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700190 {
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400191 ControlParameters params;
192 params.setName(prefix);
193 params.setFlags(flags);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700194
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400195 auto prefixToRegister = make_shared<RegisteredPrefix>(prefix, filter, options);
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700196
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000197 m_face.m_nfdController->start<nfd::RibRegisterCommand>(
198 params,
199 [=] (const ControlParameters&) { this->afterPrefixRegistered(prefixToRegister, onSuccess); },
200 [=] (uint32_t code, const std::string& reason) { onFailure(prefixToRegister->getPrefix(), reason); },
201 options);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700202
203 return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get());
204 }
205
206 void
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000207 afterPrefixRegistered(shared_ptr<RegisteredPrefix> registeredPrefix,
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700208 const RegisterPrefixSuccessCallback& onSuccess)
209 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800210 m_registeredPrefixTable.insert(registeredPrefix);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700211
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000212 if (registeredPrefix->getFilter() != nullptr) {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700213 // it was a combined operation
214 m_interestFilterTable.push_back(registeredPrefix->getFilter());
215 }
216
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000217 if (onSuccess != nullptr) {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700218 onSuccess(registeredPrefix->getPrefix());
219 }
220 }
221
222 void
223 asyncUnregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
224 const UnregisterPrefixSuccessCallback& onSuccess,
225 const UnregisterPrefixFailureCallback& onFailure)
226 {
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400227 auto i = std::find_if(m_registeredPrefixTable.begin(),
228 m_registeredPrefixTable.end(),
229 MatchRegisteredPrefixId(registeredPrefixId));
230 if (i != m_registeredPrefixTable.end()) {
231 RegisteredPrefix& record = **i;
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400232 const shared_ptr<InterestFilterRecord>& filter = record.getFilter();
233
234 if (filter != nullptr) {
235 // it was a combined operation
236 m_interestFilterTable.remove(filter);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700237 }
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400238
239 ControlParameters params;
240 params.setName(record.getPrefix());
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000241 m_face.m_nfdController->start<nfd::RibUnregisterCommand>(
242 params,
243 [=] (const ControlParameters&) { this->finalizeUnregisterPrefix(i, onSuccess); },
244 [=] (uint32_t code, const std::string& reason) { onFailure(reason); },
245 record.getCommandOptions());
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400246 }
Alexander Afanasyev851228a2014-10-20 15:55:28 -0400247 else {
Joao Pereiraba1e3b92015-06-01 17:50:37 -0400248 if (onFailure != nullptr) {
Alexander Afanasyev851228a2014-10-20 15:55:28 -0400249 onFailure("Unrecognized PrefixId");
250 }
251 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700252
253 // there cannot be two registered prefixes with the same id
254 }
255
256 void
257 finalizeUnregisterPrefix(RegisteredPrefixTable::iterator item,
258 const UnregisterPrefixSuccessCallback& onSuccess)
259 {
260 m_registeredPrefixTable.erase(item);
261
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000262 if (onSuccess != nullptr) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700263 onSuccess();
264 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700265 }
266
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000267public: // IO routine
268 void
269 ensureConnected(bool wantResume)
270 {
271 if (!m_face.m_transport->isConnected())
272 m_face.m_transport->connect(m_face.m_ioService,
273 [=] (const Block& wire) { m_face.onReceiveElement(wire); });
274
275 if (wantResume && !m_face.m_transport->isReceiving()) {
276 m_face.m_transport->resume();
277 }
278 }
279
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700280 void
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800281 onEmptyPitOrNoRegisteredPrefixes()
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700282 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800283 if (m_pendingInterestTable.empty() && m_registeredPrefixTable.empty()) {
284 m_face.m_transport->pause();
285 if (!m_ioServiceWork) {
286 m_processEventsTimeoutEvent.cancel();
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700287 }
288 }
289 }
290
291private:
292 Face& m_face;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800293 util::Scheduler m_scheduler;
294 util::scheduler::ScopedEventId m_processEventsTimeoutEvent;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700295
296 PendingInterestTable m_pendingInterestTable;
297 InterestFilterTable m_interestFilterTable;
298 RegisteredPrefixTable m_registeredPrefixTable;
299
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800300 unique_ptr<boost::asio::io_service::work> m_ioServiceWork; // if thread needs to be preserved
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700301
302 friend class Face;
303};
304
305} // namespace ndn
306
307#endif // NDN_DETAIL_FACE_IMPL_HPP