blob: 9dce46905ca0caf1c28e8ba2ebd99f980f47eeb3 [file] [log] [blame]
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -07006#ifndef NDN_FACE_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07007#define NDN_FACE_HPP
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07008
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07009#include "node.hpp"
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070010#include "transport/tcp-transport.hpp"
Jeff Thompsonbeb8b7d2013-07-16 15:49:21 -070011
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070012namespace ndn {
13
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070014/**
15 * The Face class provides the main methods for NDN communication.
16 */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070017class Face {
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070018public:
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070019 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070020 * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo.
21 * @param transport A shared_ptr to a Transport object used for communication.
22 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070023 */
Jeff Thompson10e34382013-08-22 13:34:46 -070024 Face(const ptr_lib::shared_ptr<Transport> &transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &connectionInfo)
25 : node_(transport, connectionInfo)
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070026 {
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070027 }
28
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070029 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070030 * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070031 * @param host The host of the NDN hub.
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070032 * @param port The port of the NDN hub. If omitted. use 9695.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070033 */
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070034 Face(const char *host, unsigned short port = 9695)
35 : node_(ptr_lib::make_shared<TcpTransport>(),
36 ptr_lib::make_shared<TcpTransport::ConnectionInfo>(host, port))
Jeff Thompsonb7d059d2013-07-31 10:59:51 -070037 {
38 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070039
Jeff Thompson4fe45512013-08-23 14:06:38 -070040 /**
41 * Send the Interest through the transport, read the entire response and call onData(interest, data).
42 * @param interest A reference to the Interest. This copies the Interest.
43 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
44 * use func_lib::ref() as appropriate.
45 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
46 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
47 */
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070048 void expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout = OnTimeout())
Jeff Thompson4fe45512013-08-23 14:06:38 -070049 {
50 node_.expressInterest(interest, onData, onTimeout);
51 }
52
53 /**
Jeff Thompsonc172be32013-07-16 15:08:05 -070054 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
Jeff Thompson7aec0252013-08-22 17:29:57 -070055 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070056 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompsonc172be32013-07-16 15:08:05 -070057 * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object.
Jeff Thompson7aec0252013-08-22 17:29:57 -070058 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
59 * use func_lib::ref() as appropriate.
60 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
61 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompsonc172be32013-07-16 15:08:05 -070062 */
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070063 void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData, const OnTimeout &onTimeout = OnTimeout());
Jeff Thompson7aec0252013-08-22 17:29:57 -070064
65 /**
66 * Encode name as an Interest, using a default interest lifetime.
67 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070068 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -070069 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
70 * use func_lib::ref() as appropriate.
71 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
72 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
73 */
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070074 void expressInterest(const Name &name, const OnData &onData, const OnTimeout &onTimeout = OnTimeout())
Jeff Thompson7aec0252013-08-22 17:29:57 -070075 {
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070076 expressInterest(name, 0, onData, onTimeout);
Jeff Thompsoncdf7e252013-07-31 12:41:47 -070077 }
78
Jeff Thompson432c8be2013-08-09 16:16:08 -070079 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070080 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
81 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
82 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
83 * use func_lib::ref() as appropriate.
84 * @param flags The flags for finer control of which interests are forward to the application.
85 */
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070086 void registerPrefix(const Name &prefix, const OnInterest &onInterest, int flags = 0)
Jeff Thompson86507bc2013-08-23 20:51:38 -070087 {
88 node_.registerPrefix(prefix, onInterest, flags);
89 }
Jeff Thompson86507bc2013-08-23 20:51:38 -070090
91 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070092 * Process any data to receive or call timeout callbacks.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070093 * This is non-blocking and will return immediately if there is no data to receive.
94 * 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.
Jeff Thompson432c8be2013-08-09 16:16:08 -070095 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
96 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
97 */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070098 void processEvents()
99 {
100 // Just call Node's processEvents.
101 node_.processEvents();
102 }
Jeff Thompson432c8be2013-08-09 16:16:08 -0700103
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700104 /**
105 * Shut down and disconnect this Face.
106 */
Jeff Thompson517ffa82013-08-05 16:04:34 -0700107 void shutdown();
108
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700109private:
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700110 Node node_;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700111};
112
113}
114
115#endif