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