blob: 911298b0d33c5b87a7eff68932f9e829ff2ca02c [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 /**
60 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
Jeff Thompson7aec0252013-08-22 17:29:57 -070061 * Send the interest through the transport, read the entire response and call onData(interest, data).
62 * @param name A reference to a Name for the interest. This does not keep a pointer to the Name object.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070063 * @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 -070064 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
65 * use func_lib::ref() as appropriate.
66 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
67 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070068 */
Jeff Thompson7aec0252013-08-22 17:29:57 -070069 void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData, const OnTimeout &onTimeout);
70
71 /**
72 * Encode name as an Interest, using a default interest lifetime.
73 * Send the interest through the transport, read the entire response and call onData(interest, data).
74 * @param name A reference to a Name for the interest. This does not keep a pointer to the Name object.
75 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
76 * use func_lib::ref() as appropriate.
77 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
78 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
79 */
80 void expressInterest(const Name &name, const OnData &onData, const OnTimeout &onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070081 {
Jeff Thompson7aec0252013-08-22 17:29:57 -070082 expressInterest(name, 0, onData, onTimeout);
83 }
84
85 /**
86 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
87 * Send the interest through the transport, read the entire response and call onData(interest, data).
88 * @param name A reference to a Name for the interest. This does not keep a pointer to the Name object.
89 * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object.
90 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
91 * use func_lib::ref() as appropriate.
92 */
93 void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData)
94 {
95 expressInterest(name, interestTemplate, onData, OnTimeout());
96 }
97
98 /**
99 * Encode name as an Interest, using a default interest lifetime.
100 * Send the interest through the transport, read the entire response and call onData(interest, data).
101 * @param name A reference to a Name for the interest. This does not keep a pointer to the Name object.
102 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
103 * use func_lib::ref() as appropriate.
104 */
105 void expressInterest(const Name &name, const OnData &onData)
106 {
107 expressInterest(name, 0, onData);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700108 }
109
110 /**
111 * Process any data to receive. For each element received, call onReceivedElement.
112 * This is non-blocking and will return immediately if there is no data to receive.
113 * 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.
114 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
115 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
116 */
117 void processEvents();
118
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700119 const ptr_lib::shared_ptr<Transport> &getTransport() { return transport_; }
120
Jeff Thompson10e34382013-08-22 13:34:46 -0700121 const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &getConnectionInfo() { return connectionInfo_; }
122
Jeff Thompson8b173cc2013-08-21 17:54:12 -0700123 void onReceivedElement(const unsigned char *element, unsigned int elementLength);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700124
125 void shutdown();
126
127private:
Jeff Thompson557b81e2013-08-21 15:13:51 -0700128 class PitEntry {
129 public:
130 /**
131 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson48917f02013-08-21 17:12:45 -0700132 * @param interest A shared_ptr for the interest.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700133 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
134 * use func_lib::ref() as appropriate.
135 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
136 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700137 */
Jeff Thompson7aec0252013-08-22 17:29:57 -0700138 PitEntry(const ptr_lib::shared_ptr<const Interest> &interest, const OnData &onData, const OnTimeout &onTimeout);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700139
Jeff Thompson48917f02013-08-21 17:12:45 -0700140 const ptr_lib::shared_ptr<const Interest> &getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700141
Jeff Thompson7aec0252013-08-22 17:29:57 -0700142 const OnData &getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700143
144 /**
Jeff Thompson48917f02013-08-21 17:12:45 -0700145 * Get the struct ndn_Interest for the interest_.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700146 * WARNING: Assume that this PitEntry was created with new, so that no copy constructor is invoked between calls.
147 * This class is private to Node and only used by its methods, so this should be OK.
148 * TODO: Doesn't this functionality belong in the Interest class?
149 * @return A reference to the ndn_Interest struct.
150 * WARNING: The resulting pointers in are invalid uses getInterest() to manipulate the object which could reallocate memory.
151 */
Jeff Thompson48917f02013-08-21 17:12:45 -0700152 const struct ndn_Interest &getInterestStruct()
153 {
154 return interestStruct_;
155 }
156
157 /**
Jeff Thompson7aec0252013-08-22 17:29:57 -0700158 * If this interest is timed out, call onTimeout_ (if defined) and return true.
Jeff Thompson48917f02013-08-21 17:12:45 -0700159 * @param parent The parent Node for the UpcallInfo.
160 * @param nowMilliseconds The current time in milliseconds from gettimeofday.
161 * @return true if this interest timed out and the timeout callback was called, otherwise false.
162 */
163 bool checkTimeout(Node *parent, double nowMilliseconds);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700164
165 private:
Jeff Thompson48917f02013-08-21 17:12:45 -0700166 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700167 std::vector<struct ndn_NameComponent> nameComponents_;
168 std::vector<struct ndn_ExcludeEntry> excludeEntries_;
169 struct ndn_Interest interestStruct_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700170
Jeff Thompson7aec0252013-08-22 17:29:57 -0700171 const OnData onData_;
172 const OnTimeout onTimeout_;
Jeff Thompson48917f02013-08-21 17:12:45 -0700173 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 -0700174 };
175
176 /**
177 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
178 * the entry interest name is the longest that matches name.
179 * @param name The name to find the interest for (from the incoming data packet).
180 * @return The index in pit_ of the pit entry, or -1 if not found.
181 */
182 int getEntryIndexForExpressedInterest(const Name &name);
183
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700184 ptr_lib::shared_ptr<Transport> transport_;
Jeff Thompson10e34382013-08-22 13:34:46 -0700185 ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700186 std::vector<ptr_lib::shared_ptr<PitEntry> > pit_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700187};
188
189}
190
191#endif