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 | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 9 | #include <time.h> |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 10 | #include "interest.hpp" |
| 11 | #include "closure.hpp" |
| 12 | #include "transport/udp-transport.hpp" |
| 13 | #include "encoding/binary-xml-element-reader.hpp" |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | class Face; |
| 18 | |
| 19 | class Node : public ElementListener { |
| 20 | public: |
| 21 | /** |
| 22 | * Create a new Node for communication with an NDN hub at host:port with the given Transport object. |
| 23 | * @param host The host of the NDN hub. |
| 24 | * @param port The port of the NDN hub. |
| 25 | * @param transport A pointer to a Transport object used for communication. |
| 26 | */ |
| 27 | Node(const char *host, unsigned short port, const ptr_lib::shared_ptr<Transport> &transport) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 28 | : host_(host), port_(port), transport_(transport) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 29 | { |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Create a new Node for communication with an NDN hub at host:port using the default UdpTransport. |
| 34 | * @param host The host of the NDN hub. |
| 35 | * @param port The port of the NDN hub. |
| 36 | */ |
| 37 | Node(const char *host, unsigned short port) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 38 | : host_(host), port_(port), transport_(new UdpTransport()) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Create a new Node for communication with an NDN hub at host with the default port 9695 and using the default UdpTransport. |
| 44 | * @param host The host of the NDN hub. |
| 45 | */ |
| 46 | Node(const char *host) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 47 | : host_(host), port_(9695), transport_(new UdpTransport()) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors. |
| 53 | * Send the interest through the transport, read the entire response and call |
| 54 | * closure->upcall(UPCALL_DATA (or UPCALL_DATA_UNVERIFIED), |
| 55 | * UpcallInfo(this, interest, 0, data)). |
| 56 | * @param name reference to a Name for the interest. This does not keep a pointer to the Name object. |
| 57 | * @param closure a pointer for the Closure. The caller must manage the memory for the Closure. This will not try to delete it. |
| 58 | * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object. |
| 59 | */ |
| 60 | void expressInterest(const Name &name, Closure *closure, const Interest *interestTemplate); |
| 61 | |
| 62 | void expressInterest(const Name &name, Closure *closure) |
| 63 | { |
| 64 | expressInterest(name, closure, 0); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Process any data to receive. For each element received, call onReceivedElement. |
| 69 | * This is non-blocking and will return immediately if there is no data to receive. |
| 70 | * 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. |
| 71 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 72 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 73 | */ |
| 74 | void processEvents(); |
| 75 | |
| 76 | const char *getHost() const { return host_.c_str(); } |
| 77 | |
| 78 | unsigned short getPort() const { return port_; } |
| 79 | |
| 80 | const ptr_lib::shared_ptr<Transport> &getTransport() { return transport_; } |
| 81 | |
| 82 | virtual void onReceivedElement(unsigned char *element, unsigned int elementLength); |
| 83 | |
| 84 | void shutdown(); |
| 85 | |
| 86 | private: |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 87 | class PitEntry { |
| 88 | public: |
| 89 | /** |
| 90 | * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime. |
| 91 | * @param name The name for the interest. You can use the non-const getInterest() to set other fields. |
| 92 | * (We do it like this to avoid invoking the Interest copy constructor.) |
| 93 | * @param closure A pointer to the closure with the callbacks to call on match. |
| 94 | * The caller must manage the memory for the Closure. This will not try to delete it. |
| 95 | */ |
| 96 | PitEntry(const Name &name, Closure *closure); |
| 97 | |
| 98 | Interest &getInterest() |
| 99 | { |
| 100 | // Assume that the caller will modify interest_, so mark it stale. |
| 101 | interestStructIsStale_ = true; |
| 102 | return interest_; |
| 103 | } |
| 104 | |
| 105 | Closure *getClosure() { return closure_; } |
| 106 | |
| 107 | /** |
| 108 | * Get the struct ndn_Interest for the interest_. If interestStructIsStale_, then this will |
| 109 | * re-build from interest_. |
| 110 | * WARNING: Assume that this PitEntry was created with new, so that no copy constructor is invoked between calls. |
| 111 | * This class is private to Node and only used by its methods, so this should be OK. |
| 112 | * TODO: Doesn't this functionality belong in the Interest class? |
| 113 | * @return A reference to the ndn_Interest struct. |
| 114 | * WARNING: The resulting pointers in are invalid uses getInterest() to manipulate the object which could reallocate memory. |
| 115 | */ |
| 116 | const struct ndn_Interest &getInterestStruct(); |
| 117 | |
| 118 | private: |
| 119 | Interest interest_; |
| 120 | std::vector<struct ndn_NameComponent> nameComponents_; |
| 121 | std::vector<struct ndn_ExcludeEntry> excludeEntries_; |
| 122 | struct ndn_Interest interestStruct_; |
| 123 | bool interestStructIsStale_; |
| 124 | |
| 125 | Closure *closure_; |
| 126 | clock_t timeoutTime_; /**< The clock time when the interest times out, of 0 for none. */ |
| 127 | }; |
| 128 | |
| 129 | /** |
| 130 | * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and |
| 131 | * the entry interest name is the longest that matches name. |
| 132 | * @param name The name to find the interest for (from the incoming data packet). |
| 133 | * @return The index in pit_ of the pit entry, or -1 if not found. |
| 134 | */ |
| 135 | int getEntryIndexForExpressedInterest(const Name &name); |
| 136 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 137 | ptr_lib::shared_ptr<Transport> transport_; |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 138 | std::vector<ptr_lib::shared_ptr<PitEntry> > pit_; |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 139 | std::string host_; |
| 140 | unsigned short port_; |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } |
| 144 | |
| 145 | #endif |