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