blob: 277628f9c55e5c2c216d34dfdfd330c3beaa7902 [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
Jeff Thompson7aec0252013-08-22 17:29:57 -07009#include "common.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070010#include "interest.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070011#include "transport/udp-transport.hpp"
12#include "encoding/binary-xml-element-reader.hpp"
13
14namespace ndn {
15
Jeff Thompson7aec0252013-08-22 17:29:57 -070016/**
17 * An OnData function object is used to pass a callback to expressInterest.
18 */
19typedef 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 */
24typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest> &)> OnTimeout;
25
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070026class Face;
27
28class Node : public ElementListener {
29public:
30 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070031 * 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 Thompsonbf50a1a2013-08-20 18:01:01 -070034 */
Jeff Thompson10e34382013-08-22 13:34:46 -070035 Node(const ptr_lib::shared_ptr<Transport> &transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &connectionInfo)
36 : transport_(transport), connectionInfo_(connectionInfo)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070037 {
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 Thompson10e34382013-08-22 13:34:46 -070046 : transport_(new UdpTransport()), connectionInfo_(new UdpTransport::ConnectionInfo(host, port))
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070047 {
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 Thompson10e34382013-08-22 13:34:46 -070055 : transport_(new UdpTransport()), connectionInfo_(new UdpTransport::ConnectionInfo(host, 9695))
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070056 {
57 }
58
59 /**
Jeff Thompson4fe45512013-08-23 14:06:38 -070060 * Send the Interest through the transport, read the entire response and call onData(interest, data).
61 * @param interest A reference to the Interest. This copies the Interest.
62 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
63 * use func_lib::ref() as appropriate.
64 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
65 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
66 */
67 void expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout);
68
69 /**
70 * Send the Interest through the transport, read the entire response and call onData(interest, data).
71 * @param interest A reference to the Interest. This copies the Interest.
72 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
73 * use func_lib::ref() as appropriate.
74 */
75 void expressInterest(const Interest &interest, const OnData &onData) {
76 expressInterest(interest, onData, OnTimeout());
77 }
78
79 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070080 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
Jeff Thompson7aec0252013-08-22 17:29:57 -070081 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070082 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070083 * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object.
Jeff Thompson7aec0252013-08-22 17:29:57 -070084 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
85 * use func_lib::ref() as appropriate.
86 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
87 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070088 */
Jeff Thompson7aec0252013-08-22 17:29:57 -070089 void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData, const OnTimeout &onTimeout);
90
91 /**
92 * Encode name as an Interest, using a default interest lifetime.
93 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070094 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -070095 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
96 * use func_lib::ref() as appropriate.
97 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
98 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
99 */
100 void expressInterest(const Name &name, const OnData &onData, const OnTimeout &onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700101 {
Jeff Thompson7aec0252013-08-22 17:29:57 -0700102 expressInterest(name, 0, onData, onTimeout);
103 }
104
105 /**
106 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
107 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -0700108 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700109 * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object.
110 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
111 * use func_lib::ref() as appropriate.
112 */
113 void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData)
114 {
115 expressInterest(name, interestTemplate, onData, OnTimeout());
116 }
117
118 /**
119 * Encode name as an Interest, using a default interest lifetime.
120 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -0700121 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700122 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
123 * use func_lib::ref() as appropriate.
124 */
125 void expressInterest(const Name &name, const OnData &onData)
126 {
127 expressInterest(name, 0, onData);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700128 }
129
130 /**
131 * Process any data to receive. For each element received, call onReceivedElement.
132 * This is non-blocking and will return immediately if there is no data to receive.
133 * 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.
134 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
135 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
136 */
137 void processEvents();
138
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700139 const ptr_lib::shared_ptr<Transport> &getTransport() { return transport_; }
140
Jeff Thompson10e34382013-08-22 13:34:46 -0700141 const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &getConnectionInfo() { return connectionInfo_; }
142
Jeff Thompson8b173cc2013-08-21 17:54:12 -0700143 void onReceivedElement(const unsigned char *element, unsigned int elementLength);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700144
145 void shutdown();
146
147private:
Jeff Thompson557b81e2013-08-21 15:13:51 -0700148 class PitEntry {
149 public:
150 /**
151 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson48917f02013-08-21 17:12:45 -0700152 * @param interest A shared_ptr for the interest.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700153 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
154 * use func_lib::ref() as appropriate.
155 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
156 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700157 */
Jeff Thompson7aec0252013-08-22 17:29:57 -0700158 PitEntry(const ptr_lib::shared_ptr<const Interest> &interest, const OnData &onData, const OnTimeout &onTimeout);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700159
Jeff Thompson48917f02013-08-21 17:12:45 -0700160 const ptr_lib::shared_ptr<const Interest> &getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700161
Jeff Thompson7aec0252013-08-22 17:29:57 -0700162 const OnData &getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700163
164 /**
Jeff Thompson48917f02013-08-21 17:12:45 -0700165 * Get the struct ndn_Interest for the interest_.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700166 * WARNING: Assume that this PitEntry was created with new, so that no copy constructor is invoked between calls.
167 * This class is private to Node and only used by its methods, so this should be OK.
168 * TODO: Doesn't this functionality belong in the Interest class?
169 * @return A reference to the ndn_Interest struct.
170 * WARNING: The resulting pointers in are invalid uses getInterest() to manipulate the object which could reallocate memory.
171 */
Jeff Thompson48917f02013-08-21 17:12:45 -0700172 const struct ndn_Interest &getInterestStruct()
173 {
174 return interestStruct_;
175 }
176
177 /**
Jeff Thompson7aec0252013-08-22 17:29:57 -0700178 * If this interest is timed out, call onTimeout_ (if defined) and return true.
Jeff Thompson48917f02013-08-21 17:12:45 -0700179 * @param parent The parent Node for the UpcallInfo.
180 * @param nowMilliseconds The current time in milliseconds from gettimeofday.
181 * @return true if this interest timed out and the timeout callback was called, otherwise false.
182 */
183 bool checkTimeout(Node *parent, double nowMilliseconds);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700184
185 private:
Jeff Thompson48917f02013-08-21 17:12:45 -0700186 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700187 std::vector<struct ndn_NameComponent> nameComponents_;
188 std::vector<struct ndn_ExcludeEntry> excludeEntries_;
189 struct ndn_Interest interestStruct_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700190
Jeff Thompson7aec0252013-08-22 17:29:57 -0700191 const OnData onData_;
192 const OnTimeout onTimeout_;
Jeff Thompson48917f02013-08-21 17:12:45 -0700193 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 -0700194 };
195
196 /**
197 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
198 * the entry interest name is the longest that matches name.
199 * @param name The name to find the interest for (from the incoming data packet).
200 * @return The index in pit_ of the pit entry, or -1 if not found.
201 */
202 int getEntryIndexForExpressedInterest(const Name &name);
203
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700204 ptr_lib::shared_ptr<Transport> transport_;
Jeff Thompson10e34382013-08-22 13:34:46 -0700205 ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700206 std::vector<ptr_lib::shared_ptr<PitEntry> > pit_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700207};
208
209}
210
211#endif