Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame^] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #ifndef NDN_NODE_HPP |
| 7 | #define NDN_NODE_HPP |
| 8 | |
| 9 | #include "interest.hpp" |
| 10 | #include "closure.hpp" |
| 11 | #include "transport/udp-transport.hpp" |
| 12 | #include "encoding/binary-xml-element-reader.hpp" |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
| 16 | class Face; |
| 17 | |
| 18 | class Node : public ElementListener { |
| 19 | public: |
| 20 | /** |
| 21 | * Create a new Node for communication with an NDN hub at host:port with the given Transport object. |
| 22 | * @param host The host of the NDN hub. |
| 23 | * @param port The port of the NDN hub. |
| 24 | * @param transport A pointer to a Transport object used for communication. |
| 25 | */ |
| 26 | Node(const char *host, unsigned short port, const ptr_lib::shared_ptr<Transport> &transport) |
| 27 | : host_(host), port_(port), transport_(transport), tempClosure_(0) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Create a new Node for communication with an NDN hub at host:port using the default UdpTransport. |
| 33 | * @param host The host of the NDN hub. |
| 34 | * @param port The port of the NDN hub. |
| 35 | */ |
| 36 | Node(const char *host, unsigned short port) |
| 37 | : host_(host), port_(port), transport_(new UdpTransport()), tempClosure_(0) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Create a new Node for communication with an NDN hub at host with the default port 9695 and using the default UdpTransport. |
| 43 | * @param host The host of the NDN hub. |
| 44 | */ |
| 45 | Node(const char *host) |
| 46 | : host_(host), port_(9695), transport_(new UdpTransport()), tempClosure_(0) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors. |
| 52 | * Send the interest through the transport, read the entire response and call |
| 53 | * closure->upcall(UPCALL_DATA (or UPCALL_DATA_UNVERIFIED), |
| 54 | * UpcallInfo(this, interest, 0, data)). |
| 55 | * @param name reference to a Name for the interest. This does not keep a pointer to the Name object. |
| 56 | * @param closure a pointer for the Closure. The caller must manage the memory for the Closure. This will not try to delete it. |
| 57 | * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object. |
| 58 | */ |
| 59 | void expressInterest(const Name &name, Closure *closure, const Interest *interestTemplate); |
| 60 | |
| 61 | void expressInterest(const Name &name, Closure *closure) |
| 62 | { |
| 63 | expressInterest(name, closure, 0); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Process any data to receive. For each element received, call onReceivedElement. |
| 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. |
| 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 | */ |
| 73 | void processEvents(); |
| 74 | |
| 75 | const char *getHost() const { return host_.c_str(); } |
| 76 | |
| 77 | unsigned short getPort() const { return port_; } |
| 78 | |
| 79 | const ptr_lib::shared_ptr<Transport> &getTransport() { return transport_; } |
| 80 | |
| 81 | virtual void onReceivedElement(unsigned char *element, unsigned int elementLength); |
| 82 | |
| 83 | void shutdown(); |
| 84 | |
| 85 | private: |
| 86 | ptr_lib::shared_ptr<Transport> transport_; |
| 87 | std::string host_; |
| 88 | unsigned short port_; |
| 89 | Closure *tempClosure_; |
| 90 | }; |
| 91 | |
| 92 | } |
| 93 | |
| 94 | #endif |