blob: 8bd59c26b549d55f26947d8822f47277260fa955 [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 Thompson86507bc2013-08-23 20:51:38 -070011#include "data.hpp"
12#include "transport/tcp-transport.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070013#include "encoding/binary-xml-element-reader.hpp"
14
15namespace ndn {
16
Jeff Thompson7aec0252013-08-22 17:29:57 -070017/**
18 * An OnData function object is used to pass a callback to expressInterest.
19 */
20typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest> &, const ptr_lib::shared_ptr<Data> &)> OnData;
21
22/**
23 * An OnTimeout function object is used to pass a callback to expressInterest.
24 */
25typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest> &)> OnTimeout;
26
Jeff Thompson86507bc2013-08-23 20:51:38 -070027/**
28 * An OnInterest function object is used to pass a callback to registerPrefix.
29 */
30typedef func_lib::function<void(const ptr_lib::shared_ptr<const Name> &, const ptr_lib::shared_ptr<const Interest> &)> OnInterest;
31
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070032class Face;
33
34class Node : public ElementListener {
35public:
36 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070037 * Create a new Node for communication with an NDN hub with the given Transport object and connectionInfo.
38 * @param transport A shared_ptr to a Transport object used for communication.
39 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070040 */
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070041 Node(const ptr_lib::shared_ptr<Transport> &transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &connectionInfo);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070042
43 /**
Jeff Thompson4fe45512013-08-23 14:06:38 -070044 * Send the Interest through the transport, read the entire response and call onData(interest, data).
45 * @param interest A reference to the Interest. This copies the Interest.
46 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
47 * use func_lib::ref() as appropriate.
48 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
49 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
50 */
51 void expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout);
Jeff Thompson7aec0252013-08-22 17:29:57 -070052
53 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070054 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
55 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
56 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
57 * use func_lib::ref() as appropriate.
58 * @param flags The flags for finer control of which interests are forward to the application.
59 */
60 void registerPrefix(const Name &prefix, const OnInterest &onInterest, int flags);
61
62 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070063 * Process any data to receive. For each element received, call onReceivedElement.
64 * This is non-blocking and will return immediately if there is no data to receive.
65 * 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.
66 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
67 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
68 */
69 void processEvents();
70
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070071 const ptr_lib::shared_ptr<Transport> &getTransport() { return transport_; }
72
Jeff Thompson10e34382013-08-22 13:34:46 -070073 const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &getConnectionInfo() { return connectionInfo_; }
74
Jeff Thompson8b173cc2013-08-21 17:54:12 -070075 void onReceivedElement(const unsigned char *element, unsigned int elementLength);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070076
77 void shutdown();
78
79private:
Jeff Thompson557b81e2013-08-21 15:13:51 -070080 class PitEntry {
81 public:
82 /**
83 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson48917f02013-08-21 17:12:45 -070084 * @param interest A shared_ptr for the interest.
Jeff Thompson7aec0252013-08-22 17:29:57 -070085 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
86 * use func_lib::ref() as appropriate.
87 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
88 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson557b81e2013-08-21 15:13:51 -070089 */
Jeff Thompson7aec0252013-08-22 17:29:57 -070090 PitEntry(const ptr_lib::shared_ptr<const Interest> &interest, const OnData &onData, const OnTimeout &onTimeout);
Jeff Thompson557b81e2013-08-21 15:13:51 -070091
Jeff Thompson48917f02013-08-21 17:12:45 -070092 const ptr_lib::shared_ptr<const Interest> &getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -070093
Jeff Thompson7aec0252013-08-22 17:29:57 -070094 const OnData &getOnData() { return onData_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -070095
96 /**
Jeff Thompson48917f02013-08-21 17:12:45 -070097 * Get the struct ndn_Interest for the interest_.
Jeff Thompson557b81e2013-08-21 15:13:51 -070098 * WARNING: Assume that this PitEntry was created with new, so that no copy constructor is invoked between calls.
99 * This class is private to Node and only used by its methods, so this should be OK.
100 * TODO: Doesn't this functionality belong in the Interest class?
101 * @return A reference to the ndn_Interest struct.
102 * WARNING: The resulting pointers in are invalid uses getInterest() to manipulate the object which could reallocate memory.
103 */
Jeff Thompson48917f02013-08-21 17:12:45 -0700104 const struct ndn_Interest &getInterestStruct()
105 {
106 return interestStruct_;
107 }
108
109 /**
Jeff Thompson7aec0252013-08-22 17:29:57 -0700110 * If this interest is timed out, call onTimeout_ (if defined) and return true.
Jeff Thompson48917f02013-08-21 17:12:45 -0700111 * @param parent The parent Node for the UpcallInfo.
112 * @param nowMilliseconds The current time in milliseconds from gettimeofday.
113 * @return true if this interest timed out and the timeout callback was called, otherwise false.
114 */
115 bool checkTimeout(Node *parent, double nowMilliseconds);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700116
117 private:
Jeff Thompson48917f02013-08-21 17:12:45 -0700118 ptr_lib::shared_ptr<const Interest> interest_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700119 std::vector<struct ndn_NameComponent> nameComponents_;
120 std::vector<struct ndn_ExcludeEntry> excludeEntries_;
121 struct ndn_Interest interestStruct_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700122
Jeff Thompson7aec0252013-08-22 17:29:57 -0700123 const OnData onData_;
124 const OnTimeout onTimeout_;
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 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700129 * An NdndIdFetcher receives the Data packet with the publisher public key digest for the connected NDN hub.
130 * This class is a function object for the callbacks. It only holds a pointer to an Info object, so it is OK to copy the pointer.
131 */
132 class NdndIdFetcher {
133 public:
134 class Info;
135 NdndIdFetcher(ptr_lib::shared_ptr<NdndIdFetcher::Info> info)
136 : info_(info)
137 {
138 }
139
140 /**
141 * We received the ndnd ID.
142 * @param interest
143 * @param data
144 */
145 void operator()(const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &ndndIdData);
146
147 /**
148 * We timed out fetching the ndnd ID.
149 * @param interest
150 */
151 void operator()(const ptr_lib::shared_ptr<const Interest> &timedOutInterest);
152
153 class Info {
154 public:
155 Info(Node *node, const Name &prefix, const OnInterest &onInterest, int flags)
156 : node_(*node), prefix_(prefix), onInterest_(onInterest), flags_(flags)
157 {
158 }
159
160 Node &node_;
161 Name prefix_;
162 const OnInterest onInterest_;
163 int flags_;
164 };
165
166 private:
167 ptr_lib::shared_ptr<Info> info_;
168 };
169
170 /**
Jeff Thompson557b81e2013-08-21 15:13:51 -0700171 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
172 * the entry interest name is the longest that matches name.
173 * @param name The name to find the interest for (from the incoming data packet).
174 * @return The index in pit_ of the pit entry, or -1 if not found.
175 */
176 int getEntryIndexForExpressedInterest(const Name &name);
177
Jeff Thompson86507bc2013-08-23 20:51:38 -0700178 /**
179 * Do the work of registerPrefix once we know we are connected with an ndndId_.
180 * @param prefix
181 * @param onInterest
182 * @param flags
183 */
184 void registerPrefixHelper(const Name &prefix, const OnInterest &onInterest, int flags);
185
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700186 ptr_lib::shared_ptr<Transport> transport_;
Jeff Thompson10e34382013-08-22 13:34:46 -0700187 ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700188 std::vector<ptr_lib::shared_ptr<PitEntry> > pit_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700189 Interest ndndIdFetcherInterest_;
190 std::vector<unsigned char> ndndId_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700191};
192
193}
194
195#endif