blob: 6dc19540942f1429c463d55deaa122d77670d5ca [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
Yingdi Yu0d920812014-01-30 14:50:57 -0800150 shared_ptr<boost::asio::io_service>
151 ioService() { return ioService_; }
152
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700153private:
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800154 void
155 asyncExpressInterest(const ptr_lib::shared_ptr<const Interest> &interest,
156 const OnData& onData, const OnTimeout& onTimeout);
157
158 void
159 asyncRemovePendingInterest(const PendingInterestId *pendingInterestId);
160
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800161 void
162 onReceiveElement(const Block &wire);
163
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800164 struct ProcessEventsTimeout {};
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800165 static void
166 fireProcessEventsTimeout(const boost::system::error_code& error);
167
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800168private:
Jeff Thompson11095142013-10-01 16:20:28 -0700169 class PendingInterest {
Jeff Thompson557b81e2013-08-21 15:13:51 -0700170 public:
171 /**
172 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson48917f02013-08-21 17:12:45 -0700173 * @param interest A shared_ptr for the interest.
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700174 * @param onData A function object to call when a matching data packet is received.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700175 * @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 -0700176 */
Jeff Thompson11095142013-10-01 16:20:28 -0700177 PendingInterest
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800178 (const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData,
Jeff Thompson11095142013-10-01 16:20:28 -0700179 const OnTimeout& onTimeout);
180
Jeff Thompson0050abe2013-09-17 12:50:25 -0700181 const ptr_lib::shared_ptr<const Interest>&
182 getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700183
Jeff Thompson0050abe2013-09-17 12:50:25 -0700184 const OnData&
185 getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700186
187 /**
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800188 * Check if this interest is timed out.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700189 * @param nowMilliseconds The current time in milliseconds from ndn_getNowMilliseconds.
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800190 * @return true if this interest timed out, otherwise false.
Jeff Thompson48917f02013-08-21 17:12:45 -0700191 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700192 bool
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800193 isTimedOut(MillisecondsSince1970 nowMilliseconds)
194 {
195 return timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_;
196 }
197
198 /**
199 * Call onTimeout_ (if defined). This ignores exceptions from the onTimeout_.
200 */
201 void
202 callTimeout();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700203
204 private:
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800205 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson7aec0252013-08-22 17:29:57 -0700206 const OnData onData_;
207 const OnTimeout onTimeout_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800208
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700209 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 -0700210 };
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700211
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800212 // Functor to match pending interests against PendingInterestId
213 struct MatchPendingInterestId
214 {
215 MatchPendingInterestId(const PendingInterestId *pendingInterestId)
216 : id_(pendingInterestId)
217 {
218 }
219
220 bool
221 operator()(const ptr_lib::shared_ptr<const PendingInterest> &pendingInterest) const
222 {
223 return (reinterpret_cast<const PendingInterestId *>(pendingInterest.get()) == id_);
224 }
225 private:
226 const PendingInterestId *id_;
227 };
228
229
Jeff Thompson11095142013-10-01 16:20:28 -0700230 class RegisteredPrefix {
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700231 public:
232 /**
233 * Create a new PrefixEntry.
234 * @param prefix A shared_ptr for the prefix.
235 * @param onInterest A function object to call when a matching data packet is received.
236 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800237 RegisteredPrefix(const Name& prefix, const OnInterest& onInterest)
238 : prefix_(new Name(prefix))
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800239 , onInterest_(onInterest)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700240 {
241 }
242
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800243 const Name&
244 getPrefix() const
Jeff Thompson11095142013-10-01 16:20:28 -0700245 {
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800246 return *prefix_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800247 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700248
Jeff Thompson0050abe2013-09-17 12:50:25 -0700249 const OnInterest&
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800250 getOnInterest() const
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800251 {
252 return onInterest_;
253 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700254
255 private:
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800256 ptr_lib::shared_ptr<Name> prefix_;
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700257 const OnInterest onInterest_;
258 };
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800259
260 // Functor to match pending interests against PendingInterestId
261 struct MatchRegisteredPrefixId
262 {
263 MatchRegisteredPrefixId(const RegisteredPrefixId *registeredPrefixId)
264 : id_(registeredPrefixId)
265 {
266 }
267
268 bool
269 operator()(const ptr_lib::shared_ptr<RegisteredPrefix> &registeredPrefix) const
270 {
271 return (reinterpret_cast<const RegisteredPrefixId *>(registeredPrefix.get()) == id_);
272 }
273 private:
274 const RegisteredPrefixId *id_;
275 };
276
Jeff Thompson557b81e2013-08-21 15:13:51 -0700277
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800278 typedef std::vector<ptr_lib::shared_ptr<PendingInterest> > PendingInterestTable;
279 typedef std::vector<ptr_lib::shared_ptr<RegisteredPrefix> > RegisteredPrefixTable;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700280
281 /**
Jeff Thompson557b81e2013-08-21 15:13:51 -0700282 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
283 * the entry interest name is the longest that matches name.
284 * @param name The name to find the interest for (from the incoming data packet).
285 * @return The index in pit_ of the pit entry, or -1 if not found.
286 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800287 PendingInterestTable::iterator
Jeff Thompson590ec232013-09-18 15:55:56 -0700288 getEntryIndexForExpressedInterest(const Name& name);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700289
Jeff Thompson86507bc2013-08-23 20:51:38 -0700290 /**
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700291 * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name.
292 * @param name The name to find the PrefixEntry for (from the incoming interest packet).
293 * @return A pointer to the entry, or 0 if not found.
294 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800295 RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700296 getEntryForRegisteredPrefix(const Name& name);
Jeff Thompson590ec232013-09-18 15:55:56 -0700297
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700298 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700299 * Do the work of registerPrefix once we know we are connected with an ndndId_.
Jeff Thompson11095142013-10-01 16:20:28 -0700300 * @param registeredPrefixId The PrefixEntry::getNextRegisteredPrefixId() which registerPrefix got so it could return it to the caller.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700301 * @param prefix
302 * @param onInterest
Jeff Thompson590ec232013-09-18 15:55:56 -0700303 * @param onRegisterFailed
Jeff Thompson86507bc2013-08-23 20:51:38 -0700304 * @param flags
Jeff Thompson590ec232013-09-18 15:55:56 -0700305 * @param wireFormat
306 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700307 void
Jeff Thompson590ec232013-09-18 15:55:56 -0700308 registerPrefixHelper
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800309 (const ptr_lib::shared_ptr<RegisteredPrefix> &prefixToRegister,
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800310 const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags);
311
Alexander Afanasyev18371872014-01-05 23:00:26 -0800312 /**
313 * @brief Final stage of prefix registration, invoked when registration succeeded
314 *
315 * This method actually sets entry in a local interest filter table
316 */
317 void
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800318 registerPrefixFinal(const ptr_lib::shared_ptr<RegisteredPrefix> &prefixToRegister,
Alexander Afanasyev18371872014-01-05 23:00:26 -0800319 const OnRegisterFailed& onRegisterFailed,
320 const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&);
321
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800322 void
323 checkPitExpire();
324
325private:
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800326 ptr_lib::shared_ptr<boost::asio::io_service> ioService_;
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800327 ptr_lib::shared_ptr<boost::asio::io_service::work> ioServiceWork_; // needed if thread needs to be preserved
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800328 ptr_lib::shared_ptr<boost::asio::deadline_timer> pitTimeoutCheckTimer_;
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800329 bool pitTimeoutCheckTimerActive_;
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800330 ptr_lib::shared_ptr<boost::asio::deadline_timer> processEventsTimeoutTimer_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700331
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700332 ptr_lib::shared_ptr<Transport> transport_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800333
334 PendingInterestTable pendingInterestTable_;
335 RegisteredPrefixTable registeredPrefixTable_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700336 Interest ndndIdFetcherInterest_;
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800337
338 int64_t faceId_; // internal face ID (needed for prefix de-registration)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800339 Buffer ndndId_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700340};
341
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800342} // namespace ndn
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700343
344#endif