blob: bbf89fd2719db4b1ed9436008d2117a5143600cd [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07005 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_NODE_HPP
9#define NDN_NODE_HPP
10
Jeff Thompson7aec0252013-08-22 17:29:57 -070011#include "common.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070012#include "interest.hpp"
Jeff Thompson86507bc2013-08-23 20:51:38 -070013#include "data.hpp"
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070014#include "forwarding-flags.hpp"
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080015#include "transport/transport.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070016
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070017
18namespace ndn {
19
Jeff Thompson7aec0252013-08-22 17:29:57 -070020/**
21 * An OnData function object is used to pass a callback to expressInterest.
22 */
Jeff Thompsona6d15c82013-09-17 15:34:32 -070023typedef 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 -070024
25/**
26 * An OnTimeout function object is used to pass a callback to expressInterest.
27 */
Jeff Thompsona6d15c82013-09-17 15:34:32 -070028typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest>&)> OnTimeout;
Jeff Thompson7aec0252013-08-22 17:29:57 -070029
Jeff Thompson86507bc2013-08-23 20:51:38 -070030/**
31 * An OnInterest function object is used to pass a callback to registerPrefix.
32 */
Jeff Thompson9cc4be42013-08-27 18:12:41 -070033typedef func_lib::function<void
Jeff Thompsonb510e3e2013-10-07 18:53:20 -070034 (const ptr_lib::shared_ptr<const Name>&, const ptr_lib::shared_ptr<const Interest>&, Transport&, uint64_t)> OnInterest;
Jeff Thompson86507bc2013-08-23 20:51:38 -070035
Jeff Thompson590ec232013-09-18 15:55:56 -070036/**
37 * An OnRegisterFailed function object is used to report when registerPrefix fails.
38 */
39typedef func_lib::function<void(const ptr_lib::shared_ptr<const Name>&)> OnRegisterFailed;
40
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070041class Face;
Jeff Thompson590ec232013-09-18 15:55:56 -070042
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080043class Node {
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070044public:
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080045 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
46
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070047 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070048 * Create a new Node for communication with an NDN hub with the given Transport object and connectionInfo.
49 * @param transport A shared_ptr to a Transport object used for communication.
50 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070051 */
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080052 Node(const ptr_lib::shared_ptr<Transport>& transport);
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080053
54 /**
55 * @brief Alternative (special use case) version of the constructor, can be used to aggregate
56 * several Faces within one processing thread
57 *
58 * <code>
59 * Face face1(...);
60 * Face face2(..., face1.getAsyncService());
61 *
62 * // Now the following ensures that events on both faces are processed
63 * face1.processEvents();
64 * </code>
65 */
66 Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<boost::asio::io_service> &ioService);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070067
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.
Jeff Thompson978c1522013-11-12 23:03:10 -080075 * @param wireFormat A WireFormat object used to encode the message.
Jeff Thompson11095142013-10-01 16:20:28 -070076 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -070077 */
Jeff Thompson62992e42013-10-07 18:50:51 -070078 uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080079 expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout);
Jeff Thompson7aec0252013-08-22 17:29:57 -070080
81 /**
Jeff Thompson11095142013-10-01 16:20:28 -070082 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
83 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
84 * If there is no entry with the pendingInterestId, do nothing.
85 * @param pendingInterestId The ID returned from expressInterest.
86 */
87 void
Jeff Thompson62992e42013-10-07 18:50:51 -070088 removePendingInterest(uint64_t pendingInterestId);
Jeff Thompson11095142013-10-01 16:20:28 -070089
90 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070091 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
92 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
93 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
94 * use func_lib::ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -070095 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
96 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -070097 * @param flags The flags for finer control of which interests are forward to the application.
Jeff Thompson978c1522013-11-12 23:03:10 -080098 * @param wireFormat A WireFormat object used to encode the message.
Jeff Thompson11095142013-10-01 16:20:28 -070099 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700100 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700101 uint64_t
Jeff Thompson590ec232013-09-18 15:55:56 -0700102 registerPrefix
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800103 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700104
105 /**
Jeff Thompson11095142013-10-01 16:20:28 -0700106 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
107 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
108 * If there is no entry with the registeredPrefixId, do nothing.
109 * @param registeredPrefixId The ID returned from registerPrefix.
110 */
111 void
Jeff Thompson62992e42013-10-07 18:50:51 -0700112 removeRegisteredPrefix(uint64_t registeredPrefixId);
Jeff Thompson11095142013-10-01 16:20:28 -0700113
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800114 /**
115 * @brief Publish data packet
116 *
117 * This method can be called to satisfy the incoming Interest or to put Data packet into the cache
118 * of the local NDN forwarder
119 */
120 void
121 put(const Data &data);
122
Jeff Thompson11095142013-10-01 16:20:28 -0700123 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800124 * Process any data to receive or call timeout callbacks.
125 *
126 * This call will block forever (default timeout == 0) to process IO on the face.
127 * To exit, one expected to call face.shutdown() from one of the callback methods.
128 *
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800129 * If positive timeout is specified, then processEvents will exit after this timeout,
130 * if not stopped earlier with face.shutdown() or when all active events finish.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800131 * The call can be called repeatedly, if desired.
132 *
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800133 * If negative timeout is specified, then processEvents will not block and process only pending
134 * events.
135 *
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700136 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
137 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
138 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700139 void
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800140 processEvents(Milliseconds timeout = 0, bool keepThread = false);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700141
Jeff Thompson0050abe2013-09-17 12:50:25 -0700142 const ptr_lib::shared_ptr<Transport>&
143 getTransport() { return transport_; }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700144
Jeff Thompson0050abe2013-09-17 12:50:25 -0700145 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700146 shutdown();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700147
148private:
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800149 void
150 onReceiveElement(const Block &wire);
151
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800152 struct ProcessEventsTimeout {};
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800153 static void
154 fireProcessEventsTimeout(const boost::system::error_code& error);
155
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800156private:
Jeff Thompson11095142013-10-01 16:20:28 -0700157 class PendingInterest {
Jeff Thompson557b81e2013-08-21 15:13:51 -0700158 public:
159 /**
160 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson11095142013-10-01 16:20:28 -0700161 * @param pendingInterestId A unique ID for this entry, which you should get with getNextPendingInteresId().
Jeff Thompson48917f02013-08-21 17:12:45 -0700162 * @param interest A shared_ptr for the interest.
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700163 * @param onData A function object to call when a matching data packet is received.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700164 * @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 -0700165 */
Jeff Thompson11095142013-10-01 16:20:28 -0700166 PendingInterest
Jeff Thompson62992e42013-10-07 18:50:51 -0700167 (uint64_t pendingInterestId, const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData,
Jeff Thompson11095142013-10-01 16:20:28 -0700168 const OnTimeout& onTimeout);
169
170 /**
171 * Return the next unique pending interest ID.
172 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700173 static uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700174 getNextPendingInterestId()
175 {
176 return ++lastPendingInterestId_;
177 }
178
179 /**
180 * Return the pendingInterestId given to the constructor.
181 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700182 uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700183 getPendingInterestId() { return pendingInterestId_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700184
Jeff Thompson0050abe2013-09-17 12:50:25 -0700185 const ptr_lib::shared_ptr<const Interest>&
186 getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700187
Jeff Thompson0050abe2013-09-17 12:50:25 -0700188 const OnData&
189 getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700190
191 /**
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800192 * Check if this interest is timed out.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700193 * @param nowMilliseconds The current time in milliseconds from ndn_getNowMilliseconds.
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800194 * @return true if this interest timed out, otherwise false.
Jeff Thompson48917f02013-08-21 17:12:45 -0700195 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700196 bool
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800197 isTimedOut(MillisecondsSince1970 nowMilliseconds)
198 {
199 return timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_;
200 }
201
202 /**
203 * Call onTimeout_ (if defined). This ignores exceptions from the onTimeout_.
204 */
205 void
206 callTimeout();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700207
208 private:
Jeff Thompson62992e42013-10-07 18:50:51 -0700209 static uint64_t lastPendingInterestId_; /**< A class variable used to get the next unique ID. */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800210
Jeff Thompson62992e42013-10-07 18:50:51 -0700211 uint64_t pendingInterestId_; /**< A unique identifier for this entry so it can be deleted */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800212 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson7aec0252013-08-22 17:29:57 -0700213 const OnData onData_;
214 const OnTimeout onTimeout_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800215
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700216 MillisecondsSince1970 timeoutTimeMilliseconds_; /**< The time when the interest times out in milliseconds according to ndn_getNowMilliseconds, or -1 for no timeout. */
Jeff Thompson557b81e2013-08-21 15:13:51 -0700217 };
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700218
Jeff Thompson11095142013-10-01 16:20:28 -0700219 class RegisteredPrefix {
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700220 public:
221 /**
222 * Create a new PrefixEntry.
Jeff Thompson11095142013-10-01 16:20:28 -0700223 * @param registeredPrefixId A unique ID for this entry, which you should get with getNextRegisteredPrefixId().
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700224 * @param prefix A shared_ptr for the prefix.
225 * @param onInterest A function object to call when a matching data packet is received.
226 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700227 RegisteredPrefix(uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800228 : registeredPrefixId_(registeredPrefixId)
229 , prefix_(prefix)
230 , onInterest_(onInterest)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700231 {
232 }
233
Jeff Thompson11095142013-10-01 16:20:28 -0700234 /**
235 * Return the next unique entry ID.
236 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700237 static uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700238 getNextRegisteredPrefixId()
239 {
240 return ++lastRegisteredPrefixId_;
241 }
242
243 /**
244 * Return the registeredPrefixId given to the constructor.
245 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700246 uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800247 getRegisteredPrefixId()
248 {
249 return registeredPrefixId_;
250 }
Jeff Thompson11095142013-10-01 16:20:28 -0700251
Jeff Thompson0050abe2013-09-17 12:50:25 -0700252 const ptr_lib::shared_ptr<const Name>&
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800253 getPrefix()
254 {
255 return prefix_;
256 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700257
Jeff Thompson0050abe2013-09-17 12:50:25 -0700258 const OnInterest&
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800259 getOnInterest()
260 {
261 return onInterest_;
262 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700263
264 private:
Jeff Thompson62992e42013-10-07 18:50:51 -0700265 static uint64_t lastRegisteredPrefixId_; /**< A class variable used to get the next unique ID. */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800266
Jeff Thompson62992e42013-10-07 18:50:51 -0700267 uint64_t registeredPrefixId_; /**< A unique identifier for this entry so it can be deleted */
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700268 ptr_lib::shared_ptr<const Name> prefix_;
269 const OnInterest onInterest_;
270 };
Jeff Thompson557b81e2013-08-21 15:13:51 -0700271
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800272 typedef std::vector<ptr_lib::shared_ptr<PendingInterest> > PendingInterestTable;
273 typedef std::vector<ptr_lib::shared_ptr<RegisteredPrefix> > RegisteredPrefixTable;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700274
275 /**
Jeff Thompson557b81e2013-08-21 15:13:51 -0700276 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
277 * the entry interest name is the longest that matches name.
278 * @param name The name to find the interest for (from the incoming data packet).
279 * @return The index in pit_ of the pit entry, or -1 if not found.
280 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800281 PendingInterestTable::iterator
Jeff Thompson590ec232013-09-18 15:55:56 -0700282 getEntryIndexForExpressedInterest(const Name& name);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700283
Jeff Thompson86507bc2013-08-23 20:51:38 -0700284 /**
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700285 * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name.
286 * @param name The name to find the PrefixEntry for (from the incoming interest packet).
287 * @return A pointer to the entry, or 0 if not found.
288 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800289 RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700290 getEntryForRegisteredPrefix(const Name& name);
Jeff Thompson590ec232013-09-18 15:55:56 -0700291
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700292 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700293 * Do the work of registerPrefix once we know we are connected with an ndndId_.
Jeff Thompson11095142013-10-01 16:20:28 -0700294 * @param registeredPrefixId The PrefixEntry::getNextRegisteredPrefixId() which registerPrefix got so it could return it to the caller.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700295 * @param prefix
296 * @param onInterest
Jeff Thompson590ec232013-09-18 15:55:56 -0700297 * @param onRegisterFailed
Jeff Thompson86507bc2013-08-23 20:51:38 -0700298 * @param flags
Jeff Thompson590ec232013-09-18 15:55:56 -0700299 * @param wireFormat
300 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700301 void
Jeff Thompson590ec232013-09-18 15:55:56 -0700302 registerPrefixHelper
Jeff Thompson62992e42013-10-07 18:50:51 -0700303 (uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest,
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800304 const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags);
305
Alexander Afanasyev18371872014-01-05 23:00:26 -0800306 /**
307 * @brief Final stage of prefix registration, invoked when registration succeeded
308 *
309 * This method actually sets entry in a local interest filter table
310 */
311 void
312 registerPrefixFinal(uint64_t registeredPrefixId,
313 const ptr_lib::shared_ptr<const Name>& prefix,
314 const OnInterest& onInterest,
315 const OnRegisterFailed& onRegisterFailed,
316 const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&);
317
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800318 void
319 checkPitExpire();
320
321private:
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800322 ptr_lib::shared_ptr<boost::asio::io_service> ioService_;
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800323 ptr_lib::shared_ptr<boost::asio::io_service::work> ioServiceWork_; // needed if thread needs to be preserved
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800324 ptr_lib::shared_ptr<boost::asio::deadline_timer> pitTimeoutCheckTimer_;
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800325 bool pitTimeoutCheckTimerActive_;
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800326 ptr_lib::shared_ptr<boost::asio::deadline_timer> processEventsTimeoutTimer_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700327
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700328 ptr_lib::shared_ptr<Transport> transport_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800329
330 PendingInterestTable pendingInterestTable_;
331 RegisteredPrefixTable registeredPrefixTable_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700332 Interest ndndIdFetcherInterest_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800333 Buffer ndndId_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700334};
335
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800336} // namespace ndn
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700337
338#endif