blob: 9b1fbe4751e1fb43f8b5cd0930ee14e1fc24d89c [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 */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070053class Face {
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070054public:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080055 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
Alexander Afanasyevb790d952014-01-24 12:07:53 -080056
57 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080058 * @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport.
Alexander Afanasyevb790d952014-01-24 12:07:53 -080059 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080060 Face();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080061
62 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080063 * @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport.
Alexander Afanasyevb790d952014-01-24 12:07:53 -080064 * @param ioService A shared pointer to boost::io_service object that should control all IO operations
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070065 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080066 explicit
67 Face(const ptr_lib::shared_ptr<boost::asio::io_service> &ioService);
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070068
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070069 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070070 * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070071 * @param host The host of the NDN hub.
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080072 * @param port The port or service name of the NDN hub. If omitted. use 6363.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070073 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080074 Face(const std::string &host, const std::string &port = "6363");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080075
76 /**
77 * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo.
78 * @param transport A shared_ptr to a Transport object used for communication.
79 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
80 */
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080081 explicit
82 Face(const shared_ptr<Transport>& transport);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080083
84 /**
85 * @brief Alternative (special use case) version of the constructor, can be used to aggregate
86 * several Faces within one processing thread
87 *
88 * <code>
89 * Face face1(...);
90 * Face face2(..., face1.getAsyncService());
91 *
92 * // Now the following ensures that events on both faces are processed
93 * face1.processEvents();
94 * </code>
95 */
96 Face(const shared_ptr<Transport>& transport,
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080097 const shared_ptr<boost::asio::io_service> &ioService);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080098
Jeff Thompson4fe45512013-08-23 14:06:38 -070099 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800100 * @brief Express Interest
101 *
102 * @param interest A reference to the Interest. This copies the Interest.
103 * @param onData A function object to call when a matching data packet is received.
104 * @param onTimeout A function object to call if the interest times out.
105 * If onTimeout is an empty OnTimeout(), this does not use it.
106 *
Jeff Thompson11095142013-10-01 16:20:28 -0700107 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -0700108 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800109 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800110 expressInterest(const Interest& interest,
111 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Jeff Thompson4fe45512013-08-23 14:06:38 -0700112
113 /**
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800114 * @brief Express Interest using name and Interest template
115 *
116 * @param name Name of the Interest
117 * @param tmpl Interest template to fill parameters
118 * @param onData A callback to call when a matching data packet is received.
119 * @param onTimeout A callback 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 Thompson7aec0252013-08-22 17:29:57 -0700123 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800124 const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800125 expressInterest(const Name& name,
126 const Interest &tmpl,
127 const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
128
Jeff Thompson11095142013-10-01 16:20:28 -0700129 /**
130 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
131 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
132 * If there is no entry with the pendingInterestId, do nothing.
133 * @param pendingInterestId The ID returned from expressInterest.
134 */
135 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800136 removePendingInterest(const PendingInterestId *pendingInterestId);
Jeff Thompsoncdf7e252013-07-31 12:41:47 -0700137
Jeff Thompson432c8be2013-08-09 16:16:08 -0700138 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700139 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
140 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
141 * @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 -0800142 * use ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -0700143 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
144 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800145 * @param flags The flags for finer control of which interests are forward to the application.
Jeff Thompson11095142013-10-01 16:20:28 -0700146 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700147 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800148 const RegisteredPrefixId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800149 setInterestFilter(const Name& prefix,
150 const OnInterest& onInterest,
151 const OnSetInterestFilterFailed& onSetInterestFilterFailed);
Jeff Thompson11095142013-10-01 16:20:28 -0700152
153 /**
154 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
155 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
156 * If there is no entry with the registeredPrefixId, do nothing.
157 * @param registeredPrefixId The ID returned from registerPrefix.
158 */
159 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800160 unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800161
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800162 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800163 * @brief Publish data packet
164 *
165 * This method can be called to satisfy the incoming Interest or to put Data packet into the cache
166 * of the local NDN forwarder
167 */
168 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800169 put(const Data &data);
170
Jeff Thompson86507bc2013-08-23 20:51:38 -0700171 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700172 * Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800173 *
174 * This call will block forever (default timeout == 0) to process IO on the face.
175 * To exit, one expected to call face.shutdown() from one of the callback methods.
176 *
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800177 * If positive timeout is specified, then processEvents will exit after this timeout,
178 * if not stopped earlier with face.shutdown() or when all active events finish.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800179 * The call can be called repeatedly, if desired.
180 *
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800181 * If negative timeout is specified, then processEvents will not block and process only pending
182 * events.
183 *
Jeff Thompson432c8be2013-08-09 16:16:08 -0700184 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
185 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
186 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700187 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800188 processEvents(Milliseconds timeout = 0, bool keepThread = false);
Jeff Thompson432c8be2013-08-09 16:16:08 -0700189
Jeff Thompson0050abe2013-09-17 12:50:25 -0700190 void
191 shutdown();
Yingdi Yu0d920812014-01-30 14:50:57 -0800192
193 shared_ptr<boost::asio::io_service>
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800194 ioService() { return ioService_; }
195
196private:
197 void
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800198 construct(const shared_ptr<Transport>& transport, const shared_ptr<boost::asio::io_service>& ioService);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800199
200 struct ProcessEventsTimeout {};
201 typedef std::list<shared_ptr<PendingInterest> > PendingInterestTable;
202 typedef std::list<shared_ptr<RegisteredPrefix> > RegisteredPrefixTable;
203
204 void
205 asyncExpressInterest(const shared_ptr<const Interest> &interest,
206 const OnData& onData, const OnTimeout& onTimeout);
207
208 void
209 asyncRemovePendingInterest(const PendingInterestId *pendingInterestId);
210
211 void
212 asyncUnsetInterestFilter(const RegisteredPrefixId *registeredPrefixId);
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800213
214 void
215 finalizeUnsertInterestFilter(RegisteredPrefixTable::iterator item);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800216
217 void
218 onReceiveElement(const Block &wire);
219
220
221 static void
222 fireProcessEventsTimeout(const boost::system::error_code& error);
223
224 /**
225 * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and
226 * the entry interest name is the longest that matches name.
227 * @param name The name to find the interest for (from the incoming data packet).
228 * @return The index in pit_ of the pit entry, or -1 if not found.
229 */
230 PendingInterestTable::iterator
231 getEntryIndexForExpressedInterest(const Name& name);
232
233 /**
234 * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name.
235 * @param name The name to find the PrefixEntry for (from the incoming interest packet).
236 * @return A pointer to the entry, or 0 if not found.
237 */
238 RegisteredPrefixTable::iterator
239 getEntryForRegisteredPrefix(const Name& name);
240
241
242 void
243 checkPitExpire();
Jeff Thompson517ffa82013-08-05 16:04:34 -0700244
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700245private:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800246 shared_ptr<boost::asio::io_service> ioService_;
247 shared_ptr<boost::asio::io_service::work> ioServiceWork_; // needed if thread needs to be preserved
248 shared_ptr<boost::asio::deadline_timer> pitTimeoutCheckTimer_;
249 bool pitTimeoutCheckTimerActive_;
250 shared_ptr<boost::asio::deadline_timer> processEventsTimeoutTimer_;
251
252 shared_ptr<Transport> transport_;
253
254 PendingInterestTable pendingInterestTable_;
255 RegisteredPrefixTable registeredPrefixTable_;
256
257 shared_ptr<Controller> m_fwController;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700258};
259
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800260} // namespace ndn
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700261
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800262#endif // NDN_FACE_HPP