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