blob: b72ba9c7c307b934f97322a5e56956240daaf8ec [file] [log] [blame]
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07001/**
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
14namespace ndn {
15
16class Face;
17
18class Node : public ElementListener {
19public:
20 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070021 * 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 Thompsonbf50a1a2013-08-20 18:01:01 -070024 */
Jeff Thompson10e34382013-08-22 13:34:46 -070025 Node(const ptr_lib::shared_ptr<Transport> &transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &connectionInfo)
26 : transport_(transport), connectionInfo_(connectionInfo)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070027 {
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 Thompson10e34382013-08-22 13:34:46 -070036 : transport_(new UdpTransport()), connectionInfo_(new UdpTransport::ConnectionInfo(host, port))
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070037 {
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 Thompson10e34382013-08-22 13:34:46 -070045 : transport_(new UdpTransport()), connectionInfo_(new UdpTransport::ConnectionInfo(host, 9695))
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070046 {
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 Thompsonbf50a1a2013-08-20 18:01:01 -070074 const ptr_lib::shared_ptr<Transport> &getTransport() { return transport_; }
75
Jeff Thompson10e34382013-08-22 13:34:46 -070076 const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &getConnectionInfo() { return connectionInfo_; }
77
Jeff Thompson8b173cc2013-08-21 17:54:12 -070078 void onReceivedElement(const unsigned char *element, unsigned int elementLength);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070079
80 void shutdown();
81
82private:
Jeff Thompson557b81e2013-08-21 15:13:51 -070083 class PitEntry {
84 public:
85 /**
86 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson48917f02013-08-21 17:12:45 -070087 * @param interest A shared_ptr for the interest.
Jeff Thompson557b81e2013-08-21 15:13:51 -070088 * @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 Thompson48917f02013-08-21 17:12:45 -070091 PitEntry(const ptr_lib::shared_ptr<const Interest> &interest, Closure *closure);
Jeff Thompson557b81e2013-08-21 15:13:51 -070092
Jeff Thompson48917f02013-08-21 17:12:45 -070093 const ptr_lib::shared_ptr<const Interest> &getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -070094
95 Closure *getClosure() { return closure_; }
96
97 /**
Jeff Thompson48917f02013-08-21 17:12:45 -070098 * Get the struct ndn_Interest for the interest_.
Jeff Thompson557b81e2013-08-21 15:13:51 -070099 * 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 Thompson48917f02013-08-21 17:12:45 -0700105 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 Thompson557b81e2013-08-21 15:13:51 -0700117
118 private:
Jeff Thompson48917f02013-08-21 17:12:45 -0700119 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700120 std::vector<struct ndn_NameComponent> nameComponents_;
121 std::vector<struct ndn_ExcludeEntry> excludeEntries_;
122 struct ndn_Interest interestStruct_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700123
124 Closure *closure_;
Jeff Thompson48917f02013-08-21 17:12:45 -0700125 double timeoutTimeMilliseconds_; /**< The time when the interest times out in milliseconds according to gettimeofday, or -1 for no timeout. */
Jeff Thompson557b81e2013-08-21 15:13:51 -0700126 };
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 Thompsonbf50a1a2013-08-20 18:01:01 -0700136 ptr_lib::shared_ptr<Transport> transport_;
Jeff Thompson10e34382013-08-22 13:34:46 -0700137 ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700138 std::vector<ptr_lib::shared_ptr<PitEntry> > pit_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700139};
140
141}
142
143#endif