blob: 4071501610af147b3943616ca6d5872bd6cbcf80 [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"
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080012#include "transport/transport.hpp"
13#include "transport/unix-transport.hpp"
Jeff Thompsonbeb8b7d2013-07-16 15:49:21 -070014
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070015namespace ndn {
16
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070017/**
18 * The Face class provides the main methods for NDN communication.
19 */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070020class Face {
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070021public:
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070022 /**
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080023 * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
24 * @param host The host of the NDN hub.
25 * @param port The port of the NDN hub. If omitted. use 6363.
26 */
27 Face()
28 : node_(ptr_lib::shared_ptr<UnixTransport>(new UnixTransport()))
29 {
30 }
31
32 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070033 * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo.
34 * @param transport A shared_ptr to a Transport object used for communication.
35 * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070036 */
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080037 Face(const ptr_lib::shared_ptr<Transport>& transport)
38 : node_(transport)
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070039 {
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070040 }
41
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070042 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -070043 * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070044 * @param host The host of the NDN hub.
Jeff Thompsonb629ddb2013-10-03 13:58:43 -070045 * @param port The port of the NDN hub. If omitted. use 6363.
Jeff Thompsonfe08e5a2013-08-13 11:15:59 -070046 */
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080047 // Face(const char *host, unsigned short port = 6363)
48 // : node_(ptr_lib::shared_ptr<TcpTransport>(new TcpTransport(host, port)))
49 // {
50 // }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070051
Jeff Thompson4fe45512013-08-23 14:06:38 -070052 /**
53 * Send the Interest through the transport, read the entire response and call onData(interest, data).
54 * @param interest A reference to the Interest. This copies the Interest.
55 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
56 * use func_lib::ref() as appropriate.
57 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
58 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson978c1522013-11-12 23:03:10 -080059 * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson11095142013-10-01 16:20:28 -070060 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -070061 */
Jeff Thompson62992e42013-10-07 18:50:51 -070062 uint64_t
Jeff Thompson978c1522013-11-12 23:03:10 -080063 expressInterest
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080064 (const Interest& interest, const OnData& onData, const OnTimeout& onTimeout = OnTimeout())
Jeff Thompson4fe45512013-08-23 14:06:38 -070065 {
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080066 return node_.expressInterest(interest, onData, onTimeout);
Jeff Thompson4fe45512013-08-23 14:06:38 -070067 }
68
69 /**
Jeff Thompsonc172be32013-07-16 15:08:05 -070070 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
Jeff Thompson7aec0252013-08-22 17:29:57 -070071 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070072 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompsonc172be32013-07-16 15:08:05 -070073 * @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 -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 Thompson978c1522013-11-12 23:03:10 -080078 * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson11095142013-10-01 16:20:28 -070079 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompsonc172be32013-07-16 15:08:05 -070080 */
Jeff Thompson62992e42013-10-07 18:50:51 -070081 uint64_t
Jeff Thompson978c1522013-11-12 23:03:10 -080082 expressInterest
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080083 (const Name& name, const Interest *interestTemplate, const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Jeff Thompson7aec0252013-08-22 17:29:57 -070084
85 /**
86 * Encode name as an Interest, using a default interest lifetime.
87 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070088 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -070089 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
90 * use func_lib::ref() as appropriate.
91 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
92 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson978c1522013-11-12 23:03:10 -080093 * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson11095142013-10-01 16:20:28 -070094 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson7aec0252013-08-22 17:29:57 -070095 */
Jeff Thompson62992e42013-10-07 18:50:51 -070096 uint64_t
Jeff Thompson978c1522013-11-12 23:03:10 -080097 expressInterest
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080098 (const Name& name, const OnData& onData, const OnTimeout& onTimeout = OnTimeout())
Jeff Thompson7aec0252013-08-22 17:29:57 -070099 {
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800100 return expressInterest(name, 0, onData, onTimeout);
Jeff Thompson11095142013-10-01 16:20:28 -0700101 }
102
103 /**
104 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
105 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
106 * If there is no entry with the pendingInterestId, do nothing.
107 * @param pendingInterestId The ID returned from expressInterest.
108 */
109 void
Jeff Thompson62992e42013-10-07 18:50:51 -0700110 removePendingInterest(uint64_t pendingInterestId)
Jeff Thompson11095142013-10-01 16:20:28 -0700111 {
112 node_.removePendingInterest(pendingInterestId);
Jeff Thompsoncdf7e252013-07-31 12:41:47 -0700113 }
114
Jeff Thompson432c8be2013-08-09 16:16:08 -0700115 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700116 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
117 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
118 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
119 * use func_lib::ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -0700120 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
121 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700122 * @param flags The flags for finer control of which interests are forward to the application. If omitted, use
123 * the default flags defined by the default ForwardingFlags constructor.
Jeff Thompson978c1522013-11-12 23:03:10 -0800124 * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson11095142013-10-01 16:20:28 -0700125 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700126 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700127 uint64_t
Alexander Afanasyevde2f6b52014-01-02 21:42:26 -0800128 setInterestFilter
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800129 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags = ForwardingFlags())
Jeff Thompson86507bc2013-08-23 20:51:38 -0700130 {
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800131 return node_.registerPrefix(prefix, onInterest, onRegisterFailed, flags);
Jeff Thompson11095142013-10-01 16:20:28 -0700132 }
133
134 /**
135 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
136 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
137 * If there is no entry with the registeredPrefixId, do nothing.
138 * @param registeredPrefixId The ID returned from registerPrefix.
139 */
140 void
Alexander Afanasyevde2f6b52014-01-02 21:42:26 -0800141 unsetInterestFilter(uint64_t registeredPrefixId)
Jeff Thompson11095142013-10-01 16:20:28 -0700142 {
143 node_.removeRegisteredPrefix(registeredPrefixId);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700144 }
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800145
146 /**
147 * @brief Publish data packet
148 *
149 * This method can be called to satisfy the incoming Interest or to put Data packet into the cache
150 * of the local NDN forwarder
151 */
152 void
153 put(const Data &data)
154 {
155 node_.put(data);
156 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700157
158 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700159 * Process any data to receive or call timeout callbacks.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800160 *
161 * This call will block forever (default timeout == 0) to process IO on the face.
162 * To exit, one expected to call face.shutdown() from one of the callback methods.
163 *
164 * If timeout is specified, then processEvents will exit after this timeout, if not stopped earlier with face.shutdown().
165 * The call can be called repeatedly, if desired.
166 *
Jeff Thompson432c8be2013-08-09 16:16:08 -0700167 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
168 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
169 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700170 void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800171 processEvents(Milliseconds timeout = 0)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700172 {
173 // Just call Node's processEvents.
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800174 node_.processEvents(timeout);
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700175 }
Jeff Thompson432c8be2013-08-09 16:16:08 -0700176
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700177 /**
178 * Shut down and disconnect this Face.
179 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700180 void
181 shutdown();
Jeff Thompson517ffa82013-08-05 16:04:34 -0700182
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700183private:
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700184 Node node_;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700185};
186
187}
188
189#endif