blob: 77befa36109a359c04fe06f536a08073a1e001b2 [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
Jeff Thompsona4056972013-08-22 11:52:21 -070079 // TODO: Properly check if we are already connected to the expected host.
80 if (!transport_->getIsConnected())
Jeff Thompson10e34382013-08-22 13:34:46 -070081 transport_->connect(*connectionInfo_, *this);
Jeff Thompsona4056972013-08-22 11:52:21 -070082
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070083 transport_->send(*encoding);
84}
85
86void Node::processEvents()
87{
88 transport_->processEvents();
Jeff Thompson48917f02013-08-21 17:12:45 -070089
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 Thompsonbf50a1a2013-08-20 18:01:01 -0700100}
101
Jeff Thompson8b173cc2013-08-21 17:54:12 -0700102void Node::onReceivedElement(const unsigned char *element, unsigned int elementLength)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700103{
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 Thompson557b81e2013-08-21 15:13:51 -0700110 int iPitEntry = getEntryIndexForExpressedInterest(data->getName());
111 if (iPitEntry >= 0) {
Jeff Thompson48917f02013-08-21 17:12:45 -0700112 UpcallInfo upcallInfo(this, pit_[iPitEntry]->getInterest(), 0, data);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700113
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 Thompsonbf50a1a2013-08-20 18:01:01 -0700119 }
120}
121
122void Node::shutdown()
123{
124 transport_->close();
125}
126
Jeff Thompson557b81e2013-08-21 15:13:51 -0700127int 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 Thompson48917f02013-08-21 17:12:45 -0700141 pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents)
142 // Update to the longer match.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700143 iResult = i;
144 }
145 }
146
147 return iResult;
148}
149
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700150}