blob: e0c1598c1c2849ceba5509ca967d6caa57754a24 [file] [log] [blame]
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -07006#ifndef NDN_FACE_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07007#define NDN_FACE_HPP
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07008
Jeff Thompson53412192013-08-06 13:35:50 -07009#include "closure.hpp"
10#include "interest.hpp"
11#include "transport/udp-transport.hpp"
12#include "encoding/binary-xml-element-reader.hpp"
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070013
Jeff Thompsonbeb8b7d2013-07-16 15:49:21 -070014using namespace std;
15
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070016namespace ndn {
17
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070018/**
19 * The Face class provides the main methods for NDN communication.
20 */
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -070021class Face : public ElementListener {
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070022public:
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070023 /**
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 Thompsonb9e3c8e2013-08-02 11:42:51 -070029 Face(const char *host, unsigned short port, const ptr_lib::shared_ptr<Transport> &transport)
Jeff Thompson2a4724b2013-08-07 17:13:34 -070030 : host_(host), port_(port), transport_(transport), tempClosure_(0)
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070031 {
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070032 }
33
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070034 /**
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 Thompsonb9e3c8e2013-08-02 11:42:51 -070039 Face(const char *host, unsigned short port)
Jeff Thompson2a4724b2013-08-07 17:13:34 -070040 : host_(host), port_(port), transport_(new UdpTransport()), tempClosure_(0)
Jeff Thompsonb7d059d2013-07-31 10:59:51 -070041 {
42 }
Jeff Thompson1242b1f2013-07-31 11:02:00 -070043
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070044 /**
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 Thompsonb9e3c8e2013-08-02 11:42:51 -070048 Face(const char *host)
Jeff Thompson2a4724b2013-08-07 17:13:34 -070049 : host_(host), port_(9695), transport_(new UdpTransport()), tempClosure_(0)
Jeff Thompson1242b1f2013-07-31 11:02:00 -070050 {
51 }
Jeff Thompsonb7d059d2013-07-31 10:59:51 -070052
Jeff Thompsonc172be32013-07-16 15:08:05 -070053 /**
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 Thompson56ec9e22013-08-02 11:34:07 -070056 * closure->upcall(UPCALL_DATA (or UPCALL_DATA_UNVERIFIED),
57 * UpcallInfo(this, interest, 0, data)).
Jeff Thompsonc172be32013-07-16 15:08:05 -070058 * @param name reference to a Name for the interest. This does not keep a pointer to the Name object.
Jeff Thompson2a4724b2013-08-07 17:13:34 -070059 * @param closure a pointer for the Closure. The caller must manage the memory for the Closure. This will not try to delete it.
Jeff Thompsonc172be32013-07-16 15:08:05 -070060 * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object.
61 */
Jeff Thompson2a4724b2013-08-07 17:13:34 -070062 void expressInterest(const Name &name, Closure *closure, const Interest *interestTemplate);
Jeff Thompsonc172be32013-07-16 15:08:05 -070063
Jeff Thompson2a4724b2013-08-07 17:13:34 -070064 void expressInterest(const Name &name, Closure *closure)
Jeff Thompsoncdf7e252013-07-31 12:41:47 -070065 {
66 expressInterest(name, closure, 0);
67 }
68
Jeff Thompson432c8be2013-08-09 16:16:08 -070069 /**
70 * Process any data to receive. For each element received, call face.onReceivedElement.
71 * This is non-blocking and will silently time out after a brief period if there is no data to receive.
72 * You should repeatedly call this from an event loop.
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 Thompson517ffa82013-08-05 16:04:34 -070078 void shutdown();
79
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070080 const char *getHost() const { return host_.c_str(); }
81
82 unsigned short getPort() const { return port_; }
Jeff Thompsonc172be32013-07-16 15:08:05 -070083
Jeff Thompsonc49cb322013-08-08 17:15:46 -070084 const ptr_lib::shared_ptr<Transport> &getTransport() { return transport_; }
Jeff Thompsonab13ac72013-08-01 10:37:21 -070085
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070086 virtual void onReceivedElement(unsigned char *element, unsigned int elementLength);
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070087
88private:
Jeff Thompsonc172be32013-07-16 15:08:05 -070089 ptr_lib::shared_ptr<Transport> transport_;
Jeff Thompsonbeb8b7d2013-07-16 15:49:21 -070090 string host_;
91 unsigned short port_;
Jeff Thompson2a4724b2013-08-07 17:13:34 -070092 Closure *tempClosure_;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070093};
94
95}
96
97#endif