Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #ifndef NDN_NODE_HPP |
| 7 | #define NDN_NODE_HPP |
| 8 | |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 9 | #include "common.hpp" |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 10 | #include "interest.hpp" |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 11 | #include "data.hpp" |
| 12 | #include "transport/tcp-transport.hpp" |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 13 | #include "encoding/binary-xml-element-reader.hpp" |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 17 | /** |
| 18 | * An OnData function object is used to pass a callback to expressInterest. |
| 19 | */ |
| 20 | typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest> &, const ptr_lib::shared_ptr<Data> &)> OnData; |
| 21 | |
| 22 | /** |
| 23 | * An OnTimeout function object is used to pass a callback to expressInterest. |
| 24 | */ |
| 25 | typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest> &)> OnTimeout; |
| 26 | |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 27 | /** |
| 28 | * An OnInterest function object is used to pass a callback to registerPrefix. |
| 29 | */ |
| 30 | typedef func_lib::function<void(const ptr_lib::shared_ptr<const Name> &, const ptr_lib::shared_ptr<const Interest> &)> OnInterest; |
| 31 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 32 | class Face; |
| 33 | |
| 34 | class Node : public ElementListener { |
| 35 | public: |
| 36 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 37 | * Create a new Node for communication with an NDN hub with the given Transport object and connectionInfo. |
| 38 | * @param transport A shared_ptr to a Transport object used for communication. |
| 39 | * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport. |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 40 | */ |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 41 | Node(const ptr_lib::shared_ptr<Transport> &transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &connectionInfo) |
| 42 | : transport_(transport), connectionInfo_(connectionInfo) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 43 | { |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 44 | construct(); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 48 | * Create a new Node for communication with an NDN hub at host:port using the default TcpTransport. |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 49 | * @param host The host of the NDN hub. |
| 50 | * @param port The port of the NDN hub. |
| 51 | */ |
| 52 | Node(const char *host, unsigned short port) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 53 | : transport_(new TcpTransport()), connectionInfo_(new TcpTransport::ConnectionInfo(host, port)) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 54 | { |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 55 | construct(); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 59 | * Create a new Node for communication with an NDN hub at host with the default port 9695 and using the default TcpTransport. |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 60 | * @param host The host of the NDN hub. |
| 61 | */ |
| 62 | Node(const char *host) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 63 | : transport_(new TcpTransport()), connectionInfo_(new TcpTransport::ConnectionInfo(host, 9695)) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 64 | { |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 65 | construct(); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 69 | * Send the Interest through the transport, read the entire response and call onData(interest, data). |
| 70 | * @param interest A reference to the Interest. This copies the Interest. |
| 71 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 72 | * use func_lib::ref() as appropriate. |
| 73 | * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it. |
| 74 | * This copies the function object, so you may need to use func_lib::ref() as appropriate. |
| 75 | */ |
| 76 | void expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout); |
| 77 | |
| 78 | /** |
| 79 | * Send the Interest through the transport, read the entire response and call onData(interest, data). |
| 80 | * @param interest A reference to the Interest. This copies the Interest. |
| 81 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 82 | * use func_lib::ref() as appropriate. |
| 83 | */ |
| 84 | void expressInterest(const Interest &interest, const OnData &onData) { |
| 85 | expressInterest(interest, onData, OnTimeout()); |
| 86 | } |
| 87 | |
| 88 | /** |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 89 | * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 90 | * Send the interest through the transport, read the entire response and call onData(interest, data). |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 91 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 92 | * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 93 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 94 | * use func_lib::ref() as appropriate. |
| 95 | * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it. |
| 96 | * This copies the function object, so you may need to use func_lib::ref() as appropriate. |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 97 | */ |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 98 | void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData, const OnTimeout &onTimeout); |
| 99 | |
| 100 | /** |
| 101 | * Encode name as an Interest, using a default interest lifetime. |
| 102 | * Send the interest through the transport, read the entire response and call onData(interest, data). |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 103 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 104 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 105 | * use func_lib::ref() as appropriate. |
| 106 | * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it. |
| 107 | * This copies the function object, so you may need to use func_lib::ref() as appropriate. |
| 108 | */ |
| 109 | void expressInterest(const Name &name, const OnData &onData, const OnTimeout &onTimeout) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 110 | { |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 111 | expressInterest(name, 0, onData, onTimeout); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors. |
| 116 | * Send the interest through the transport, read the entire response and call onData(interest, data). |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 117 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 118 | * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object. |
| 119 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 120 | * use func_lib::ref() as appropriate. |
| 121 | */ |
| 122 | void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData) |
| 123 | { |
| 124 | expressInterest(name, interestTemplate, onData, OnTimeout()); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Encode name as an Interest, using a default interest lifetime. |
| 129 | * Send the interest through the transport, read the entire response and call onData(interest, data). |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 130 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 131 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 132 | * use func_lib::ref() as appropriate. |
| 133 | */ |
| 134 | void expressInterest(const Name &name, const OnData &onData) |
| 135 | { |
| 136 | expressInterest(name, 0, onData); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 140 | * Register prefix with the connected NDN hub and call onInterest when a matching interest is received. |
| 141 | * @param prefix A reference to a Name for the prefix to register. This copies the Name. |
| 142 | * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to |
| 143 | * use func_lib::ref() as appropriate. |
| 144 | * @param flags The flags for finer control of which interests are forward to the application. |
| 145 | */ |
| 146 | void registerPrefix(const Name &prefix, const OnInterest &onInterest, int flags); |
| 147 | |
| 148 | /** |
| 149 | * Register prefix with the connected NDN hub and call onInterest when a matching interest is received. |
| 150 | * @param prefix A reference to a Name for the prefix to register. This copies the Name. |
| 151 | * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to |
| 152 | * use func_lib::ref() as appropriate. |
| 153 | */ |
| 154 | void registerPrefix(const Name &prefix, const OnInterest &onInterest) |
| 155 | { |
| 156 | registerPrefix(prefix, onInterest, 0); |
| 157 | } |
| 158 | |
| 159 | /** |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 160 | * Process any data to receive. For each element received, call onReceivedElement. |
| 161 | * This is non-blocking and will return immediately if there is no data to receive. |
| 162 | * 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. |
| 163 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 164 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 165 | */ |
| 166 | void processEvents(); |
| 167 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 168 | const ptr_lib::shared_ptr<Transport> &getTransport() { return transport_; } |
| 169 | |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 170 | const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &getConnectionInfo() { return connectionInfo_; } |
| 171 | |
Jeff Thompson | 8b173cc | 2013-08-21 17:54:12 -0700 | [diff] [blame] | 172 | void onReceivedElement(const unsigned char *element, unsigned int elementLength); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 173 | |
| 174 | void shutdown(); |
| 175 | |
| 176 | private: |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 177 | class PitEntry { |
| 178 | public: |
| 179 | /** |
| 180 | * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime. |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 181 | * @param interest A shared_ptr for the interest. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 182 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 183 | * use func_lib::ref() as appropriate. |
| 184 | * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it. |
| 185 | * This copies the function object, so you may need to use func_lib::ref() as appropriate. |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 186 | */ |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 187 | PitEntry(const ptr_lib::shared_ptr<const Interest> &interest, const OnData &onData, const OnTimeout &onTimeout); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 188 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 189 | const ptr_lib::shared_ptr<const Interest> &getInterest() { return interest_; } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 190 | |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 191 | const OnData &getOnData() { return onData_; } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 192 | |
| 193 | /** |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 194 | * Get the struct ndn_Interest for the interest_. |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 195 | * WARNING: Assume that this PitEntry was created with new, so that no copy constructor is invoked between calls. |
| 196 | * This class is private to Node and only used by its methods, so this should be OK. |
| 197 | * TODO: Doesn't this functionality belong in the Interest class? |
| 198 | * @return A reference to the ndn_Interest struct. |
| 199 | * WARNING: The resulting pointers in are invalid uses getInterest() to manipulate the object which could reallocate memory. |
| 200 | */ |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 201 | const struct ndn_Interest &getInterestStruct() |
| 202 | { |
| 203 | return interestStruct_; |
| 204 | } |
| 205 | |
| 206 | /** |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 207 | * If this interest is timed out, call onTimeout_ (if defined) and return true. |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 208 | * @param parent The parent Node for the UpcallInfo. |
| 209 | * @param nowMilliseconds The current time in milliseconds from gettimeofday. |
| 210 | * @return true if this interest timed out and the timeout callback was called, otherwise false. |
| 211 | */ |
| 212 | bool checkTimeout(Node *parent, double nowMilliseconds); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 213 | |
| 214 | private: |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 215 | ptr_lib::shared_ptr<const Interest> interest_; |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 216 | std::vector<struct ndn_NameComponent> nameComponents_; |
| 217 | std::vector<struct ndn_ExcludeEntry> excludeEntries_; |
| 218 | struct ndn_Interest interestStruct_; |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 219 | |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 220 | const OnData onData_; |
| 221 | const OnTimeout onTimeout_; |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 222 | double timeoutTimeMilliseconds_; /**< The time when the interest times out in milliseconds according to gettimeofday, or -1 for no timeout. */ |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 226 | * An NdndIdFetcher receives the Data packet with the publisher public key digest for the connected NDN hub. |
| 227 | * This class is a function object for the callbacks. It only holds a pointer to an Info object, so it is OK to copy the pointer. |
| 228 | */ |
| 229 | class NdndIdFetcher { |
| 230 | public: |
| 231 | class Info; |
| 232 | NdndIdFetcher(ptr_lib::shared_ptr<NdndIdFetcher::Info> info) |
| 233 | : info_(info) |
| 234 | { |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * We received the ndnd ID. |
| 239 | * @param interest |
| 240 | * @param data |
| 241 | */ |
| 242 | void operator()(const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &ndndIdData); |
| 243 | |
| 244 | /** |
| 245 | * We timed out fetching the ndnd ID. |
| 246 | * @param interest |
| 247 | */ |
| 248 | void operator()(const ptr_lib::shared_ptr<const Interest> &timedOutInterest); |
| 249 | |
| 250 | class Info { |
| 251 | public: |
| 252 | Info(Node *node, const Name &prefix, const OnInterest &onInterest, int flags) |
| 253 | : node_(*node), prefix_(prefix), onInterest_(onInterest), flags_(flags) |
| 254 | { |
| 255 | } |
| 256 | |
| 257 | Node &node_; |
| 258 | Name prefix_; |
| 259 | const OnInterest onInterest_; |
| 260 | int flags_; |
| 261 | }; |
| 262 | |
| 263 | private: |
| 264 | ptr_lib::shared_ptr<Info> info_; |
| 265 | }; |
| 266 | |
| 267 | /** |
| 268 | * Finish constructing this object. |
| 269 | */ |
| 270 | void construct(); |
| 271 | |
| 272 | /** |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 273 | * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and |
| 274 | * the entry interest name is the longest that matches name. |
| 275 | * @param name The name to find the interest for (from the incoming data packet). |
| 276 | * @return The index in pit_ of the pit entry, or -1 if not found. |
| 277 | */ |
| 278 | int getEntryIndexForExpressedInterest(const Name &name); |
| 279 | |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 280 | /** |
| 281 | * Do the work of registerPrefix once we know we are connected with an ndndId_. |
| 282 | * @param prefix |
| 283 | * @param onInterest |
| 284 | * @param flags |
| 285 | */ |
| 286 | void registerPrefixHelper(const Name &prefix, const OnInterest &onInterest, int flags); |
| 287 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 288 | ptr_lib::shared_ptr<Transport> transport_; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 289 | ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_; |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 290 | std::vector<ptr_lib::shared_ptr<PitEntry> > pit_; |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 291 | Interest ndndIdFetcherInterest_; |
| 292 | std::vector<unsigned char> ndndId_; |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | } |
| 296 | |
| 297 | #endif |