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 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 8 | #include "common.hpp" |
| 9 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 10 | #include "face.hpp" |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 11 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 12 | #include "security/signature-sha256-with-rsa.hpp" |
| 13 | |
| 14 | #include "util/time.hpp" |
| 15 | #include "util/random.hpp" |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 16 | #include <cstdlib> |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 17 | |
| 18 | #include "management/ndnd-controller.hpp" |
| 19 | #include "management/nfd-controller.hpp" |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 20 | #include "management/nrd-controller.hpp" |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 21 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 22 | namespace ndn { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 24 | Face::Face() |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 25 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 26 | construct(shared_ptr<Transport>(new UnixTransport()), |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 27 | make_shared<boost::asio::io_service>()); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 28 | } |
| 29 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 30 | Face::Face(const shared_ptr<boost::asio::io_service> &ioService) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 31 | { |
| 32 | construct(shared_ptr<Transport>(new UnixTransport()), |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 33 | ioService); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 36 | Face::Face(const std::string &host, const std::string &port/* = "6363"*/) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 37 | { |
| 38 | construct(shared_ptr<Transport>(new TcpTransport(host, port)), |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 39 | make_shared<boost::asio::io_service>()); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 42 | Face::Face(const shared_ptr<Transport>& transport) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 43 | { |
| 44 | construct(transport, |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 45 | make_shared<boost::asio::io_service>()); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | Face::Face(const shared_ptr<Transport>& transport, |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 49 | const shared_ptr<boost::asio::io_service> &ioService) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 50 | { |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 51 | construct(transport, ioService); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void |
| 55 | Face::construct(const shared_ptr<Transport>& transport, |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 56 | const shared_ptr<boost::asio::io_service> &ioService) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 57 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 58 | m_pitTimeoutCheckTimerActive = false; |
| 59 | m_transport = transport; |
| 60 | m_ioService = ioService; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 62 | m_pitTimeoutCheckTimer = make_shared<boost::asio::deadline_timer>(boost::ref(*m_ioService)); |
| 63 | m_processEventsTimeoutTimer = make_shared<boost::asio::deadline_timer>(boost::ref(*m_ioService)); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 65 | if (std::getenv("NFD") != 0) |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 66 | { |
| 67 | if (std::getenv("NRD") != 0) |
| 68 | m_fwController = make_shared<nrd::Controller>(boost::ref(*this)); |
| 69 | else |
| 70 | m_fwController = make_shared<nfd::Controller>(boost::ref(*this)); |
| 71 | } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 72 | else |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 73 | m_fwController = make_shared<ndnd::Controller>(boost::ref(*this)); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | const PendingInterestId* |
| 78 | Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout) |
| 79 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 80 | if (!m_transport->isConnected()) |
| 81 | m_transport->connect(*m_ioService, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 82 | bind(&Face::onReceiveElement, this, _1)); |
| 83 | |
| 84 | shared_ptr<const Interest> interestToExpress(new Interest(interest)); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 85 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 86 | m_ioService->post(bind(&Face::asyncExpressInterest, this, interestToExpress, onData, onTimeout)); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 88 | return reinterpret_cast<const PendingInterestId*>(interestToExpress.get()); |
| 89 | } |
| 90 | |
| 91 | const PendingInterestId* |
| 92 | Face::expressInterest(const Name& name, |
| 93 | const Interest &tmpl, |
| 94 | const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/) |
| 95 | { |
| 96 | return expressInterest(Interest(name, |
| 97 | tmpl.getMinSuffixComponents(), |
| 98 | tmpl.getMaxSuffixComponents(), |
| 99 | tmpl.getExclude(), |
| 100 | tmpl.getChildSelector(), |
| 101 | tmpl.getMustBeFresh(), |
| 102 | tmpl.getScope(), |
| 103 | tmpl.getInterestLifetime()), |
| 104 | onData, onTimeout); |
| 105 | } |
| 106 | |
| 107 | void |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 108 | Face::asyncExpressInterest(const shared_ptr<const Interest>& interest, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 109 | const OnData& onData, const OnTimeout& onTimeout) |
| 110 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 111 | m_pendingInterestTable.push_back(shared_ptr<PendingInterest>(new PendingInterest |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 112 | (interest, onData, onTimeout))); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 114 | if (!interest->getLocalControlHeader().empty(false, true)) |
| 115 | { |
| 116 | // encode only NextHopFaceId towards the forwarder |
| 117 | m_transport->send(interest->getLocalControlHeader().wireEncode(*interest, false, true), |
| 118 | interest->wireEncode()); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | m_transport->send(interest->wireEncode()); |
| 123 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 125 | if (!m_pitTimeoutCheckTimerActive) { |
| 126 | m_pitTimeoutCheckTimerActive = true; |
| 127 | m_pitTimeoutCheckTimer->expires_from_now(boost::posix_time::milliseconds(100)); |
| 128 | m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 131 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 132 | void |
| 133 | Face::put(const Data &data) |
| 134 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 135 | if (!m_transport->isConnected()) |
| 136 | m_transport->connect(*m_ioService, |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 137 | bind(&Face::onReceiveElement, this, _1)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 139 | if (!data.getLocalControlHeader().empty(false, true)) |
| 140 | { |
Alexander Afanasyev | 8301835 | 2014-02-18 19:52:15 -0800 | [diff] [blame] | 141 | m_transport->send(data.getLocalControlHeader().wireEncode(data, false, true), |
| 142 | data.wireEncode()); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 143 | } |
| 144 | else |
| 145 | { |
Alexander Afanasyev | 8301835 | 2014-02-18 19:52:15 -0800 | [diff] [blame] | 146 | m_transport->send(data.wireEncode()); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 147 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 150 | void |
| 151 | Face::removePendingInterest(const PendingInterestId *pendingInterestId) |
| 152 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 153 | m_ioService->post(bind(&Face::asyncRemovePendingInterest, this, pendingInterestId)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
| 157 | void |
| 158 | Face::asyncRemovePendingInterest(const PendingInterestId *pendingInterestId) |
| 159 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 160 | m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | const RegisteredPrefixId* |
| 164 | Face::setInterestFilter(const Name& prefix, |
| 165 | const OnInterest& onInterest, |
| 166 | const OnSetInterestFilterFailed& onSetInterestFilterFailed) |
| 167 | { |
| 168 | shared_ptr<RegisteredPrefix> prefixToRegister(new RegisteredPrefix(prefix, onInterest)); |
| 169 | |
| 170 | m_fwController->selfRegisterPrefix(prefixToRegister->getPrefix(), |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 171 | bind(&RegisteredPrefixTable::push_back, &m_registeredPrefixTable, prefixToRegister), |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 172 | bind(onSetInterestFilterFailed, prefixToRegister->getPrefix(), _1)); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 173 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 174 | return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get()); |
| 175 | } |
| 176 | |
| 177 | void |
| 178 | Face::unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId) |
| 179 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 180 | m_ioService->post(bind(&Face::asyncUnsetInterestFilter, this, registeredPrefixId)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void |
| 184 | Face::asyncUnsetInterestFilter(const RegisteredPrefixId *registeredPrefixId) |
| 185 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 186 | RegisteredPrefixTable::iterator i = std::find_if(m_registeredPrefixTable.begin(), m_registeredPrefixTable.end(), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 187 | MatchRegisteredPrefixId(registeredPrefixId)); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 188 | if (i != m_registeredPrefixTable.end()) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 189 | { |
| 190 | m_fwController->selfDeregisterPrefix((*i)->getPrefix(), |
Alexander Afanasyev | 12dfbad | 2014-02-11 14:42:46 -0800 | [diff] [blame] | 191 | bind(&Face::finalizeUnsertInterestFilter, this, i), |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 192 | Controller::FailCallback()); |
| 193 | } |
| 194 | |
| 195 | // there cannot be two registered prefixes with the same id. if there are, then something is broken |
| 196 | } |
| 197 | |
Alexander Afanasyev | 12dfbad | 2014-02-11 14:42:46 -0800 | [diff] [blame] | 198 | void |
| 199 | Face::finalizeUnsertInterestFilter(RegisteredPrefixTable::iterator item) |
| 200 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 201 | m_registeredPrefixTable.erase(item); |
Alexander Afanasyev | 12dfbad | 2014-02-11 14:42:46 -0800 | [diff] [blame] | 202 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 203 | if (!m_pitTimeoutCheckTimerActive && m_registeredPrefixTable.empty()) |
Alexander Afanasyev | 12dfbad | 2014-02-11 14:42:46 -0800 | [diff] [blame] | 204 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 205 | m_transport->close(); |
| 206 | if (!m_ioServiceWork) { |
| 207 | m_processEventsTimeoutTimer->cancel(); |
Alexander Afanasyev | 12dfbad | 2014-02-11 14:42:46 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 212 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 213 | Face::processEvents(Milliseconds timeout/* = 0 */, bool keepThread/* = false*/) |
| 214 | { |
| 215 | try |
| 216 | { |
| 217 | if (timeout < 0) |
| 218 | { |
| 219 | // do not block if timeout is negative, but process pending events |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 220 | m_ioService->poll(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
| 224 | if (timeout > 0) |
| 225 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 226 | m_processEventsTimeoutTimer->expires_from_now(boost::posix_time::milliseconds(timeout)); |
| 227 | m_processEventsTimeoutTimer->async_wait(&fireProcessEventsTimeout); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 228 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 229 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 230 | if (keepThread) { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 231 | // work will ensure that m_ioService is running until work object exists |
| 232 | m_ioServiceWork = make_shared<boost::asio::io_service::work>(boost::ref(*m_ioService)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 233 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 234 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 235 | m_ioService->run(); |
| 236 | 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] | 237 | } |
| 238 | catch(Face::ProcessEventsTimeout &) |
| 239 | { |
| 240 | // break |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 241 | m_ioService->reset(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 242 | } |
| 243 | catch(const std::exception &) |
| 244 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 245 | m_ioService->reset(); |
| 246 | m_pendingInterestTable.clear(); |
| 247 | m_registeredPrefixTable.clear(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 248 | throw; |
| 249 | } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 252 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 253 | Face::shutdown() |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 254 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 255 | m_pendingInterestTable.clear(); |
| 256 | m_registeredPrefixTable.clear(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 257 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 258 | m_transport->close(); |
| 259 | m_pitTimeoutCheckTimer->cancel(); |
| 260 | m_processEventsTimeoutTimer->cancel(); |
| 261 | m_pitTimeoutCheckTimerActive = false; |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 264 | void |
| 265 | Face::fireProcessEventsTimeout(const boost::system::error_code& error) |
| 266 | { |
| 267 | if (!error) // can fire for some other reason, e.g., cancelled |
| 268 | throw Face::ProcessEventsTimeout(); |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 269 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 270 | |
| 271 | void |
| 272 | Face::checkPitExpire() |
| 273 | { |
| 274 | // Check for PIT entry timeouts. Go backwards through the list so we can erase entries. |
| 275 | MillisecondsSince1970 nowMilliseconds = getNowMilliseconds(); |
| 276 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 277 | PendingInterestTable::iterator i = m_pendingInterestTable.begin(); |
| 278 | while (i != m_pendingInterestTable.end()) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 279 | { |
| 280 | if ((*i)->isTimedOut(nowMilliseconds)) |
| 281 | { |
| 282 | // Save the PendingInterest and remove it from the PIT. Then call the callback. |
| 283 | shared_ptr<PendingInterest> pendingInterest = *i; |
| 284 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 285 | i = m_pendingInterestTable.erase(i); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 286 | |
| 287 | pendingInterest->callTimeout(); |
| 288 | } |
| 289 | else |
| 290 | ++i; |
| 291 | } |
| 292 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 293 | if (!m_pendingInterestTable.empty()) { |
| 294 | m_pitTimeoutCheckTimerActive = true; |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 295 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 296 | m_pitTimeoutCheckTimer->expires_from_now(boost::posix_time::milliseconds(100)); |
| 297 | m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this)); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 298 | } |
| 299 | else { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 300 | m_pitTimeoutCheckTimerActive = false; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 301 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 302 | if (m_registeredPrefixTable.empty()) { |
| 303 | m_transport->close(); |
| 304 | if (!m_ioServiceWork) { |
| 305 | m_processEventsTimeoutTimer->cancel(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 312 | void |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 313 | Face::onReceiveElement(const Block& blockFromDaemon) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 314 | { |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 315 | const Block& block = nfd::LocalControlHeader::getPayload(blockFromDaemon); |
| 316 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 317 | if (block.type() == Tlv::Interest) |
| 318 | { |
| 319 | shared_ptr<Interest> interest(new Interest()); |
| 320 | interest->wireDecode(block); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 321 | if (&block != &blockFromDaemon) |
| 322 | interest->getLocalControlHeader().wireDecode(blockFromDaemon); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 323 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 324 | RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName()); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 325 | if (entry != m_registeredPrefixTable.end()) { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 326 | (*entry)->getOnInterest()((*entry)->getPrefix(), *interest); |
| 327 | } |
| 328 | } |
| 329 | else if (block.type() == Tlv::Data) |
| 330 | { |
| 331 | shared_ptr<Data> data(new Data()); |
| 332 | data->wireDecode(block); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 333 | if (&block != &blockFromDaemon) |
| 334 | data->getLocalControlHeader().wireDecode(blockFromDaemon); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 335 | |
| 336 | PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName()); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 337 | if (entry != m_pendingInterestTable.end()) { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 338 | // Copy pointers to the needed objects and remove the PIT entry before the calling the callback. |
| 339 | const OnData onData = (*entry)->getOnData(); |
| 340 | const shared_ptr<const Interest> interest = (*entry)->getInterest(); |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 341 | m_pendingInterestTable.erase(entry); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 342 | |
| 343 | if (onData) { |
| 344 | onData(*interest, *data); |
| 345 | } |
| 346 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 347 | if (m_pendingInterestTable.empty()) { |
| 348 | m_pitTimeoutCheckTimer->cancel(); // this will cause checkPitExpire invocation |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | } |
Yingdi Yu | f9fa52f | 2014-02-06 12:27:32 -0800 | [diff] [blame] | 352 | // ignore any other type |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 355 | Face::PendingInterestTable::iterator |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 356 | Face::getEntryIndexForExpressedInterest(const Name& name) |
| 357 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 358 | for (PendingInterestTable::iterator i = m_pendingInterestTable.begin (); |
| 359 | i != m_pendingInterestTable.end(); ++i) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 360 | { |
| 361 | if ((*i)->getInterest()->matchesName(name)) |
| 362 | { |
| 363 | return i; |
| 364 | } |
| 365 | } |
| 366 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 367 | return m_pendingInterestTable.end(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 368 | } |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 369 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 370 | Face::RegisteredPrefixTable::iterator |
| 371 | Face::getEntryForRegisteredPrefix(const Name& name) |
| 372 | { |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 373 | RegisteredPrefixTable::iterator longestPrefix = m_registeredPrefixTable.end(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 374 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 375 | for (RegisteredPrefixTable::iterator i = m_registeredPrefixTable.begin(); |
| 376 | i != m_registeredPrefixTable.end(); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 377 | ++i) |
| 378 | { |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 379 | if ((*i)->getPrefix().isPrefixOf(name)) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 380 | { |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 381 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 382 | if (longestPrefix == m_registeredPrefixTable.end() || |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 383 | (*i)->getPrefix().size() > (*longestPrefix)->getPrefix().size()) |
| 384 | { |
| 385 | longestPrefix = i; |
| 386 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | return longestPrefix; |
| 390 | } |
| 391 | |
| 392 | } // namespace ndn |