blob: 895c39e5b8d8d9c863131195d1dc9ff61ff7e12b [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
20#include "detail/registered-prefix.hpp"
21#include "detail/pending-interest.hpp"
22
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070023namespace ndn {
24
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080025struct PendingInterestId;
26struct RegisteredPrefixId;
27
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070028/**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080029 * An OnData function object is used to pass a callback to expressInterest.
30 */
31typedef function<void(const Interest&, Data&)> OnData;
32
33/**
34 * An OnTimeout function object is used to pass a callback to expressInterest.
35 */
36typedef function<void(const Interest&)> OnTimeout;
37
38/**
39 * An OnInterest function object is used to pass a callback to registerPrefix.
40 */
41typedef function<void (const Name&, const Interest&)> OnInterest;
42
43/**
44 * An OnRegisterFailed function object is used to report when registerPrefix fails.
45 */
46typedef function<void(const Name&, const std::string&)> OnSetInterestFilterFailed;
47
48
49
50/**
51 * @brief Abstraction to communicate with local or remote NDN forwarder
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070052 */
Alexander Afanasyev8460afb2014-02-15 20:31:42 -080053class Face : noncopyable
54{
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070055public:
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080056 struct Error : public std::runtime_error { Error(const std::string& what) : std::runtime_error(what) {} };
Alexander Afanasyevb790d952014-01-24 12:07:53 -080057
58 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080059 * @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport.
Alexander Afanasyevb790d952014-01-24 12:07:53 -080060 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080061 Face();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080062
63 /**
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080064 * @brief Create face, explicitly selecting which controller to use
65 *
66 * Controller controls the way prefixes are registered with local forwarder.
67 */
68 explicit
69 Face(const shared_ptr<Controller>& controller);
70
71 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080072 * @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport.
Alexander Afanasyevb790d952014-01-24 12:07:53 -080073 * @param ioService A shared pointer to boost::io_service object that should control all IO operations
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070074 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080075 explicit
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080076 Face(const shared_ptr<boost::asio::io_service>& ioService);
77
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070078 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070079 * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070080 * @param host The host of the NDN hub.
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080081 * @param port The port or service name of the NDN hub. If omitted. use 6363.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070082 */
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080083 Face(const std::string& host, const std::string& port = "6363");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080084
85 /**
86 * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo.
87 * @param transport A shared_ptr to a Transport object used for communication.
88 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
89 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080090 explicit
91 Face(const shared_ptr<Transport>& transport);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080092
93 /**
94 * @brief Alternative (special use case) version of the constructor, can be used to aggregate
95 * several Faces within one processing thread
96 *
97 * <code>
98 * Face face1(...);
99 * Face face2(..., face1.getAsyncService());
100 *
101 * // Now the following ensures that events on both faces are processed
102 * face1.processEvents();
103 * </code>
104 */
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 /**
109 * @brief Create face, explicitly selecting which controller to use
110 *
111 * Controller controls the way prefixes are registered with local forwarder.
112 */
113 Face(const shared_ptr<Transport>& transport,
114 const shared_ptr<boost::asio::io_service>& ioService,
115 const shared_ptr<Controller>& controller);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800116
Jeff Thompson4fe45512013-08-23 14:06:38 -0700117 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800118 * @brief Express Interest
119 *
120 * @param interest A reference to the Interest. This copies the Interest.
121 * @param onData A function object to call when a matching data packet is received.
122 * @param onTimeout A function object to call if the interest times out.
123 * If onTimeout is an empty OnTimeout(), this does not use it.
124 *
Jeff Thompson11095142013-10-01 16:20:28 -0700125 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -0700126 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800127 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800128 expressInterest(const Interest& interest,
129 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Jeff Thompson4fe45512013-08-23 14:06:38 -0700130
131 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800132 * @brief Express Interest using name and Interest template
133 *
134 * @param name Name of the Interest
135 * @param tmpl Interest template to fill parameters
136 * @param onData A callback to call when a matching data packet is received.
137 * @param onTimeout A callback to call if the interest times out.
138 * If onTimeout is an empty OnTimeout(), this does not use it.
139 *
Jeff Thompson11095142013-10-01 16:20:28 -0700140 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700141 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800142 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800143 expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800144 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800145 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
146
Jeff Thompson11095142013-10-01 16:20:28 -0700147 /**
148 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
149 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
150 * If there is no entry with the pendingInterestId, do nothing.
151 * @param pendingInterestId The ID returned from expressInterest.
152 */
153 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800154 removePendingInterest(const PendingInterestId* pendingInterestId);
Jeff Thompsoncdf7e252013-07-31 12:41:47 -0700155
Jeff Thompson432c8be2013-08-09 16:16:08 -0700156 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700157 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
158 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
159 * @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 -0800160 * use ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -0700161 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
162 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800163 * @param flags The flags for finer control of which interests are forward to the application.
Jeff Thompson11095142013-10-01 16:20:28 -0700164 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700165 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800166 const RegisteredPrefixId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800167 setInterestFilter(const Name& prefix,
168 const OnInterest& onInterest,
169 const OnSetInterestFilterFailed& onSetInterestFilterFailed);
Jeff Thompson11095142013-10-01 16:20:28 -0700170
171 /**
172 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
173 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
174 * If there is no entry with the registeredPrefixId, do nothing.
175 * @param registeredPrefixId The ID returned from registerPrefix.
176 */
177 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800178 unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800179
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800180 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800181 * @brief Publish data packet
182 *
183 * This method can be called to satisfy the incoming Interest or to put Data packet into the cache
184 * of the local NDN forwarder
185 */
186 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800187 put(const Data& data);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800188
Jeff Thompson86507bc2013-08-23 20:51:38 -0700189 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700190 * Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800191 *
192 * This call will block forever (default timeout == 0) to process IO on the face.
193 * To exit, one expected to call face.shutdown() from one of the callback methods.
194 *
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800195 * If positive timeout is specified, then processEvents will exit after this timeout,
196 * if not stopped earlier with face.shutdown() or when all active events finish.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800197 * The call can be called repeatedly, if desired.
198 *
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800199 * If negative timeout is specified, then processEvents will not block and process only pending
200 * events.
201 *
Jeff Thompson432c8be2013-08-09 16:16:08 -0700202 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
203 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
204 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700205 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800206 processEvents(Milliseconds timeout = 0, bool keepThread = false);
Jeff Thompson432c8be2013-08-09 16:16:08 -0700207
Jeff Thompson0050abe2013-09-17 12:50:25 -0700208 void
209 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800210
211 shared_ptr<boost::asio::io_service>
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800212 ioService() { return m_ioService; }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800213
214private:
215 void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800216 construct(const shared_ptr<Transport>& transport,
217 const shared_ptr<boost::asio::io_service>& ioService,
218 const shared_ptr<Controller>& controller);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800219
220 struct ProcessEventsTimeout {};
221 typedef std::list<shared_ptr<PendingInterest> > PendingInterestTable;
222 typedef std::list<shared_ptr<RegisteredPrefix> > RegisteredPrefixTable;
223
224 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800225 asyncExpressInterest(const shared_ptr<const Interest>& interest,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800226 const OnData& onData, const OnTimeout& onTimeout);
227
228 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800229 asyncRemovePendingInterest(const PendingInterestId* pendingInterestId);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800230
231 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800232 asyncUnsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800233
234 void
235 finalizeUnsertInterestFilter(RegisteredPrefixTable::iterator item);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800236
237 void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800238 onReceiveElement(const Block& wire);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800239
240
241 static void
242 fireProcessEventsTimeout(const boost::system::error_code& error);
243
244 /**
245 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
246 * the entry interest name is the longest that matches name.
247 * @param name The name to find the interest for (from the incoming data packet).
248 * @return The index in pit_ of the pit entry, or -1 if not found.
249 */
250 PendingInterestTable::iterator
251 getEntryIndexForExpressedInterest(const Name& name);
252
253 /**
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800254 * Find the first entry from the m_registeredPrefixTable where the entry prefix is the longest that matches name.
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800255 * @param name The name to find the PrefixEntry for (from the incoming interest packet).
256 * @return A pointer to the entry, or 0 if not found.
257 */
258 RegisteredPrefixTable::iterator
259 getEntryForRegisteredPrefix(const Name& name);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800260
261 void
262 checkPitExpire();
Yingdi Yuf9fa52f2014-02-06 12:27:32 -0800263
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700264private:
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800265 shared_ptr<boost::asio::io_service> m_ioService;
266 shared_ptr<boost::asio::io_service::work> m_ioServiceWork; // needed if thread needs to be preserved
267 shared_ptr<boost::asio::deadline_timer> m_pitTimeoutCheckTimer;
268 bool m_pitTimeoutCheckTimerActive;
269 shared_ptr<boost::asio::deadline_timer> m_processEventsTimeoutTimer;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800270
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800271 shared_ptr<Transport> m_transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800272
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800273 PendingInterestTable m_pendingInterestTable;
274 RegisteredPrefixTable m_registeredPrefixTable;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800275
276 shared_ptr<Controller> m_fwController;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700277};
278
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800279} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700280
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800281#endif // NDN_FACE_HPP