blob: 40923302973469f29be22ac5119785a92cb8dc2b [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);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070053
54 /**
Jeff Thompson4fe45512013-08-23 14:06:38 -070055 * Send the Interest through the transport, read the entire response and call onData(interest, data).
56 * @param interest A reference to the Interest. This copies the Interest.
57 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
58 * use func_lib::ref() as appropriate.
59 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
60 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson978c1522013-11-12 23:03:10 -080061 * @param wireFormat A WireFormat object used to encode the message.
Jeff Thompson11095142013-10-01 16:20:28 -070062 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -070063 */
Jeff Thompson62992e42013-10-07 18:50:51 -070064 uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080065 expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout);
Jeff Thompson7aec0252013-08-22 17:29:57 -070066
67 /**
Jeff Thompson11095142013-10-01 16:20:28 -070068 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
69 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
70 * If there is no entry with the pendingInterestId, do nothing.
71 * @param pendingInterestId The ID returned from expressInterest.
72 */
73 void
Jeff Thompson62992e42013-10-07 18:50:51 -070074 removePendingInterest(uint64_t pendingInterestId);
Jeff Thompson11095142013-10-01 16:20:28 -070075
76 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070077 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
78 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
79 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
80 * use func_lib::ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -070081 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
82 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -070083 * @param flags The flags for finer control of which interests are forward to the application.
Jeff Thompson978c1522013-11-12 23:03:10 -080084 * @param wireFormat A WireFormat object used to encode the message.
Jeff Thompson11095142013-10-01 16:20:28 -070085 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -070086 */
Jeff Thompson62992e42013-10-07 18:50:51 -070087 uint64_t
Jeff Thompson590ec232013-09-18 15:55:56 -070088 registerPrefix
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080089 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags);
Jeff Thompson86507bc2013-08-23 20:51:38 -070090
91 /**
Jeff Thompson11095142013-10-01 16:20:28 -070092 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
93 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
94 * If there is no entry with the registeredPrefixId, do nothing.
95 * @param registeredPrefixId The ID returned from registerPrefix.
96 */
97 void
Jeff Thompson62992e42013-10-07 18:50:51 -070098 removeRegisteredPrefix(uint64_t registeredPrefixId);
Jeff Thompson11095142013-10-01 16:20:28 -070099
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800100 /**
101 * @brief Publish data packet
102 *
103 * This method can be called to satisfy the incoming Interest or to put Data packet into the cache
104 * of the local NDN forwarder
105 */
106 void
107 put(const Data &data);
108
Jeff Thompson11095142013-10-01 16:20:28 -0700109 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800110 * Process any data to receive or call timeout callbacks.
111 *
112 * This call will block forever (default timeout == 0) to process IO on the face.
113 * To exit, one expected to call face.shutdown() from one of the callback methods.
114 *
115 * If timeout is specified, then processEvents will exit after this timeout, if not stopped earlier with face.shutdown().
116 * The call can be called repeatedly, if desired.
117 *
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700118 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
119 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
120 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700121 void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800122 processEvents(Milliseconds timeout = 0);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700123
Jeff Thompson0050abe2013-09-17 12:50:25 -0700124 const ptr_lib::shared_ptr<Transport>&
125 getTransport() { return transport_; }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700126
Jeff Thompson0050abe2013-09-17 12:50:25 -0700127 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700128 shutdown();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700129
130private:
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800131 void
132 onReceiveElement(const Block &wire);
133
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800134 void
135 onTransportError();
136
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800137 struct ProcessEventsTimeout {};
138
139 static void
140 fireProcessEventsTimeout(const boost::system::error_code& error);
141
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800142private:
Jeff Thompson11095142013-10-01 16:20:28 -0700143 class PendingInterest {
Jeff Thompson557b81e2013-08-21 15:13:51 -0700144 public:
145 /**
146 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson11095142013-10-01 16:20:28 -0700147 * @param pendingInterestId A unique ID for this entry, which you should get with getNextPendingInteresId().
Jeff Thompson48917f02013-08-21 17:12:45 -0700148 * @param interest A shared_ptr for the interest.
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700149 * @param onData A function object to call when a matching data packet is received.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700150 * @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 -0700151 */
Jeff Thompson11095142013-10-01 16:20:28 -0700152 PendingInterest
Jeff Thompson62992e42013-10-07 18:50:51 -0700153 (uint64_t pendingInterestId, const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData,
Jeff Thompson11095142013-10-01 16:20:28 -0700154 const OnTimeout& onTimeout);
155
156 /**
157 * Return the next unique pending interest ID.
158 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700159 static uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700160 getNextPendingInterestId()
161 {
162 return ++lastPendingInterestId_;
163 }
164
165 /**
166 * Return the pendingInterestId given to the constructor.
167 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700168 uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700169 getPendingInterestId() { return pendingInterestId_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700170
Jeff Thompson0050abe2013-09-17 12:50:25 -0700171 const ptr_lib::shared_ptr<const Interest>&
172 getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700173
Jeff Thompson0050abe2013-09-17 12:50:25 -0700174 const OnData&
175 getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700176
177 /**
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800178 * Check if this interest is timed out.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700179 * @param nowMilliseconds The current time in milliseconds from ndn_getNowMilliseconds.
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800180 * @return true if this interest timed out, otherwise false.
Jeff Thompson48917f02013-08-21 17:12:45 -0700181 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700182 bool
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800183 isTimedOut(MillisecondsSince1970 nowMilliseconds)
184 {
185 return timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_;
186 }
187
188 /**
189 * Call onTimeout_ (if defined). This ignores exceptions from the onTimeout_.
190 */
191 void
192 callTimeout();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700193
194 private:
Jeff Thompson62992e42013-10-07 18:50:51 -0700195 static uint64_t lastPendingInterestId_; /**< A class variable used to get the next unique ID. */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800196
Jeff Thompson62992e42013-10-07 18:50:51 -0700197 uint64_t pendingInterestId_; /**< A unique identifier for this entry so it can be deleted */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800198 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson7aec0252013-08-22 17:29:57 -0700199 const OnData onData_;
200 const OnTimeout onTimeout_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800201
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700202 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 -0700203 };
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700204
Jeff Thompson11095142013-10-01 16:20:28 -0700205 class RegisteredPrefix {
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700206 public:
207 /**
208 * Create a new PrefixEntry.
Jeff Thompson11095142013-10-01 16:20:28 -0700209 * @param registeredPrefixId A unique ID for this entry, which you should get with getNextRegisteredPrefixId().
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700210 * @param prefix A shared_ptr for the prefix.
211 * @param onInterest A function object to call when a matching data packet is received.
212 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700213 RegisteredPrefix(uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800214 : registeredPrefixId_(registeredPrefixId)
215 , prefix_(prefix)
216 , onInterest_(onInterest)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700217 {
218 }
219
Jeff Thompson11095142013-10-01 16:20:28 -0700220 /**
221 * Return the next unique entry ID.
222 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700223 static uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700224 getNextRegisteredPrefixId()
225 {
226 return ++lastRegisteredPrefixId_;
227 }
228
229 /**
230 * Return the registeredPrefixId given to the constructor.
231 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700232 uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800233 getRegisteredPrefixId()
234 {
235 return registeredPrefixId_;
236 }
Jeff Thompson11095142013-10-01 16:20:28 -0700237
Jeff Thompson0050abe2013-09-17 12:50:25 -0700238 const ptr_lib::shared_ptr<const Name>&
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800239 getPrefix()
240 {
241 return prefix_;
242 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700243
Jeff Thompson0050abe2013-09-17 12:50:25 -0700244 const OnInterest&
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800245 getOnInterest()
246 {
247 return onInterest_;
248 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700249
250 private:
Jeff Thompson62992e42013-10-07 18:50:51 -0700251 static uint64_t lastRegisteredPrefixId_; /**< A class variable used to get the next unique ID. */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800252
Jeff Thompson62992e42013-10-07 18:50:51 -0700253 uint64_t registeredPrefixId_; /**< A unique identifier for this entry so it can be deleted */
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700254 ptr_lib::shared_ptr<const Name> prefix_;
255 const OnInterest onInterest_;
256 };
Jeff Thompson557b81e2013-08-21 15:13:51 -0700257
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800258 typedef std::vector<ptr_lib::shared_ptr<PendingInterest> > PendingInterestTable;
259 typedef std::vector<ptr_lib::shared_ptr<RegisteredPrefix> > RegisteredPrefixTable;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700260
261 /**
Jeff Thompson557b81e2013-08-21 15:13:51 -0700262 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
263 * the entry interest name is the longest that matches name.
264 * @param name The name to find the interest for (from the incoming data packet).
265 * @return The index in pit_ of the pit entry, or -1 if not found.
266 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800267 PendingInterestTable::iterator
Jeff Thompson590ec232013-09-18 15:55:56 -0700268 getEntryIndexForExpressedInterest(const Name& name);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700269
Jeff Thompson86507bc2013-08-23 20:51:38 -0700270 /**
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700271 * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name.
272 * @param name The name to find the PrefixEntry for (from the incoming interest packet).
273 * @return A pointer to the entry, or 0 if not found.
274 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800275 RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700276 getEntryForRegisteredPrefix(const Name& name);
Jeff Thompson590ec232013-09-18 15:55:56 -0700277
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700278 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700279 * Do the work of registerPrefix once we know we are connected with an ndndId_.
Jeff Thompson11095142013-10-01 16:20:28 -0700280 * @param registeredPrefixId The PrefixEntry::getNextRegisteredPrefixId() which registerPrefix got so it could return it to the caller.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700281 * @param prefix
282 * @param onInterest
Jeff Thompson590ec232013-09-18 15:55:56 -0700283 * @param onRegisterFailed
Jeff Thompson86507bc2013-08-23 20:51:38 -0700284 * @param flags
Jeff Thompson590ec232013-09-18 15:55:56 -0700285 * @param wireFormat
286 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700287 void
Jeff Thompson590ec232013-09-18 15:55:56 -0700288 registerPrefixHelper
Jeff Thompson62992e42013-10-07 18:50:51 -0700289 (uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest,
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800290 const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags);
291
Alexander Afanasyev18371872014-01-05 23:00:26 -0800292 /**
293 * @brief Final stage of prefix registration, invoked when registration succeeded
294 *
295 * This method actually sets entry in a local interest filter table
296 */
297 void
298 registerPrefixFinal(uint64_t registeredPrefixId,
299 const ptr_lib::shared_ptr<const Name>& prefix,
300 const OnInterest& onInterest,
301 const OnRegisterFailed& onRegisterFailed,
302 const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&);
303
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800304 void
305 checkPitExpire();
306
307private:
308 boost::asio::io_service ioService_;
309 boost::asio::deadline_timer timer_;
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800310 boost::asio::deadline_timer processEventsTimeoutTimer_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700311
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700312 ptr_lib::shared_ptr<Transport> transport_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800313
314 PendingInterestTable pendingInterestTable_;
315 RegisteredPrefixTable registeredPrefixTable_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700316 Interest ndndIdFetcherInterest_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800317 Buffer ndndId_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700318};
319
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800320} // namespace ndn
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700321
322#endif