Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 15 | #include "common.hpp" |
| 16 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 17 | #include "face.hpp" |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 18 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 19 | #include "security/signature-sha256-with-rsa.hpp" |
| 20 | |
| 21 | #include "util/time.hpp" |
| 22 | #include "util/random.hpp" |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 23 | #include "util/config-file.hpp" |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 24 | #include <cstdlib> |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 26 | #include "management/nfd-controller.hpp" |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 27 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 28 | namespace ndn { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 30 | Face::Face() |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 31 | : m_nfdController(new nfd::Controller(*this)) |
| 32 | , m_isDirectNfdFibManagementRequested(false) |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 33 | { |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 34 | const std::string socketName = UnixTransport::getDefaultSocketName(m_config); |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 35 | construct(make_shared<UnixTransport>(socketName), |
Alexander Afanasyev | 505646e | 2014-02-24 20:13:37 -0800 | [diff] [blame] | 36 | make_shared<boost::asio::io_service>()); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 39 | Face::Face(const shared_ptr<boost::asio::io_service>& ioService) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 40 | : m_nfdController(new nfd::Controller(*this)) |
| 41 | , m_isDirectNfdFibManagementRequested(false) |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 42 | { |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 43 | const std::string socketName = UnixTransport::getDefaultSocketName(m_config); |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 44 | construct(make_shared<UnixTransport>(socketName), |
Alexander Afanasyev | 505646e | 2014-02-24 20:13:37 -0800 | [diff] [blame] | 45 | ioService); |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 48 | class NullIoDeleter |
| 49 | { |
| 50 | public: |
| 51 | void |
| 52 | operator()(boost::asio::io_service*) |
| 53 | { |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | Face::Face(boost::asio::io_service& ioService) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 58 | : m_nfdController(new nfd::Controller(*this)) |
| 59 | , m_isDirectNfdFibManagementRequested(false) |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 60 | { |
| 61 | const std::string socketName = UnixTransport::getDefaultSocketName(m_config); |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 62 | construct(make_shared<UnixTransport>(socketName), |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 63 | shared_ptr<boost::asio::io_service>(&ioService, NullIoDeleter())); |
| 64 | } |
| 65 | |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 66 | Face::Face(const std::string& host, const std::string& port/* = "6363"*/) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 67 | : m_nfdController(new nfd::Controller(*this)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 68 | { |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 69 | construct(make_shared<TcpTransport>(host, port), |
Alexander Afanasyev | 505646e | 2014-02-24 20:13:37 -0800 | [diff] [blame] | 70 | make_shared<boost::asio::io_service>()); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 73 | Face::Face(const shared_ptr<Transport>& transport) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 74 | : m_nfdController(new nfd::Controller(*this)) |
| 75 | , m_isDirectNfdFibManagementRequested(false) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 76 | { |
| 77 | construct(transport, |
Alexander Afanasyev | 505646e | 2014-02-24 20:13:37 -0800 | [diff] [blame] | 78 | make_shared<boost::asio::io_service>()); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | Face::Face(const shared_ptr<Transport>& transport, |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 82 | boost::asio::io_service& ioService) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 83 | : m_nfdController(new nfd::Controller(*this)) |
| 84 | , m_isDirectNfdFibManagementRequested(false) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 85 | { |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 86 | construct(transport, |
| 87 | shared_ptr<boost::asio::io_service>(&ioService, NullIoDeleter())); |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 90 | Face::~Face() |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 91 | { |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 92 | delete m_nfdController; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
| 96 | Face::construct(const shared_ptr<Transport>& transport, |
Alexander Afanasyev | 505646e | 2014-02-24 20:13:37 -0800 | [diff] [blame] | 97 | const shared_ptr<boost::asio::io_service>& ioService) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 98 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 99 | m_pitTimeoutCheckTimerActive = false; |
| 100 | m_transport = transport; |
| 101 | m_ioService = ioService; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 102 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 103 | m_pitTimeoutCheckTimer = make_shared<monotonic_deadline_timer>(ref(*m_ioService)); |
| 104 | m_processEventsTimeoutTimer = make_shared<monotonic_deadline_timer>(ref(*m_ioService)); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 105 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 106 | std::string protocol = "nrd-0.1"; |
| 107 | |
| 108 | try |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 109 | { |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 110 | protocol = m_config.getParsedConfiguration().get<std::string>("protocol"); |
| 111 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 112 | catch (boost::property_tree::ptree_bad_path& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 113 | { |
| 114 | // protocol not specified |
| 115 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 116 | catch (boost::property_tree::ptree_bad_data& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 117 | { |
| 118 | throw ConfigFile::Error(error.what()); |
| 119 | } |
| 120 | |
| 121 | if (isSupportedNrdProtocol(protocol)) |
| 122 | { |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 123 | // do nothing |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 124 | } |
Steve DiBenedetto | acab880 | 2014-03-24 11:15:57 -0600 | [diff] [blame] | 125 | else if (isSupportedNfdProtocol(protocol)) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 126 | { |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 127 | m_isDirectNfdFibManagementRequested = true; |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 128 | } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 129 | else |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 130 | { |
| 131 | throw Face::Error("Cannot create controller for unsupported protocol \"" + protocol + "\""); |
| 132 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 135 | const PendingInterestId* |
| 136 | Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout) |
| 137 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 138 | if (!m_transport->isConnected()) |
| 139 | m_transport->connect(*m_ioService, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 140 | bind(&Face::onReceiveElement, this, _1)); |
| 141 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 142 | shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 143 | |
Alexander Afanasyev | 4e50b97 | 2014-03-25 10:57:50 -0700 | [diff] [blame] | 144 | // If the same ioService thread, dispatch directly calls the method |
| 145 | m_ioService->dispatch(bind(&Face::asyncExpressInterest, this, |
| 146 | interestToExpress, onData, onTimeout)); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 147 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 148 | return reinterpret_cast<const PendingInterestId*>(interestToExpress.get()); |
| 149 | } |
| 150 | |
| 151 | const PendingInterestId* |
| 152 | Face::expressInterest(const Name& name, |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 153 | const Interest& tmpl, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 154 | const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/) |
| 155 | { |
| 156 | return expressInterest(Interest(name, |
| 157 | tmpl.getMinSuffixComponents(), |
| 158 | tmpl.getMaxSuffixComponents(), |
| 159 | tmpl.getExclude(), |
| 160 | tmpl.getChildSelector(), |
| 161 | tmpl.getMustBeFresh(), |
| 162 | tmpl.getScope(), |
| 163 | tmpl.getInterestLifetime()), |
| 164 | onData, onTimeout); |
| 165 | } |
| 166 | |
| 167 | void |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 168 | Face::asyncExpressInterest(const shared_ptr<const Interest>& interest, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 169 | const OnData& onData, const OnTimeout& onTimeout) |
| 170 | { |
Alexander Afanasyev | 4e50b97 | 2014-03-25 10:57:50 -0700 | [diff] [blame] | 171 | if (!m_transport->isExpectingData()) |
| 172 | m_transport->resume(); |
| 173 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 174 | m_pendingInterestTable.push_back(make_shared<PendingInterest>(interest, onData, onTimeout)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 175 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 176 | if (!interest->getLocalControlHeader().empty(false, true)) |
| 177 | { |
| 178 | // encode only NextHopFaceId towards the forwarder |
| 179 | m_transport->send(interest->getLocalControlHeader().wireEncode(*interest, false, true), |
| 180 | interest->wireEncode()); |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | m_transport->send(interest->wireEncode()); |
| 185 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 186 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 187 | if (!m_pitTimeoutCheckTimerActive) { |
| 188 | m_pitTimeoutCheckTimerActive = true; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 189 | m_pitTimeoutCheckTimer->expires_from_now(time::milliseconds(100)); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 190 | m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 193 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 194 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 195 | Face::put(const Data& data) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 196 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 197 | if (!m_transport->isConnected()) |
| 198 | m_transport->connect(*m_ioService, |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 199 | bind(&Face::onReceiveElement, this, _1)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 200 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 201 | if (!data.getLocalControlHeader().empty(false, true)) |
| 202 | { |
Alexander Afanasyev | 8301835 | 2014-02-18 19:52:15 -0800 | [diff] [blame] | 203 | m_transport->send(data.getLocalControlHeader().wireEncode(data, false, true), |
| 204 | data.wireEncode()); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 205 | } |
| 206 | else |
| 207 | { |
Alexander Afanasyev | 8301835 | 2014-02-18 19:52:15 -0800 | [diff] [blame] | 208 | m_transport->send(data.wireEncode()); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 209 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 212 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 213 | Face::removePendingInterest(const PendingInterestId* pendingInterestId) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 214 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 215 | m_ioService->post(bind(&Face::asyncRemovePendingInterest, this, pendingInterestId)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
| 219 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 220 | Face::asyncRemovePendingInterest(const PendingInterestId* pendingInterestId) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 221 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 222 | m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 225 | |
| 226 | template<class SignatureGenerator> |
| 227 | const RegisteredPrefixId* |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 228 | Face::registerPrefixImpl(const Name& prefix, |
| 229 | const shared_ptr<InterestFilterRecord>& filter, |
| 230 | const RegisterPrefixSuccessCallback& onSuccess, |
| 231 | const RegisterPrefixFailureCallback& onFailure, |
| 232 | const SignatureGenerator& signatureGenerator) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 233 | { |
| 234 | typedef void (nfd::Controller::*Registrator) |
| 235 | (const nfd::ControlParameters&, |
| 236 | const nfd::Controller::CommandSucceedCallback&, |
| 237 | const nfd::Controller::CommandFailCallback&, |
| 238 | const SignatureGenerator&, |
| 239 | const time::milliseconds&); |
| 240 | |
| 241 | Registrator registrator, unregistrator; |
| 242 | if (!m_isDirectNfdFibManagementRequested) { |
| 243 | registrator = static_cast<Registrator>(&nfd::Controller::start<nfd::RibRegisterCommand>); |
| 244 | unregistrator = static_cast<Registrator>(&nfd::Controller::start<nfd::RibUnregisterCommand>); |
| 245 | } |
| 246 | else { |
| 247 | registrator = static_cast<Registrator>(&nfd::Controller::start<nfd::FibAddNextHopCommand>); |
| 248 | unregistrator = static_cast<Registrator>(&nfd::Controller::start<nfd::FibRemoveNextHopCommand>); |
| 249 | } |
| 250 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 251 | nfd::ControlParameters parameters; |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 252 | parameters.setName(prefix); |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 253 | |
| 254 | RegisteredPrefix::Unregistrator bindedUnregistrator = |
| 255 | bind(unregistrator, m_nfdController, parameters, _1, _2, |
| 256 | signatureGenerator, |
| 257 | m_nfdController->getDefaultCommandTimeout()); |
| 258 | |
| 259 | shared_ptr<RegisteredPrefix> prefixToRegister = |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 260 | ndn::make_shared<RegisteredPrefix>(prefix, filter, bindedUnregistrator); |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 261 | |
| 262 | (m_nfdController->*registrator)(parameters, |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 263 | bind(&Face::afterPrefixRegistered, this, prefixToRegister, onSuccess), |
| 264 | bind(onFailure, prefixToRegister->getPrefix(), _2), |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 265 | signatureGenerator, |
| 266 | m_nfdController->getDefaultCommandTimeout()); |
| 267 | |
| 268 | return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get()); |
| 269 | } |
| 270 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 271 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 272 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 273 | const OnInterest& onInterest, |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 274 | const RegisterPrefixSuccessCallback& onSuccess, |
| 275 | const RegisterPrefixFailureCallback& onFailure, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 276 | const IdentityCertificate& certificate) |
| 277 | { |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 278 | shared_ptr<InterestFilterRecord> filter = |
| 279 | make_shared<InterestFilterRecord>(interestFilter, onInterest); |
| 280 | |
| 281 | return registerPrefixImpl(interestFilter.getPrefix(), filter, |
| 282 | onSuccess, onFailure, |
| 283 | certificate); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 287 | Face::setInterestFilter(const InterestFilter& interestFilter, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 288 | const OnInterest& onInterest, |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 289 | const RegisterPrefixFailureCallback& onFailure, |
| 290 | const IdentityCertificate& certificate) |
| 291 | { |
| 292 | shared_ptr<InterestFilterRecord> filter = |
| 293 | make_shared<InterestFilterRecord>(interestFilter, onInterest); |
| 294 | |
| 295 | return registerPrefixImpl(interestFilter.getPrefix(), filter, |
| 296 | RegisterPrefixSuccessCallback(), onFailure, |
| 297 | certificate); |
| 298 | } |
| 299 | |
| 300 | const RegisteredPrefixId* |
| 301 | Face::setInterestFilter(const InterestFilter& interestFilter, |
| 302 | const OnInterest& onInterest, |
| 303 | const RegisterPrefixSuccessCallback& onSuccess, |
| 304 | const RegisterPrefixFailureCallback& onFailure, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 305 | const Name& identity) |
| 306 | { |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 307 | shared_ptr<InterestFilterRecord> filter = |
| 308 | make_shared<InterestFilterRecord>(interestFilter, onInterest); |
| 309 | |
| 310 | return registerPrefixImpl(interestFilter.getPrefix(), filter, |
| 311 | onSuccess, onFailure, |
| 312 | identity); |
| 313 | } |
| 314 | |
| 315 | const RegisteredPrefixId* |
| 316 | Face::setInterestFilter(const InterestFilter& interestFilter, |
| 317 | const OnInterest& onInterest, |
| 318 | const RegisterPrefixFailureCallback& onFailure, |
| 319 | const Name& identity) |
| 320 | { |
| 321 | shared_ptr<InterestFilterRecord> filter = |
| 322 | make_shared<InterestFilterRecord>(interestFilter, onInterest); |
| 323 | |
| 324 | return registerPrefixImpl(interestFilter.getPrefix(), filter, |
| 325 | RegisterPrefixSuccessCallback(), onFailure, |
| 326 | identity); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | const InterestFilterId* |
| 331 | Face::setInterestFilter(const InterestFilter& interestFilter, |
| 332 | const OnInterest& onInterest) |
| 333 | { |
| 334 | shared_ptr<InterestFilterRecord> filter = |
| 335 | make_shared<InterestFilterRecord>(interestFilter, onInterest); |
| 336 | |
| 337 | getIoService().post(bind(&Face::asyncSetInterestFilter, this, filter)); |
| 338 | |
| 339 | return reinterpret_cast<const InterestFilterId*>(filter.get()); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 342 | void |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 343 | Face::asyncSetInterestFilter(const shared_ptr<InterestFilterRecord>& interestFilterRecord) |
| 344 | { |
| 345 | m_interestFilterTable.push_back(interestFilterRecord); |
| 346 | } |
| 347 | |
| 348 | const RegisteredPrefixId* |
| 349 | Face::registerPrefix(const Name& prefix, |
| 350 | const RegisterPrefixSuccessCallback& onSuccess, |
| 351 | const RegisterPrefixFailureCallback& onFailure, |
| 352 | const IdentityCertificate& certificate) |
| 353 | { |
| 354 | return registerPrefixImpl(prefix, shared_ptr<InterestFilterRecord>(), |
| 355 | onSuccess, onFailure, |
| 356 | certificate); |
| 357 | } |
| 358 | |
| 359 | const RegisteredPrefixId* |
| 360 | Face::registerPrefix(const Name& prefix, |
| 361 | const RegisterPrefixSuccessCallback& onSuccess, |
| 362 | const RegisterPrefixFailureCallback& onFailure, |
| 363 | const Name& identity) |
| 364 | { |
| 365 | return registerPrefixImpl(prefix, shared_ptr<InterestFilterRecord>(), |
| 366 | onSuccess, onFailure, |
| 367 | identity); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | void |
| 372 | Face::afterPrefixRegistered(const shared_ptr<RegisteredPrefix>& registeredPrefix, |
| 373 | const RegisterPrefixSuccessCallback& onSuccess) |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 374 | { |
| 375 | m_registeredPrefixTable.push_back(registeredPrefix); |
| 376 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 377 | if (static_cast<bool>(registeredPrefix->getFilter())) { |
| 378 | // it was a combined operation |
| 379 | m_interestFilterTable.push_back(registeredPrefix->getFilter()); |
| 380 | } |
| 381 | |
| 382 | if (static_cast<bool>(onSuccess)) { |
| 383 | onSuccess(registeredPrefix->getPrefix()); |
| 384 | } |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 388 | Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 389 | { |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 390 | m_ioService->post(bind(&Face::asyncUnregisterPrefix, this, registeredPrefixId, |
| 391 | UnregisterPrefixSuccessCallback(), UnregisterPrefixFailureCallback())); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 394 | |
| 395 | void |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 396 | Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId, |
| 397 | const UnregisterPrefixSuccessCallback& onSuccess, |
| 398 | const UnregisterPrefixFailureCallback& onFailure) |
| 399 | { |
| 400 | m_ioService->post(bind(&Face::asyncUnregisterPrefix, this, registeredPrefixId, |
| 401 | onSuccess, onFailure)); |
| 402 | } |
| 403 | |
| 404 | |
| 405 | void |
| 406 | Face::asyncUnsetInterestFilter(const InterestFilterId* interestFilterId) |
| 407 | { |
| 408 | InterestFilterTable::iterator i = std::find_if(m_interestFilterTable.begin(), |
| 409 | m_interestFilterTable.end(), |
| 410 | MatchInterestFilterId(interestFilterId)); |
| 411 | if (i != m_interestFilterTable.end()) |
| 412 | { |
| 413 | m_interestFilterTable.erase(i); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | |
| 418 | void |
| 419 | Face::asyncUnregisterPrefix(const RegisteredPrefixId* registeredPrefixId, |
| 420 | const UnregisterPrefixSuccessCallback& onSuccess, |
| 421 | const UnregisterPrefixFailureCallback& onFailure) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 422 | { |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 423 | RegisteredPrefixTable::iterator i = std::find_if(m_registeredPrefixTable.begin(), |
| 424 | m_registeredPrefixTable.end(), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 425 | MatchRegisteredPrefixId(registeredPrefixId)); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 426 | if (i != m_registeredPrefixTable.end()) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 427 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 428 | const shared_ptr<InterestFilterRecord>& filter = (*i)->getFilter(); |
| 429 | if (static_cast<bool>(filter)) |
| 430 | { |
| 431 | // it was a combined operation |
| 432 | m_interestFilterTable.remove(filter); |
| 433 | } |
| 434 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 435 | (*i)->unregister(bind(&Face::finalizeUnregisterPrefix, this, i, onSuccess), |
| 436 | bind(onFailure, _2)); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 437 | } |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 438 | else |
| 439 | onFailure("Unrecognized PrefixId"); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 440 | |
| 441 | // there cannot be two registered prefixes with the same id |
| 442 | } |
| 443 | |
| 444 | void |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 445 | Face::finalizeUnregisterPrefix(RegisteredPrefixTable::iterator item, |
| 446 | const UnregisterPrefixSuccessCallback& onSuccess) |
Alexander Afanasyev | 12dfbad | 2014-02-11 14:42:46 -0800 | [diff] [blame] | 447 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 448 | m_registeredPrefixTable.erase(item); |
Alexander Afanasyev | 12dfbad | 2014-02-11 14:42:46 -0800 | [diff] [blame] | 449 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 450 | if (!m_pitTimeoutCheckTimerActive && m_registeredPrefixTable.empty()) |
Alexander Afanasyev | 12dfbad | 2014-02-11 14:42:46 -0800 | [diff] [blame] | 451 | { |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 452 | m_transport->pause(); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 453 | if (!m_ioServiceWork) { |
| 454 | m_processEventsTimeoutTimer->cancel(); |
Alexander Afanasyev | 12dfbad | 2014-02-11 14:42:46 -0800 | [diff] [blame] | 455 | } |
| 456 | } |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 457 | onSuccess(); |
Alexander Afanasyev | 12dfbad | 2014-02-11 14:42:46 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 460 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 461 | Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/, |
| 462 | bool keepThread/* = false*/) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 463 | { |
| 464 | try |
| 465 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 466 | if (timeout < time::milliseconds::zero()) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 467 | { |
| 468 | // do not block if timeout is negative, but process pending events |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 469 | m_ioService->poll(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 473 | if (timeout > time::milliseconds::zero()) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 474 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 475 | m_processEventsTimeoutTimer->expires_from_now(time::milliseconds(timeout)); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 476 | m_processEventsTimeoutTimer->async_wait(&fireProcessEventsTimeout); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 477 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 478 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 479 | if (keepThread) { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 480 | // work will ensure that m_ioService is running until work object exists |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 481 | m_ioServiceWork = make_shared<boost::asio::io_service::work>(ref(*m_ioService)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 482 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 483 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 484 | m_ioService->run(); |
| 485 | m_ioService->reset(); // so it is possible to run processEvents again (if necessary) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 486 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 487 | catch (Face::ProcessEventsTimeout&) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 488 | { |
| 489 | // break |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 490 | m_ioService->reset(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 491 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 492 | catch (std::exception&) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 493 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 494 | m_ioService->reset(); |
| 495 | m_pendingInterestTable.clear(); |
| 496 | m_registeredPrefixTable.clear(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 497 | throw; |
| 498 | } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 501 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 502 | Face::shutdown() |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 503 | { |
Alexander Afanasyev | 7dced46 | 2014-03-19 15:12:32 -0700 | [diff] [blame] | 504 | m_ioService->post(bind(&Face::asyncShutdown, this)); |
| 505 | } |
| 506 | |
| 507 | void |
| 508 | Face::asyncShutdown() |
| 509 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 510 | m_pendingInterestTable.clear(); |
| 511 | m_registeredPrefixTable.clear(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 512 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 513 | if (m_transport->isConnected()) |
| 514 | m_transport->close(); |
| 515 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 516 | m_pitTimeoutCheckTimer->cancel(); |
| 517 | m_processEventsTimeoutTimer->cancel(); |
| 518 | m_pitTimeoutCheckTimerActive = false; |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 521 | void |
| 522 | Face::fireProcessEventsTimeout(const boost::system::error_code& error) |
| 523 | { |
| 524 | if (!error) // can fire for some other reason, e.g., cancelled |
| 525 | throw Face::ProcessEventsTimeout(); |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 526 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 527 | |
| 528 | void |
| 529 | Face::checkPitExpire() |
| 530 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 531 | // Check for PIT entry timeouts. |
| 532 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 533 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 534 | PendingInterestTable::iterator i = m_pendingInterestTable.begin(); |
| 535 | while (i != m_pendingInterestTable.end()) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 536 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 537 | if ((*i)->isTimedOut(now)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 538 | { |
| 539 | // Save the PendingInterest and remove it from the PIT. Then call the callback. |
| 540 | shared_ptr<PendingInterest> pendingInterest = *i; |
| 541 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 542 | i = m_pendingInterestTable.erase(i); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 543 | |
| 544 | pendingInterest->callTimeout(); |
| 545 | } |
| 546 | else |
| 547 | ++i; |
| 548 | } |
| 549 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 550 | if (!m_pendingInterestTable.empty()) { |
| 551 | m_pitTimeoutCheckTimerActive = true; |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 552 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 553 | m_pitTimeoutCheckTimer->expires_from_now(time::milliseconds(100)); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 554 | m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 555 | } |
| 556 | else { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 557 | m_pitTimeoutCheckTimerActive = false; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 558 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 559 | if (m_registeredPrefixTable.empty()) { |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 560 | m_transport->pause(); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 561 | if (!m_ioServiceWork) { |
| 562 | m_processEventsTimeoutTimer->cancel(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 569 | void |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 570 | Face::onReceiveElement(const Block& blockFromDaemon) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 571 | { |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 572 | const Block& block = nfd::LocalControlHeader::getPayload(blockFromDaemon); |
| 573 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 574 | if (block.type() == Tlv::Interest) |
| 575 | { |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 576 | shared_ptr<Interest> interest = make_shared<Interest>(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 577 | interest->wireDecode(block); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 578 | if (&block != &blockFromDaemon) |
| 579 | interest->getLocalControlHeader().wireDecode(blockFromDaemon); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 580 | |
Alexander Afanasyev | 42c8185 | 2014-02-25 21:37:26 -0800 | [diff] [blame] | 581 | processInterestFilters(*interest); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 582 | } |
| 583 | else if (block.type() == Tlv::Data) |
| 584 | { |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 585 | shared_ptr<Data> data = make_shared<Data>(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 586 | data->wireDecode(block); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 587 | if (&block != &blockFromDaemon) |
| 588 | data->getLocalControlHeader().wireDecode(blockFromDaemon); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 589 | |
Alexander Afanasyev | 42c8185 | 2014-02-25 21:37:26 -0800 | [diff] [blame] | 590 | satisfyPendingInterests(*data); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 591 | |
Alexander Afanasyev | 42c8185 | 2014-02-25 21:37:26 -0800 | [diff] [blame] | 592 | if (m_pendingInterestTable.empty()) { |
| 593 | m_pitTimeoutCheckTimer->cancel(); // this will cause checkPitExpire invocation |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 594 | } |
| 595 | } |
Yingdi Yu | f9fa52f | 2014-02-06 12:27:32 -0800 | [diff] [blame] | 596 | // ignore any other type |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Alexander Afanasyev | 42c8185 | 2014-02-25 21:37:26 -0800 | [diff] [blame] | 599 | void |
| 600 | Face::satisfyPendingInterests(Data& data) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 601 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 602 | for (PendingInterestTable::iterator i = m_pendingInterestTable.begin (); |
Alexander Afanasyev | 42c8185 | 2014-02-25 21:37:26 -0800 | [diff] [blame] | 603 | i != m_pendingInterestTable.end(); |
| 604 | ) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 605 | { |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 606 | if ((*i)->getInterest()->matchesData(data)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 607 | { |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 608 | // Copy pointers to the objects and remove the PIT entry before calling the callback. |
Alexander Afanasyev | 42c8185 | 2014-02-25 21:37:26 -0800 | [diff] [blame] | 609 | OnData onData = (*i)->getOnData(); |
| 610 | shared_ptr<const Interest> interest = (*i)->getInterest(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 611 | |
Alexander Afanasyev | 42c8185 | 2014-02-25 21:37:26 -0800 | [diff] [blame] | 612 | PendingInterestTable::iterator next = i; |
| 613 | ++next; |
| 614 | m_pendingInterestTable.erase(i); |
| 615 | i = next; |
| 616 | |
| 617 | if (static_cast<bool>(onData)) { |
| 618 | onData(*interest, data); |
| 619 | } |
| 620 | } |
| 621 | else |
| 622 | ++i; |
| 623 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 624 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 625 | |
Alexander Afanasyev | 42c8185 | 2014-02-25 21:37:26 -0800 | [diff] [blame] | 626 | void |
| 627 | Face::processInterestFilters(Interest& interest) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 628 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 629 | for (InterestFilterTable::iterator i = m_interestFilterTable.begin(); |
| 630 | i != m_interestFilterTable.end(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 631 | ++i) |
| 632 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 633 | if ((*i)->doesMatch(interest.getName())) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 634 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 635 | (**i)(interest); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 636 | } |
| 637 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | } // namespace ndn |