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" |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 12 | #include "transport/transport.hpp" |
| 13 | #include "transport/unix-transport.hpp" |
Jeff Thompson | beb8b7d | 2013-07-16 15:49:21 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 15 | namespace ndn { |
| 16 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 17 | /** |
| 18 | * The Face class provides the main methods for NDN communication. |
| 19 | */ |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 20 | class Face { |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 21 | public: |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 22 | /** |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 23 | * @brief Create a new Face for communication with an NDN hub using the default TcpTransport. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 24 | */ |
| 25 | Face() |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 26 | : node_(ptr_lib::shared_ptr<UnixTransport>(new UnixTransport())) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @brief Create a new Face for communication with an NDN hub using the default Transport. |
| 32 | * @param ioService A shared pointer to boost::io_service object that should control all IO operations |
| 33 | */ |
| 34 | Face(const ptr_lib::shared_ptr<boost::asio::io_service> &ioService) |
| 35 | : node_(ptr_lib::shared_ptr<UnixTransport>(new UnixTransport()), ioService) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo. |
| 41 | * @param transport A shared_ptr to a Transport object used for communication. |
| 42 | */ |
| 43 | Face(const ptr_lib::shared_ptr<Transport>& transport) |
| 44 | : node_(transport) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
| 48 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 49 | * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo. |
| 50 | * @param transport A shared_ptr to a Transport object used for communication. |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 51 | * @param ioService A shared pointer to boost::io_service object that should control all IO operations |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 52 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 53 | Face(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<boost::asio::io_service> &ioService) |
| 54 | : node_(transport, ioService) |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 55 | { |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 58 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 59 | * 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] | 60 | * @param host The host of the NDN hub. |
Jeff Thompson | b629ddb | 2013-10-03 13:58:43 -0700 | [diff] [blame] | 61 | * @param port The port of the NDN hub. If omitted. use 6363. |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 62 | */ |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 63 | // Face(const char *host, unsigned short port = 6363) |
| 64 | // : node_(ptr_lib::shared_ptr<TcpTransport>(new TcpTransport(host, port))) |
| 65 | // { |
| 66 | // } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 67 | |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Send the Interest through the transport, read the entire response and call onData(interest, data). |
| 70 | * @param interest A reference to the Interest. This copies the Interest. |
| 71 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 72 | * use func_lib::ref() as appropriate. |
| 73 | * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it. |
| 74 | * This copies the function object, so you may need to use func_lib::ref() as appropriate. |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 75 | * @return The pending interest ID which can be used with removePendingInterest. |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 76 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 77 | const PendingInterestId* |
Jeff Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 78 | expressInterest |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 79 | (const Interest& interest, const OnData& onData, const OnTimeout& onTimeout = OnTimeout()) |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 80 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 81 | return node_.expressInterest(interest, onData, onTimeout); |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 85 | * 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] | 86 | * 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] | 87 | * @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] | 88 | * @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] | 89 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 90 | * use func_lib::ref() as appropriate. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 91 | * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it. |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 92 | * This copies the function object, so you may need to use func_lib::ref() as appropriate. |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 93 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 94 | const PendingInterestId* |
Jeff Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 95 | expressInterest |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 96 | (const Name& name, const Interest *interestTemplate, const OnData& onData, const OnTimeout& onTimeout = OnTimeout()); |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Encode name as an Interest, using a default interest lifetime. |
| 100 | * 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] | 101 | * @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] | 102 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 103 | * use func_lib::ref() as appropriate. |
| 104 | * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it. |
| 105 | * 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] | 106 | * @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] | 107 | * @return The pending interest ID which can be used with removePendingInterest. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 108 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 109 | const PendingInterestId* |
Jeff Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 110 | expressInterest |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 111 | (const Name& name, const OnData& onData, const OnTimeout& onTimeout = OnTimeout()) |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 112 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 113 | return expressInterest(name, 0, onData, onTimeout); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Remove the pending interest entry with the pendingInterestId from the pending interest table. |
| 118 | * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name. |
| 119 | * If there is no entry with the pendingInterestId, do nothing. |
| 120 | * @param pendingInterestId The ID returned from expressInterest. |
| 121 | */ |
| 122 | void |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 123 | removePendingInterest(const PendingInterestId* pendingInterestId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 124 | { |
| 125 | node_.removePendingInterest(pendingInterestId); |
Jeff Thompson | cdf7e25 | 2013-07-31 12:41:47 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 128 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 129 | * Register prefix with the connected NDN hub and call onInterest when a matching interest is received. |
| 130 | * @param prefix A reference to a Name for the prefix to register. This copies the Name. |
| 131 | * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to |
| 132 | * use func_lib::ref() as appropriate. |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 133 | * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix. |
| 134 | * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix. |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 135 | * @param flags The flags for finer control of which interests are forward to the application. If omitted, use |
| 136 | * the default flags defined by the default ForwardingFlags constructor. |
Jeff Thompson | 978c152 | 2013-11-12 23:03:10 -0800 | [diff] [blame] | 137 | * @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] | 138 | * @return The registered prefix ID which can be used with removeRegisteredPrefix. |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 139 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 140 | const RegisteredPrefixId* |
Alexander Afanasyev | de2f6b5 | 2014-01-02 21:42:26 -0800 | [diff] [blame] | 141 | setInterestFilter |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 142 | (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags = ForwardingFlags()) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 143 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 144 | return node_.registerPrefix(prefix, onInterest, onRegisterFailed, flags); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Remove the registered prefix entry with the registeredPrefixId from the pending interest table. |
| 149 | * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name. |
| 150 | * If there is no entry with the registeredPrefixId, do nothing. |
| 151 | * @param registeredPrefixId The ID returned from registerPrefix. |
| 152 | */ |
| 153 | void |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame^] | 154 | unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 155 | { |
| 156 | node_.removeRegisteredPrefix(registeredPrefixId); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 157 | } |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * @brief Publish data packet |
| 161 | * |
| 162 | * This method can be called to satisfy the incoming Interest or to put Data packet into the cache |
| 163 | * of the local NDN forwarder |
| 164 | */ |
| 165 | void |
| 166 | put(const Data &data) |
| 167 | { |
| 168 | node_.put(data); |
| 169 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 170 | |
| 171 | /** |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 172 | * Process any data to receive or call timeout callbacks. |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 173 | * |
| 174 | * This call will block forever (default timeout == 0) to process IO on the face. |
| 175 | * To exit, one expected to call face.shutdown() from one of the callback methods. |
| 176 | * |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 177 | * If positive timeout is specified, then processEvents will exit after this timeout, |
| 178 | * if not stopped earlier with face.shutdown() or when all active events finish. |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 179 | * The call can be called repeatedly, if desired. |
| 180 | * |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 181 | * If negative timeout is specified, then processEvents will not block and process only pending |
| 182 | * events. |
| 183 | * |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 184 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 185 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 186 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 187 | void |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 188 | processEvents(Milliseconds timeout = 0, bool keepThread = false) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 189 | { |
| 190 | // Just call Node's processEvents. |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 191 | node_.processEvents(timeout, keepThread); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 192 | } |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 193 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Shut down and disconnect this Face. |
| 196 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 197 | void |
| 198 | shutdown(); |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 199 | |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 200 | private: |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 201 | Node node_; |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | } |
| 205 | |
| 206 | #endif |