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 | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame^] | 46 | |
| 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 Thompson | b7d059d | 2013-07-31 10:59:51 -0700 | [diff] [blame] | 69 | |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 70 | /** |
| 71 | * 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] | 72 | * Send the interest through the transport, read the entire response and call onData(interest, data). |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame^] | 73 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 74 | * @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] | 75 | * @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 Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 79 | */ |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 80 | 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] | 81 | { |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 82 | 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 Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame^] | 88 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 89 | * @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 Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 97 | } |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 98 | |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 99 | /** |
| 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 Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame^] | 102 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 103 | * @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 Thompson | cdf7e25 | 2013-07-31 12:41:47 -0700 | [diff] [blame] | 108 | { |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 109 | 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 Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame^] | 115 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 116 | * @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 Thompson | cdf7e25 | 2013-07-31 12:41:47 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 124 | /** |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 125 | * Process any data to receive or call timeout callbacks. |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 126 | * This is non-blocking and will return immediately if there is no data to receive. |
| 127 | * 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] | 128 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 129 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 130 | */ |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 131 | void processEvents() |
| 132 | { |
| 133 | // Just call Node's processEvents. |
| 134 | node_.processEvents(); |
| 135 | } |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 136 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 137 | /** |
| 138 | * Shut down and disconnect this Face. |
| 139 | */ |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 140 | void shutdown(); |
| 141 | |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 142 | private: |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 143 | Node node_; |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | } |
| 147 | |
| 148 | #endif |