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