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. |
Jeff Thompson | 7a67cb6 | 2013-08-26 11:43:18 -0700 | [diff] [blame^] | 60 | // TODO: If there are multiple connected hubs, the NDN ID is really stored per connected hub. |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 61 | info_->node_.ndndId_ = ndndIdData->getSignedInfo().getPublisherPublicKeyDigest().getPublisherPublicKeyDigest(); |
| 62 | info_->node_.registerPrefixHelper(info_->prefix_, info_->onInterest_, info_->flags_); |
| 63 | } |
| 64 | // TODO: else need to log not getting the ndndId. |
| 65 | } |
| 66 | |
| 67 | void Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest> &timedOutInterest) |
| 68 | { |
| 69 | // TODO: Log the timeout. |
| 70 | } |
| 71 | |
| 72 | void Node::registerPrefixHelper(const Name &prefix, const OnInterest &onInterest, int flags) |
| 73 | { |
| 74 | throw logic_error("need to finish implementing registerPrefix"); |
| 75 | } |
| 76 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 77 | void Node::processEvents() |
| 78 | { |
| 79 | transport_->processEvents(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 80 | |
| 81 | // Check for PIT entry timeouts. Go backwards through the list so we can erase entries. |
| 82 | double nowMilliseconds = getNowMilliseconds(); |
| 83 | for (int i = (int)pit_.size() - 1; i >= 0; --i) { |
| 84 | if (pit_[i]->checkTimeout(this, nowMilliseconds)) { |
| 85 | pit_.erase(pit_.begin() + i); |
| 86 | |
| 87 | // Refresh now since the timeout callback might have delayed. |
| 88 | nowMilliseconds = getNowMilliseconds(); |
| 89 | } |
| 90 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Jeff Thompson | 8b173cc | 2013-08-21 17:54:12 -0700 | [diff] [blame] | 93 | void Node::onReceivedElement(const unsigned char *element, unsigned int elementLength) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 94 | { |
| 95 | BinaryXmlDecoder decoder(element, elementLength); |
| 96 | |
| 97 | if (decoder.peekDTag(ndn_BinaryXml_DTag_ContentObject)) { |
| 98 | shared_ptr<Data> data(new Data()); |
| 99 | data->wireDecode(element, elementLength); |
| 100 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 101 | int iPitEntry = getEntryIndexForExpressedInterest(data->getName()); |
| 102 | if (iPitEntry >= 0) { |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 103 | // Copy pointers to the needed objects and remove the PIT entry before the calling the callback. |
| 104 | const OnData onData = pit_[iPitEntry]->getOnData(); |
| 105 | const ptr_lib::shared_ptr<const Interest> interest = pit_[iPitEntry]->getInterest(); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 106 | pit_.erase(pit_.begin() + iPitEntry); |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 107 | onData(interest, data); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 108 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | void Node::shutdown() |
| 113 | { |
| 114 | transport_->close(); |
| 115 | } |
| 116 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 117 | int Node::getEntryIndexForExpressedInterest(const Name &name) |
| 118 | { |
| 119 | // TODO: Doesn't this belong in the Name class? |
| 120 | vector<struct ndn_NameComponent> nameComponents; |
| 121 | nameComponents.reserve(name.getComponentCount()); |
| 122 | struct ndn_Name nameStruct; |
| 123 | ndn_Name_init(&nameStruct, &nameComponents[0], nameComponents.capacity()); |
| 124 | name.get(nameStruct); |
| 125 | |
| 126 | int iResult = -1; |
| 127 | |
| 128 | for (unsigned int i = 0; i < pit_.size(); ++i) { |
| 129 | if (ndn_Interest_matchesName((struct ndn_Interest *)&pit_[i]->getInterestStruct(), &nameStruct)) { |
| 130 | if (iResult < 0 || |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 131 | pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents) |
| 132 | // Update to the longer match. |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 133 | iResult = i; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return iResult; |
| 138 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 139 | |
| 140 | Node::PitEntry::PitEntry(const ptr_lib::shared_ptr<const Interest> &interest, const OnData &onData, const OnTimeout &onTimeout) |
| 141 | : interest_(interest), onData_(onData), onTimeout_(onTimeout) |
| 142 | { |
| 143 | // Set up timeoutTime_. |
| 144 | if (interest_->getInterestLifetimeMilliseconds() >= 0.0) |
| 145 | timeoutTimeMilliseconds_ = getNowMilliseconds() + interest_->getInterestLifetimeMilliseconds(); |
| 146 | else |
| 147 | // No timeout. |
| 148 | timeoutTimeMilliseconds_ = -1.0; |
| 149 | |
| 150 | // Set up interestStruct_. |
| 151 | // TODO: Doesn't this belong in the Interest class? |
| 152 | nameComponents_.reserve(interest_->getName().getComponentCount()); |
| 153 | excludeEntries_.reserve(interest_->getExclude().getEntryCount()); |
| 154 | ndn_Interest_init |
| 155 | (&interestStruct_, &nameComponents_[0], nameComponents_.capacity(), &excludeEntries_[0], excludeEntries_.capacity()); |
| 156 | interest_->get(interestStruct_); |
| 157 | } |
| 158 | |
| 159 | bool Node::PitEntry::checkTimeout(Node *parent, double nowMilliseconds) |
| 160 | { |
| 161 | if (timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_) { |
| 162 | if (onTimeout_) { |
| 163 | // Ignore all exceptions. |
| 164 | try { |
| 165 | onTimeout_(interest_); |
| 166 | } |
| 167 | catch (...) { } |
| 168 | } |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | else |
| 173 | return false; |
| 174 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 175 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 176 | } |