blob: 794120c9ddbc078d5e263edc38e7111670fa7826 [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"
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070015#include "forwarding-flags.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070016
17namespace ndn {
18
Jeff Thompson7aec0252013-08-22 17:29:57 -070019/**
20 * An OnData function object is used to pass a callback to expressInterest.
21 */
Jeff Thompsona6d15c82013-09-17 15:34:32 -070022typedef 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 -070023
24/**
25 * An OnTimeout function object is used to pass a callback to expressInterest.
26 */
Jeff Thompsona6d15c82013-09-17 15:34:32 -070027typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest>&)> OnTimeout;
Jeff Thompson7aec0252013-08-22 17:29:57 -070028
Jeff Thompson86507bc2013-08-23 20:51:38 -070029/**
30 * An OnInterest function object is used to pass a callback to registerPrefix.
31 */
Jeff Thompson9cc4be42013-08-27 18:12:41 -070032typedef func_lib::function<void
Jeff Thompsona6d15c82013-09-17 15:34:32 -070033 (const ptr_lib::shared_ptr<const Name>&, const ptr_lib::shared_ptr<const Interest>&, Transport&)> OnInterest;
Jeff Thompson86507bc2013-08-23 20:51:38 -070034
Jeff Thompson590ec232013-09-18 15:55:56 -070035/**
36 * An OnRegisterFailed function object is used to report when registerPrefix fails.
37 */
38typedef func_lib::function<void(const ptr_lib::shared_ptr<const Name>&)> OnRegisterFailed;
39
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070040class Face;
Jeff Thompson590ec232013-09-18 15:55:56 -070041class KeyChain;
42
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070043class Node : public ElementListener {
44public:
45 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070046 * Create a new Node for communication with an NDN hub with the given Transport object and connectionInfo.
47 * @param transport A shared_ptr to a Transport object used for communication.
48 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070049 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070050 Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070051
52 /**
Jeff Thompson4fe45512013-08-23 14:06:38 -070053 * Send the Interest through the transport, read the entire response and call onData(interest, data).
54 * @param interest A reference to the Interest. This copies the Interest.
55 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
56 * use func_lib::ref() as appropriate.
57 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
58 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson11095142013-10-01 16:20:28 -070059 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -070060 */
Jeff Thompson11095142013-10-01 16:20:28 -070061 unsigned int
Jeff Thompson0050abe2013-09-17 12:50:25 -070062 expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout);
Jeff Thompson7aec0252013-08-22 17:29:57 -070063
64 /**
Jeff Thompson11095142013-10-01 16:20:28 -070065 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
66 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
67 * If there is no entry with the pendingInterestId, do nothing.
68 * @param pendingInterestId The ID returned from expressInterest.
69 */
70 void
71 removePendingInterest(unsigned int pendingInterestId);
72
73 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070074 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
75 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
76 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
77 * use func_lib::ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -070078 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
79 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -070080 * @param flags The flags for finer control of which interests are forward to the application.
Jeff Thompson590ec232013-09-18 15:55:56 -070081 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson11095142013-10-01 16:20:28 -070082 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -070083 */
Jeff Thompson11095142013-10-01 16:20:28 -070084 unsigned int
Jeff Thompson590ec232013-09-18 15:55:56 -070085 registerPrefix
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070086 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags,
87 WireFormat& wireFormat);
Jeff Thompson86507bc2013-08-23 20:51:38 -070088
89 /**
Jeff Thompson11095142013-10-01 16:20:28 -070090 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
91 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
92 * If there is no entry with the registeredPrefixId, do nothing.
93 * @param registeredPrefixId The ID returned from registerPrefix.
94 */
95 void
96 removeRegisteredPrefix(unsigned int registeredPrefixId);
97
98 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070099 * Process any data to receive. For each element received, call onReceivedElement.
100 * This is non-blocking and will return immediately if there is no data to receive.
101 * 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.
102 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
103 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
104 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700105 void
106 processEvents();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700107
Jeff Thompson0050abe2013-09-17 12:50:25 -0700108 const ptr_lib::shared_ptr<Transport>&
109 getTransport() { return transport_; }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700110
Jeff Thompson0050abe2013-09-17 12:50:25 -0700111 const ptr_lib::shared_ptr<const Transport::ConnectionInfo>&
112 getConnectionInfo() { return connectionInfo_; }
Jeff Thompson10e34382013-08-22 13:34:46 -0700113
Jeff Thompson0050abe2013-09-17 12:50:25 -0700114 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700115 onReceivedElement(const uint8_t *element, size_t elementLength);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700116
Jeff Thompson0050abe2013-09-17 12:50:25 -0700117 void
118 shutdown();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700119
120private:
Jeff Thompson11095142013-10-01 16:20:28 -0700121 class PendingInterest {
Jeff Thompson557b81e2013-08-21 15:13:51 -0700122 public:
123 /**
124 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson11095142013-10-01 16:20:28 -0700125 * @param pendingInterestId A unique ID for this entry, which you should get with getNextPendingInteresId().
Jeff Thompson48917f02013-08-21 17:12:45 -0700126 * @param interest A shared_ptr for the interest.
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700127 * @param onData A function object to call when a matching data packet is received.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700128 * @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 -0700129 */
Jeff Thompson11095142013-10-01 16:20:28 -0700130 PendingInterest
131 (unsigned int pendingInterestId, const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData,
132 const OnTimeout& onTimeout);
133
134 /**
135 * Return the next unique pending interest ID.
136 */
137 static unsigned int
138 getNextPendingInterestId()
139 {
140 return ++lastPendingInterestId_;
141 }
142
143 /**
144 * Return the pendingInterestId given to the constructor.
145 */
146 unsigned int
147 getPendingInterestId() { return pendingInterestId_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700148
Jeff Thompson0050abe2013-09-17 12:50:25 -0700149 const ptr_lib::shared_ptr<const Interest>&
150 getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700151
Jeff Thompson0050abe2013-09-17 12:50:25 -0700152 const OnData&
153 getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700154
155 /**
Jeff Thompson48917f02013-08-21 17:12:45 -0700156 * Get the struct ndn_Interest for the interest_.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700157 * WARNING: Assume that this PitEntry was created with new, so that no copy constructor is invoked between calls.
158 * This class is private to Node and only used by its methods, so this should be OK.
159 * TODO: Doesn't this functionality belong in the Interest class?
160 * @return A reference to the ndn_Interest struct.
161 * WARNING: The resulting pointers in are invalid uses getInterest() to manipulate the object which could reallocate memory.
162 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700163 const struct ndn_Interest&
164 getInterestStruct()
Jeff Thompson48917f02013-08-21 17:12:45 -0700165 {
166 return interestStruct_;
167 }
168
169 /**
Jeff Thompson7aec0252013-08-22 17:29:57 -0700170 * If this interest is timed out, call onTimeout_ (if defined) and return true.
Jeff Thompson48917f02013-08-21 17:12:45 -0700171 * @param parent The parent Node for the UpcallInfo.
172 * @param nowMilliseconds The current time in milliseconds from gettimeofday.
173 * @return true if this interest timed out and the timeout callback was called, otherwise false.
174 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700175 bool
176 checkTimeout(Node *parent, double nowMilliseconds);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700177
178 private:
Jeff Thompson48917f02013-08-21 17:12:45 -0700179 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700180 std::vector<struct ndn_NameComponent> nameComponents_;
181 std::vector<struct ndn_ExcludeEntry> excludeEntries_;
182 struct ndn_Interest interestStruct_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700183
Jeff Thompson11095142013-10-01 16:20:28 -0700184 static unsigned int lastPendingInterestId_; /**< A class variable used to get the next unique ID. */
185 unsigned int pendingInterestId_; /**< A unique identifier for this entry so it can be deleted */
Jeff Thompson7aec0252013-08-22 17:29:57 -0700186 const OnData onData_;
187 const OnTimeout onTimeout_;
Jeff Thompson48917f02013-08-21 17:12:45 -0700188 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 -0700189 };
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700190
Jeff Thompson11095142013-10-01 16:20:28 -0700191 class RegisteredPrefix {
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700192 public:
193 /**
194 * Create a new PrefixEntry.
Jeff Thompson11095142013-10-01 16:20:28 -0700195 * @param registeredPrefixId A unique ID for this entry, which you should get with getNextRegisteredPrefixId().
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700196 * @param prefix A shared_ptr for the prefix.
197 * @param onInterest A function object to call when a matching data packet is received.
198 */
Jeff Thompson11095142013-10-01 16:20:28 -0700199 RegisteredPrefix(unsigned int registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest)
200 : registeredPrefixId_(registeredPrefixId), prefix_(prefix), onInterest_(onInterest)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700201 {
202 }
203
Jeff Thompson11095142013-10-01 16:20:28 -0700204 /**
205 * Return the next unique entry ID.
206 */
207 static unsigned int
208 getNextRegisteredPrefixId()
209 {
210 return ++lastRegisteredPrefixId_;
211 }
212
213 /**
214 * Return the registeredPrefixId given to the constructor.
215 */
216 unsigned int
217 getRegisteredPrefixId() { return registeredPrefixId_; }
218
Jeff Thompson0050abe2013-09-17 12:50:25 -0700219 const ptr_lib::shared_ptr<const Name>&
220 getPrefix() { return prefix_; }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700221
Jeff Thompson0050abe2013-09-17 12:50:25 -0700222 const OnInterest&
223 getOnInterest() { return onInterest_; }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700224
225 private:
Jeff Thompson11095142013-10-01 16:20:28 -0700226 static unsigned int lastRegisteredPrefixId_; /**< A class variable used to get the next unique ID. */
227 unsigned int registeredPrefixId_; /**< A unique identifier for this entry so it can be deleted */
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700228 ptr_lib::shared_ptr<const Name> prefix_;
229 const OnInterest onInterest_;
230 };
Jeff Thompson557b81e2013-08-21 15:13:51 -0700231
232 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700233 * An NdndIdFetcher receives the Data packet with the publisher public key digest for the connected NDN hub.
234 * 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.
235 */
236 class NdndIdFetcher {
237 public:
238 class Info;
239 NdndIdFetcher(ptr_lib::shared_ptr<NdndIdFetcher::Info> info)
240 : info_(info)
241 {
242 }
243
244 /**
245 * We received the ndnd ID.
246 * @param interest
247 * @param data
248 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700249 void
250 operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& ndndIdData);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700251
252 /**
253 * We timed out fetching the ndnd ID.
254 * @param interest
255 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700256 void
257 operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700258
259 class Info {
260 public:
Jeff Thompson11095142013-10-01 16:20:28 -0700261 /**
262 *
263 * @param node
264 * @param registeredPrefixId The PrefixEntry::getNextRegisteredPrefixId() which registerPrefix got so it could return it to the caller.
265 * @param prefix
266 * @param onInterest
267 * @param onRegisterFailed
268 * @param flags
269 * @param wireFormat
270 */
271 Info(Node *node, unsigned int registeredPrefixId, const Name& prefix, const OnInterest& onInterest,
272 const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags, WireFormat& wireFormat)
273 : node_(*node), registeredPrefixId_(registeredPrefixId), prefix_(new Name(prefix)), onInterest_(onInterest), onRegisterFailed_(onRegisterFailed),
Jeff Thompson6d7369b2013-09-19 14:40:21 -0700274 flags_(flags), wireFormat_(wireFormat)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700275 {
276 }
277
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700278 Node& node_;
Jeff Thompson11095142013-10-01 16:20:28 -0700279 unsigned int registeredPrefixId_;
Jeff Thompson590ec232013-09-18 15:55:56 -0700280 ptr_lib::shared_ptr<const Name> prefix_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700281 const OnInterest onInterest_;
Jeff Thompson590ec232013-09-18 15:55:56 -0700282 const OnRegisterFailed onRegisterFailed_;
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700283 ForwardingFlags flags_;
Jeff Thompson590ec232013-09-18 15:55:56 -0700284 WireFormat& wireFormat_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700285 };
286
287 private:
288 ptr_lib::shared_ptr<Info> info_;
289 };
290
291 /**
Jeff Thompson557b81e2013-08-21 15:13:51 -0700292 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
293 * the entry interest name is the longest that matches name.
294 * @param name The name to find the interest for (from the incoming data packet).
295 * @return The index in pit_ of the pit entry, or -1 if not found.
296 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700297 int
Jeff Thompson590ec232013-09-18 15:55:56 -0700298 getEntryIndexForExpressedInterest(const Name& name);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700299
Jeff Thompson86507bc2013-08-23 20:51:38 -0700300 /**
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700301 * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name.
302 * @param name The name to find the PrefixEntry for (from the incoming interest packet).
303 * @return A pointer to the entry, or 0 if not found.
304 */
Jeff Thompson11095142013-10-01 16:20:28 -0700305 RegisteredPrefix*
Jeff Thompson0050abe2013-09-17 12:50:25 -0700306 getEntryForRegisteredPrefix(const Name& name);
Jeff Thompson590ec232013-09-18 15:55:56 -0700307
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700308 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700309 * Do the work of registerPrefix once we know we are connected with an ndndId_.
Jeff Thompson11095142013-10-01 16:20:28 -0700310 * @param registeredPrefixId The PrefixEntry::getNextRegisteredPrefixId() which registerPrefix got so it could return it to the caller.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700311 * @param prefix
312 * @param onInterest
Jeff Thompson590ec232013-09-18 15:55:56 -0700313 * @param onRegisterFailed
Jeff Thompson86507bc2013-08-23 20:51:38 -0700314 * @param flags
Jeff Thompson590ec232013-09-18 15:55:56 -0700315 * @param wireFormat
316 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700317 void
Jeff Thompson590ec232013-09-18 15:55:56 -0700318 registerPrefixHelper
Jeff Thompson11095142013-10-01 16:20:28 -0700319 (unsigned int registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest,
320 const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags, WireFormat& wireFormat);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700321
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700322 ptr_lib::shared_ptr<Transport> transport_;
Jeff Thompson10e34382013-08-22 13:34:46 -0700323 ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
Jeff Thompson11095142013-10-01 16:20:28 -0700324 std::vector<ptr_lib::shared_ptr<PendingInterest> > pendingInterestTable_;
325 std::vector<ptr_lib::shared_ptr<RegisteredPrefix> > registeredPrefixTable_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700326 Interest ndndIdFetcherInterest_;
Jeff Thompson03616c92013-09-12 14:30:01 -0700327 Blob ndndId_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700328};
329
330}
331
332#endif