blob: bcb290f43a2b0d39b504078cf53bd28ca76fc2b0 [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/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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"
30
31#include "../util/scheduler.hpp"
32#include "../util/config-file.hpp"
33
34#include "transport/transport.hpp"
35#include "transport/unix-transport.hpp"
36#include "transport/tcp-transport.hpp"
37
38#include "management/nfd-controller.hpp"
39
40namespace ndn {
41
42class Face::Impl : noncopyable
43{
44public:
45 typedef std::list<shared_ptr<PendingInterest> > PendingInterestTable;
46 typedef std::list<shared_ptr<InterestFilterRecord> > InterestFilterTable;
47 typedef std::list<shared_ptr<RegisteredPrefix> > RegisteredPrefixTable;
48
49 explicit
50 Impl(Face& face)
51 : m_face(face)
52 {
53 }
54
55 /////////////////////////////////////////////////////////////////////////////////////////////////
56 /////////////////////////////////////////////////////////////////////////////////////////////////
57
58 void
59 satisfyPendingInterests(Data& data)
60 {
61 for (PendingInterestTable::iterator i = m_pendingInterestTable.begin();
62 i != m_pendingInterestTable.end();
63 )
64 {
65 if ((*i)->getInterest()->matchesData(data))
66 {
67 // Copy pointers to the objects and remove the PIT entry before calling the callback.
68 OnData onData = (*i)->getOnData();
69 shared_ptr<const Interest> interest = (*i)->getInterest();
70
71 PendingInterestTable::iterator next = i;
72 ++next;
73 m_pendingInterestTable.erase(i);
74 i = next;
75
76 if (static_cast<bool>(onData)) {
77 onData(*interest, data);
78 }
79 }
80 else
81 ++i;
82 }
83 }
84
85 void
86 processInterestFilters(Interest& interest)
87 {
88 for (InterestFilterTable::iterator i = m_interestFilterTable.begin();
89 i != m_interestFilterTable.end();
90 ++i)
91 {
92 if ((*i)->doesMatch(interest.getName()))
93 {
94 (**i)(interest);
95 }
96 }
97 }
98
99 /////////////////////////////////////////////////////////////////////////////////////////////////
100 /////////////////////////////////////////////////////////////////////////////////////////////////
101
102 void
103 asyncExpressInterest(const shared_ptr<const Interest>& interest,
104 const OnData& onData, const OnTimeout& onTimeout)
105 {
106 if (!m_face.m_transport->isExpectingData())
107 m_face.m_transport->resume();
108
109 m_pendingInterestTable.push_back(make_shared<PendingInterest>(interest, onData, onTimeout));
110
111 if (!interest->getLocalControlHeader().empty(false, true))
112 {
113 // encode only NextHopFaceId towards the forwarder
114 m_face.m_transport->send(interest->getLocalControlHeader()
115 .wireEncode(*interest, false, true),
116 interest->wireEncode());
117 }
118 else
119 {
120 m_face.m_transport->send(interest->wireEncode());
121 }
122
123 if (!m_pitTimeoutCheckTimerActive) {
124 m_pitTimeoutCheckTimerActive = true;
125 m_pitTimeoutCheckTimer->expires_from_now(time::milliseconds(100));
126 m_pitTimeoutCheckTimer->async_wait(bind(&Impl::checkPitExpire, this));
127 }
128 }
129
130 void
131 asyncRemovePendingInterest(const PendingInterestId* pendingInterestId)
132 {
133 m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId));
134 }
135
136 /////////////////////////////////////////////////////////////////////////////////////////////////
137 /////////////////////////////////////////////////////////////////////////////////////////////////
138
139 void
140 asyncSetInterestFilter(const shared_ptr<InterestFilterRecord>& interestFilterRecord)
141 {
142 m_interestFilterTable.push_back(interestFilterRecord);
143 }
144
145 void
146 asyncUnsetInterestFilter(const InterestFilterId* interestFilterId)
147 {
148 InterestFilterTable::iterator i = std::find_if(m_interestFilterTable.begin(),
149 m_interestFilterTable.end(),
150 MatchInterestFilterId(interestFilterId));
151 if (i != m_interestFilterTable.end())
152 {
153 m_interestFilterTable.erase(i);
154 }
155 }
156
157 /////////////////////////////////////////////////////////////////////////////////////////////////
158 /////////////////////////////////////////////////////////////////////////////////////////////////
159
160 template<class SignatureGenerator>
161 const RegisteredPrefixId*
162 registerPrefix(const Name& prefix,
163 const shared_ptr<InterestFilterRecord>& filter,
164 const RegisterPrefixSuccessCallback& onSuccess,
165 const RegisterPrefixFailureCallback& onFailure,
166 const SignatureGenerator& signatureGenerator)
167 {
168 using namespace nfd;
169
170 typedef void (nfd::Controller::*Registrator)
171 (const nfd::ControlParameters&,
172 const nfd::Controller::CommandSucceedCallback&,
173 const nfd::Controller::CommandFailCallback&,
174 const SignatureGenerator&,
175 const time::milliseconds&);
176
177 Registrator registrator, unregistrator;
178 if (!m_face.m_isDirectNfdFibManagementRequested) {
179 registrator = static_cast<Registrator>(&Controller::start<RibRegisterCommand>);
180 unregistrator = static_cast<Registrator>(&Controller::start<RibUnregisterCommand>);
181 }
182 else {
183 registrator = static_cast<Registrator>(&Controller::start<FibAddNextHopCommand>);
184 unregistrator = static_cast<Registrator>(&Controller::start<FibRemoveNextHopCommand>);
185 }
186
187 ControlParameters parameters;
188 parameters.setName(prefix);
189
190 RegisteredPrefix::Unregistrator bindedUnregistrator =
191 bind(unregistrator, m_face.m_nfdController, parameters, _1, _2,
192 signatureGenerator,
193 m_face.m_nfdController->getDefaultCommandTimeout());
194
195 shared_ptr<RegisteredPrefix> prefixToRegister =
196 ndn::make_shared<RegisteredPrefix>(prefix, filter, bindedUnregistrator);
197
198 ((*m_face.m_nfdController).*registrator)(parameters,
199 bind(&Impl::afterPrefixRegistered, this,
200 prefixToRegister, onSuccess),
201 bind(onFailure, prefixToRegister->getPrefix(), _2),
202 signatureGenerator,
203 m_face.m_nfdController->getDefaultCommandTimeout());
204
205 return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get());
206 }
207
208 void
209 afterPrefixRegistered(const shared_ptr<RegisteredPrefix>& registeredPrefix,
210 const RegisterPrefixSuccessCallback& onSuccess)
211 {
212 m_registeredPrefixTable.push_back(registeredPrefix);
213
214 if (static_cast<bool>(registeredPrefix->getFilter())) {
215 // it was a combined operation
216 m_interestFilterTable.push_back(registeredPrefix->getFilter());
217 }
218
219 if (static_cast<bool>(onSuccess)) {
220 onSuccess(registeredPrefix->getPrefix());
221 }
222 }
223
224 void
225 asyncUnregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
226 const UnregisterPrefixSuccessCallback& onSuccess,
227 const UnregisterPrefixFailureCallback& onFailure)
228 {
229 RegisteredPrefixTable::iterator i = std::find_if(m_registeredPrefixTable.begin(),
230 m_registeredPrefixTable.end(),
231 MatchRegisteredPrefixId(registeredPrefixId));
232 if (i != m_registeredPrefixTable.end())
233 {
234 const shared_ptr<InterestFilterRecord>& filter = (*i)->getFilter();
235 if (static_cast<bool>(filter))
236 {
237 // it was a combined operation
238 m_interestFilterTable.remove(filter);
239 }
240
241 (*i)->unregister(bind(&Impl::finalizeUnregisterPrefix, this, i, onSuccess),
242 bind(onFailure, _2));
243 }
244 else
245 onFailure("Unrecognized PrefixId");
246
247 // there cannot be two registered prefixes with the same id
248 }
249
250 void
251 finalizeUnregisterPrefix(RegisteredPrefixTable::iterator item,
252 const UnregisterPrefixSuccessCallback& onSuccess)
253 {
254 m_registeredPrefixTable.erase(item);
255
256 if (!m_pitTimeoutCheckTimerActive && m_registeredPrefixTable.empty())
257 {
258 m_face.m_transport->pause();
259 if (!m_ioServiceWork) {
260 m_processEventsTimeoutTimer->cancel();
261 }
262 }
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700263
264 if (static_cast<bool>(onSuccess)) {
265 onSuccess();
266 }
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700267 }
268
269 /////////////////////////////////////////////////////////////////////////////////////////////////
270 /////////////////////////////////////////////////////////////////////////////////////////////////
271
272 void
273 checkPitExpire()
274 {
275 // Check for PIT entry timeouts.
276 time::steady_clock::TimePoint now = time::steady_clock::now();
277
278 PendingInterestTable::iterator i = m_pendingInterestTable.begin();
279 while (i != m_pendingInterestTable.end())
280 {
281 if ((*i)->isTimedOut(now))
282 {
283 // Save the PendingInterest and remove it from the PIT. Then call the callback.
284 shared_ptr<PendingInterest> pendingInterest = *i;
285
286 i = m_pendingInterestTable.erase(i);
287
288 pendingInterest->callTimeout();
289 }
290 else
291 ++i;
292 }
293
294 if (!m_pendingInterestTable.empty()) {
295 m_pitTimeoutCheckTimerActive = true;
296
297 m_pitTimeoutCheckTimer->expires_from_now(time::milliseconds(100));
298 m_pitTimeoutCheckTimer->async_wait(bind(&Impl::checkPitExpire, this));
299 }
300 else {
301 m_pitTimeoutCheckTimerActive = false;
302
303 if (m_registeredPrefixTable.empty()) {
304 m_face.m_transport->pause();
305 if (!m_ioServiceWork) {
306 m_processEventsTimeoutTimer->cancel();
307 }
308 }
309 }
310 }
311
312private:
313 Face& m_face;
314
315 PendingInterestTable m_pendingInterestTable;
316 InterestFilterTable m_interestFilterTable;
317 RegisteredPrefixTable m_registeredPrefixTable;
318
319 ConfigFile m_config;
320
321 shared_ptr<boost::asio::io_service::work> m_ioServiceWork; // if thread needs to be preserved
322 shared_ptr<monotonic_deadline_timer> m_pitTimeoutCheckTimer;
323 bool m_pitTimeoutCheckTimerActive;
324 shared_ptr<monotonic_deadline_timer> m_processEventsTimeoutTimer;
325
326 friend class Face;
327};
328
329} // namespace ndn
330
331#endif // NDN_DETAIL_FACE_IMPL_HPP