blob: 7859000d5799f50bbda4ea468bf8baded7cbbfea [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 Thompson11095142013-10-01 16:20:28 -070049 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson4fe45512013-08-23 14:06:38 -070050 */
Jeff Thompson62992e42013-10-07 18:50:51 -070051 uint64_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070052 expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout = OnTimeout())
Jeff Thompson4fe45512013-08-23 14:06:38 -070053 {
Jeff Thompson11095142013-10-01 16:20:28 -070054 return node_.expressInterest(interest, onData, onTimeout);
Jeff Thompson4fe45512013-08-23 14:06:38 -070055 }
56
57 /**
Jeff Thompsonc172be32013-07-16 15:08:05 -070058 * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors.
Jeff Thompson7aec0252013-08-22 17:29:57 -070059 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070060 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompsonc172be32013-07-16 15:08:05 -070061 * @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 -070062 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
63 * use func_lib::ref() as appropriate.
64 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
65 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson11095142013-10-01 16:20:28 -070066 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompsonc172be32013-07-16 15:08:05 -070067 */
Jeff Thompson62992e42013-10-07 18:50:51 -070068 uint64_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070069 expressInterest(const Name& name, const Interest *interestTemplate, const OnData& onData, const OnTimeout& onTimeout = OnTimeout());
Jeff Thompson7aec0252013-08-22 17:29:57 -070070
71 /**
72 * Encode name as an Interest, using a default interest lifetime.
73 * Send the interest through the transport, read the entire response and call onData(interest, data).
Jeff Thompson4fe45512013-08-23 14:06:38 -070074 * @param name A reference to a Name for the interest. This copies the Name.
Jeff Thompson7aec0252013-08-22 17:29:57 -070075 * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to
76 * use func_lib::ref() as appropriate.
77 * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it.
78 * This copies the function object, so you may need to use func_lib::ref() as appropriate.
Jeff Thompson11095142013-10-01 16:20:28 -070079 * @return The pending interest ID which can be used with removePendingInterest.
Jeff Thompson7aec0252013-08-22 17:29:57 -070080 */
Jeff Thompson62992e42013-10-07 18:50:51 -070081 uint64_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070082 expressInterest(const Name& name, const OnData& onData, const OnTimeout& onTimeout = OnTimeout())
Jeff Thompson7aec0252013-08-22 17:29:57 -070083 {
Jeff Thompson11095142013-10-01 16:20:28 -070084 return expressInterest(name, 0, onData, onTimeout);
85 }
86
87 /**
88 * Remove the pending interest entry with the pendingInterestId from the pending interest table.
89 * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name.
90 * If there is no entry with the pendingInterestId, do nothing.
91 * @param pendingInterestId The ID returned from expressInterest.
92 */
93 void
Jeff Thompson62992e42013-10-07 18:50:51 -070094 removePendingInterest(uint64_t pendingInterestId)
Jeff Thompson11095142013-10-01 16:20:28 -070095 {
96 node_.removePendingInterest(pendingInterestId);
Jeff Thompsoncdf7e252013-07-31 12:41:47 -070097 }
98
Jeff Thompson432c8be2013-08-09 16:16:08 -070099 /**
Jeff Thompson86507bc2013-08-23 20:51:38 -0700100 * Register prefix with the connected NDN hub and call onInterest when a matching interest is received.
101 * @param prefix A reference to a Name for the prefix to register. This copies the Name.
102 * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to
103 * use func_lib::ref() as appropriate.
Jeff Thompson590ec232013-09-18 15:55:56 -0700104 * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix.
105 * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix.
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700106 * @param flags The flags for finer control of which interests are forward to the application. If omitted, use
107 * the default flags defined by the default ForwardingFlags constructor.
Jeff Thompson590ec232013-09-18 15:55:56 -0700108 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson11095142013-10-01 16:20:28 -0700109 * @return The registered prefix ID which can be used with removeRegisteredPrefix.
Jeff Thompson86507bc2013-08-23 20:51:38 -0700110 */
Jeff Thompson62992e42013-10-07 18:50:51 -0700111 uint64_t
Jeff Thompson590ec232013-09-18 15:55:56 -0700112 registerPrefix
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700113 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags = ForwardingFlags(),
Jeff Thompson6d7369b2013-09-19 14:40:21 -0700114 WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson86507bc2013-08-23 20:51:38 -0700115 {
Jeff Thompson11095142013-10-01 16:20:28 -0700116 return node_.registerPrefix(prefix, onInterest, onRegisterFailed, flags, wireFormat);
117 }
118
119 /**
120 * Remove the registered prefix entry with the registeredPrefixId from the pending interest table.
121 * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name.
122 * If there is no entry with the registeredPrefixId, do nothing.
123 * @param registeredPrefixId The ID returned from registerPrefix.
124 */
125 void
Jeff Thompson62992e42013-10-07 18:50:51 -0700126 removeRegisteredPrefix(uint64_t registeredPrefixId)
Jeff Thompson11095142013-10-01 16:20:28 -0700127 {
128 node_.removeRegisteredPrefix(registeredPrefixId);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700129 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700130
131 /**
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700132 * Process any data to receive or call timeout callbacks.
Jeff Thompsonc7e07442013-08-19 15:25:43 -0700133 * This is non-blocking and will return immediately if there is no data to receive.
134 * 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 -0700135 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
136 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
137 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700138 void
139 processEvents()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700140 {
141 // Just call Node's processEvents.
142 node_.processEvents();
143 }
Jeff Thompson432c8be2013-08-09 16:16:08 -0700144
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700145 /**
146 * Shut down and disconnect this Face.
147 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700148 void
149 shutdown();
Jeff Thompson517ffa82013-08-05 16:04:34 -0700150
Jeff Thompsonb982b6d2013-07-15 18:15:45 -0700151private:
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700152 Node node_;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700153};
154
155}
156
157#endif