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