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