Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 6 | #include <sys/time.h> |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 7 | #include "encoding/binary-xml-decoder.hpp" |
| 8 | #include "c/encoding/binary-xml.h" |
Jeff Thompson | b09fcc1 | 2013-08-22 10:37:10 -0700 | [diff] [blame] | 9 | #include "node.hpp" |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 10 | |
| 11 | using namespace std; |
| 12 | using namespace ndn::ptr_lib; |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 16 | // Use gettimeofday to return the current time in milliseconds. |
| 17 | static inline double getNowMilliseconds() |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 18 | { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 19 | timeval t; |
| 20 | gettimeofday(&t, NULL); |
| 21 | return t.tv_sec * 1000.0 + t.tv_usec / 1000.0; |
| 22 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 23 | |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame^] | 24 | Node::Node(const ptr_lib::shared_ptr<Transport> &transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &connectionInfo) |
| 25 | : transport_(transport), connectionInfo_(connectionInfo), |
| 26 | 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] | 27 | { |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 30 | void Node::expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 31 | { |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 32 | // TODO: Properly check if we are already connected to the expected host. |
| 33 | if (!transport_->getIsConnected()) |
| 34 | transport_->connect(*connectionInfo_, *this); |
| 35 | |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 36 | shared_ptr<PitEntry> pitEntry(new PitEntry(shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout)); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 37 | pit_.push_back(pitEntry); |
| 38 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 39 | shared_ptr<vector<unsigned char> > encoding = pitEntry->getInterest()->wireEncode(); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 40 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 41 | transport_->send(*encoding); |
| 42 | } |
| 43 | |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 44 | void Node::registerPrefix(const Name &prefix, const OnInterest &onInterest, int flags) |
| 45 | { |
| 46 | if (ndndId_.size() == 0) { |
| 47 | // First fetch the ndndId of the connected hub. |
| 48 | NdndIdFetcher fetcher(make_shared<NdndIdFetcher::Info>(this, prefix, onInterest, flags)); |
| 49 | // It is OK for func_lib::function make a copy of the function object because the Info is in a shared_ptr. |
| 50 | expressInterest(ndndIdFetcherInterest_, fetcher, fetcher); |
| 51 | } |
| 52 | else |
| 53 | registerPrefixHelper(prefix, onInterest, flags); |
| 54 | } |
| 55 | |
| 56 | void Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &ndndIdData) |
| 57 | { |
| 58 | if (ndndIdData->getSignedInfo().getPublisherPublicKeyDigest().getPublisherPublicKeyDigest().size() > 0) { |
| 59 | // Set the ndndId_ and continue. |
| 60 | info_->node_.ndndId_ = ndndIdData->getSignedInfo().getPublisherPublicKeyDigest().getPublisherPublicKeyDigest(); |
| 61 | info_->node_.registerPrefixHelper(info_->prefix_, info_->onInterest_, info_->flags_); |
| 62 | } |
| 63 | // TODO: else need to log not getting the ndndId. |
| 64 | } |
| 65 | |
| 66 | void Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest> &timedOutInterest) |
| 67 | { |
| 68 | // TODO: Log the timeout. |
| 69 | } |
| 70 | |
| 71 | void Node::registerPrefixHelper(const Name &prefix, const OnInterest &onInterest, int flags) |
| 72 | { |
| 73 | throw logic_error("need to finish implementing registerPrefix"); |
| 74 | } |
| 75 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 76 | void Node::processEvents() |
| 77 | { |
| 78 | transport_->processEvents(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 79 | |
| 80 | // Check for PIT entry timeouts. Go backwards through the list so we can erase entries. |
| 81 | double nowMilliseconds = getNowMilliseconds(); |
| 82 | for (int i = (int)pit_.size() - 1; i >= 0; --i) { |
| 83 | if (pit_[i]->checkTimeout(this, nowMilliseconds)) { |
| 84 | pit_.erase(pit_.begin() + i); |
| 85 | |
| 86 | // Refresh now since the timeout callback might have delayed. |
| 87 | nowMilliseconds = getNowMilliseconds(); |
| 88 | } |
| 89 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Jeff Thompson | 8b173cc | 2013-08-21 17:54:12 -0700 | [diff] [blame] | 92 | void Node::onReceivedElement(const unsigned char *element, unsigned int elementLength) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 93 | { |
| 94 | BinaryXmlDecoder decoder(element, elementLength); |
| 95 | |
| 96 | if (decoder.peekDTag(ndn_BinaryXml_DTag_ContentObject)) { |
| 97 | shared_ptr<Data> data(new Data()); |
| 98 | data->wireDecode(element, elementLength); |
| 99 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 100 | int iPitEntry = getEntryIndexForExpressedInterest(data->getName()); |
| 101 | if (iPitEntry >= 0) { |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 102 | // Copy pointers to the needed objects and remove the PIT entry before the calling the callback. |
| 103 | const OnData onData = pit_[iPitEntry]->getOnData(); |
| 104 | const ptr_lib::shared_ptr<const Interest> interest = pit_[iPitEntry]->getInterest(); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 105 | pit_.erase(pit_.begin() + iPitEntry); |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 106 | onData(interest, data); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 107 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | void Node::shutdown() |
| 112 | { |
| 113 | transport_->close(); |
| 114 | } |
| 115 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 116 | int Node::getEntryIndexForExpressedInterest(const Name &name) |
| 117 | { |
| 118 | // TODO: Doesn't this belong in the Name class? |
| 119 | vector<struct ndn_NameComponent> nameComponents; |
| 120 | nameComponents.reserve(name.getComponentCount()); |
| 121 | struct ndn_Name nameStruct; |
| 122 | ndn_Name_init(&nameStruct, &nameComponents[0], nameComponents.capacity()); |
| 123 | name.get(nameStruct); |
| 124 | |
| 125 | int iResult = -1; |
| 126 | |
| 127 | for (unsigned int i = 0; i < pit_.size(); ++i) { |
| 128 | if (ndn_Interest_matchesName((struct ndn_Interest *)&pit_[i]->getInterestStruct(), &nameStruct)) { |
| 129 | if (iResult < 0 || |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 130 | pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents) |
| 131 | // Update to the longer match. |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 132 | iResult = i; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return iResult; |
| 137 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 138 | |
| 139 | Node::PitEntry::PitEntry(const ptr_lib::shared_ptr<const Interest> &interest, const OnData &onData, const OnTimeout &onTimeout) |
| 140 | : interest_(interest), onData_(onData), onTimeout_(onTimeout) |
| 141 | { |
| 142 | // Set up timeoutTime_. |
| 143 | if (interest_->getInterestLifetimeMilliseconds() >= 0.0) |
| 144 | timeoutTimeMilliseconds_ = getNowMilliseconds() + interest_->getInterestLifetimeMilliseconds(); |
| 145 | else |
| 146 | // No timeout. |
| 147 | timeoutTimeMilliseconds_ = -1.0; |
| 148 | |
| 149 | // Set up interestStruct_. |
| 150 | // TODO: Doesn't this belong in the Interest class? |
| 151 | nameComponents_.reserve(interest_->getName().getComponentCount()); |
| 152 | excludeEntries_.reserve(interest_->getExclude().getEntryCount()); |
| 153 | ndn_Interest_init |
| 154 | (&interestStruct_, &nameComponents_[0], nameComponents_.capacity(), &excludeEntries_[0], excludeEntries_.capacity()); |
| 155 | interest_->get(interestStruct_); |
| 156 | } |
| 157 | |
| 158 | bool Node::PitEntry::checkTimeout(Node *parent, double nowMilliseconds) |
| 159 | { |
| 160 | if (timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_) { |
| 161 | if (onTimeout_) { |
| 162 | // Ignore all exceptions. |
| 163 | try { |
| 164 | onTimeout_(interest_); |
| 165 | } |
| 166 | catch (...) { } |
| 167 | } |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | else |
| 172 | return false; |
| 173 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 174 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 175 | } |