blob: 8eab12a2f03a6f813b07394667178a541d2bba19 [file] [log] [blame]
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07004 * See COPYING for copyright and distribution information.
5 */
6
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -07007#ifndef NDN_FACE_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07008#define NDN_FACE_HPP
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07009
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070010#include "node.hpp"
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070011#include "transport/tcp-transport.hpp"
Jeff Thompsonbeb8b7d2013-07-16 15:49:21 -070012
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070013namespace ndn {
14
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070015/**
16 * The Face class provides the main methods for NDN communication.
17 */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070018class Face {
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070019public:
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070020 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070021 * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo.
22 * @param transport A shared_ptr to a Transport object used for communication.
23 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070024 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070025 Face(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo)
Jeff Thompson10e34382013-08-22 13:34:46 -070026 : node_(transport, connectionInfo)
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070027 {
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070028 }
29
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070030 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070031 * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070032 * @param host The host of the NDN hub.
Jeff Thompsonb629ddb2013-10-03 13:58:43 -070033 * @param port The port of the NDN hub. If omitted. use 6363.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070034 */
Jeff Thompsonb629ddb2013-10-03 13:58:43 -070035 Face(const char *host, unsigned short port = 6363)
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070036 : node_(ptr_lib::make_shared<TcpTransport>(),
37 ptr_lib::make_shared<TcpTransport::ConnectionInfo>(host, port))
Jeff Thompsonb7d059d2013-07-31 10:59:51 -070038 {
39 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070040
Jeff Thompson4fe45512013-08-23 14:06:38 -070041 /**
42 * Send the Interest through the transport, read the entire response and call onData(interest, data).
43 * @param interest A reference to the Interest. This copies the Interest.
44 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
45 * use func_lib::ref() as appropriate.
46 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
47 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson11095142013-10-01 16:20:28 -070048 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -070049 */
Jeff Thompson62992e42013-10-07 18:50:51 -070050 uint64_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070051 expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout = OnTimeout())
Jeff Thompson4fe45512013-08-23 14:06:38 -070052 {
Jeff Thompson11095142013-10-01 16:20:28 -070053 return node_.expressInterest(interest, onData, onTimeout);
Jeff Thompson4fe45512013-08-23 14:06:38 -070054 }
55
56 /**
Jeff Thompsonc172be32013-07-16 15:08:05 -070057 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
Jeff Thompson7aec0252013-08-22 17:29:57 -070058 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070059 * @param name A reference to a Name for the interest. This copies the Name.
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.
Jeff Thompson7aec0252013-08-22 17:29:57 -070061 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
62 * use func_lib::ref() as appropriate.
63 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
64 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson11095142013-10-01 16:20:28 -070065 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompsonc172be32013-07-16 15:08:05 -070066 */
Jeff Thompson62992e42013-10-07 18:50:51 -070067 uint64_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070068 expressInterest(const Name& name, const Interest *interestTemplate, const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Jeff Thompson7aec0252013-08-22 17:29:57 -070069
70 /**
71 * Encode name as an Interest, using a default interest lifetime.
72 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070073 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -070074 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
75 * use func_lib::ref() as appropriate.
76 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
77 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson11095142013-10-01 16:20:28 -070078 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson7aec0252013-08-22 17:29:57 -070079 */
Jeff Thompson62992e42013-10-07 18:50:51 -070080 uint64_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070081 expressInterest(const Name& name, const OnData& onData, const OnTimeout& onTimeout = OnTimeout())
Jeff Thompson7aec0252013-08-22 17:29:57 -070082 {
Jeff Thompson11095142013-10-01 16:20:28 -070083 return expressInterest(name, 0, onData, onTimeout);
84 }
85
86 /**
87 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
88 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
89 * If there is no entry with the pendingInterestId, do nothing.
90 * @param pendingInterestId The ID returned from expressInterest.
91 */
92 void
Jeff Thompson62992e42013-10-07 18:50:51 -070093 removePendingInterest(uint64_t pendingInterestId)
Jeff Thompson11095142013-10-01 16:20:28 -070094 {
95 node_.removePendingInterest(pendingInterestId);
Jeff Thompsoncdf7e252013-07-31 12:41:47 -070096 }
97
Jeff Thompson432c8be2013-08-09 16:16:08 -070098 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070099 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
100 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
101 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
102 * use func_lib::ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -0700103 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
104 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700105 * @param flags The flags for finer control of which interests are forward to the application. If omitted, use
106 * the default flags defined by the default ForwardingFlags constructor.
Jeff Thompson590ec232013-09-18 15:55:56 -0700107 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson11095142013-10-01 16:20:28 -0700108 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700109 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700110 uint64_t
Jeff Thompson590ec232013-09-18 15:55:56 -0700111 registerPrefix
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700112 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags = ForwardingFlags(),
Jeff Thompson6d7369b2013-09-19 14:40:21 -0700113 WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson86507bc2013-08-23 20:51:38 -0700114 {
Jeff Thompson11095142013-10-01 16:20:28 -0700115 return node_.registerPrefix(prefix, onInterest, onRegisterFailed, flags, wireFormat);
116 }
117
118 /**
119 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
120 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
121 * If there is no entry with the registeredPrefixId, do nothing.
122 * @param registeredPrefixId The ID returned from registerPrefix.
123 */
124 void
Jeff Thompson62992e42013-10-07 18:50:51 -0700125 removeRegisteredPrefix(uint64_t registeredPrefixId)
Jeff Thompson11095142013-10-01 16:20:28 -0700126 {
127 node_.removeRegisteredPrefix(registeredPrefixId);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700128 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700129
130 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700131 * Process any data to receive or call timeout callbacks.
Jeff Thompsonc7e07442013-08-19 15:25:43 -0700132 * This is non-blocking and will return immediately if there is no data to receive.
133 * 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 Thompson432c8be2013-08-09 16:16:08 -0700134 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
135 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
136 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700137 void
138 processEvents()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700139 {
140 // Just call Node's processEvents.
141 node_.processEvents();
142 }
Jeff Thompson432c8be2013-08-09 16:16:08 -0700143
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700144 /**
145 * Shut down and disconnect this Face.
146 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700147 void
148 shutdown();
Jeff Thompson517ffa82013-08-05 16:04:34 -0700149
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700150private:
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700151 Node node_;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700152};
153
154}
155
156#endif