Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 6 | #ifndef NDN_FACE_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 7 | #define NDN_FACE_HPP |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 9 | #include "node.hpp" |
Jeff Thompson | beb8b7d | 2013-07-16 15:49:21 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 11 | namespace ndn { |
| 12 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 13 | /** |
| 14 | * The Face class provides the main methods for NDN communication. |
| 15 | */ |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 16 | class Face { |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 17 | public: |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 18 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 19 | * 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 Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 22 | */ |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 23 | Face(const ptr_lib::shared_ptr<Transport> &transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo> &connectionInfo) |
| 24 | : node_(transport, connectionInfo) |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 25 | { |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 28 | /** |
| 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 Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 33 | Face(const char *host, unsigned short port) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 34 | : node_(host, port) |
Jeff Thompson | b7d059d | 2013-07-31 10:59:51 -0700 | [diff] [blame] | 35 | { |
| 36 | } |
Jeff Thompson | 1242b1f | 2013-07-31 11:02:00 -0700 | [diff] [blame] | 37 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 38 | /** |
| 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 Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 42 | Face(const char *host) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 43 | : node_(host) |
Jeff Thompson | 1242b1f | 2013-07-31 11:02:00 -0700 | [diff] [blame] | 44 | { |
| 45 | } |
Jeff Thompson | b7d059d | 2013-07-31 10:59:51 -0700 | [diff] [blame] | 46 | |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors. |
| 49 | * Send the interest through the transport, read the entire response and call |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 50 | * closure->upcall(UPCALL_DATA (or UPCALL_DATA_UNVERIFIED), |
| 51 | * UpcallInfo(this, interest, 0, data)). |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 52 | * @param name reference to a Name for the interest. This does not keep a pointer to the Name object. |
Jeff Thompson | 2a4724b | 2013-08-07 17:13:34 -0700 | [diff] [blame] | 53 | * @param closure a pointer for the Closure. The caller must manage the memory for the Closure. This will not try to delete it. |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 54 | * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object. |
| 55 | */ |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 56 | void expressInterest(const Name &name, Closure *closure, const Interest *interestTemplate) |
| 57 | { |
| 58 | node_.expressInterest(name, closure, interestTemplate); |
| 59 | } |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 60 | |
Jeff Thompson | 2a4724b | 2013-08-07 17:13:34 -0700 | [diff] [blame] | 61 | void expressInterest(const Name &name, Closure *closure) |
Jeff Thompson | cdf7e25 | 2013-07-31 12:41:47 -0700 | [diff] [blame] | 62 | { |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 63 | node_.expressInterest(name, closure); |
Jeff Thompson | cdf7e25 | 2013-07-31 12:41:47 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 66 | /** |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 67 | * Process any data to receive or call timeout callbacks. |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 68 | * This is non-blocking and will return immediately if there is no data to receive. |
| 69 | * 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 Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 70 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 71 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 72 | */ |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 73 | void processEvents() |
| 74 | { |
| 75 | // Just call Node's processEvents. |
| 76 | node_.processEvents(); |
| 77 | } |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 78 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Shut down and disconnect this Face. |
| 81 | */ |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 82 | void shutdown(); |
| 83 | |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 84 | private: |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 85 | Node node_; |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } |
| 89 | |
| 90 | #endif |