blob: 54a9a1676a0a2afc6f5b7faaaed1086f22753a69 [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
6#ifndef NDN_NODE_HPP
7#define NDN_NODE_HPP
8
Jeff Thompson7aec0252013-08-22 17:29:57 -07009#include "common.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070010#include "interest.hpp"
Jeff Thompson86507bc2013-08-23 20:51:38 -070011#include "data.hpp"
12#include "transport/tcp-transport.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070013#include "encoding/binary-xml-element-reader.hpp"
14
15namespace ndn {
16
Jeff Thompson7aec0252013-08-22 17:29:57 -070017/**
18 * An OnData function object is used to pass a callback to expressInterest.
19 */
20typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest> &, const ptr_lib::shared_ptr<Data> &)> OnData;
21
22/**
23 * An OnTimeout function object is used to pass a callback to expressInterest.
24 */
25typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest> &)> OnTimeout;
26
Jeff Thompson86507bc2013-08-23 20:51:38 -070027/**
28 * An OnInterest function object is used to pass a callback to registerPrefix.
29 */
30typedef func_lib::function<void(const ptr_lib::shared_ptr<const Name> &, const ptr_lib::shared_ptr<const Interest> &)> OnInterest;
31
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070032class Face;
33
34class Node : public ElementListener {
35public:
36 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070037 * Create a new Node for communication with an NDN hub with the given Transport object and connectionInfo.
38 * @param transport A shared_ptr to a Transport object used for communication.
39 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070040 */
Jeff Thompson10e34382013-08-22 13:34:46 -070041 Node(const ptr_lib::shared_ptr<Transport> &transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &connectionInfo)
42 : transport_(transport), connectionInfo_(connectionInfo)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070043 {
Jeff Thompson86507bc2013-08-23 20:51:38 -070044 construct();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070045 }
46
47 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070048 * Create a new Node for communication with an NDN hub at host:port using the default TcpTransport.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070049 * @param host The host of the NDN hub.
50 * @param port The port of the NDN hub.
51 */
52 Node(const char *host, unsigned short port)
Jeff Thompson86507bc2013-08-23 20:51:38 -070053 : transport_(new TcpTransport()), connectionInfo_(new TcpTransport::ConnectionInfo(host, port))
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070054 {
Jeff Thompson86507bc2013-08-23 20:51:38 -070055 construct();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070056 }
57
58 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070059 * Create a new Node for communication with an NDN hub at host with the default port 9695 and using the default TcpTransport.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070060 * @param host The host of the NDN hub.
61 */
62 Node(const char *host)
Jeff Thompson86507bc2013-08-23 20:51:38 -070063 : transport_(new TcpTransport()), connectionInfo_(new TcpTransport::ConnectionInfo(host, 9695))
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070064 {
Jeff Thompson86507bc2013-08-23 20:51:38 -070065 construct();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070066 }
67
68 /**
Jeff Thompson4fe45512013-08-23 14:06:38 -070069 * Send the Interest through the transport, read the entire response and call onData(interest, data).
70 * @param interest A reference to the Interest. This copies the Interest.
71 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
72 * use func_lib::ref() as appropriate.
73 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
74 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
75 */
76 void expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout);
77
78 /**
79 * Send the Interest through the transport, read the entire response and call onData(interest, data).
80 * @param interest A reference to the Interest. This copies the Interest.
81 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
82 * use func_lib::ref() as appropriate.
83 */
84 void expressInterest(const Interest &interest, const OnData &onData) {
85 expressInterest(interest, onData, OnTimeout());
86 }
87
88 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070089 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
Jeff Thompson7aec0252013-08-22 17:29:57 -070090 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070091 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070092 * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object.
Jeff Thompson7aec0252013-08-22 17:29:57 -070093 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
94 * use func_lib::ref() as appropriate.
95 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
96 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070097 */
Jeff Thompson7aec0252013-08-22 17:29:57 -070098 void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData, const OnTimeout &onTimeout);
99
100 /**
101 * Encode name as an Interest, using a default interest lifetime.
102 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -0700103 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700104 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
105 * use func_lib::ref() as appropriate.
106 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
107 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
108 */
109 void expressInterest(const Name &name, const OnData &onData, const OnTimeout &onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700110 {
Jeff Thompson7aec0252013-08-22 17:29:57 -0700111 expressInterest(name, 0, onData, onTimeout);
112 }
113
114 /**
115 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
116 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -0700117 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700118 * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object.
119 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
120 * use func_lib::ref() as appropriate.
121 */
122 void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData)
123 {
124 expressInterest(name, interestTemplate, onData, OnTimeout());
125 }
126
127 /**
128 * Encode name as an Interest, using a default interest lifetime.
129 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -0700130 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700131 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
132 * use func_lib::ref() as appropriate.
133 */
134 void expressInterest(const Name &name, const OnData &onData)
135 {
136 expressInterest(name, 0, onData);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700137 }
138
139 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700140 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
141 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
142 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
143 * use func_lib::ref() as appropriate.
144 * @param flags The flags for finer control of which interests are forward to the application.
145 */
146 void registerPrefix(const Name &prefix, const OnInterest &onInterest, int flags);
147
148 /**
149 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
150 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
151 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
152 * use func_lib::ref() as appropriate.
153 */
154 void registerPrefix(const Name &prefix, const OnInterest &onInterest)
155 {
156 registerPrefix(prefix, onInterest, 0);
157 }
158
159 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700160 * Process any data to receive. For each element received, call onReceivedElement.
161 * This is non-blocking and will return immediately if there is no data to receive.
162 * 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.
163 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
164 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
165 */
166 void processEvents();
167
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700168 const ptr_lib::shared_ptr<Transport> &getTransport() { return transport_; }
169
Jeff Thompson10e34382013-08-22 13:34:46 -0700170 const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &getConnectionInfo() { return connectionInfo_; }
171
Jeff Thompson8b173cc2013-08-21 17:54:12 -0700172 void onReceivedElement(const unsigned char *element, unsigned int elementLength);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700173
174 void shutdown();
175
176private:
Jeff Thompson557b81e2013-08-21 15:13:51 -0700177 class PitEntry {
178 public:
179 /**
180 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson48917f02013-08-21 17:12:45 -0700181 * @param interest A shared_ptr for the interest.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700182 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
183 * use func_lib::ref() as appropriate.
184 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
185 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700186 */
Jeff Thompson7aec0252013-08-22 17:29:57 -0700187 PitEntry(const ptr_lib::shared_ptr<const Interest> &interest, const OnData &onData, const OnTimeout &onTimeout);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700188
Jeff Thompson48917f02013-08-21 17:12:45 -0700189 const ptr_lib::shared_ptr<const Interest> &getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700190
Jeff Thompson7aec0252013-08-22 17:29:57 -0700191 const OnData &getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700192
193 /**
Jeff Thompson48917f02013-08-21 17:12:45 -0700194 * Get the struct ndn_Interest for the interest_.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700195 * WARNING: Assume that this PitEntry was created with new, so that no copy constructor is invoked between calls.
196 * This class is private to Node and only used by its methods, so this should be OK.
197 * TODO: Doesn't this functionality belong in the Interest class?
198 * @return A reference to the ndn_Interest struct.
199 * WARNING: The resulting pointers in are invalid uses getInterest() to manipulate the object which could reallocate memory.
200 */
Jeff Thompson48917f02013-08-21 17:12:45 -0700201 const struct ndn_Interest &getInterestStruct()
202 {
203 return interestStruct_;
204 }
205
206 /**
Jeff Thompson7aec0252013-08-22 17:29:57 -0700207 * If this interest is timed out, call onTimeout_ (if defined) and return true.
Jeff Thompson48917f02013-08-21 17:12:45 -0700208 * @param parent The parent Node for the UpcallInfo.
209 * @param nowMilliseconds The current time in milliseconds from gettimeofday.
210 * @return true if this interest timed out and the timeout callback was called, otherwise false.
211 */
212 bool checkTimeout(Node *parent, double nowMilliseconds);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700213
214 private:
Jeff Thompson48917f02013-08-21 17:12:45 -0700215 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700216 std::vector<struct ndn_NameComponent> nameComponents_;
217 std::vector<struct ndn_ExcludeEntry> excludeEntries_;
218 struct ndn_Interest interestStruct_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700219
Jeff Thompson7aec0252013-08-22 17:29:57 -0700220 const OnData onData_;
221 const OnTimeout onTimeout_;
Jeff Thompson48917f02013-08-21 17:12:45 -0700222 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 -0700223 };
224
225 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700226 * An NdndIdFetcher receives the Data packet with the publisher public key digest for the connected NDN hub.
227 * 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.
228 */
229 class NdndIdFetcher {
230 public:
231 class Info;
232 NdndIdFetcher(ptr_lib::shared_ptr<NdndIdFetcher::Info> info)
233 : info_(info)
234 {
235 }
236
237 /**
238 * We received the ndnd ID.
239 * @param interest
240 * @param data
241 */
242 void operator()(const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &ndndIdData);
243
244 /**
245 * We timed out fetching the ndnd ID.
246 * @param interest
247 */
248 void operator()(const ptr_lib::shared_ptr<const Interest> &timedOutInterest);
249
250 class Info {
251 public:
252 Info(Node *node, const Name &prefix, const OnInterest &onInterest, int flags)
253 : node_(*node), prefix_(prefix), onInterest_(onInterest), flags_(flags)
254 {
255 }
256
257 Node &node_;
258 Name prefix_;
259 const OnInterest onInterest_;
260 int flags_;
261 };
262
263 private:
264 ptr_lib::shared_ptr<Info> info_;
265 };
266
267 /**
268 * Finish constructing this object.
269 */
270 void construct();
271
272 /**
Jeff Thompson557b81e2013-08-21 15:13:51 -0700273 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
274 * the entry interest name is the longest that matches name.
275 * @param name The name to find the interest for (from the incoming data packet).
276 * @return The index in pit_ of the pit entry, or -1 if not found.
277 */
278 int getEntryIndexForExpressedInterest(const Name &name);
279
Jeff Thompson86507bc2013-08-23 20:51:38 -0700280 /**
281 * Do the work of registerPrefix once we know we are connected with an ndndId_.
282 * @param prefix
283 * @param onInterest
284 * @param flags
285 */
286 void registerPrefixHelper(const Name &prefix, const OnInterest &onInterest, int flags);
287
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700288 ptr_lib::shared_ptr<Transport> transport_;
Jeff Thompson10e34382013-08-22 13:34:46 -0700289 ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700290 std::vector<ptr_lib::shared_ptr<PitEntry> > pit_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700291 Interest ndndIdFetcherInterest_;
292 std::vector<unsigned char> ndndId_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700293};
294
295}
296
297#endif