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 | bf50a1a | 2013-08-20 18:01:01 -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 | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame^] | 8 | #include "common.hpp" |
| 9 | |
Alexander Afanasyev | e9fdb80 | 2014-02-05 17:36:51 -0800 | [diff] [blame] | 10 | #include "node.hpp" |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 11 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 12 | #include "forwarding-entry.hpp" |
| 13 | #include "face-instance.hpp" |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 14 | #include "status-response.hpp" |
Alexander Afanasyev | e9fdb80 | 2014-02-05 17:36:51 -0800 | [diff] [blame] | 15 | #include "security/signature-sha256-with-rsa.hpp" |
| 16 | |
| 17 | #include "util/time.hpp" |
| 18 | #include "util/random.hpp" |
| 19 | #include "util/ndnd-id-fetcher.hpp" |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 20 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 21 | using namespace std; |
Alexander Afanasyev | 6be1a6a | 2014-01-06 00:08:14 -0800 | [diff] [blame] | 22 | #if NDN_CPP_HAVE_CXX11 |
| 23 | // In the std library, the placeholders are in a different namespace than boost. |
| 24 | using namespace ndn::func_lib::placeholders; |
| 25 | #endif |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | |
Alexander Afanasyev | 0b688dc | 2013-12-18 16:43:37 -0800 | [diff] [blame] | 29 | Node::Node(const ptr_lib::shared_ptr<Transport>& transport) |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 30 | : pitTimeoutCheckTimerActive_(false) |
| 31 | , transport_(transport) |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 32 | , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0) |
| 33 | { |
| 34 | ioService_ = ptr_lib::make_shared<boost::asio::io_service>(); |
| 35 | pitTimeoutCheckTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_)); |
| 36 | processEventsTimeoutTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_)); |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | Node::Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<boost::asio::io_service> &ioService) |
| 40 | : ioService_(ioService) |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 41 | , pitTimeoutCheckTimerActive_(false) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 42 | , transport_(transport) |
| 43 | , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 44 | { |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 45 | pitTimeoutCheckTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_)); |
| 46 | processEventsTimeoutTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_)); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 49 | const PendingInterestId* |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 50 | Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 51 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 52 | if (!transport_->isConnected()) |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 53 | transport_->connect(*ioService_, |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 54 | ptr_lib::bind(&Node::onReceiveElement, this, _1)); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 56 | ptr_lib::shared_ptr<const Interest> interestToExpress(new Interest(interest)); |
| 57 | |
| 58 | ioService_->post(func_lib::bind(&Node::asyncExpressInterest, this, interestToExpress, onData, onTimeout)); |
| 59 | |
| 60 | return reinterpret_cast<const PendingInterestId*>(interestToExpress.get()); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | Node::asyncExpressInterest(const ptr_lib::shared_ptr<const Interest> &interest, const OnData& onData, const OnTimeout& onTimeout) |
| 65 | { |
| 66 | pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest |
| 67 | (interest, onData, onTimeout))); |
| 68 | |
| 69 | transport_->send(interest->wireEncode()); |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 70 | |
| 71 | if (!pitTimeoutCheckTimerActive_) { |
| 72 | pitTimeoutCheckTimerActive_ = true; |
| 73 | pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100)); |
| 74 | pitTimeoutCheckTimer_->async_wait(func_lib::bind(&Node::checkPitExpire, this)); |
| 75 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 76 | } |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 77 | |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 78 | void |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 79 | Node::put(const Data &data) |
| 80 | { |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 81 | if (!transport_->isConnected()) |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 82 | transport_->connect(*ioService_, |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 83 | ptr_lib::bind(&Node::onReceiveElement, this, _1)); |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 84 | |
| 85 | transport_->send(data.wireEncode()); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 90 | Node::removePendingInterest(const PendingInterestId *pendingInterestId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 91 | { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 92 | ioService_->post(func_lib::bind(&Node::asyncRemovePendingInterest, this, pendingInterestId)); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 95 | |
| 96 | void |
| 97 | Node::asyncRemovePendingInterest(const PendingInterestId *pendingInterestId) |
| 98 | { |
| 99 | std::remove_if(pendingInterestTable_.begin(), pendingInterestTable_.end(), |
| 100 | MatchPendingInterestId(pendingInterestId)); |
| 101 | } |
| 102 | |
| 103 | const RegisteredPrefixId* |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 104 | Node::registerPrefix |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 105 | (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 106 | { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 107 | ptr_lib::shared_ptr<RegisteredPrefix> prefixToRegister(new RegisteredPrefix(prefix, onInterest)); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 108 | |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 109 | if (ndndId_.size() == 0) { |
| 110 | // First fetch the ndndId of the connected hub. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 111 | NdndIdFetcher fetcher(ndndId_, |
| 112 | func_lib::bind(&Node::registerPrefixHelper, this, |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 113 | prefixToRegister, onRegisterFailed, flags), |
| 114 | func_lib::bind(onRegisterFailed, prefixToRegister->getPrefix().shared_from_this())); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 115 | |
| 116 | // @todo: Check if this crash |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 117 | // It is OK for func_lib::function make a copy of the function object because the Info is in a ptr_lib::shared_ptr. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 118 | expressInterest(ndndIdFetcherInterest_, fetcher, fetcher); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 119 | } |
| 120 | else |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 121 | registerPrefixHelper(prefixToRegister, onRegisterFailed, flags); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 122 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 123 | return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get()); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 127 | Node::removeRegisteredPrefix(const RegisteredPrefixId *registeredPrefixId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 128 | { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 129 | RegisteredPrefixTable::iterator i = std::find_if(registeredPrefixTable_.begin(), registeredPrefixTable_.end(), |
| 130 | MatchRegisteredPrefixId(registeredPrefixId)); |
| 131 | if (i != registeredPrefixTable_.end()) |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 132 | { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 133 | ForwardingEntry forwardingEntry("unreg", (*i)->getPrefix(), faceId_); |
| 134 | Data data; |
Alexander Afanasyev | e9fdb80 | 2014-02-05 17:36:51 -0800 | [diff] [blame] | 135 | // This ensures uniqueness of each prefix registration commands |
| 136 | data.setName(Name().appendVersion(random::generateWord32())); |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 137 | data.setContent(forwardingEntry.wireEncode()); |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 139 | SignatureSha256WithRsa signature; |
| 140 | signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>())); |
| 141 | data.setSignature(signature); |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 143 | // Create an interest where the name has the encoded Data packet. |
| 144 | Name interestName; |
| 145 | interestName.append("ndnx"); |
| 146 | interestName.append(ndndId_); |
| 147 | interestName.append("unreg"); |
| 148 | interestName.append(data.wireEncode()); |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 150 | Interest interest(interestName); |
| 151 | interest.setScope(1); |
| 152 | interest.setInterestLifetime(1000); |
Alexander Afanasyev | fb44c97 | 2014-01-26 11:38:07 -0800 | [diff] [blame] | 153 | interest.setMustBeFresh(true); |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 154 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 155 | expressInterest(interest, OnData(), OnTimeout()); |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 156 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 157 | registeredPrefixTable_.erase(i); |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 158 | } |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 159 | |
| 160 | // there cannot be two registered prefixes with the same id. if there are, then something is broken |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 163 | void |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 164 | Node::registerPrefixHelper(const ptr_lib::shared_ptr<RegisteredPrefix> &prefixToRegister, |
Alexander Afanasyev | 7910049 | 2014-01-03 15:35:38 -0800 | [diff] [blame] | 165 | const OnRegisterFailed& onRegisterFailed, |
| 166 | const ForwardingFlags& flags) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 167 | { |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 168 | // Create a ForwardingEntry. |
Alexander Afanasyev | fbdfa09 | 2013-12-28 20:44:49 -0800 | [diff] [blame] | 169 | |
| 170 | // AlexA: ndnd ignores any freshness that is larger than 3600 sec and sets 300 sec instead |
| 171 | // to register "forever" (=2000000000 sec), freshnessPeriod must be omitted |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 172 | ForwardingEntry forwardingEntry("selfreg", prefixToRegister->getPrefix(), -1, flags, -1); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 173 | Block content = forwardingEntry.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 174 | |
Alexander Afanasyev | e9fdb80 | 2014-02-05 17:36:51 -0800 | [diff] [blame] | 175 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 176 | // Set the ForwardingEntry as the content of a Data packet and sign. |
| 177 | Data data; |
Alexander Afanasyev | e9fdb80 | 2014-02-05 17:36:51 -0800 | [diff] [blame] | 178 | // This ensures uniqueness of each prefix registration commands |
| 179 | data.setName(Name().appendVersion(random::generateWord32())); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 180 | data.setContent(content); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 181 | |
Alexander Afanasyev | 7910049 | 2014-01-03 15:35:38 -0800 | [diff] [blame] | 182 | // Create an empty signature, since nobody going to verify it for now |
| 183 | // @todo In the future, we may require real signatures to do the registration |
| 184 | SignatureSha256WithRsa signature; |
| 185 | signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>())); |
| 186 | data.setSignature(signature); |
| 187 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 188 | // Create an interest where the name has the encoded Data packet. |
| 189 | Name interestName; |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 190 | interestName.append("ndnx"); |
Jeff Thompson | 3a71563 | 2013-10-31 11:36:35 -0700 | [diff] [blame] | 191 | interestName.append(ndndId_); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 192 | interestName.append("selfreg"); |
Alexander Afanasyev | 7910049 | 2014-01-03 15:35:38 -0800 | [diff] [blame] | 193 | interestName.append(data.wireEncode()); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 194 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 195 | Interest interest(interestName); |
| 196 | interest.setScope(1); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 197 | interest.setInterestLifetime(1000); |
Alexander Afanasyev | fb44c97 | 2014-01-26 11:38:07 -0800 | [diff] [blame] | 198 | interest.setMustBeFresh(true); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 199 | |
| 200 | expressInterest(interest, |
| 201 | func_lib::bind(&Node::registerPrefixFinal, this, |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 202 | prefixToRegister, onRegisterFailed, _1, _2), |
| 203 | func_lib::bind(onRegisterFailed, prefixToRegister->getPrefix().shared_from_this())); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 207 | Node::registerPrefixFinal(const ptr_lib::shared_ptr<RegisteredPrefix> &prefixToRegister, |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 208 | const OnRegisterFailed& onRegisterFailed, |
| 209 | const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&data) |
| 210 | { |
| 211 | Block content = data->getContent(); |
| 212 | content.parse(); |
| 213 | |
| 214 | if (content.getAll().empty()) |
| 215 | { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 216 | onRegisterFailed(prefixToRegister->getPrefix().shared_from_this()); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 220 | Block::element_iterator val = content.getAll().begin(); |
| 221 | |
| 222 | switch(val->type()) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 223 | { |
| 224 | case Tlv::FaceManagement::ForwardingEntry: |
| 225 | { |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 226 | ForwardingEntry entry; |
| 227 | entry.wireDecode(*val); |
| 228 | |
| 229 | // Save the onInterest callback and send the registration interest. |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 230 | registeredPrefixTable_.push_back(prefixToRegister); |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 231 | |
| 232 | /// @todo Notify user about successful registration |
| 233 | |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 234 | // succeeded |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 235 | return; |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 236 | } |
| 237 | case Tlv::FaceManagement::StatusResponse: |
| 238 | { |
| 239 | // failed :( |
| 240 | StatusResponse resp; |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 241 | resp.wireDecode(*val); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 242 | |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 243 | // std::cerr << "StatusReponse: " << resp << std::endl; |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 244 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 245 | onRegisterFailed(prefixToRegister->getPrefix().shared_from_this()); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 246 | return; |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 247 | } |
| 248 | default: |
| 249 | { |
| 250 | // failed :( |
| 251 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 252 | onRegisterFailed(prefixToRegister->getPrefix().shared_from_this()); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 253 | return; |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 254 | } |
| 255 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 258 | void |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 259 | Node::processEvents(Milliseconds timeout/* = 0 */, bool keepThread/* = false*/) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 260 | { |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 261 | try |
| 262 | { |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 263 | if (timeout < 0) |
| 264 | { |
| 265 | // do not block if timeout is negative, but process pending events |
| 266 | ioService_->poll(); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | if (timeout > 0) |
| 271 | { |
| 272 | processEventsTimeoutTimer_->expires_from_now(boost::posix_time::milliseconds(timeout)); |
Jeff Thompson | 6e9fc18 | 2014-01-23 13:04:08 -0800 | [diff] [blame] | 273 | processEventsTimeoutTimer_->async_wait(&fireProcessEventsTimeout); |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | if (keepThread) { |
| 277 | // work will ensure that ioService_ is running until work object exists |
| 278 | ioServiceWork_ = ptr_lib::make_shared<boost::asio::io_service::work>(boost::ref(*ioService_)); |
| 279 | } |
| 280 | |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 281 | ioService_->run(); |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 282 | ioService_->reset(); // so it is possible to run processEvents again (if necessary) |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 283 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 284 | catch(Node::ProcessEventsTimeout &) |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 285 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 286 | // break |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 287 | ioService_->reset(); |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 288 | } |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 289 | catch(const std::exception &) |
| 290 | { |
| 291 | ioService_->reset(); |
| 292 | pendingInterestTable_.clear(); |
| 293 | registeredPrefixTable_.clear(); |
| 294 | throw; |
| 295 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 299 | Node::fireProcessEventsTimeout(const boost::system::error_code& error) |
| 300 | { |
| 301 | if (!error) // can fire for some other reason, e.g., cancelled |
| 302 | throw Node::ProcessEventsTimeout(); |
| 303 | } |
| 304 | |
| 305 | void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 306 | Node::checkPitExpire() |
| 307 | { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 308 | // Check for PIT entry timeouts. Go backwards through the list so we can erase entries. |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 309 | MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds(); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 310 | for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) { |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 311 | if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) { |
| 312 | // Save the PendingInterest and remove it from the PIT. Then call the callback. |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 313 | ptr_lib::shared_ptr<PendingInterest> pendingInterest = pendingInterestTable_[i]; |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 314 | pendingInterestTable_.erase(pendingInterestTable_.begin() + i); |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 315 | pendingInterest->callTimeout(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 316 | |
| 317 | // Refresh now since the timeout callback might have delayed. |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 318 | nowMilliseconds = ndn_getNowMilliseconds(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 321 | |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 322 | if (!pendingInterestTable_.empty()) { |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 323 | pitTimeoutCheckTimerActive_ = true; |
| 324 | |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 325 | pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100)); |
| 326 | pitTimeoutCheckTimer_->async_wait(func_lib::bind(&Node::checkPitExpire, this)); |
| 327 | } |
| 328 | else { |
| 329 | pitTimeoutCheckTimerActive_ = false; |
| 330 | |
| 331 | if (registeredPrefixTable_.empty()) { |
| 332 | transport_->close(); |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 333 | if (!ioServiceWork_) { |
| 334 | processEventsTimeoutTimer_->cancel(); |
| 335 | } |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 336 | } |
| 337 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 340 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 341 | void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 342 | Node::onReceiveElement(const Block &block) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 343 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 344 | if (block.type() == Tlv::Interest) |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 345 | { |
| 346 | ptr_lib::shared_ptr<Interest> interest(new Interest()); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 347 | interest->wireDecode(block); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 348 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 349 | RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName()); |
| 350 | if (entry != registeredPrefixTable_.end()) { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 351 | (*entry)->getOnInterest()((*entry)->getPrefix().shared_from_this(), interest); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 352 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 353 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 354 | else if (block.type() == Tlv::Data) |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 355 | { |
| 356 | ptr_lib::shared_ptr<Data> data(new Data()); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 357 | data->wireDecode(block); |
| 358 | |
| 359 | PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName()); |
| 360 | if (entry != pendingInterestTable_.end()) { |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 361 | // Copy pointers to the needed objects and remove the PIT entry before the calling the callback. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 362 | const OnData onData = (*entry)->getOnData(); |
| 363 | const ptr_lib::shared_ptr<const Interest> interest = (*entry)->getInterest(); |
| 364 | pendingInterestTable_.erase(entry); |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 365 | |
| 366 | if (onData) { |
| 367 | onData(interest, data); |
| 368 | } |
| 369 | |
| 370 | if (pendingInterestTable_.empty()) { |
| 371 | pitTimeoutCheckTimer_->cancel(); // this will cause checkPitExpire invocation |
| 372 | } |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 377 | void |
| 378 | Node::shutdown() |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 379 | { |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 380 | pendingInterestTable_.clear(); |
| 381 | registeredPrefixTable_.clear(); |
| 382 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 383 | transport_->close(); |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 384 | pitTimeoutCheckTimer_->cancel(); |
| 385 | processEventsTimeoutTimer_->cancel(); |
| 386 | pitTimeoutCheckTimerActive_ = false; |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 389 | Node::PendingInterestTable::iterator |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 390 | Node::getEntryIndexForExpressedInterest(const Name& name) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 391 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 392 | for (PendingInterestTable::iterator i = pendingInterestTable_.begin (); |
| 393 | i != pendingInterestTable_.end(); ++i) |
| 394 | { |
| 395 | if ((*i)->getInterest()->matchesName(name)) |
| 396 | { |
| 397 | return i; |
| 398 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 399 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 400 | |
| 401 | return pendingInterestTable_.end(); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 402 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 403 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 404 | Node::RegisteredPrefixTable::iterator |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 405 | Node::getEntryForRegisteredPrefix(const Name& name) |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 406 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 407 | RegisteredPrefixTable::iterator longestPrefix = registeredPrefixTable_.end(); |
| 408 | |
| 409 | for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin(); |
| 410 | i != registeredPrefixTable_.end(); |
| 411 | ++i) |
| 412 | { |
| 413 | if (longestPrefix == registeredPrefixTable_.end() || |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 414 | (*i)->getPrefix().size() > (*longestPrefix)->getPrefix().size()) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 415 | { |
| 416 | longestPrefix = i; |
| 417 | } |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 418 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 419 | return longestPrefix; |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 422 | Node::PendingInterest::PendingInterest(const ptr_lib::shared_ptr<const Interest>& interest, |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 423 | const OnData& onData, const OnTimeout& onTimeout) |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 424 | : interest_(interest), |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 425 | onData_(onData), onTimeout_(onTimeout) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 426 | { |
| 427 | // Set up timeoutTime_. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 428 | if (interest_->getInterestLifetime() >= 0) |
| 429 | timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + interest_->getInterestLifetime(); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 430 | else |
| 431 | // No timeout. |
Alexander Afanasyev | b24a68a | 2013-12-28 16:53:21 -0800 | [diff] [blame] | 432 | /** |
| 433 | * @todo Set more meaningful default timeout. This timeout MUST exist. |
| 434 | */ |
| 435 | timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + 4000; |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 438 | void |
| 439 | Node::PendingInterest::callTimeout() |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 440 | { |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 441 | if (onTimeout_) { |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 442 | onTimeout_(interest_); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 443 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 444 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 445 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 446 | } |