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" |
| 9 | #include "data.hpp" |
Jeff Thompson | b09fcc1 | 2013-08-22 10:37:10 -0700 | [diff] [blame] | 10 | #include "node.hpp" |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 11 | |
| 12 | using namespace std; |
| 13 | using namespace ndn::ptr_lib; |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 17 | // Use gettimeofday to return the current time in milliseconds. |
| 18 | static inline double getNowMilliseconds() |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 19 | { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 20 | timeval t; |
| 21 | gettimeofday(&t, NULL); |
| 22 | return t.tv_sec * 1000.0 + t.tv_usec / 1000.0; |
| 23 | } |
| 24 | |
| 25 | Node::PitEntry::PitEntry(const ptr_lib::shared_ptr<const Interest> &interest, Closure *closure) |
| 26 | : interest_(interest), closure_(closure) |
| 27 | { |
| 28 | // Set up timeoutTime_. |
| 29 | if (interest_->getInterestLifetimeMilliseconds() >= 0.0) |
| 30 | timeoutTimeMilliseconds_ = getNowMilliseconds() + interest_->getInterestLifetimeMilliseconds(); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 31 | else |
| 32 | // No timeout. |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 33 | timeoutTimeMilliseconds_ = -1.0; |
| 34 | |
| 35 | // Set up interestStruct_. |
| 36 | // TODO: Doesn't this belong in the Interest class? |
| 37 | nameComponents_.reserve(interest_->getName().getComponentCount()); |
| 38 | excludeEntries_.reserve(interest_->getExclude().getEntryCount()); |
| 39 | ndn_Interest_init |
| 40 | (&interestStruct_, &nameComponents_[0], nameComponents_.capacity(), &excludeEntries_[0], excludeEntries_.capacity()); |
| 41 | interest_->get(interestStruct_); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 44 | bool Node::PitEntry::checkTimeout(Node *parent, double nowMilliseconds) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 45 | { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 46 | if (timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_) { |
| 47 | shared_ptr<Data> dummyData; |
| 48 | UpcallInfo upcallInfo(parent, interest_, 0, dummyData); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 49 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 50 | // Ignore all exceptions. |
| 51 | try { |
| 52 | closure_->upcall(UPCALL_INTEREST_TIMED_OUT, upcallInfo); |
| 53 | } |
| 54 | catch (...) { } |
| 55 | |
| 56 | return true; |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 57 | } |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 58 | else |
| 59 | return false; |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 62 | void Node::expressInterest(const Name &name, Closure *closure, const Interest *interestTemplate) |
| 63 | { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 64 | shared_ptr<const Interest> interest; |
| 65 | if (interestTemplate) |
| 66 | interest.reset(new Interest |
| 67 | (name, interestTemplate->getMinSuffixComponents(), interestTemplate->getMaxSuffixComponents(), |
| 68 | interestTemplate->getPublisherPublicKeyDigest(), interestTemplate->getExclude(), |
| 69 | interestTemplate->getChildSelector(), interestTemplate->getAnswerOriginKind(), |
| 70 | interestTemplate->getScope(), interestTemplate->getInterestLifetimeMilliseconds())); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 71 | else |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 72 | interest.reset(new Interest(name, 4000.0)); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 73 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 74 | shared_ptr<PitEntry> pitEntry(new PitEntry(interest, closure)); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 75 | pit_.push_back(pitEntry); |
| 76 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 77 | shared_ptr<vector<unsigned char> > encoding = pitEntry->getInterest()->wireEncode(); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 78 | |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 79 | // TODO: Properly check if we are already connected to the expected host. |
| 80 | if (!transport_->getIsConnected()) |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame^] | 81 | transport_->connect(*connectionInfo_, *this); |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 82 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 83 | transport_->send(*encoding); |
| 84 | } |
| 85 | |
| 86 | void Node::processEvents() |
| 87 | { |
| 88 | transport_->processEvents(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 89 | |
| 90 | // Check for PIT entry timeouts. Go backwards through the list so we can erase entries. |
| 91 | double nowMilliseconds = getNowMilliseconds(); |
| 92 | for (int i = (int)pit_.size() - 1; i >= 0; --i) { |
| 93 | if (pit_[i]->checkTimeout(this, nowMilliseconds)) { |
| 94 | pit_.erase(pit_.begin() + i); |
| 95 | |
| 96 | // Refresh now since the timeout callback might have delayed. |
| 97 | nowMilliseconds = getNowMilliseconds(); |
| 98 | } |
| 99 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Jeff Thompson | 8b173cc | 2013-08-21 17:54:12 -0700 | [diff] [blame] | 102 | void Node::onReceivedElement(const unsigned char *element, unsigned int elementLength) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 103 | { |
| 104 | BinaryXmlDecoder decoder(element, elementLength); |
| 105 | |
| 106 | if (decoder.peekDTag(ndn_BinaryXml_DTag_ContentObject)) { |
| 107 | shared_ptr<Data> data(new Data()); |
| 108 | data->wireDecode(element, elementLength); |
| 109 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 110 | int iPitEntry = getEntryIndexForExpressedInterest(data->getName()); |
| 111 | if (iPitEntry >= 0) { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 112 | UpcallInfo upcallInfo(this, pit_[iPitEntry]->getInterest(), 0, data); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 113 | |
| 114 | // Remove the PIT entry before the calling the callback. |
| 115 | Closure *closure = pit_[iPitEntry]->getClosure(); |
| 116 | pit_.erase(pit_.begin() + iPitEntry); |
| 117 | closure->upcall(UPCALL_DATA, upcallInfo); |
| 118 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | void Node::shutdown() |
| 123 | { |
| 124 | transport_->close(); |
| 125 | } |
| 126 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 127 | int Node::getEntryIndexForExpressedInterest(const Name &name) |
| 128 | { |
| 129 | // TODO: Doesn't this belong in the Name class? |
| 130 | vector<struct ndn_NameComponent> nameComponents; |
| 131 | nameComponents.reserve(name.getComponentCount()); |
| 132 | struct ndn_Name nameStruct; |
| 133 | ndn_Name_init(&nameStruct, &nameComponents[0], nameComponents.capacity()); |
| 134 | name.get(nameStruct); |
| 135 | |
| 136 | int iResult = -1; |
| 137 | |
| 138 | for (unsigned int i = 0; i < pit_.size(); ++i) { |
| 139 | if (ndn_Interest_matchesName((struct ndn_Interest *)&pit_[i]->getInterestStruct(), &nameStruct)) { |
| 140 | if (iResult < 0 || |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 141 | pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents) |
| 142 | // Update to the longer match. |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 143 | iResult = i; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return iResult; |
| 148 | } |
| 149 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 150 | } |