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