blob: 6301be7bfe4d0341f9e80321b9cf5ce36d9600a2 [file] [log] [blame]
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_NODE_HPP
8#define NDN_NODE_HPP
9
Jeff Thompson7aec0252013-08-22 17:29:57 -070010#include "common.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070011#include "interest.hpp"
Jeff Thompson86507bc2013-08-23 20:51:38 -070012#include "data.hpp"
13#include "transport/tcp-transport.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070014#include "encoding/binary-xml-element-reader.hpp"
15
16namespace ndn {
17
Jeff Thompson7aec0252013-08-22 17:29:57 -070018/**
19 * An OnData function object is used to pass a callback to expressInterest.
20 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070021typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest>& , const ptr_lib::shared_ptr<Data>& )> OnData;
Jeff Thompson7aec0252013-08-22 17:29:57 -070022
23/**
24 * An OnTimeout function object is used to pass a callback to expressInterest.
25 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070026typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest>& )> OnTimeout;
Jeff Thompson7aec0252013-08-22 17:29:57 -070027
Jeff Thompson86507bc2013-08-23 20:51:38 -070028/**
29 * An OnInterest function object is used to pass a callback to registerPrefix.
30 */
Jeff Thompson9cc4be42013-08-27 18:12:41 -070031typedef func_lib::function<void
Jeff Thompson1656e6a2013-08-29 18:01:48 -070032 (const ptr_lib::shared_ptr<const Name>& , const ptr_lib::shared_ptr<const Interest>& , Transport& )> OnInterest;
Jeff Thompson86507bc2013-08-23 20:51:38 -070033
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070034class Face;
35
36class Node : public ElementListener {
37public:
38 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070039 * Create a new Node for communication with an NDN hub with the given Transport object and connectionInfo.
40 * @param transport A shared_ptr to a Transport object used for communication.
41 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070042 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070043 Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070044
45 /**
Jeff Thompson4fe45512013-08-23 14:06:38 -070046 * Send the Interest through the transport, read the entire response and call onData(interest, data).
47 * @param interest A reference to the Interest. This copies the Interest.
48 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
49 * use func_lib::ref() as appropriate.
50 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
51 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
52 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070053 void expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout);
Jeff Thompson7aec0252013-08-22 17:29:57 -070054
55 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070056 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
57 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
58 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
59 * use func_lib::ref() as appropriate.
60 * @param flags The flags for finer control of which interests are forward to the application.
61 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070062 void registerPrefix(const Name& prefix, const OnInterest& onInterest, int flags);
Jeff Thompson86507bc2013-08-23 20:51:38 -070063
64 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070065 * Process any data to receive. For each element received, call onReceivedElement.
66 * This is non-blocking and will return immediately if there is no data to receive.
67 * 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.
68 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
69 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
70 */
71 void processEvents();
72
Jeff Thompson1656e6a2013-08-29 18:01:48 -070073 const ptr_lib::shared_ptr<Transport>& getTransport() { return transport_; }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070074
Jeff Thompson1656e6a2013-08-29 18:01:48 -070075 const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& getConnectionInfo() { return connectionInfo_; }
Jeff Thompson10e34382013-08-22 13:34:46 -070076
Jeff Thompson8b173cc2013-08-21 17:54:12 -070077 void onReceivedElement(const unsigned char *element, unsigned int elementLength);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070078
79 void shutdown();
80
81private:
Jeff Thompson557b81e2013-08-21 15:13:51 -070082 class PitEntry {
83 public:
84 /**
85 * Create a new PitEntry and set the timeoutTime_ based on the current time and the interest lifetime.
Jeff Thompson48917f02013-08-21 17:12:45 -070086 * @param interest A shared_ptr for the interest.
Jeff Thompson9cc4be42013-08-27 18:12:41 -070087 * @param onData A function object to call when a matching data packet is received.
Jeff Thompson7aec0252013-08-22 17:29:57 -070088 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
Jeff Thompson557b81e2013-08-21 15:13:51 -070089 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -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 Thompson1656e6a2013-08-29 18:01:48 -070092 const ptr_lib::shared_ptr<const Interest>& getInterest() { return interest_; }
Jeff Thompson557b81e2013-08-21 15:13:51 -070093
Jeff Thompson1656e6a2013-08-29 18:01:48 -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 Thompson1656e6a2013-08-29 18:01:48 -0700104 const struct ndn_Interest& getInterestStruct()
Jeff Thompson48917f02013-08-21 17:12:45 -0700105 {
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 };
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700127
128 class PrefixEntry {
129 public:
130 /**
131 * Create a new PrefixEntry.
132 * @param prefix A shared_ptr for the prefix.
133 * @param onInterest A function object to call when a matching data packet is received.
134 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700135 PrefixEntry(const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700136 : prefix_(prefix), onInterest_(onInterest)
137 {
138 }
139
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700140 const ptr_lib::shared_ptr<const Name>& getPrefix() { return prefix_; }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700141
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700142 const OnInterest& getOnInterest() { return onInterest_; }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700143
144 private:
145 ptr_lib::shared_ptr<const Name> prefix_;
146 const OnInterest onInterest_;
147 };
Jeff Thompson557b81e2013-08-21 15:13:51 -0700148
149 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700150 * An NdndIdFetcher receives the Data packet with the publisher public key digest for the connected NDN hub.
151 * 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.
152 */
153 class NdndIdFetcher {
154 public:
155 class Info;
156 NdndIdFetcher(ptr_lib::shared_ptr<NdndIdFetcher::Info> info)
157 : info_(info)
158 {
159 }
160
161 /**
162 * We received the ndnd ID.
163 * @param interest
164 * @param data
165 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700166 void operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& ndndIdData);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700167
168 /**
169 * We timed out fetching the ndnd ID.
170 * @param interest
171 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700172 void operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700173
174 class Info {
175 public:
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700176 Info(Node *node, const Name& prefix, const OnInterest& onInterest, int flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700177 : node_(*node), prefix_(prefix), onInterest_(onInterest), flags_(flags)
178 {
179 }
180
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700181 Node& node_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700182 Name prefix_;
183 const OnInterest onInterest_;
184 int flags_;
185 };
186
187 private:
188 ptr_lib::shared_ptr<Info> info_;
189 };
190
191 /**
Jeff Thompson557b81e2013-08-21 15:13:51 -0700192 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
193 * the entry interest name is the longest that matches name.
194 * @param name The name to find the interest for (from the incoming data packet).
195 * @return The index in pit_ of the pit entry, or -1 if not found.
196 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700197 int getEntryIndexForExpressedInterest(const Name& name);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700198
Jeff Thompson86507bc2013-08-23 20:51:38 -0700199 /**
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700200 * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name.
201 * @param name The name to find the PrefixEntry for (from the incoming interest packet).
202 * @return A pointer to the entry, or 0 if not found.
203 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700204 PrefixEntry *getEntryForRegisteredPrefix(const Name& name);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700205
206 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700207 * Do the work of registerPrefix once we know we are connected with an ndndId_.
208 * @param prefix
209 * @param onInterest
210 * @param flags
211 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700212 void registerPrefixHelper(const Name& prefix, const OnInterest& onInterest, int flags);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700213
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700214 ptr_lib::shared_ptr<Transport> transport_;
Jeff Thompson10e34382013-08-22 13:34:46 -0700215 ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
Jeff Thompson557b81e2013-08-21 15:13:51 -0700216 std::vector<ptr_lib::shared_ptr<PitEntry> > pit_;
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700217 std::vector<ptr_lib::shared_ptr<PrefixEntry> > registeredPrefixTable_;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700218 Interest ndndIdFetcherInterest_;
Jeff Thompson03616c92013-09-12 14:30:01 -0700219 Blob ndndId_;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700220};
221
222}
223
224#endif