blob: 3c434804fb2b86a827afc5a18a0a6e319c178265 [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:
45 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070046 * Create a new Node for communication with an NDN hub with the given Transport object and connectionInfo.
47 * @param transport A shared_ptr to a Transport object used for communication.
48 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070049 */
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080050 Node(const ptr_lib::shared_ptr<Transport>& transport);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070051
52 /**
Jeff Thompson4fe45512013-08-23 14:06:38 -070053 * Send the Interest through the transport, read the entire response and call onData(interest, data).
54 * @param interest A reference to the Interest. This copies the Interest.
55 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
56 * use func_lib::ref() as appropriate.
57 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
58 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson978c1522013-11-12 23:03:10 -080059 * @param wireFormat A WireFormat object used to encode the message.
Jeff Thompson11095142013-10-01 16:20:28 -070060 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -070061 */
Jeff Thompson62992e42013-10-07 18:50:51 -070062 uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080063 expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout);
Jeff Thompson7aec0252013-08-22 17:29:57 -070064
65 /**
Jeff Thompson11095142013-10-01 16:20:28 -070066 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
67 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
68 * If there is no entry with the pendingInterestId, do nothing.
69 * @param pendingInterestId The ID returned from expressInterest.
70 */
71 void
Jeff Thompson62992e42013-10-07 18:50:51 -070072 removePendingInterest(uint64_t pendingInterestId);
Jeff Thompson11095142013-10-01 16:20:28 -070073
74 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070075 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
76 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
77 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
78 * use func_lib::ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -070079 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
80 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -070081 * @param flags The flags for finer control of which interests are forward to the application.
Jeff Thompson978c1522013-11-12 23:03:10 -080082 * @param wireFormat A WireFormat object used to encode the message.
Jeff Thompson11095142013-10-01 16:20:28 -070083 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -070084 */
Jeff Thompson62992e42013-10-07 18:50:51 -070085 uint64_t
Jeff Thompson590ec232013-09-18 15:55:56 -070086 registerPrefix
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080087 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags);
Jeff Thompson86507bc2013-08-23 20:51:38 -070088
89 /**
Jeff Thompson11095142013-10-01 16:20:28 -070090 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
91 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
92 * If there is no entry with the registeredPrefixId, do nothing.
93 * @param registeredPrefixId The ID returned from registerPrefix.
94 */
95 void
Jeff Thompson62992e42013-10-07 18:50:51 -070096 removeRegisteredPrefix(uint64_t registeredPrefixId);
Jeff Thompson11095142013-10-01 16:20:28 -070097
98 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070099 * Process any data to receive. For each element received, call onReceivedElement.
100 * This is non-blocking and will return immediately if there is no data to receive.
101 * You should repeatedly call this from an event loop, with calls to sleep as needed so that the loop doesn't use 100% of the CPU.
102 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
103 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
104 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700105 void
106 processEvents();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700107
Jeff Thompson0050abe2013-09-17 12:50:25 -0700108 const ptr_lib::shared_ptr<Transport>&
109 getTransport() { return transport_; }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700110
Jeff Thompson0050abe2013-09-17 12:50:25 -0700111 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700112 shutdown();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700113
114private:
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800115 void
116 onReceiveElement(const Block &wire);
117
118private:
Jeff Thompson11095142013-10-01 16:20:28 -0700119 class PendingInterest {
Jeff Thompson557b81e2013-08-21 15:13:51 -0700120 public:
121 /**
122 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson11095142013-10-01 16:20:28 -0700123 * @param pendingInterestId A unique ID for this entry, which you should get with getNextPendingInteresId().
Jeff Thompson48917f02013-08-21 17:12:45 -0700124 * @param interest A shared_ptr for the interest.
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700125 * @param onData A function object to call when a matching data packet is received.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700126 * @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 -0700127 */
Jeff Thompson11095142013-10-01 16:20:28 -0700128 PendingInterest
Jeff Thompson62992e42013-10-07 18:50:51 -0700129 (uint64_t pendingInterestId, const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData,
Jeff Thompson11095142013-10-01 16:20:28 -0700130 const OnTimeout& onTimeout);
131
132 /**
133 * Return the next unique pending interest ID.
134 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700135 static uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700136 getNextPendingInterestId()
137 {
138 return ++lastPendingInterestId_;
139 }
140
141 /**
142 * Return the pendingInterestId given to the constructor.
143 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700144 uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700145 getPendingInterestId() { return pendingInterestId_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700146
Jeff Thompson0050abe2013-09-17 12:50:25 -0700147 const ptr_lib::shared_ptr<const Interest>&
148 getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700149
Jeff Thompson0050abe2013-09-17 12:50:25 -0700150 const OnData&
151 getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700152
153 /**
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800154 * Check if this interest is timed out.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700155 * @param nowMilliseconds The current time in milliseconds from ndn_getNowMilliseconds.
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800156 * @return true if this interest timed out, otherwise false.
Jeff Thompson48917f02013-08-21 17:12:45 -0700157 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700158 bool
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800159 isTimedOut(MillisecondsSince1970 nowMilliseconds)
160 {
161 return timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_;
162 }
163
164 /**
165 * Call onTimeout_ (if defined). This ignores exceptions from the onTimeout_.
166 */
167 void
168 callTimeout();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700169
170 private:
Jeff Thompson62992e42013-10-07 18:50:51 -0700171 static uint64_t lastPendingInterestId_; /**< A class variable used to get the next unique ID. */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800172
Jeff Thompson62992e42013-10-07 18:50:51 -0700173 uint64_t pendingInterestId_; /**< A unique identifier for this entry so it can be deleted */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800174 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson7aec0252013-08-22 17:29:57 -0700175 const OnData onData_;
176 const OnTimeout onTimeout_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800177
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700178 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 -0700179 };
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700180
Jeff Thompson11095142013-10-01 16:20:28 -0700181 class RegisteredPrefix {
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700182 public:
183 /**
184 * Create a new PrefixEntry.
Jeff Thompson11095142013-10-01 16:20:28 -0700185 * @param registeredPrefixId A unique ID for this entry, which you should get with getNextRegisteredPrefixId().
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700186 * @param prefix A shared_ptr for the prefix.
187 * @param onInterest A function object to call when a matching data packet is received.
188 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700189 RegisteredPrefix(uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800190 : registeredPrefixId_(registeredPrefixId)
191 , prefix_(prefix)
192 , onInterest_(onInterest)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700193 {
194 }
195
Jeff Thompson11095142013-10-01 16:20:28 -0700196 /**
197 * Return the next unique entry ID.
198 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700199 static uint64_t
Jeff Thompson11095142013-10-01 16:20:28 -0700200 getNextRegisteredPrefixId()
201 {
202 return ++lastRegisteredPrefixId_;
203 }
204
205 /**
206 * Return the registeredPrefixId given to the constructor.
207 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700208 uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800209 getRegisteredPrefixId()
210 {
211 return registeredPrefixId_;
212 }
Jeff Thompson11095142013-10-01 16:20:28 -0700213
Jeff Thompson0050abe2013-09-17 12:50:25 -0700214 const ptr_lib::shared_ptr<const Name>&
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800215 getPrefix()
216 {
217 return prefix_;
218 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700219
Jeff Thompson0050abe2013-09-17 12:50:25 -0700220 const OnInterest&
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800221 getOnInterest()
222 {
223 return onInterest_;
224 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700225
226 private:
Jeff Thompson62992e42013-10-07 18:50:51 -0700227 static uint64_t lastRegisteredPrefixId_; /**< A class variable used to get the next unique ID. */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800228
Jeff Thompson62992e42013-10-07 18:50:51 -0700229 uint64_t registeredPrefixId_; /**< A unique identifier for this entry so it can be deleted */
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700230 ptr_lib::shared_ptr<const Name> prefix_;
231 const OnInterest onInterest_;
232 };
Jeff Thompson557b81e2013-08-21 15:13:51 -0700233
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800234 typedef std::vector<ptr_lib::shared_ptr<PendingInterest> > PendingInterestTable;
235 typedef std::vector<ptr_lib::shared_ptr<RegisteredPrefix> > RegisteredPrefixTable;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700236
237 /**
Jeff Thompson557b81e2013-08-21 15:13:51 -0700238 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
239 * the entry interest name is the longest that matches name.
240 * @param name The name to find the interest for (from the incoming data packet).
241 * @return The index in pit_ of the pit entry, or -1 if not found.
242 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800243 PendingInterestTable::iterator
Jeff Thompson590ec232013-09-18 15:55:56 -0700244 getEntryIndexForExpressedInterest(const Name& name);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700245
Jeff Thompson86507bc2013-08-23 20:51:38 -0700246 /**
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700247 * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name.
248 * @param name The name to find the PrefixEntry for (from the incoming interest packet).
249 * @return A pointer to the entry, or 0 if not found.
250 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800251 RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700252 getEntryForRegisteredPrefix(const Name& name);
Jeff Thompson590ec232013-09-18 15:55:56 -0700253
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700254 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700255 * Do the work of registerPrefix once we know we are connected with an ndndId_.
Jeff Thompson11095142013-10-01 16:20:28 -0700256 * @param registeredPrefixId The PrefixEntry::getNextRegisteredPrefixId() which registerPrefix got so it could return it to the caller.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700257 * @param prefix
258 * @param onInterest
Jeff Thompson590ec232013-09-18 15:55:56 -0700259 * @param onRegisterFailed
Jeff Thompson86507bc2013-08-23 20:51:38 -0700260 * @param flags
Jeff Thompson590ec232013-09-18 15:55:56 -0700261 * @param wireFormat
262 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700263 void
Jeff Thompson590ec232013-09-18 15:55:56 -0700264 registerPrefixHelper
Jeff Thompson62992e42013-10-07 18:50:51 -0700265 (uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest,
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800266 const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags);
267
268 void
269 checkPitExpire();
270
271private:
272 boost::asio::io_service ioService_;
273 boost::asio::deadline_timer timer_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700274
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700275 ptr_lib::shared_ptr<Transport> transport_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800276
277 PendingInterestTable pendingInterestTable_;
278 RegisteredPrefixTable registeredPrefixTable_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700279 Interest ndndIdFetcherInterest_;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800280 Buffer ndndId_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700281};
282
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800283} // namespace ndn
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700284
285#endif