blob: 256e3aa7286a1308c08bb02c38dd5d0063349212 [file] [log] [blame]
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_NODE_HPP
8#define NDN_NODE_HPP
9
Jeff Thompson7aec0252013-08-22 17:29:57 -070010#include "common.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070011#include "interest.hpp"
Jeff Thompson86507bc2013-08-23 20:51:38 -070012#include "data.hpp"
13#include "transport/tcp-transport.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070014#include "encoding/binary-xml-element-reader.hpp"
15
16namespace ndn {
17
Jeff Thompson7aec0252013-08-22 17:29:57 -070018/**
19 * An OnData function object is used to pass a callback to expressInterest.
20 */
Jeff Thompsona6d15c82013-09-17 15:34:32 -070021typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&)> OnData;
Jeff Thompson7aec0252013-08-22 17:29:57 -070022
23/**
24 * An OnTimeout function object is used to pass a callback to expressInterest.
25 */
Jeff Thompsona6d15c82013-09-17 15:34:32 -070026typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest>&)> OnTimeout;
Jeff Thompson7aec0252013-08-22 17:29:57 -070027
Jeff Thompson86507bc2013-08-23 20:51:38 -070028/**
29 * An OnInterest function object is used to pass a callback to registerPrefix.
30 */
Jeff Thompson9cc4be42013-08-27 18:12:41 -070031typedef func_lib::function<void
Jeff Thompsona6d15c82013-09-17 15:34:32 -070032 (const ptr_lib::shared_ptr<const Name>&, const ptr_lib::shared_ptr<const Interest>&, Transport&)> OnInterest;
Jeff Thompson86507bc2013-08-23 20:51:38 -070033
Jeff Thompson590ec232013-09-18 15:55:56 -070034/**
35 * An OnRegisterFailed function object is used to report when registerPrefix fails.
36 */
37typedef func_lib::function<void(const ptr_lib::shared_ptr<const Name>&)> OnRegisterFailed;
38
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070039class Face;
Jeff Thompson590ec232013-09-18 15:55:56 -070040class KeyChain;
41
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070042class Node : public ElementListener {
43public:
44 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070045 * Create a new Node for communication with an NDN hub with the given Transport object and connectionInfo.
46 * @param transport A shared_ptr to a Transport object used for communication.
47 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070048 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070049 Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070050
51 /**
Jeff Thompson4fe45512013-08-23 14:06:38 -070052 * Send the Interest through the transport, read the entire response and call onData(interest, data).
53 * @param interest A reference to the Interest. This copies the Interest.
54 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
55 * use func_lib::ref() as appropriate.
56 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
57 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
58 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070059 void
60 expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout);
Jeff Thompson7aec0252013-08-22 17:29:57 -070061
62 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070063 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
64 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
65 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
66 * use func_lib::ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -070067 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
68 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
69 * @param keyChain The KeyChain object whose signData is called to sign the Data packet with the ForwardingEntry to register the prefix.
70 * @param signerName The signing identity or certificate name, depending on byKeyName. This is used to sign the Data packet
71 * with the ForwardingEntry to register the prefix.This copies the function object, so you may need to use func_lib::ref() as appropriate.
72 * @param byKeyName If true, the signerName is the key name, otherwise it is the certificate name. If omitted, the default is true.
Jeff Thompson86507bc2013-08-23 20:51:38 -070073 * @param flags The flags for finer control of which interests are forward to the application.
Jeff Thompson590ec232013-09-18 15:55:56 -070074 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson86507bc2013-08-23 20:51:38 -070075 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070076 void
Jeff Thompson590ec232013-09-18 15:55:56 -070077 registerPrefix
78 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, KeyChain &keyChain,
79 const Name& signerName, bool byKeyName, int flags, WireFormat& wireFormat);
Jeff Thompson86507bc2013-08-23 20:51:38 -070080
81 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070082 * Process any data to receive. For each element received, call onReceivedElement.
83 * This is non-blocking and will return immediately if there is no data to receive.
84 * You should repeatedly call this from an event loop, with calls to sleep as needed so that the loop doesn't use 100% of the CPU.
85 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
86 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
87 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070088 void
89 processEvents();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070090
Jeff Thompson0050abe2013-09-17 12:50:25 -070091 const ptr_lib::shared_ptr<Transport>&
92 getTransport() { return transport_; }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070093
Jeff Thompson0050abe2013-09-17 12:50:25 -070094 const ptr_lib::shared_ptr<const Transport::ConnectionInfo>&
95 getConnectionInfo() { return connectionInfo_; }
Jeff Thompson10e34382013-08-22 13:34:46 -070096
Jeff Thompson0050abe2013-09-17 12:50:25 -070097 void
98 onReceivedElement(const unsigned char *element, unsigned int elementLength);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070099
Jeff Thompson0050abe2013-09-17 12:50:25 -0700100 void
101 shutdown();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700102
103private:
Jeff Thompson557b81e2013-08-21 15:13:51 -0700104 class PitEntry {
105 public:
106 /**
107 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson48917f02013-08-21 17:12:45 -0700108 * @param interest A shared_ptr for the interest.
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700109 * @param onData A function object to call when a matching data packet is received.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700110 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700111 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700112 PitEntry(const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData, const OnTimeout& onTimeout);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700113
Jeff Thompson0050abe2013-09-17 12:50:25 -0700114 const ptr_lib::shared_ptr<const Interest>&
115 getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700116
Jeff Thompson0050abe2013-09-17 12:50:25 -0700117 const OnData&
118 getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700119
120 /**
Jeff Thompson48917f02013-08-21 17:12:45 -0700121 * Get the struct ndn_Interest for the interest_.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700122 * WARNING: Assume that this PitEntry was created with new, so that no copy constructor is invoked between calls.
123 * This class is private to Node and only used by its methods, so this should be OK.
124 * TODO: Doesn't this functionality belong in the Interest class?
125 * @return A reference to the ndn_Interest struct.
126 * WARNING: The resulting pointers in are invalid uses getInterest() to manipulate the object which could reallocate memory.
127 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700128 const struct ndn_Interest&
129 getInterestStruct()
Jeff Thompson48917f02013-08-21 17:12:45 -0700130 {
131 return interestStruct_;
132 }
133
134 /**
Jeff Thompson7aec0252013-08-22 17:29:57 -0700135 * If this interest is timed out, call onTimeout_ (if defined) and return true.
Jeff Thompson48917f02013-08-21 17:12:45 -0700136 * @param parent The parent Node for the UpcallInfo.
137 * @param nowMilliseconds The current time in milliseconds from gettimeofday.
138 * @return true if this interest timed out and the timeout callback was called, otherwise false.
139 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700140 bool
141 checkTimeout(Node *parent, double nowMilliseconds);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700142
143 private:
Jeff Thompson48917f02013-08-21 17:12:45 -0700144 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700145 std::vector<struct ndn_NameComponent> nameComponents_;
146 std::vector<struct ndn_ExcludeEntry> excludeEntries_;
147 struct ndn_Interest interestStruct_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700148
Jeff Thompson7aec0252013-08-22 17:29:57 -0700149 const OnData onData_;
150 const OnTimeout onTimeout_;
Jeff Thompson48917f02013-08-21 17:12:45 -0700151 double timeoutTimeMilliseconds_; /**< The time when the interest times out in milliseconds according to gettimeofday, or -1 for no timeout. */
Jeff Thompson557b81e2013-08-21 15:13:51 -0700152 };
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700153
154 class PrefixEntry {
155 public:
156 /**
157 * Create a new PrefixEntry.
158 * @param prefix A shared_ptr for the prefix.
159 * @param onInterest A function object to call when a matching data packet is received.
160 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700161 PrefixEntry(const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700162 : prefix_(prefix), onInterest_(onInterest)
163 {
164 }
165
Jeff Thompson0050abe2013-09-17 12:50:25 -0700166 const ptr_lib::shared_ptr<const Name>&
167 getPrefix() { return prefix_; }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700168
Jeff Thompson0050abe2013-09-17 12:50:25 -0700169 const OnInterest&
170 getOnInterest() { return onInterest_; }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700171
172 private:
173 ptr_lib::shared_ptr<const Name> prefix_;
174 const OnInterest onInterest_;
175 };
Jeff Thompson557b81e2013-08-21 15:13:51 -0700176
177 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700178 * An NdndIdFetcher receives the Data packet with the publisher public key digest for the connected NDN hub.
179 * This class is a function object for the callbacks. It only holds a pointer to an Info object, so it is OK to copy the pointer.
180 */
181 class NdndIdFetcher {
182 public:
183 class Info;
184 NdndIdFetcher(ptr_lib::shared_ptr<NdndIdFetcher::Info> info)
185 : info_(info)
186 {
187 }
188
189 /**
190 * We received the ndnd ID.
191 * @param interest
192 * @param data
193 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700194 void
195 operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& ndndIdData);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700196
197 /**
198 * We timed out fetching the ndnd ID.
199 * @param interest
200 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700201 void
202 operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700203
204 class Info {
205 public:
Jeff Thompson590ec232013-09-18 15:55:56 -0700206 Info(Node *node, const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed,
207 KeyChain &keyChain, const Name& signerName, bool byKeyName, int flags, WireFormat& wireFormat)
208 : node_(*node), prefix_(new Name(prefix)), onInterest_(onInterest), onRegisterFailed_(onRegisterFailed),
209 keyChain_(keyChain), signerName_(new Name(signerName)), byKeyName_(byKeyName), flags_(flags), wireFormat_(wireFormat)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700210 {
211 }
212
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700213 Node& node_;
Jeff Thompson590ec232013-09-18 15:55:56 -0700214 ptr_lib::shared_ptr<const Name> prefix_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700215 const OnInterest onInterest_;
Jeff Thompson590ec232013-09-18 15:55:56 -0700216 const OnRegisterFailed onRegisterFailed_;
217 KeyChain &keyChain_;
218 ptr_lib::shared_ptr<const Name> signerName_;
219 bool byKeyName_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700220 int flags_;
Jeff Thompson590ec232013-09-18 15:55:56 -0700221 WireFormat& wireFormat_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700222 };
223
224 private:
225 ptr_lib::shared_ptr<Info> info_;
226 };
227
228 /**
Jeff Thompson557b81e2013-08-21 15:13:51 -0700229 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
230 * the entry interest name is the longest that matches name.
231 * @param name The name to find the interest for (from the incoming data packet).
232 * @return The index in pit_ of the pit entry, or -1 if not found.
233 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700234 int
Jeff Thompson590ec232013-09-18 15:55:56 -0700235 getEntryIndexForExpressedInterest(const Name& name);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700236
Jeff Thompson86507bc2013-08-23 20:51:38 -0700237 /**
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700238 * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name.
239 * @param name The name to find the PrefixEntry for (from the incoming interest packet).
240 * @return A pointer to the entry, or 0 if not found.
241 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700242 PrefixEntry*
243 getEntryForRegisteredPrefix(const Name& name);
Jeff Thompson590ec232013-09-18 15:55:56 -0700244
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700245 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700246 * Do the work of registerPrefix once we know we are connected with an ndndId_.
247 * @param prefix
248 * @param onInterest
Jeff Thompson590ec232013-09-18 15:55:56 -0700249 * @param onRegisterFailed
250 * @param keyChain
251 * @param signerName
252 * @param byKeyName
Jeff Thompson86507bc2013-08-23 20:51:38 -0700253 * @param flags
Jeff Thompson590ec232013-09-18 15:55:56 -0700254 * @param wireFormat
255 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700256 void
Jeff Thompson590ec232013-09-18 15:55:56 -0700257 registerPrefixHelper
258 (const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed,
259 KeyChain &keyChain, const ptr_lib::shared_ptr<const Name>& signerName, bool byKeyName, int flags, WireFormat& wireFormat);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700260
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700261 ptr_lib::shared_ptr<Transport> transport_;
Jeff Thompson10e34382013-08-22 13:34:46 -0700262 ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700263 std::vector<ptr_lib::shared_ptr<PitEntry> > pit_;
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700264 std::vector<ptr_lib::shared_ptr<PrefixEntry> > registeredPrefixTable_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700265 Interest ndndIdFetcherInterest_;
Jeff Thompson03616c92013-09-12 14:30:01 -0700266 Blob ndndId_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700267};
268
269}
270
271#endif