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 | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 9 | #include "closure.hpp" |
| 10 | #include "interest.hpp" |
| 11 | #include "transport/udp-transport.hpp" |
| 12 | #include "encoding/binary-xml-element-reader.hpp" |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 13 | |
Jeff Thompson | beb8b7d | 2013-07-16 15:49:21 -0700 | [diff] [blame] | 14 | using namespace std; |
| 15 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 16 | namespace ndn { |
| 17 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 18 | /** |
| 19 | * The Face class provides the main methods for NDN communication. |
| 20 | */ |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 21 | class Face : public ElementListener { |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 22 | public: |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Create a new Face for communication with an NDN hub at host:port with the given Transport object. |
| 25 | * @param host The host of the NDN hub. |
| 26 | * @param port The port of the NDN hub. |
| 27 | * @param transport A pointer to a Transport object used for communication. |
| 28 | */ |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 29 | Face(const char *host, unsigned short port, const ptr_lib::shared_ptr<Transport> &transport) |
Jeff Thompson | 2a4724b | 2013-08-07 17:13:34 -0700 | [diff] [blame] | 30 | : host_(host), port_(port), transport_(transport), tempClosure_(0) |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 31 | { |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Create a new Face for communication with an NDN hub at host:port using the default UdpTransport. |
| 36 | * @param host The host of the NDN hub. |
| 37 | * @param port The port of the NDN hub. |
| 38 | */ |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 39 | Face(const char *host, unsigned short port) |
Jeff Thompson | 2a4724b | 2013-08-07 17:13:34 -0700 | [diff] [blame] | 40 | : host_(host), port_(port), transport_(new UdpTransport()), tempClosure_(0) |
Jeff Thompson | b7d059d | 2013-07-31 10:59:51 -0700 | [diff] [blame] | 41 | { |
| 42 | } |
Jeff Thompson | 1242b1f | 2013-07-31 11:02:00 -0700 | [diff] [blame] | 43 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Create a new Face for communication with an NDN hub at host with the default port 9695 and using the default UdpTransport. |
| 46 | * @param host The host of the NDN hub. |
| 47 | */ |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 48 | Face(const char *host) |
Jeff Thompson | 2a4724b | 2013-08-07 17:13:34 -0700 | [diff] [blame] | 49 | : host_(host), port_(9695), transport_(new UdpTransport()), tempClosure_(0) |
Jeff Thompson | 1242b1f | 2013-07-31 11:02:00 -0700 | [diff] [blame] | 50 | { |
| 51 | } |
Jeff Thompson | b7d059d | 2013-07-31 10:59:51 -0700 | [diff] [blame] | 52 | |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors. |
| 55 | * Send the interest through the transport, read the entire response and call |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 56 | * closure->upcall(UPCALL_DATA (or UPCALL_DATA_UNVERIFIED), |
| 57 | * UpcallInfo(this, interest, 0, data)). |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 58 | * @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] | 59 | * @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] | 60 | * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object. |
| 61 | */ |
Jeff Thompson | 2a4724b | 2013-08-07 17:13:34 -0700 | [diff] [blame] | 62 | void expressInterest(const Name &name, Closure *closure, const Interest *interestTemplate); |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 63 | |
Jeff Thompson | 2a4724b | 2013-08-07 17:13:34 -0700 | [diff] [blame] | 64 | void expressInterest(const Name &name, Closure *closure) |
Jeff Thompson | cdf7e25 | 2013-07-31 12:41:47 -0700 | [diff] [blame] | 65 | { |
| 66 | expressInterest(name, closure, 0); |
| 67 | } |
| 68 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Process any data to receive. For each element received, call face.onReceivedElement. |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame^] | 71 | * This is non-blocking and will return immediately if there is no data to receive. |
| 72 | * 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] | 73 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 74 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 75 | */ |
| 76 | void processEvents(); |
| 77 | |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 78 | void shutdown(); |
| 79 | |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 80 | const char *getHost() const { return host_.c_str(); } |
| 81 | |
| 82 | unsigned short getPort() const { return port_; } |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 83 | |
Jeff Thompson | c49cb32 | 2013-08-08 17:15:46 -0700 | [diff] [blame] | 84 | const ptr_lib::shared_ptr<Transport> &getTransport() { return transport_; } |
Jeff Thompson | ab13ac7 | 2013-08-01 10:37:21 -0700 | [diff] [blame] | 85 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 86 | virtual void onReceivedElement(unsigned char *element, unsigned int elementLength); |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 87 | |
| 88 | private: |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 89 | ptr_lib::shared_ptr<Transport> transport_; |
Jeff Thompson | beb8b7d | 2013-07-16 15:49:21 -0700 | [diff] [blame] | 90 | string host_; |
| 91 | unsigned short port_; |
Jeff Thompson | 2a4724b | 2013-08-07 17:13:34 -0700 | [diff] [blame] | 92 | Closure *tempClosure_; |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | #endif |