blob: 369f3ca54ef4c96a79c6f20403378e0411cb4f25 [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"
Jeff Thompsonb09fcc12013-08-22 10:37:10 -07009#include "node.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070010
11using namespace std;
12using namespace ndn::ptr_lib;
13
14namespace ndn {
15
Jeff Thompson48917f02013-08-21 17:12:45 -070016// Use gettimeofday to return the current time in milliseconds.
17static inline double getNowMilliseconds()
Jeff Thompson557b81e2013-08-21 15:13:51 -070018{
Jeff Thompson48917f02013-08-21 17:12:45 -070019 timeval t;
20 gettimeofday(&t, NULL);
21 return t.tv_sec * 1000.0 + t.tv_usec / 1000.0;
22}
Jeff Thompson557b81e2013-08-21 15:13:51 -070023
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070024Node::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 Thompson557b81e2013-08-21 15:13:51 -070027{
Jeff Thompson557b81e2013-08-21 15:13:51 -070028}
29
Jeff Thompson4fe45512013-08-23 14:06:38 -070030void Node::expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070031{
Jeff Thompson86507bc2013-08-23 20:51:38 -070032 // TODO: Properly check if we are already connected to the expected host.
33 if (!transport_->getIsConnected())
34 transport_->connect(*connectionInfo_, *this);
35
Jeff Thompson4fe45512013-08-23 14:06:38 -070036 shared_ptr<PitEntry> pitEntry(new PitEntry(shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout));
Jeff Thompson557b81e2013-08-21 15:13:51 -070037 pit_.push_back(pitEntry);
38
Jeff Thompson48917f02013-08-21 17:12:45 -070039 shared_ptr<vector<unsigned char> > encoding = pitEntry->getInterest()->wireEncode();
Jeff Thompson557b81e2013-08-21 15:13:51 -070040
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070041 transport_->send(*encoding);
42}
43
Jeff Thompson86507bc2013-08-23 20:51:38 -070044void 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
56void 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
66void Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest> &timedOutInterest)
67{
68 // TODO: Log the timeout.
69}
70
71void Node::registerPrefixHelper(const Name &prefix, const OnInterest &onInterest, int flags)
72{
73 throw logic_error("need to finish implementing registerPrefix");
74}
75
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070076void Node::processEvents()
77{
78 transport_->processEvents();
Jeff Thompson48917f02013-08-21 17:12:45 -070079
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 Thompsonbf50a1a2013-08-20 18:01:01 -070090}
91
Jeff Thompson8b173cc2013-08-21 17:54:12 -070092void Node::onReceivedElement(const unsigned char *element, unsigned int elementLength)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070093{
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 Thompson557b81e2013-08-21 15:13:51 -0700100 int iPitEntry = getEntryIndexForExpressedInterest(data->getName());
101 if (iPitEntry >= 0) {
Jeff Thompson7aec0252013-08-22 17:29:57 -0700102 // 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 Thompson557b81e2013-08-21 15:13:51 -0700105 pit_.erase(pit_.begin() + iPitEntry);
Jeff Thompson7aec0252013-08-22 17:29:57 -0700106 onData(interest, data);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700107 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700108 }
109}
110
111void Node::shutdown()
112{
113 transport_->close();
114}
115
Jeff Thompson557b81e2013-08-21 15:13:51 -0700116int 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 Thompson48917f02013-08-21 17:12:45 -0700130 pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents)
131 // Update to the longer match.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700132 iResult = i;
133 }
134 }
135
136 return iResult;
137}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700138
139Node::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
158bool 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 Thompson557b81e2013-08-21 15:13:51 -0700174
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700175}