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