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