blob: a93ebd7ff24f3e97e8fd98cec7efd2759b2afd2e [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.
Jeff Thompson7a67cb62013-08-26 11:43:18 -070060 // TODO: If there are multiple connected hubs, the NDN ID is really stored per connected hub.
Jeff Thompson86507bc2013-08-23 20:51:38 -070061 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
67void Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest> &timedOutInterest)
68{
69 // TODO: Log the timeout.
70}
71
72void Node::registerPrefixHelper(const Name &prefix, const OnInterest &onInterest, int flags)
73{
74 throw logic_error("need to finish implementing registerPrefix");
75}
76
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070077void Node::processEvents()
78{
79 transport_->processEvents();
Jeff Thompson48917f02013-08-21 17:12:45 -070080
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 Thompsonbf50a1a2013-08-20 18:01:01 -070091}
92
Jeff Thompson8b173cc2013-08-21 17:54:12 -070093void Node::onReceivedElement(const unsigned char *element, unsigned int elementLength)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070094{
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 Thompson557b81e2013-08-21 15:13:51 -0700101 int iPitEntry = getEntryIndexForExpressedInterest(data->getName());
102 if (iPitEntry >= 0) {
Jeff Thompson7aec0252013-08-22 17:29:57 -0700103 // 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 Thompson557b81e2013-08-21 15:13:51 -0700106 pit_.erase(pit_.begin() + iPitEntry);
Jeff Thompson7aec0252013-08-22 17:29:57 -0700107 onData(interest, data);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700108 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700109 }
110}
111
112void Node::shutdown()
113{
114 transport_->close();
115}
116
Jeff Thompson557b81e2013-08-21 15:13:51 -0700117int 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 Thompson48917f02013-08-21 17:12:45 -0700131 pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents)
132 // Update to the longer match.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700133 iResult = i;
134 }
135 }
136
137 return iResult;
138}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700139
140Node::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
159bool 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 Thompson557b81e2013-08-21 15:13:51 -0700175
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700176}