blob: ef1d40e6ab5bc48deb4f8d87bb1714a07270273c [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 Afanasyeve2e0d752014-01-03 13:30:30 -0800137private:
Jeff Thompson11095142013-10-01 16:20:28 -0700138 class PendingInterest {
Jeff Thompson557b81e2013-08-21 15:13:51 -0700139 public:
140 /**
141 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson11095142013-10-01 16:20:28 -0700142 * @param pendingInterestId A unique ID for this entry, which you should get with getNextPendingInteresId().
Jeff Thompson48917f02013-08-21 17:12:45 -0700143 * @param interest A shared_ptr for the interest.
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700144 * @param onData A function object to call when a matching data packet is received.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700145 * @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 -0700146 */
Jeff Thompson11095142013-10-01 16:20:28 -0700147 PendingInterest
Jeff Thompson62992e42013-10-07 18:50:51 -0700148 (uint64_t pendingInterestId, const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData,
Jeff Thompson11095142013-10-01 16:20:28 -0700149 const OnTimeout& onTimeout);
150
151 /**
152 * Return the next unique pending interest ID.
153 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700154 static uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700155 getNextPendingInterestId()
156 {
157 return ++lastPendingInterestId_;
158 }
159
160 /**
161 * Return the pendingInterestId given to the constructor.
162 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700163 uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700164 getPendingInterestId() { return pendingInterestId_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700165
Jeff Thompson0050abe2013-09-17 12:50:25 -0700166 const ptr_lib::shared_ptr<const Interest>&
167 getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700168
Jeff Thompson0050abe2013-09-17 12:50:25 -0700169 const OnData&
170 getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700171
172 /**
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800173 * Check if this interest is timed out.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700174 * @param nowMilliseconds The current time in milliseconds from ndn_getNowMilliseconds.
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800175 * @return true if this interest timed out, otherwise false.
Jeff Thompson48917f02013-08-21 17:12:45 -0700176 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700177 bool
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800178 isTimedOut(MillisecondsSince1970 nowMilliseconds)
179 {
180 return timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_;
181 }
182
183 /**
184 * Call onTimeout_ (if defined). This ignores exceptions from the onTimeout_.
185 */
186 void
187 callTimeout();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700188
189 private:
Jeff Thompson62992e42013-10-07 18:50:51 -0700190 static uint64_t lastPendingInterestId_; /**< A class variable used to get the next unique ID. */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800191
Jeff Thompson62992e42013-10-07 18:50:51 -0700192 uint64_t pendingInterestId_; /**< A unique identifier for this entry so it can be deleted */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800193 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson7aec0252013-08-22 17:29:57 -0700194 const OnData onData_;
195 const OnTimeout onTimeout_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800196
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700197 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 -0700198 };
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700199
Jeff Thompson11095142013-10-01 16:20:28 -0700200 class RegisteredPrefix {
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700201 public:
202 /**
203 * Create a new PrefixEntry.
Jeff Thompson11095142013-10-01 16:20:28 -0700204 * @param registeredPrefixId A unique ID for this entry, which you should get with getNextRegisteredPrefixId().
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700205 * @param prefix A shared_ptr for the prefix.
206 * @param onInterest A function object to call when a matching data packet is received.
207 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700208 RegisteredPrefix(uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800209 : registeredPrefixId_(registeredPrefixId)
210 , prefix_(prefix)
211 , onInterest_(onInterest)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700212 {
213 }
214
Jeff Thompson11095142013-10-01 16:20:28 -0700215 /**
216 * Return the next unique entry ID.
217 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700218 static uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700219 getNextRegisteredPrefixId()
220 {
221 return ++lastRegisteredPrefixId_;
222 }
223
224 /**
225 * Return the registeredPrefixId given to the constructor.
226 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700227 uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800228 getRegisteredPrefixId()
229 {
230 return registeredPrefixId_;
231 }
Jeff Thompson11095142013-10-01 16:20:28 -0700232
Jeff Thompson0050abe2013-09-17 12:50:25 -0700233 const ptr_lib::shared_ptr<const Name>&
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800234 getPrefix()
235 {
236 return prefix_;
237 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700238
Jeff Thompson0050abe2013-09-17 12:50:25 -0700239 const OnInterest&
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800240 getOnInterest()
241 {
242 return onInterest_;
243 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700244
245 private:
Jeff Thompson62992e42013-10-07 18:50:51 -0700246 static uint64_t lastRegisteredPrefixId_; /**< A class variable used to get the next unique ID. */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800247
Jeff Thompson62992e42013-10-07 18:50:51 -0700248 uint64_t registeredPrefixId_; /**< A unique identifier for this entry so it can be deleted */
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700249 ptr_lib::shared_ptr<const Name> prefix_;
250 const OnInterest onInterest_;
251 };
Jeff Thompson557b81e2013-08-21 15:13:51 -0700252
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800253 typedef std::vector<ptr_lib::shared_ptr<PendingInterest> > PendingInterestTable;
254 typedef std::vector<ptr_lib::shared_ptr<RegisteredPrefix> > RegisteredPrefixTable;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700255
256 /**
Jeff Thompson557b81e2013-08-21 15:13:51 -0700257 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
258 * the entry interest name is the longest that matches name.
259 * @param name The name to find the interest for (from the incoming data packet).
260 * @return The index in pit_ of the pit entry, or -1 if not found.
261 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800262 PendingInterestTable::iterator
Jeff Thompson590ec232013-09-18 15:55:56 -0700263 getEntryIndexForExpressedInterest(const Name& name);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700264
Jeff Thompson86507bc2013-08-23 20:51:38 -0700265 /**
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700266 * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name.
267 * @param name The name to find the PrefixEntry for (from the incoming interest packet).
268 * @return A pointer to the entry, or 0 if not found.
269 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800270 RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700271 getEntryForRegisteredPrefix(const Name& name);
Jeff Thompson590ec232013-09-18 15:55:56 -0700272
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700273 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700274 * Do the work of registerPrefix once we know we are connected with an ndndId_.
Jeff Thompson11095142013-10-01 16:20:28 -0700275 * @param registeredPrefixId The PrefixEntry::getNextRegisteredPrefixId() which registerPrefix got so it could return it to the caller.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700276 * @param prefix
277 * @param onInterest
Jeff Thompson590ec232013-09-18 15:55:56 -0700278 * @param onRegisterFailed
Jeff Thompson86507bc2013-08-23 20:51:38 -0700279 * @param flags
Jeff Thompson590ec232013-09-18 15:55:56 -0700280 * @param wireFormat
281 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700282 void
Jeff Thompson590ec232013-09-18 15:55:56 -0700283 registerPrefixHelper
Jeff Thompson62992e42013-10-07 18:50:51 -0700284 (uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest,
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800285 const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags);
286
Alexander Afanasyev18371872014-01-05 23:00:26 -0800287 /**
288 * @brief Final stage of prefix registration, invoked when registration succeeded
289 *
290 * This method actually sets entry in a local interest filter table
291 */
292 void
293 registerPrefixFinal(uint64_t registeredPrefixId,
294 const ptr_lib::shared_ptr<const Name>& prefix,
295 const OnInterest& onInterest,
296 const OnRegisterFailed& onRegisterFailed,
297 const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&);
298
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800299 void
300 checkPitExpire();
301
302private:
303 boost::asio::io_service ioService_;
304 boost::asio::deadline_timer timer_;
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800305 boost::asio::deadline_timer processEventsTimeoutTimer_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700306
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700307 ptr_lib::shared_ptr<Transport> transport_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800308
309 PendingInterestTable pendingInterestTable_;
310 RegisteredPrefixTable registeredPrefixTable_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700311 Interest ndndIdFetcherInterest_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800312 Buffer ndndId_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700313};
314
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800315} // namespace ndn
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700316
317#endif