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