blob: d168e735a84ea31f758fce411ab195ba3dc5504d [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07004 * See COPYING for copyright and distribution information.
5 */
6
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -07007#ifndef NDN_FACE_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07008#define NDN_FACE_HPP
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07009
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080010#include "common.hpp"
11#include "interest.hpp"
12#include "data.hpp"
13
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080014#include "transport/transport.hpp"
15#include "transport/unix-transport.hpp"
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080016#include "transport/tcp-transport.hpp"
Jeff Thompsonbeb8b7d2013-07-16 15:49:21 -070017
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080018#include "management/controller.hpp"
19
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070020#include "util/scheduler.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080021#include "detail/registered-prefix.hpp"
22#include "detail/pending-interest.hpp"
23
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070024namespace ndn {
25
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080026struct PendingInterestId;
27struct RegisteredPrefixId;
28
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070029/**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080030 * An OnData function object is used to pass a callback to expressInterest.
31 */
32typedef function<void(const Interest&, Data&)> OnData;
33
34/**
35 * An OnTimeout function object is used to pass a callback to expressInterest.
36 */
37typedef function<void(const Interest&)> OnTimeout;
38
39/**
40 * An OnInterest function object is used to pass a callback to registerPrefix.
41 */
42typedef function<void (const Name&, const Interest&)> OnInterest;
43
44/**
45 * An OnRegisterFailed function object is used to report when registerPrefix fails.
46 */
47typedef function<void(const Name&, const std::string&)> OnSetInterestFilterFailed;
48
49
50
51/**
52 * @brief Abstraction to communicate with local or remote NDN forwarder
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070053 */
Alexander Afanasyev8460afb2014-02-15 20:31:42 -080054class Face : noncopyable
55{
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070056public:
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080057 struct Error : public std::runtime_error { Error(const std::string& what) : std::runtime_error(what) {} };
Alexander Afanasyevb790d952014-01-24 12:07:53 -080058
59 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080060 * @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport.
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060061 * @throws ConfigFile::Error on configuration file parse failure
62 * @throws Face::Error on unsupported protocol
Alexander Afanasyevb790d952014-01-24 12:07:53 -080063 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080064 Face();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080065
66 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080067 * @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport.
Alexander Afanasyevb790d952014-01-24 12:07:53 -080068 * @param ioService A shared pointer to boost::io_service object that should control all IO operations
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060069 * @throws ConfigFile::Error on configuration file parse failure
70 * @throws Face::Error on unsupported protocol
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070071 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080072 explicit
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080073 Face(const shared_ptr<boost::asio::io_service>& ioService);
74
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070075 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070076 * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070077 * @param host The host of the NDN hub.
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080078 * @param port The port or service name of the NDN hub. If omitted. use 6363.
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060079 * @throws Face::Error on unsupported protocol
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070080 */
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080081 Face(const std::string& host, const std::string& port = "6363");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080082
83 /**
84 * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo.
85 * @param transport A shared_ptr to a Transport object used for communication.
86 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060087 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080088 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080089 explicit
90 Face(const shared_ptr<Transport>& transport);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080091
92 /**
93 * @brief Alternative (special use case) version of the constructor, can be used to aggregate
94 * several Faces within one processing thread
95 *
96 * <code>
97 * Face face1(...);
98 * Face face2(..., face1.getAsyncService());
99 *
100 * // Now the following ensures that events on both faces are processed
101 * face1.processEvents();
102 * </code>
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600103 * @throws Face::Error on unsupported protocol
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800104 */
105 Face(const shared_ptr<Transport>& transport,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800106 const shared_ptr<boost::asio::io_service>& ioService);
107
108 /**
Alexander Afanasyev505646e2014-02-24 20:13:37 -0800109 * @brief Set controller used for prefix registration
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800110 */
Alexander Afanasyev505646e2014-02-24 20:13:37 -0800111 void
112 setController(const shared_ptr<Controller>& controller);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800113
Jeff Thompson4fe45512013-08-23 14:06:38 -0700114 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800115 * @brief Express Interest
116 *
117 * @param interest A reference to the Interest. This copies the Interest.
118 * @param onData A function object to call when a matching data packet is received.
119 * @param onTimeout A function object to call if the interest times out.
120 * If onTimeout is an empty OnTimeout(), this does not use it.
121 *
Jeff Thompson11095142013-10-01 16:20:28 -0700122 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -0700123 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800124 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800125 expressInterest(const Interest& interest,
126 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Jeff Thompson4fe45512013-08-23 14:06:38 -0700127
128 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800129 * @brief Express Interest using name and Interest template
130 *
131 * @param name Name of the Interest
132 * @param tmpl Interest template to fill parameters
133 * @param onData A callback to call when a matching data packet is received.
134 * @param onTimeout A callback to call if the interest times out.
135 * If onTimeout is an empty OnTimeout(), this does not use it.
136 *
Jeff Thompson11095142013-10-01 16:20:28 -0700137 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700138 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800139 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800140 expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800141 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800142 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
143
Jeff Thompson11095142013-10-01 16:20:28 -0700144 /**
145 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
146 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
147 * If there is no entry with the pendingInterestId, do nothing.
148 * @param pendingInterestId The ID returned from expressInterest.
149 */
150 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800151 removePendingInterest(const PendingInterestId* pendingInterestId);
Jeff Thompsoncdf7e252013-07-31 12:41:47 -0700152
Jeff Thompson432c8be2013-08-09 16:16:08 -0700153 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700154 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
155 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
156 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800157 * use ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -0700158 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
159 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800160 * @param flags The flags for finer control of which interests are forward to the application.
Jeff Thompson11095142013-10-01 16:20:28 -0700161 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700162 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800163 const RegisteredPrefixId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800164 setInterestFilter(const Name& prefix,
165 const OnInterest& onInterest,
166 const OnSetInterestFilterFailed& onSetInterestFilterFailed);
Jeff Thompson11095142013-10-01 16:20:28 -0700167
168 /**
169 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
170 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
171 * If there is no entry with the registeredPrefixId, do nothing.
172 * @param registeredPrefixId The ID returned from registerPrefix.
173 */
174 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800175 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800176
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800177 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800178 * @brief Publish data packet
179 *
180 * This method can be called to satisfy the incoming Interest or to put Data packet into the cache
181 * of the local NDN forwarder
182 */
183 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800184 put(const Data& data);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800185
Jeff Thompson86507bc2013-08-23 20:51:38 -0700186 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700187 * Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800188 *
189 * This call will block forever (default timeout == 0) to process IO on the face.
190 * To exit, one expected to call face.shutdown() from one of the callback methods.
191 *
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800192 * If positive timeout is specified, then processEvents will exit after this timeout,
193 * if not stopped earlier with face.shutdown() or when all active events finish.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800194 * The call can be called repeatedly, if desired.
195 *
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800196 * If negative timeout is specified, then processEvents will not block and process only pending
197 * events.
198 *
Jeff Thompson432c8be2013-08-09 16:16:08 -0700199 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
200 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
201 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700202 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700203 processEvents(const time::milliseconds& timeout = time::milliseconds::zero(),
204 bool keepThread = false);
Jeff Thompson432c8be2013-08-09 16:16:08 -0700205
Jeff Thompson0050abe2013-09-17 12:50:25 -0700206 void
207 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800208
209 shared_ptr<boost::asio::io_service>
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800210 ioService() { return m_ioService; }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800211
212private:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600213
214 /**
215 * @throws Face::Error on unsupported protocol
216 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800217 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800218 construct(const shared_ptr<Transport>& transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -0800219 const shared_ptr<boost::asio::io_service>& ioService);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600220
221 bool
222 isSupportedNfdProtocol(const std::string& protocol);
223
224 bool
225 isSupportedNrdProtocol(const std::string& protocol);
226
227 bool
228 isSupportedNdndProtocol(const std::string& protocol);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800229
230 struct ProcessEventsTimeout {};
231 typedef std::list<shared_ptr<PendingInterest> > PendingInterestTable;
232 typedef std::list<shared_ptr<RegisteredPrefix> > RegisteredPrefixTable;
233
234 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800235 asyncExpressInterest(const shared_ptr<const Interest>& interest,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800236 const OnData& onData, const OnTimeout& onTimeout);
237
238 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800239 asyncRemovePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800240
241 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800242 asyncUnsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800243
244 void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000245 finalizeUnsetInterestFilter(RegisteredPrefixTable::iterator item);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800246
247 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800248 onReceiveElement(const Block& wire);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800249
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700250 void
251 asyncShutdown();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800252
253 static void
254 fireProcessEventsTimeout(const boost::system::error_code& error);
255
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800256 void
257 satisfyPendingInterests(Data& data);
258
259 void
260 processInterestFilters(Interest& interest);
261
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800262 void
263 checkPitExpire();
Yingdi Yuf9fa52f2014-02-06 12:27:32 -0800264
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700265private:
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800266 shared_ptr<boost::asio::io_service> m_ioService;
267 shared_ptr<boost::asio::io_service::work> m_ioServiceWork; // needed if thread needs to be preserved
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700268 shared_ptr<monotonic_deadline_timer> m_pitTimeoutCheckTimer;
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800269 bool m_pitTimeoutCheckTimerActive;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700270 shared_ptr<monotonic_deadline_timer> m_processEventsTimeoutTimer;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800271
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800272 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800273
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800274 PendingInterestTable m_pendingInterestTable;
275 RegisteredPrefixTable m_registeredPrefixTable;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800276
277 shared_ptr<Controller> m_fwController;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600278
279 ConfigFile m_config;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700280};
281
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600282inline bool
283Face::isSupportedNfdProtocol(const std::string& protocol)
284{
285 return protocol == "nfd-0.1";
286}
287
288inline bool
289Face::isSupportedNrdProtocol(const std::string& protocol)
290{
291 return protocol == "nrd-0.1";
292}
293
294inline bool
295Face::isSupportedNdndProtocol(const std::string& protocol)
296{
297 return protocol == "ndnd-tlv-0.7";
298}
299
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800300} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700301
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800302#endif // NDN_FACE_HPP