blob: b5d61d99faa79dd3ddb9c7415151dc14d6667d76 [file] [log] [blame]
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
Jeff Thompson48917f02013-08-21 17:12:45 -07006#include <sys/time.h>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07007#include "encoding/binary-xml-decoder.hpp"
8#include "c/encoding/binary-xml.h"
9#include "data.hpp"
Jeff Thompsonb09fcc12013-08-22 10:37:10 -070010#include "node.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070011
12using namespace std;
13using namespace ndn::ptr_lib;
14
15namespace ndn {
16
Jeff Thompson48917f02013-08-21 17:12:45 -070017// Use gettimeofday to return the current time in milliseconds.
18static inline double getNowMilliseconds()
Jeff Thompson557b81e2013-08-21 15:13:51 -070019{
Jeff Thompson48917f02013-08-21 17:12:45 -070020 timeval t;
21 gettimeofday(&t, NULL);
22 return t.tv_sec * 1000.0 + t.tv_usec / 1000.0;
23}
24
25Node::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 Thompson557b81e2013-08-21 15:13:51 -070031 else
32 // No timeout.
Jeff Thompson48917f02013-08-21 17:12:45 -070033 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 Thompson557b81e2013-08-21 15:13:51 -070042}
43
Jeff Thompson48917f02013-08-21 17:12:45 -070044bool Node::PitEntry::checkTimeout(Node *parent, double nowMilliseconds)
Jeff Thompson557b81e2013-08-21 15:13:51 -070045{
Jeff Thompson48917f02013-08-21 17:12:45 -070046 if (timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_) {
47 shared_ptr<Data> dummyData;
48 UpcallInfo upcallInfo(parent, interest_, 0, dummyData);
Jeff Thompson557b81e2013-08-21 15:13:51 -070049
Jeff Thompson48917f02013-08-21 17:12:45 -070050 // Ignore all exceptions.
51 try {
52 closure_->upcall(UPCALL_INTEREST_TIMED_OUT, upcallInfo);
53 }
54 catch (...) { }
55
56 return true;
Jeff Thompson557b81e2013-08-21 15:13:51 -070057 }
Jeff Thompson48917f02013-08-21 17:12:45 -070058 else
59 return false;
Jeff Thompson557b81e2013-08-21 15:13:51 -070060}
61
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070062void Node::expressInterest(const Name &name, Closure *closure, const Interest *interestTemplate)
63{
Jeff Thompson48917f02013-08-21 17:12:45 -070064 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 Thompson557b81e2013-08-21 15:13:51 -070071 else
Jeff Thompson48917f02013-08-21 17:12:45 -070072 interest.reset(new Interest(name, 4000.0));
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070073
Jeff Thompson48917f02013-08-21 17:12:45 -070074 shared_ptr<PitEntry> pitEntry(new PitEntry(interest, closure));
Jeff Thompson557b81e2013-08-21 15:13:51 -070075 pit_.push_back(pitEntry);
76
Jeff Thompson48917f02013-08-21 17:12:45 -070077 shared_ptr<vector<unsigned char> > encoding = pitEntry->getInterest()->wireEncode();
Jeff Thompson557b81e2013-08-21 15:13:51 -070078
79 // TODO: Check if we are already connected.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070080 transport_->connect(*this);
81 transport_->send(*encoding);
82}
83
84void Node::processEvents()
85{
86 transport_->processEvents();
Jeff Thompson48917f02013-08-21 17:12:45 -070087
88 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
89 double nowMilliseconds = getNowMilliseconds();
90 for (int i = (int)pit_.size() - 1; i >= 0; --i) {
91 if (pit_[i]->checkTimeout(this, nowMilliseconds)) {
92 pit_.erase(pit_.begin() + i);
93
94 // Refresh now since the timeout callback might have delayed.
95 nowMilliseconds = getNowMilliseconds();
96 }
97 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070098}
99
Jeff Thompson8b173cc2013-08-21 17:54:12 -0700100void Node::onReceivedElement(const unsigned char *element, unsigned int elementLength)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700101{
102 BinaryXmlDecoder decoder(element, elementLength);
103
104 if (decoder.peekDTag(ndn_BinaryXml_DTag_ContentObject)) {
105 shared_ptr<Data> data(new Data());
106 data->wireDecode(element, elementLength);
107
Jeff Thompson557b81e2013-08-21 15:13:51 -0700108 int iPitEntry = getEntryIndexForExpressedInterest(data->getName());
109 if (iPitEntry >= 0) {
Jeff Thompson48917f02013-08-21 17:12:45 -0700110 UpcallInfo upcallInfo(this, pit_[iPitEntry]->getInterest(), 0, data);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700111
112 // Remove the PIT entry before the calling the callback.
113 Closure *closure = pit_[iPitEntry]->getClosure();
114 pit_.erase(pit_.begin() + iPitEntry);
115 closure->upcall(UPCALL_DATA, upcallInfo);
116 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700117 }
118}
119
120void Node::shutdown()
121{
122 transport_->close();
123}
124
Jeff Thompson557b81e2013-08-21 15:13:51 -0700125int Node::getEntryIndexForExpressedInterest(const Name &name)
126{
127 // TODO: Doesn't this belong in the Name class?
128 vector<struct ndn_NameComponent> nameComponents;
129 nameComponents.reserve(name.getComponentCount());
130 struct ndn_Name nameStruct;
131 ndn_Name_init(&nameStruct, &nameComponents[0], nameComponents.capacity());
132 name.get(nameStruct);
133
134 int iResult = -1;
135
136 for (unsigned int i = 0; i < pit_.size(); ++i) {
137 if (ndn_Interest_matchesName((struct ndn_Interest *)&pit_[i]->getInterestStruct(), &nameStruct)) {
138 if (iResult < 0 ||
Jeff Thompson48917f02013-08-21 17:12:45 -0700139 pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents)
140 // Update to the longer match.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700141 iResult = i;
142 }
143 }
144
145 return iResult;
146}
147
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700148}