All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
face.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #ifndef NDN_FACE_HPP
8 #define NDN_FACE_HPP
9 
10 #include "common.hpp"
11 #include "interest.hpp"
12 #include "data.hpp"
13 
14 #include "transport/transport.hpp"
17 
19 
20 #include "util/scheduler.hpp"
23 
24 namespace ndn {
25 
26 struct PendingInterestId;
27 struct RegisteredPrefixId;
28 
32 typedef function<void(const Interest&, Data&)> OnData;
33 
37 typedef function<void(const Interest&)> OnTimeout;
38 
42 typedef function<void (const Name&, const Interest&)> OnInterest;
43 
47 typedef function<void(const Name&, const std::string&)> OnSetInterestFilterFailed;
48 
49 
50 
54 class Face : noncopyable
55 {
56 public:
57  class Error : public std::runtime_error
58  {
59  public:
60  explicit
61  Error(const std::string& what)
62  : std::runtime_error(what)
63  {
64  }
65  };
66 
73  Face();
74 
86  explicit
87  Face(const shared_ptr<boost::asio::io_service>& ioService);
88 
115  explicit
116  Face(boost::asio::io_service& ioService);
117 
125  Face(const std::string& host, const std::string& port = "6363");
126 
132  explicit
133  Face(const shared_ptr<Transport>& transport);
134 
142  Face(const shared_ptr<Transport>& transport,
143  boost::asio::io_service& ioService);
144 
148  void
149  setController(const shared_ptr<Controller>& controller);
150 
161  const PendingInterestId*
162  expressInterest(const Interest& interest,
163  const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
164 
176  const PendingInterestId*
177  expressInterest(const Name& name,
178  const Interest& tmpl,
179  const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
180 
191  void
192  removePendingInterest(const PendingInterestId* pendingInterestId);
193 
210  const RegisteredPrefixId*
211  setInterestFilter(const Name& prefix,
212  const OnInterest& onInterest,
213  const OnSetInterestFilterFailed& onSetInterestFilterFailed);
214 
225  void
226  unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
227 
234  void
235  put(const Data& data);
236 
258  void
259  processEvents(const time::milliseconds& timeout = time::milliseconds::zero(),
260  bool keepThread = false);
261 
270  void
271  shutdown();
272 
278  shared_ptr<boost::asio::io_service>
280  {
281  return m_ioService;
282  }
283 
287  boost::asio::io_service&
289  {
290  return *m_ioService;
291  }
292 
293 private:
294 
298  void
299  construct(const shared_ptr<Transport>& transport,
300  const shared_ptr<boost::asio::io_service>& ioService);
301 
302  bool
303  isSupportedNfdProtocol(const std::string& protocol);
304 
305  bool
306  isSupportedNrdProtocol(const std::string& protocol);
307 
308  bool
309  isSupportedNdndProtocol(const std::string& protocol);
310 
311  class ProcessEventsTimeout
312  {
313  };
314 
315  typedef std::list<shared_ptr<PendingInterest> > PendingInterestTable;
316  typedef std::list<shared_ptr<RegisteredPrefix> > RegisteredPrefixTable;
317 
318  void
319  asyncExpressInterest(const shared_ptr<const Interest>& interest,
320  const OnData& onData, const OnTimeout& onTimeout);
321 
322  void
323  asyncRemovePendingInterest(const PendingInterestId* pendingInterestId);
324 
325  void
326  asyncUnsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
327 
328  void
329  finalizeUnsetInterestFilter(RegisteredPrefixTable::iterator item);
330 
331  void
332  onReceiveElement(const Block& wire);
333 
334  void
335  asyncShutdown();
336 
337  static void
338  fireProcessEventsTimeout(const boost::system::error_code& error);
339 
340  void
341  satisfyPendingInterests(Data& data);
342 
343  void
344  processInterestFilters(Interest& interest);
345 
346  void
347  checkPitExpire();
348 
349 private:
350  shared_ptr<boost::asio::io_service> m_ioService;
351  shared_ptr<boost::asio::io_service::work> m_ioServiceWork; // if thread needs to be preserved
352  shared_ptr<monotonic_deadline_timer> m_pitTimeoutCheckTimer;
353  bool m_pitTimeoutCheckTimerActive;
354  shared_ptr<monotonic_deadline_timer> m_processEventsTimeoutTimer;
355 
356  shared_ptr<Transport> m_transport;
357 
358  PendingInterestTable m_pendingInterestTable;
359  RegisteredPrefixTable m_registeredPrefixTable;
360 
361  shared_ptr<Controller> m_fwController;
362 
363  ConfigFile m_config;
364 };
365 
366 inline bool
367 Face::isSupportedNfdProtocol(const std::string& protocol)
368 {
369  return protocol == "nfd-0.1";
370 }
371 
372 inline bool
373 Face::isSupportedNrdProtocol(const std::string& protocol)
374 {
375  return protocol == "nrd-0.1";
376 }
377 
378 inline bool
379 Face::isSupportedNdndProtocol(const std::string& protocol)
380 {
381  return protocol == "ndnd-tlv-0.7";
382 }
383 
384 } // namespace ndn
385 
386 #endif // NDN_FACE_HPP
Error(const std::string &what)
Definition: face.hpp:61
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:24
function< void(const Interest &)> OnTimeout
An OnTimeout function object is used to pass a callback to expressInterest.
Definition: face.hpp:37
function< void(const Name &, const Interest &)> OnInterest
An OnInterest function object is used to pass a callback to registerPrefix.
Definition: face.hpp:42
Face()
Create a new Face using the default transport (UnixTransport)
Definition: face.cpp:25
void setController(const shared_ptr< Controller > &controller)
Set controller used for prefix registration.
Definition: face.cpp:75
const RegisteredPrefixId * setInterestFilter(const Name &prefix, const OnInterest &onInterest, const OnSetInterestFilterFailed &onSetInterestFilterFailed)
Register prefix with the connected NDN hub and call onInterest when a matching interest is received...
Definition: face.cpp:216
shared_ptr< boost::asio::io_service > ioService()
Get shared_ptr of the IO service object.
Definition: face.hpp:279
void shutdown()
Shutdown face operations.
Definition: face.cpp:309
function< void(const Name &, const std::string &)> OnSetInterestFilterFailed
An OnRegisterFailed function object is used to report when registerPrefix fails.
Definition: face.hpp:47
Abstraction to communicate with local or remote NDN forwarder.
Definition: face.hpp:54
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
const PendingInterestId * expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout=OnTimeout())
Express Interest.
Definition: face.cpp:125
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId from the pending interest table...
Definition: face.cpp:232
boost::asio::io_service & getIoService()
Get reference to IO service object.
Definition: face.hpp:288
function< void(const Interest &, Data &)> OnData
An OnData function object is used to pass a callback to expressInterest.
Definition: face.hpp:27
void processEvents(const time::milliseconds &timeout=time::milliseconds::zero(), bool keepThread=false)
Process any data to receive or call timeout callbacks.
Definition: face.cpp:268
void removePendingInterest(const PendingInterestId *pendingInterestId)
Remove the pending interest entry with the pendingInterestId from the pending interest table...
Definition: face.cpp:203
void put(const Data &data)
Publish data packet.
Definition: face.cpp:185