blob: 8db72a89c3b3fcd053f85301bce687489162ef9d [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 /**
29 * Create a new Face for communication with an NDN hub at host:port using the default UdpTransport.
30 * @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 /**
39 * Create a new Face for communication with an NDN hub at host with the default port 9695 and using the default UdpTransport.
40 * @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 Thompsonb7d059d2013-07-31 10:59:51 -070046
Jeff Thompsonc172be32013-07-16 15:08:05 -070047 /**
48 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
Jeff Thompson7aec0252013-08-22 17:29:57 -070049 * Send the interest through the transport, read the entire response and call onData(interest, data).
50 * @param name A reference to a Name for the interest. This does not keep a pointer to the Name object.
Jeff Thompsonc172be32013-07-16 15:08:05 -070051 * @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 -070052 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
53 * use func_lib::ref() as appropriate.
54 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
55 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompsonc172be32013-07-16 15:08:05 -070056 */
Jeff Thompson7aec0252013-08-22 17:29:57 -070057 void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData, const OnTimeout &onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070058 {
Jeff Thompson7aec0252013-08-22 17:29:57 -070059 node_.expressInterest(name, interestTemplate, onData, onTimeout);
60 }
61
62 /**
63 * Encode name as an Interest, using a default interest lifetime.
64 * Send the interest through the transport, read the entire response and call onData(interest, data).
65 * @param name A reference to a Name for the interest. This does not keep a pointer to the Name object.
66 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
67 * use func_lib::ref() as appropriate.
68 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
69 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
70 */
71 void expressInterest(const Name &name, const OnData &onData, const OnTimeout &onTimeout)
72 {
73 node_.expressInterest(name, onData, onTimeout);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070074 }
Jeff Thompsonc172be32013-07-16 15:08:05 -070075
Jeff Thompson7aec0252013-08-22 17:29:57 -070076 /**
77 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
78 * Send the interest through the transport, read the entire response and call onData(interest, data).
79 * @param name A reference to a Name for the interest. This does not keep a pointer to the Name object.
80 * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object.
81 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
82 * use func_lib::ref() as appropriate.
83 */
84 void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData)
Jeff Thompsoncdf7e252013-07-31 12:41:47 -070085 {
Jeff Thompson7aec0252013-08-22 17:29:57 -070086 node_.expressInterest(name, interestTemplate, onData);
87 }
88
89 /**
90 * Encode name as an Interest, using a default interest lifetime.
91 * Send the interest through the transport, read the entire response and call onData(interest, data).
92 * @param name A reference to a Name for the interest. This does not keep a pointer to the Name object.
93 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
94 * use func_lib::ref() as appropriate.
95 */
96 void expressInterest(const Name &name, const OnData &onData)
97 {
98 node_.expressInterest(name, onData);
Jeff Thompsoncdf7e252013-07-31 12:41:47 -070099 }
100
Jeff Thompson432c8be2013-08-09 16:16:08 -0700101 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700102 * Process any data to receive or call timeout callbacks.
Jeff Thompsonc7e07442013-08-19 15:25:43 -0700103 * This is non-blocking and will return immediately if there is no data to receive.
104 * 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 -0700105 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
106 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
107 */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700108 void processEvents()
109 {
110 // Just call Node's processEvents.
111 node_.processEvents();
112 }
Jeff Thompson432c8be2013-08-09 16:16:08 -0700113
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700114 /**
115 * Shut down and disconnect this Face.
116 */
Jeff Thompson517ffa82013-08-05 16:04:34 -0700117 void shutdown();
118
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700119private:
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700120 Node node_;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700121};
122
123}
124
125#endif