blob: de317c601f72768b1f5d4bd8e550e4c05177609a [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -07008#ifndef NDN_FACE_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07009#define NDN_FACE_HPP
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070010
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070011#include "node.hpp"
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070012#include "transport/tcp-transport.hpp"
Jeff Thompsonbeb8b7d2013-07-16 15:49:21 -070013
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070014namespace ndn {
15
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070016/**
17 * The Face class provides the main methods for NDN communication.
18 */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070019class Face {
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070020public:
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070021 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070022 * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo.
23 * @param transport A shared_ptr to a Transport object used for communication.
24 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070025 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070026 Face(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo)
Jeff Thompson10e34382013-08-22 13:34:46 -070027 : node_(transport, connectionInfo)
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070028 {
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070029 }
30
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070031 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070032 * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070033 * @param host The host of the NDN hub.
Jeff Thompsonb629ddb2013-10-03 13:58:43 -070034 * @param port The port of the NDN hub. If omitted. use 6363.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070035 */
Jeff Thompsonb629ddb2013-10-03 13:58:43 -070036 Face(const char *host, unsigned short port = 6363)
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070037 : node_(ptr_lib::make_shared<TcpTransport>(),
38 ptr_lib::make_shared<TcpTransport::ConnectionInfo>(host, port))
Jeff Thompsonb7d059d2013-07-31 10:59:51 -070039 {
40 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070041
Jeff Thompson4fe45512013-08-23 14:06:38 -070042 /**
43 * Send the Interest through the transport, read the entire response and call onData(interest, data).
44 * @param interest A reference to the Interest. This copies the Interest.
45 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
46 * use func_lib::ref() as appropriate.
47 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
48 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson978c1522013-11-12 23:03:10 -080049 * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson11095142013-10-01 16:20:28 -070050 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -070051 */
Jeff Thompson62992e42013-10-07 18:50:51 -070052 uint64_t
Jeff Thompson978c1522013-11-12 23:03:10 -080053 expressInterest
54 (const Interest& interest, const OnData& onData, const OnTimeout& onTimeout = OnTimeout(),
55 WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson4fe45512013-08-23 14:06:38 -070056 {
Jeff Thompson978c1522013-11-12 23:03:10 -080057 return node_.expressInterest(interest, onData, onTimeout, wireFormat);
Jeff Thompson4fe45512013-08-23 14:06:38 -070058 }
59
60 /**
Jeff Thompsonc172be32013-07-16 15:08:05 -070061 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
Jeff Thompson7aec0252013-08-22 17:29:57 -070062 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070063 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompsonc172be32013-07-16 15:08:05 -070064 * @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 -070065 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
66 * use func_lib::ref() as appropriate.
67 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
68 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson978c1522013-11-12 23:03:10 -080069 * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson11095142013-10-01 16:20:28 -070070 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompsonc172be32013-07-16 15:08:05 -070071 */
Jeff Thompson62992e42013-10-07 18:50:51 -070072 uint64_t
Jeff Thompson978c1522013-11-12 23:03:10 -080073 expressInterest
74 (const Name& name, const Interest *interestTemplate, const OnData& onData, const OnTimeout& onTimeout = OnTimeout(),
75 WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompson7aec0252013-08-22 17:29:57 -070076
77 /**
78 * Encode name as an Interest, using a default interest lifetime.
79 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070080 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -070081 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
82 * use func_lib::ref() as appropriate.
83 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
84 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson978c1522013-11-12 23:03:10 -080085 * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson11095142013-10-01 16:20:28 -070086 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson7aec0252013-08-22 17:29:57 -070087 */
Jeff Thompson62992e42013-10-07 18:50:51 -070088 uint64_t
Jeff Thompson978c1522013-11-12 23:03:10 -080089 expressInterest
90 (const Name& name, const OnData& onData, const OnTimeout& onTimeout = OnTimeout(),
91 WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson7aec0252013-08-22 17:29:57 -070092 {
Jeff Thompson978c1522013-11-12 23:03:10 -080093 return expressInterest(name, 0, onData, onTimeout, wireFormat);
Jeff Thompson11095142013-10-01 16:20:28 -070094 }
95
96 /**
97 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
98 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
99 * If there is no entry with the pendingInterestId, do nothing.
100 * @param pendingInterestId The ID returned from expressInterest.
101 */
102 void
Jeff Thompson62992e42013-10-07 18:50:51 -0700103 removePendingInterest(uint64_t pendingInterestId)
Jeff Thompson11095142013-10-01 16:20:28 -0700104 {
105 node_.removePendingInterest(pendingInterestId);
Jeff Thompsoncdf7e252013-07-31 12:41:47 -0700106 }
107
Jeff Thompson432c8be2013-08-09 16:16:08 -0700108 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700109 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
110 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
111 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
112 * use func_lib::ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -0700113 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
114 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700115 * @param flags The flags for finer control of which interests are forward to the application. If omitted, use
116 * the default flags defined by the default ForwardingFlags constructor.
Jeff Thompson978c1522013-11-12 23:03:10 -0800117 * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson11095142013-10-01 16:20:28 -0700118 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700119 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700120 uint64_t
Jeff Thompson590ec232013-09-18 15:55:56 -0700121 registerPrefix
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700122 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags = ForwardingFlags(),
Jeff Thompson6d7369b2013-09-19 14:40:21 -0700123 WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson86507bc2013-08-23 20:51:38 -0700124 {
Jeff Thompson11095142013-10-01 16:20:28 -0700125 return node_.registerPrefix(prefix, onInterest, onRegisterFailed, flags, wireFormat);
126 }
127
128 /**
129 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
130 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
131 * If there is no entry with the registeredPrefixId, do nothing.
132 * @param registeredPrefixId The ID returned from registerPrefix.
133 */
134 void
Jeff Thompson62992e42013-10-07 18:50:51 -0700135 removeRegisteredPrefix(uint64_t registeredPrefixId)
Jeff Thompson11095142013-10-01 16:20:28 -0700136 {
137 node_.removeRegisteredPrefix(registeredPrefixId);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700138 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700139
140 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700141 * Process any data to receive or call timeout callbacks.
Jeff Thompsonc7e07442013-08-19 15:25:43 -0700142 * This is non-blocking and will return immediately if there is no data to receive.
143 * 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 -0700144 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
145 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
146 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700147 void
148 processEvents()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700149 {
150 // Just call Node's processEvents.
151 node_.processEvents();
152 }
Jeff Thompson432c8be2013-08-09 16:16:08 -0700153
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700154 /**
155 * Shut down and disconnect this Face.
156 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700157 void
158 shutdown();
Jeff Thompson517ffa82013-08-05 16:04:34 -0700159
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700160private:
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700161 Node node_;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700162};
163
164}
165
166#endif