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. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 49 | * 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 Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 51 | * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 52 | * @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 Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 56 | */ |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 57 | void expressInterest(const Name &name, const Interest *interestTemplate, const OnData &onData, const OnTimeout &onTimeout) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 58 | { |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 59 | 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 Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 74 | } |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 75 | |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 76 | /** |
| 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 Thompson | cdf7e25 | 2013-07-31 12:41:47 -0700 | [diff] [blame] | 85 | { |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 86 | 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 Thompson | cdf7e25 | 2013-07-31 12:41:47 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 101 | /** |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 102 | * Process any data to receive or call timeout callbacks. |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 103 | * 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 Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 105 | * @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 Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 108 | void processEvents() |
| 109 | { |
| 110 | // Just call Node's processEvents. |
| 111 | node_.processEvents(); |
| 112 | } |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 113 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Shut down and disconnect this Face. |
| 116 | */ |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 117 | void shutdown(); |
| 118 | |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 119 | private: |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 120 | Node node_; |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | } |
| 124 | |
| 125 | #endif |