blob: a7e5548144c8fcb5713f27714b04ddd612d5e63d [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 /**
21 * Create a new Node for communication with an NDN hub at host:port with the given Transport object.
22 * @param host The host of the NDN hub.
23 * @param port The port of the NDN hub.
24 * @param transport A pointer to a Transport object used for communication.
25 */
26 Node(const char *host, unsigned short port, const ptr_lib::shared_ptr<Transport> &transport)
Jeff Thompson557b81e2013-08-21 15:13:51 -070027 : host_(host), port_(port), transport_(transport)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070028 {
29 }
30
31 /**
32 * Create a new Node for communication with an NDN hub at host:port using the default UdpTransport.
33 * @param host The host of the NDN hub.
34 * @param port The port of the NDN hub.
35 */
36 Node(const char *host, unsigned short port)
Jeff Thompson557b81e2013-08-21 15:13:51 -070037 : host_(host), port_(port), transport_(new UdpTransport())
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070038 {
39 }
40
41 /**
42 * Create a new Node for communication with an NDN hub at host with the default port 9695 and using the default UdpTransport.
43 * @param host The host of the NDN hub.
44 */
45 Node(const char *host)
Jeff Thompson557b81e2013-08-21 15:13:51 -070046 : host_(host), port_(9695), transport_(new UdpTransport())
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070047 {
48 }
49
50 /**
51 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
52 * Send the interest through the transport, read the entire response and call
53 * closure->upcall(UPCALL_DATA (or UPCALL_DATA_UNVERIFIED),
54 * UpcallInfo(this, interest, 0, data)).
55 * @param name reference to a Name for the interest. This does not keep a pointer to the Name object.
56 * @param closure a pointer for the Closure. The caller must manage the memory for the Closure. This will not try to delete it.
57 * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object.
58 */
59 void expressInterest(const Name &name, Closure *closure, const Interest *interestTemplate);
60
61 void expressInterest(const Name &name, Closure *closure)
62 {
63 expressInterest(name, closure, 0);
64 }
65
66 /**
67 * Process any data to receive. For each element received, call onReceivedElement.
68 * This is non-blocking and will return immediately if there is no data to receive.
69 * 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.
70 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
71 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
72 */
73 void processEvents();
74
75 const char *getHost() const { return host_.c_str(); }
76
77 unsigned short getPort() const { return port_; }
78
79 const ptr_lib::shared_ptr<Transport> &getTransport() { return transport_; }
80
81 virtual void onReceivedElement(unsigned char *element, unsigned int elementLength);
82
83 void shutdown();
84
85private:
Jeff Thompson557b81e2013-08-21 15:13:51 -070086 class PitEntry {
87 public:
88 /**
89 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson48917f02013-08-21 17:12:45 -070090 * @param interest A shared_ptr for the interest.
Jeff Thompson557b81e2013-08-21 15:13:51 -070091 * @param closure A pointer to the closure with the callbacks to call on match.
92 * The caller must manage the memory for the Closure. This will not try to delete it.
93 */
Jeff Thompson48917f02013-08-21 17:12:45 -070094 PitEntry(const ptr_lib::shared_ptr<const Interest> &interest, Closure *closure);
Jeff Thompson557b81e2013-08-21 15:13:51 -070095
Jeff Thompson48917f02013-08-21 17:12:45 -070096 const ptr_lib::shared_ptr<const Interest> &getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -070097
98 Closure *getClosure() { return closure_; }
99
100 /**
Jeff Thompson48917f02013-08-21 17:12:45 -0700101 * Get the struct ndn_Interest for the interest_.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700102 * WARNING: Assume that this PitEntry was created with new, so that no copy constructor is invoked between calls.
103 * This class is private to Node and only used by its methods, so this should be OK.
104 * TODO: Doesn't this functionality belong in the Interest class?
105 * @return A reference to the ndn_Interest struct.
106 * WARNING: The resulting pointers in are invalid uses getInterest() to manipulate the object which could reallocate memory.
107 */
Jeff Thompson48917f02013-08-21 17:12:45 -0700108 const struct ndn_Interest &getInterestStruct()
109 {
110 return interestStruct_;
111 }
112
113 /**
114 * If this interest is timed out, call the timeout callback and return true.
115 * @param parent The parent Node for the UpcallInfo.
116 * @param nowMilliseconds The current time in milliseconds from gettimeofday.
117 * @return true if this interest timed out and the timeout callback was called, otherwise false.
118 */
119 bool checkTimeout(Node *parent, double nowMilliseconds);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700120
121 private:
Jeff Thompson48917f02013-08-21 17:12:45 -0700122 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700123 std::vector<struct ndn_NameComponent> nameComponents_;
124 std::vector<struct ndn_ExcludeEntry> excludeEntries_;
125 struct ndn_Interest interestStruct_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700126
127 Closure *closure_;
Jeff Thompson48917f02013-08-21 17:12:45 -0700128 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 -0700129 };
130
131 /**
132 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
133 * the entry interest name is the longest that matches name.
134 * @param name The name to find the interest for (from the incoming data packet).
135 * @return The index in pit_ of the pit entry, or -1 if not found.
136 */
137 int getEntryIndexForExpressedInterest(const Name &name);
138
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700139 ptr_lib::shared_ptr<Transport> transport_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700140 std::vector<ptr_lib::shared_ptr<PitEntry> > pit_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700141 std::string host_;
142 unsigned short port_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700143};
144
145}
146
147#endif