blob: 0b7aa54826ffc446aa0ed2db1db65710e2e8a2ab [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 Thompsonbeb8b7d2013-07-16 15:49:21 -070010
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070011namespace ndn {
12
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070013/**
14 * The Face class provides the main methods for NDN communication.
15 */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070016class Face {
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070017public:
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070018 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070019 * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo.
20 * @param transport A shared_ptr to a Transport object used for communication.
21 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070022 */
Jeff Thompson10e34382013-08-22 13:34:46 -070023 Face(const ptr_lib::shared_ptr<Transport> &transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &connectionInfo)
24 : node_(transport, connectionInfo)
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070025 {
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070026 }
27
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070028 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070029 * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070030 * @param host The host of the NDN hub.
31 * @param port The port of the NDN hub.
32 */
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -070033 Face(const char *host, unsigned short port)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070034 : node_(host, port)
Jeff Thompsonb7d059d2013-07-31 10:59:51 -070035 {
36 }
Jeff Thompson1242b1f2013-07-31 11:02:00 -070037
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070038 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070039 * Create a new Face for communication with an NDN hub at host with the default port 9695 and using the default TcpTransport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070040 * @param host The host of the NDN hub.
41 */
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -070042 Face(const char *host)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070043 : node_(host)
Jeff Thompson1242b1f2013-07-31 11:02:00 -070044 {
45 }
Jeff Thompson4fe45512013-08-23 14:06:38 -070046
47 /**
48 * Send the Interest through the transport, read the entire response and call onData(interest, data).
49 * @param interest A reference to the Interest. This copies the Interest.
50 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
51 * use func_lib::ref() as appropriate.
52 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
53 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
54 */
55 void expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout)
56 {
57 node_.expressInterest(interest, onData, onTimeout);
58 }
59
60 /**
61 * Send the Interest through the transport, read the entire response and call onData(interest, data).
62 * @param interest A reference to the Interest. This copies the Interest.
63 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
64 * use func_lib::ref() as appropriate.
65 */
66 void expressInterest(const Interest &interest, const OnData &onData) {
67 node_.expressInterest(interest, onData);
68 }
Jeff Thompsonb7d059d2013-07-31 10:59:51 -070069
Jeff Thompsonc172be32013-07-16 15:08:05 -070070 /**
71 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
Jeff Thompson7aec0252013-08-22 17:29:57 -070072 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070073 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompsonc172be32013-07-16 15:08:05 -070074 * @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 -070075 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
76 * use func_lib::ref() as appropriate.
77 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
78 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompsonc172be32013-07-16 15:08:05 -070079 */
Jeff Thompson7aec0252013-08-22 17:29:57 -070080 void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData, const OnTimeout &onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070081 {
Jeff Thompson7aec0252013-08-22 17:29:57 -070082 node_.expressInterest(name, interestTemplate, onData, onTimeout);
83 }
84
85 /**
86 * Encode name as an Interest, using a default interest lifetime.
87 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070088 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -070089 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
90 * use func_lib::ref() as appropriate.
91 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
92 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
93 */
94 void expressInterest(const Name &name, const OnData &onData, const OnTimeout &onTimeout)
95 {
96 node_.expressInterest(name, onData, onTimeout);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070097 }
Jeff Thompsonc172be32013-07-16 15:08:05 -070098
Jeff Thompson7aec0252013-08-22 17:29:57 -070099 /**
100 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
101 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -0700102 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700103 * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object.
104 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
105 * use func_lib::ref() as appropriate.
106 */
107 void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData)
Jeff Thompsoncdf7e252013-07-31 12:41:47 -0700108 {
Jeff Thompson7aec0252013-08-22 17:29:57 -0700109 node_.expressInterest(name, interestTemplate, onData);
110 }
111
112 /**
113 * Encode name as an Interest, using a default interest lifetime.
114 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -0700115 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -0700116 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
117 * use func_lib::ref() as appropriate.
118 */
119 void expressInterest(const Name &name, const OnData &onData)
120 {
121 node_.expressInterest(name, onData);
Jeff Thompsoncdf7e252013-07-31 12:41:47 -0700122 }
123
Jeff Thompson432c8be2013-08-09 16:16:08 -0700124 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700125 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
126 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
127 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
128 * use func_lib::ref() as appropriate.
129 * @param flags The flags for finer control of which interests are forward to the application.
130 */
131 void registerPrefix(const Name &prefix, const OnInterest &onInterest, int flags)
132 {
133 node_.registerPrefix(prefix, onInterest, flags);
134 }
135
136 /**
137 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
138 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
139 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
140 * use func_lib::ref() as appropriate.
141 */
142 void registerPrefix(const Name &prefix, const OnInterest &onInterest)
143 {
144 node_.registerPrefix(prefix, onInterest);
145 }
146
147 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700148 * Process any data to receive or call timeout callbacks.
Jeff Thompsonc7e07442013-08-19 15:25:43 -0700149 * This is non-blocking and will return immediately if there is no data to receive.
150 * 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 -0700151 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
152 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
153 */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700154 void processEvents()
155 {
156 // Just call Node's processEvents.
157 node_.processEvents();
158 }
Jeff Thompson432c8be2013-08-09 16:16:08 -0700159
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700160 /**
161 * Shut down and disconnect this Face.
162 */
Jeff Thompson517ffa82013-08-05 16:04:34 -0700163 void shutdown();
164
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700165private:
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700166 Node node_;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700167};
168
169}
170
171#endif