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