blob: 78920756e678e1a682fca7fbc8f7c538157e6b3e [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 -070017namespace ndn {
18
Alexander Afanasyevb790d952014-01-24 12:07:53 -080019struct PendingInterestId;
20struct RegisteredPrefixId;
21
Jeff Thompson7aec0252013-08-22 17:29:57 -070022/**
23 * An OnData function object is used to pass a callback to expressInterest.
24 */
Jeff Thompsona6d15c82013-09-17 15:34:32 -070025typedef 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 -070026
27/**
28 * An OnTimeout function object is used to pass a callback to expressInterest.
29 */
Jeff Thompsona6d15c82013-09-17 15:34:32 -070030typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest>&)> OnTimeout;
Jeff Thompson7aec0252013-08-22 17:29:57 -070031
Jeff Thompson86507bc2013-08-23 20:51:38 -070032/**
33 * An OnInterest function object is used to pass a callback to registerPrefix.
34 */
Jeff Thompson9cc4be42013-08-27 18:12:41 -070035typedef func_lib::function<void
Alexander Afanasyevb790d952014-01-24 12:07:53 -080036 (const ptr_lib::shared_ptr<const Name>&, const ptr_lib::shared_ptr<const Interest>&)> OnInterest;
Jeff Thompson86507bc2013-08-23 20:51:38 -070037
Jeff Thompson590ec232013-09-18 15:55:56 -070038/**
39 * An OnRegisterFailed function object is used to report when registerPrefix fails.
40 */
41typedef func_lib::function<void(const ptr_lib::shared_ptr<const Name>&)> OnRegisterFailed;
42
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070043class Face;
Jeff Thompson590ec232013-09-18 15:55:56 -070044
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080045class Node {
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070046public:
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080047 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
48
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070049 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070050 * Create a new Node for communication with an NDN hub with the given Transport object and connectionInfo.
51 * @param transport A shared_ptr to a Transport object used for communication.
52 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070053 */
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080054 Node(const ptr_lib::shared_ptr<Transport>& transport);
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080055
56 /**
57 * @brief Alternative (special use case) version of the constructor, can be used to aggregate
58 * several Faces within one processing thread
59 *
60 * <code>
61 * Face face1(...);
62 * Face face2(..., face1.getAsyncService());
63 *
64 * // Now the following ensures that events on both faces are processed
65 * face1.processEvents();
66 * </code>
67 */
68 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 -070069
70 /**
Jeff Thompson4fe45512013-08-23 14:06:38 -070071 * Send the Interest through the transport, read the entire response and call onData(interest, data).
72 * @param interest A reference to the Interest. This copies the Interest.
73 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
74 * use func_lib::ref() as appropriate.
75 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
76 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson978c1522013-11-12 23:03:10 -080077 * @param wireFormat A WireFormat object used to encode the message.
Jeff Thompson11095142013-10-01 16:20:28 -070078 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -070079 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -080080 const PendingInterestId*
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080081 expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout);
Jeff Thompson7aec0252013-08-22 17:29:57 -070082
83 /**
Jeff Thompson11095142013-10-01 16:20:28 -070084 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
85 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
86 * If there is no entry with the pendingInterestId, do nothing.
87 * @param pendingInterestId The ID returned from expressInterest.
88 */
89 void
Alexander Afanasyevb790d952014-01-24 12:07:53 -080090 removePendingInterest(const PendingInterestId *pendingInterestId);
Jeff Thompson11095142013-10-01 16:20:28 -070091
92 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070093 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
94 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
95 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
96 * use func_lib::ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -070097 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
98 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -070099 * @param flags The flags for finer control of which interests are forward to the application.
Jeff Thompson978c1522013-11-12 23:03:10 -0800100 * @param wireFormat A WireFormat object used to encode the message.
Jeff Thompson11095142013-10-01 16:20:28 -0700101 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700102 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800103 const RegisteredPrefixId*
Jeff Thompson590ec232013-09-18 15:55:56 -0700104 registerPrefix
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800105 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700106
107 /**
Jeff Thompson11095142013-10-01 16:20:28 -0700108 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
109 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
110 * If there is no entry with the registeredPrefixId, do nothing.
111 * @param registeredPrefixId The ID returned from registerPrefix.
112 */
113 void
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800114 removeRegisteredPrefix(const RegisteredPrefixId *registeredPrefixId);
Jeff Thompson11095142013-10-01 16:20:28 -0700115
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800116 /**
117 * @brief Publish data packet
118 *
119 * This method can be called to satisfy the incoming Interest or to put Data packet into the cache
120 * of the local NDN forwarder
121 */
122 void
123 put(const Data &data);
124
Jeff Thompson11095142013-10-01 16:20:28 -0700125 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800126 * Process any data to receive or call timeout callbacks.
127 *
128 * This call will block forever (default timeout == 0) to process IO on the face.
129 * To exit, one expected to call face.shutdown() from one of the callback methods.
130 *
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800131 * If positive timeout is specified, then processEvents will exit after this timeout,
132 * if not stopped earlier with face.shutdown() or when all active events finish.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800133 * The call can be called repeatedly, if desired.
134 *
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800135 * If negative timeout is specified, then processEvents will not block and process only pending
136 * events.
137 *
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700138 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
139 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
140 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700141 void
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800142 processEvents(Milliseconds timeout = 0, bool keepThread = false);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700143
Jeff Thompson0050abe2013-09-17 12:50:25 -0700144 const ptr_lib::shared_ptr<Transport>&
145 getTransport() { return transport_; }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700146
Jeff Thompson0050abe2013-09-17 12:50:25 -0700147 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700148 shutdown();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700149
150private:
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800151 void
152 asyncExpressInterest(const ptr_lib::shared_ptr<const Interest> &interest,
153 const OnData& onData, const OnTimeout& onTimeout);
154
155 void
156 asyncRemovePendingInterest(const PendingInterestId *pendingInterestId);
157
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800158 void
159 onReceiveElement(const Block &wire);
160
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800161 struct ProcessEventsTimeout {};
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800162 static void
163 fireProcessEventsTimeout(const boost::system::error_code& error);
164
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800165private:
Jeff Thompson11095142013-10-01 16:20:28 -0700166 class PendingInterest {
Jeff Thompson557b81e2013-08-21 15:13:51 -0700167 public:
168 /**
169 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson48917f02013-08-21 17:12:45 -0700170 * @param interest A shared_ptr for the interest.
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700171 * @param onData A function object to call when a matching data packet is received.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700172 * @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 -0700173 */
Jeff Thompson11095142013-10-01 16:20:28 -0700174 PendingInterest
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800175 (const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData,
Jeff Thompson11095142013-10-01 16:20:28 -0700176 const OnTimeout& onTimeout);
177
Jeff Thompson0050abe2013-09-17 12:50:25 -0700178 const ptr_lib::shared_ptr<const Interest>&
179 getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700180
Jeff Thompson0050abe2013-09-17 12:50:25 -0700181 const OnData&
182 getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700183
184 /**
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800185 * Check if this interest is timed out.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700186 * @param nowMilliseconds The current time in milliseconds from ndn_getNowMilliseconds.
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800187 * @return true if this interest timed out, otherwise false.
Jeff Thompson48917f02013-08-21 17:12:45 -0700188 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700189 bool
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800190 isTimedOut(MillisecondsSince1970 nowMilliseconds)
191 {
192 return timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_;
193 }
194
195 /**
196 * Call onTimeout_ (if defined). This ignores exceptions from the onTimeout_.
197 */
198 void
199 callTimeout();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700200
201 private:
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800202 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson7aec0252013-08-22 17:29:57 -0700203 const OnData onData_;
204 const OnTimeout onTimeout_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800205
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700206 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 -0700207 };
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700208
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800209 // Functor to match pending interests against PendingInterestId
210 struct MatchPendingInterestId
211 {
212 MatchPendingInterestId(const PendingInterestId *pendingInterestId)
213 : id_(pendingInterestId)
214 {
215 }
216
217 bool
218 operator()(const ptr_lib::shared_ptr<const PendingInterest> &pendingInterest) const
219 {
220 return (reinterpret_cast<const PendingInterestId *>(pendingInterest.get()) == id_);
221 }
222 private:
223 const PendingInterestId *id_;
224 };
225
226
Jeff Thompson11095142013-10-01 16:20:28 -0700227 class RegisteredPrefix {
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700228 public:
229 /**
230 * Create a new PrefixEntry.
231 * @param prefix A shared_ptr for the prefix.
232 * @param onInterest A function object to call when a matching data packet is received.
233 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800234 RegisteredPrefix(const Name& prefix, const OnInterest& onInterest)
235 : prefix_(new Name(prefix))
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800236 , onInterest_(onInterest)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700237 {
238 }
239
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800240 const Name&
241 getPrefix() const
Jeff Thompson11095142013-10-01 16:20:28 -0700242 {
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800243 return *prefix_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800244 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700245
Jeff Thompson0050abe2013-09-17 12:50:25 -0700246 const OnInterest&
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800247 getOnInterest() const
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800248 {
249 return onInterest_;
250 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700251
252 private:
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800253 ptr_lib::shared_ptr<Name> prefix_;
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700254 const OnInterest onInterest_;
255 };
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800256
257 // Functor to match pending interests against PendingInterestId
258 struct MatchRegisteredPrefixId
259 {
260 MatchRegisteredPrefixId(const RegisteredPrefixId *registeredPrefixId)
261 : id_(registeredPrefixId)
262 {
263 }
264
265 bool
266 operator()(const ptr_lib::shared_ptr<RegisteredPrefix> &registeredPrefix) const
267 {
268 return (reinterpret_cast<const RegisteredPrefixId *>(registeredPrefix.get()) == id_);
269 }
270 private:
271 const RegisteredPrefixId *id_;
272 };
273
Jeff Thompson557b81e2013-08-21 15:13:51 -0700274
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800275 typedef std::vector<ptr_lib::shared_ptr<PendingInterest> > PendingInterestTable;
276 typedef std::vector<ptr_lib::shared_ptr<RegisteredPrefix> > RegisteredPrefixTable;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700277
278 /**
Jeff Thompson557b81e2013-08-21 15:13:51 -0700279 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
280 * the entry interest name is the longest that matches name.
281 * @param name The name to find the interest for (from the incoming data packet).
282 * @return The index in pit_ of the pit entry, or -1 if not found.
283 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800284 PendingInterestTable::iterator
Jeff Thompson590ec232013-09-18 15:55:56 -0700285 getEntryIndexForExpressedInterest(const Name& name);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700286
Jeff Thompson86507bc2013-08-23 20:51:38 -0700287 /**
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700288 * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name.
289 * @param name The name to find the PrefixEntry for (from the incoming interest packet).
290 * @return A pointer to the entry, or 0 if not found.
291 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800292 RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700293 getEntryForRegisteredPrefix(const Name& name);
Jeff Thompson590ec232013-09-18 15:55:56 -0700294
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700295 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700296 * Do the work of registerPrefix once we know we are connected with an ndndId_.
Jeff Thompson11095142013-10-01 16:20:28 -0700297 * @param registeredPrefixId The PrefixEntry::getNextRegisteredPrefixId() which registerPrefix got so it could return it to the caller.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700298 * @param prefix
299 * @param onInterest
Jeff Thompson590ec232013-09-18 15:55:56 -0700300 * @param onRegisterFailed
Jeff Thompson86507bc2013-08-23 20:51:38 -0700301 * @param flags
Jeff Thompson590ec232013-09-18 15:55:56 -0700302 * @param wireFormat
303 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700304 void
Jeff Thompson590ec232013-09-18 15:55:56 -0700305 registerPrefixHelper
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800306 (const ptr_lib::shared_ptr<RegisteredPrefix> &prefixToRegister,
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800307 const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags);
308
Alexander Afanasyev18371872014-01-05 23:00:26 -0800309 /**
310 * @brief Final stage of prefix registration, invoked when registration succeeded
311 *
312 * This method actually sets entry in a local interest filter table
313 */
314 void
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800315 registerPrefixFinal(const ptr_lib::shared_ptr<RegisteredPrefix> &prefixToRegister,
Alexander Afanasyev18371872014-01-05 23:00:26 -0800316 const OnRegisterFailed& onRegisterFailed,
317 const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&);
318
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800319 void
320 checkPitExpire();
321
322private:
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800323 ptr_lib::shared_ptr<boost::asio::io_service> ioService_;
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800324 ptr_lib::shared_ptr<boost::asio::io_service::work> ioServiceWork_; // needed if thread needs to be preserved
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800325 ptr_lib::shared_ptr<boost::asio::deadline_timer> pitTimeoutCheckTimer_;
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800326 bool pitTimeoutCheckTimerActive_;
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800327 ptr_lib::shared_ptr<boost::asio::deadline_timer> processEventsTimeoutTimer_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700328
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700329 ptr_lib::shared_ptr<Transport> transport_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800330
331 PendingInterestTable pendingInterestTable_;
332 RegisteredPrefixTable registeredPrefixTable_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700333 Interest ndndIdFetcherInterest_;
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800334
335 int64_t faceId_; // internal face ID (needed for prefix de-registration)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800336 Buffer ndndId_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700337};
338
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800339} // namespace ndn
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700340
341#endif