Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 8 | #ifndef NDN_FACE_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 9 | #define NDN_FACE_HPP |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 11 | #include "node.hpp" |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 12 | #include "transport/tcp-transport.hpp" |
Jeff Thompson | beb8b7d | 2013-07-16 15:49:21 -0700 | [diff] [blame] | 13 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 14 | namespace ndn { |
| 15 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 16 | /** |
| 17 | * The Face class provides the main methods for NDN communication. |
| 18 | */ |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 19 | class Face { |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 20 | public: |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 21 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 22 | * 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 Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 25 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 26 | Face(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo) |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 27 | : node_(transport, connectionInfo) |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 28 | { |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 31 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 32 | * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport. |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 33 | * @param host The host of the NDN hub. |
Jeff Thompson | b629ddb | 2013-10-03 13:58:43 -0700 | [diff] [blame] | 34 | * @param port The port of the NDN hub. If omitted. use 6363. |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 35 | */ |
Jeff Thompson | b629ddb | 2013-10-03 13:58:43 -0700 | [diff] [blame] | 36 | Face(const char *host, unsigned short port = 6363) |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 37 | : node_(ptr_lib::make_shared<TcpTransport>(), |
| 38 | ptr_lib::make_shared<TcpTransport::ConnectionInfo>(host, port)) |
Jeff Thompson | b7d059d | 2013-07-31 10:59:51 -0700 | [diff] [blame] | 39 | { |
| 40 | } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 41 | |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 42 | /** |
| 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 Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 49 | * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat(). |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 50 | * @return The pending interest ID which can be used with removePendingInterest. |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 51 | */ |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 52 | uint64_t |
Jeff Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 53 | expressInterest |
| 54 | (const Interest& interest, const OnData& onData, const OnTimeout& onTimeout = OnTimeout(), |
| 55 | WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 56 | { |
Jeff Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 57 | return node_.expressInterest(interest, onData, onTimeout, wireFormat); |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 61 | * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 62 | * Send the interest through the transport, read the entire response and call onData(interest, data). |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 63 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 64 | * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 65 | * @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 Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 69 | * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat(). |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 70 | * @return The pending interest ID which can be used with removePendingInterest. |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 71 | */ |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 72 | uint64_t |
Jeff Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 73 | expressInterest |
| 74 | (const Name& name, const Interest *interestTemplate, const OnData& onData, const OnTimeout& onTimeout = OnTimeout(), |
| 75 | WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()); |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 76 | |
| 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 Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 80 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 81 | * @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 Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 85 | * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat(). |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 86 | * @return The pending interest ID which can be used with removePendingInterest. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 87 | */ |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 88 | uint64_t |
Jeff Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 89 | expressInterest |
| 90 | (const Name& name, const OnData& onData, const OnTimeout& onTimeout = OnTimeout(), |
| 91 | WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 92 | { |
Jeff Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 93 | return expressInterest(name, 0, onData, onTimeout, wireFormat); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 94 | } |
| 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 Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 103 | removePendingInterest(uint64_t pendingInterestId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 104 | { |
| 105 | node_.removePendingInterest(pendingInterestId); |
Jeff Thompson | cdf7e25 | 2013-07-31 12:41:47 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 108 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 109 | * 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 Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 113 | * @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 Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 115 | * @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 Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 117 | * @param wireFormat A WireFormat object used to encode the message. If omitted, use WireFormat getDefaultWireFormat(). |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 118 | * @return The registered prefix ID which can be used with removeRegisteredPrefix. |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 119 | */ |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 120 | uint64_t |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 121 | registerPrefix |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 122 | (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags = ForwardingFlags(), |
Jeff Thompson | 6d7369b | 2013-09-19 14:40:21 -0700 | [diff] [blame] | 123 | WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 124 | { |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 125 | 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 Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 135 | removeRegisteredPrefix(uint64_t registeredPrefixId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 136 | { |
| 137 | node_.removeRegisteredPrefix(registeredPrefixId); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 138 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 141 | * Process any data to receive or call timeout callbacks. |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 142 | * 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 Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 144 | * @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 Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 147 | void |
| 148 | processEvents() |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 149 | { |
| 150 | // Just call Node's processEvents. |
| 151 | node_.processEvents(); |
| 152 | } |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 153 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 154 | /** |
| 155 | * Shut down and disconnect this Face. |
| 156 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 157 | void |
| 158 | shutdown(); |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 159 | |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 160 | private: |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 161 | Node node_; |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | } |
| 165 | |
| 166 | #endif |