Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 24 | #ifndef NDN_FACE_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 25 | #define NDN_FACE_HPP |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 27 | #include "common.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 28 | |
| 29 | #include "name.hpp" |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 30 | #include "interest.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 31 | #include "interest-filter.hpp" |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 32 | #include "data.hpp" |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 33 | #include "security/identity-certificate.hpp" |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 35 | namespace boost { |
| 36 | namespace asio { |
| 37 | class io_service; |
| 38 | } |
| 39 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 40 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 41 | namespace ndn { |
| 42 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 43 | class Transport; |
| 44 | |
| 45 | class PendingInterestId; |
| 46 | class RegisteredPrefixId; |
| 47 | class InterestFilterId; |
| 48 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 49 | namespace nfd { |
| 50 | class Controller; |
| 51 | } |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 52 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 53 | /** |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 54 | * @brief Callback called when expressed Interest gets satisfied with Data packet |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 55 | */ |
| 56 | typedef function<void(const Interest&, Data&)> OnData; |
| 57 | |
| 58 | /** |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 59 | * @brief Callback called when expressed Interest times out |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 60 | */ |
| 61 | typedef function<void(const Interest&)> OnTimeout; |
| 62 | |
| 63 | /** |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 64 | * @brief Callback called when incoming Interest matches the specified InterestFilter |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 65 | */ |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 66 | typedef function<void (const InterestFilter&, const Interest&)> OnInterest; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 69 | * @brief Callback called when registerPrefix or setInterestFilter command succeeds |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 70 | */ |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 71 | typedef function<void(const Name&)> RegisterPrefixSuccessCallback; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 73 | /** |
| 74 | * @brief Callback called when registerPrefix or setInterestFilter command fails |
| 75 | */ |
| 76 | typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback; |
| 77 | |
| 78 | /** |
| 79 | * @brief Callback called when unregisterPrefix or unsetInterestFilter command succeeds |
| 80 | */ |
| 81 | typedef function<void()> UnregisterPrefixSuccessCallback; |
| 82 | |
| 83 | /** |
| 84 | * @brief Callback called when unregisterPrefix or unsetInterestFilter command fails |
| 85 | */ |
| 86 | typedef function<void(const std::string&)> UnregisterPrefixFailureCallback; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 87 | |
| 88 | |
| 89 | /** |
| 90 | * @brief Abstraction to communicate with local or remote NDN forwarder |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 91 | */ |
Alexander Afanasyev | 8460afb | 2014-02-15 20:31:42 -0800 | [diff] [blame] | 92 | class Face : noncopyable |
| 93 | { |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 94 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 95 | class Error : public std::runtime_error |
| 96 | { |
| 97 | public: |
| 98 | explicit |
| 99 | Error(const std::string& what) |
| 100 | : std::runtime_error(what) |
| 101 | { |
| 102 | } |
| 103 | }; |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 104 | |
| 105 | /** |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 106 | * @brief Create a new Face using the default transport (UnixTransport) |
| 107 | * |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 108 | * @throws ConfigFile::Error on configuration file parse failure |
| 109 | * @throws Face::Error on unsupported protocol |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 110 | */ |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 111 | Face(); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 112 | |
| 113 | /** |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 114 | * @brief Create a new Face using the default transport (UnixTransport) |
| 115 | * |
| 116 | * @deprecated This constructor is deprecated. Use `Face(boost::asio::io_service&)` |
| 117 | * instead. |
| 118 | * |
| 119 | * @param ioService A shared pointer to boost::io_service object that should control all |
| 120 | * IO operations |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 121 | * @throws ConfigFile::Error on configuration file parse failure |
| 122 | * @throws Face::Error on unsupported protocol |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 123 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 124 | DEPRECATED( |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 125 | explicit |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 126 | Face(const shared_ptr<boost::asio::io_service>& ioService)); |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 127 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 128 | /** |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 129 | * @brief Create a new Face using the default transport (UnixTransport) |
| 130 | * |
| 131 | * @par Usage examples: |
| 132 | * |
| 133 | * Face face1; |
| 134 | * Face face2(face1.getIoService()); |
| 135 | * |
| 136 | * // Now the following ensures that events on both faces are processed |
| 137 | * face1.processEvents(); |
| 138 | * // or face1.getIoService().run(); |
| 139 | * |
| 140 | * @par or |
| 141 | * |
| 142 | * boost::asio::io_service ioService; |
| 143 | * Face face1(ioService); |
| 144 | * Face face2(ioService); |
| 145 | * ... |
| 146 | * |
| 147 | * ioService.run(); |
| 148 | * |
| 149 | * @param ioService A reference to boost::io_service object that should control all |
| 150 | * IO operations. |
| 151 | * @throws ConfigFile::Error on configuration file parse failure |
| 152 | * @throws Face::Error on unsupported protocol |
| 153 | */ |
| 154 | explicit |
| 155 | Face(boost::asio::io_service& ioService); |
| 156 | |
| 157 | /** |
| 158 | * @brief Create a new Face using TcpTransport |
| 159 | * |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 160 | * @param host The host of the NDN forwarder |
| 161 | * @param port (optional) The port or service name of the NDN forwarder (**default**: "6363") |
| 162 | * |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 163 | * @throws Face::Error on unsupported protocol |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 164 | */ |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 165 | Face(const std::string& host, const std::string& port = "6363"); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 166 | |
| 167 | /** |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 168 | * @brief Create a new Face using the given Transport |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 169 | * |
| 170 | * @param transport A shared_ptr to a Transport object used for communication |
| 171 | * |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 172 | * @throws Face::Error on unsupported protocol |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 173 | */ |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 174 | explicit |
| 175 | Face(const shared_ptr<Transport>& transport); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 176 | |
| 177 | /** |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 178 | * @brief Create a new Face using the given Transport and IO service object |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 179 | * |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 180 | * @sa Face(boost::asio::io_service&) |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 181 | * |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 182 | * @throws Face::Error on unsupported protocol |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 183 | */ |
| 184 | Face(const shared_ptr<Transport>& transport, |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 185 | boost::asio::io_service& ioService); |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 186 | |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 187 | /** |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 188 | * @brief Express Interest |
| 189 | * |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 190 | * @param interest An Interest to be expressed |
| 191 | * @param onData Callback to be called when a matching data packet is received |
| 192 | * @param onTimeout (optional) A function object to call if the interest times out |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 193 | * |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 194 | * @return The pending interest ID which can be used with removePendingInterest |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 195 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 196 | const PendingInterestId* |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 197 | expressInterest(const Interest& interest, |
| 198 | const OnData& onData, const OnTimeout& onTimeout = OnTimeout()); |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 199 | |
| 200 | /** |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 201 | * @brief Express Interest using name and Interest template |
| 202 | * |
| 203 | * @param name Name of the Interest |
| 204 | * @param tmpl Interest template to fill parameters |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 205 | * @param onData Callback to be called when a matching data packet is received |
| 206 | * @param onTimeout (optional) A function object to call if the interest times out |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 207 | * |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 208 | * @return Opaque pending interest ID which can be used with removePendingInterest |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 209 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 210 | const PendingInterestId* |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 211 | expressInterest(const Name& name, |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 212 | const Interest& tmpl, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 213 | const OnData& onData, const OnTimeout& onTimeout = OnTimeout()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 214 | |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 215 | /** |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 216 | * @brief Cancel previously expressed Interest |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 217 | * |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 218 | * @param pendingInterestId The ID returned from expressInterest. |
| 219 | */ |
| 220 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 221 | removePendingInterest(const PendingInterestId* pendingInterestId); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 222 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 223 | /** |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 224 | * @brief Set InterestFilter to dispatch incoming matching interest to onInterest |
| 225 | * callback and register the filtered prefix with the connected NDN forwarder |
| 226 | * |
| 227 | * This version of setInterestFilter combines setInterestFilter and registerPrefix |
| 228 | * operations and is intended to be used when only one filter for the same prefix needed |
| 229 | * to be set. When multiple names sharing the same prefix should be dispatched to |
| 230 | * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by |
| 231 | * a series of setInterestFilter calls. |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 232 | * |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 233 | * @param interestFilter Interest filter (prefix part will be registered with the forwarder) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 234 | * @param onInterest A callback to be called when a matching interest is received |
| 235 | * @param onSuccess A callback to be called when prefixRegister command succeeds |
| 236 | * @param onFailure A callback to be called when prefixRegister command fails |
| 237 | * @param certificate (optional) A certificate under which the prefix registration |
| 238 | * command interest is signed. When omitted, a default certificate |
| 239 | * of the default identity is used to sign the registration command |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 240 | * |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 241 | * @return Opaque registered prefix ID which can be used with unsetInterestFilter or |
| 242 | * removeRegisteredPrefix |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 243 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 244 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 245 | setInterestFilter(const InterestFilter& interestFilter, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 246 | const OnInterest& onInterest, |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 247 | const RegisterPrefixSuccessCallback& onSuccess, |
| 248 | const RegisterPrefixFailureCallback& onFailure, |
| 249 | const IdentityCertificate& certificate = IdentityCertificate()); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 250 | |
| 251 | /** |
Alexander Afanasyev | 1b01c31 | 2014-05-26 16:58:10 +0300 | [diff] [blame] | 252 | * @brief Set InterestFilter to dispatch incoming matching interest to onInterest |
| 253 | * callback and register the filtered prefix with the connected NDN forwarder |
| 254 | * |
| 255 | * This version of setInterestFilter combines setInterestFilter and registerPrefix |
| 256 | * operations and is intended to be used when only one filter for the same prefix needed |
| 257 | * to be set. When multiple names sharing the same prefix should be dispatched to |
| 258 | * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by |
| 259 | * a series of setInterestFilter calls. |
| 260 | * |
| 261 | * @param interestFilter Interest filter (prefix part will be registered with the forwarder) |
| 262 | * @param onInterest A callback to be called when a matching interest is received |
| 263 | * @param onFailure A callback to be called when prefixRegister command fails |
| 264 | * @param certificate (optional) A certificate under which the prefix registration |
| 265 | * command interest is signed. When omitted, a default certificate |
| 266 | * of the default identity is used to sign the registration command |
| 267 | * |
| 268 | * @return Opaque registered prefix ID which can be used with unsetInterestFilter or |
| 269 | * removeRegisteredPrefix |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 270 | */ |
| 271 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 272 | setInterestFilter(const InterestFilter& interestFilter, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 273 | const OnInterest& onInterest, |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 274 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 1b01c31 | 2014-05-26 16:58:10 +0300 | [diff] [blame] | 275 | const IdentityCertificate& certificate = IdentityCertificate()); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 276 | |
| 277 | /** |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 278 | * @brief Set InterestFilter to dispatch incoming matching interest to onInterest |
| 279 | * callback and register the filtered prefix with the connected NDN forwarder |
| 280 | * |
| 281 | * This version of setInterestFilter combines setInterestFilter and registerPrefix |
| 282 | * operations and is intended to be used when only one filter for the same prefix needed |
| 283 | * to be set. When multiple names sharing the same prefix should be dispatched to |
| 284 | * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by |
| 285 | * a series of setInterestFilter calls. |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 286 | * |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 287 | * @param interestFilter Interest filter (prefix part will be registered with the forwarder) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 288 | * @param onInterest A callback to be called when a matching interest is received |
| 289 | * @param onSuccess A callback to be called when prefixRegister command succeeds |
| 290 | * @param onFailure A callback to be called when prefixRegister command fails |
| 291 | * @param identity A signing identity. A command interest is signed under the default |
| 292 | * certificate of this identity |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 293 | * |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 294 | * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 295 | */ |
| 296 | const RegisteredPrefixId* |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 297 | setInterestFilter(const InterestFilter& interestFilter, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 298 | const OnInterest& onInterest, |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 299 | const RegisterPrefixSuccessCallback& onSuccess, |
| 300 | const RegisterPrefixFailureCallback& onFailure, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 301 | const Name& identity); |
| 302 | |
| 303 | /** |
Alexander Afanasyev | 1b01c31 | 2014-05-26 16:58:10 +0300 | [diff] [blame] | 304 | * @brief Set InterestFilter to dispatch incoming matching interest to onInterest |
| 305 | * callback and register the filtered prefix with the connected NDN forwarder |
| 306 | * |
| 307 | * This version of setInterestFilter combines setInterestFilter and registerPrefix |
| 308 | * operations and is intended to be used when only one filter for the same prefix needed |
| 309 | * to be set. When multiple names sharing the same prefix should be dispatched to |
| 310 | * different callbacks, use one registerPrefix call, followed (in onSuccess callback) by |
| 311 | * a series of setInterestFilter calls. |
| 312 | * |
| 313 | * @param interestFilter Interest filter (prefix part will be registered with the forwarder) |
| 314 | * @param onInterest A callback to be called when a matching interest is received |
| 315 | * @param onFailure A callback to be called when prefixRegister command fails |
| 316 | * @param identity A signing identity. A command interest is signed under the default |
| 317 | * certificate of this identity |
| 318 | * |
| 319 | * @return Opaque registered prefix ID which can be used with removeRegisteredPrefix |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 320 | */ |
| 321 | const RegisteredPrefixId* |
| 322 | setInterestFilter(const InterestFilter& interestFilter, |
| 323 | const OnInterest& onInterest, |
| 324 | const RegisterPrefixFailureCallback& onFailure, |
Alexander Afanasyev | 1b01c31 | 2014-05-26 16:58:10 +0300 | [diff] [blame] | 325 | const Name& identity); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 326 | |
| 327 | /** |
| 328 | * @brief Set InterestFilter to dispatch incoming matching interest to onInterest callback |
| 329 | * |
| 330 | * @param interestFilter Interest |
| 331 | * @param onInterest A callback to be called when a matching interest is received |
| 332 | * |
| 333 | * This method modifies library's FIB only, and does not register the prefix with the |
| 334 | * forwarder. It will always succeed. To register prefix with the forwarder, use |
| 335 | * registerPrefix, or use the setInterestFilter overload taking two callbacks. |
| 336 | * |
| 337 | * @return Opaque interest filter ID which can be used with unsetInterestFilter |
| 338 | */ |
| 339 | const InterestFilterId* |
| 340 | setInterestFilter(const InterestFilter& interestFilter, |
| 341 | const OnInterest& onInterest); |
| 342 | |
| 343 | |
| 344 | /** |
| 345 | * @brief Register prefix with the connected NDN forwarder |
| 346 | * |
| 347 | * This method only modifies forwarder's RIB (or FIB) and does not associate any |
| 348 | * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to |
| 349 | * the right callbacks. |
| 350 | * |
| 351 | * @param prefix A prefix to register with the connected NDN forwarder |
| 352 | * @param onSuccess A callback to be called when prefixRegister command succeeds |
| 353 | * @param onFailure A callback to be called when prefixRegister command fails |
| 354 | * @param certificate (optional) A certificate under which the prefix registration |
| 355 | * command interest is signed. When omitted, a default certificate |
| 356 | * of the default identity is used to sign the registration command |
| 357 | * |
| 358 | * @return The registered prefix ID which can be used with unregisterPrefix |
| 359 | */ |
| 360 | const RegisteredPrefixId* |
| 361 | registerPrefix(const Name& prefix, |
| 362 | const RegisterPrefixSuccessCallback& onSuccess, |
| 363 | const RegisterPrefixFailureCallback& onFailure, |
| 364 | const IdentityCertificate& certificate = IdentityCertificate()); |
| 365 | |
| 366 | /** |
| 367 | * @brief Register prefix with the connected NDN forwarder and call onInterest when a matching |
| 368 | * interest is received. |
| 369 | * |
| 370 | * This method only modifies forwarder's RIB (or FIB) and does not associate any |
| 371 | * onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to |
| 372 | * the right callbacks. |
| 373 | * |
| 374 | * @param prefix A prefix to register with the connected NDN forwarder |
| 375 | * @param onSuccess A callback to be called when prefixRegister command succeeds |
| 376 | * @param onFailure A callback to be called when prefixRegister command fails |
| 377 | * @param identity A signing identity. A command interest is signed under the default |
| 378 | * certificate of this identity |
| 379 | * |
| 380 | * @return The registered prefix ID which can be used with unregisterPrefix |
| 381 | */ |
| 382 | const RegisteredPrefixId* |
| 383 | registerPrefix(const Name& prefix, |
| 384 | const RegisterPrefixSuccessCallback& onSuccess, |
| 385 | const RegisterPrefixFailureCallback& onFailure, |
| 386 | const Name& identity); |
| 387 | |
| 388 | |
| 389 | /** |
Alexander Afanasyev | 1b01c31 | 2014-05-26 16:58:10 +0300 | [diff] [blame] | 390 | * @brief Remove the registered prefix entry with the registeredPrefixId |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 391 | * |
| 392 | * This does not affect another registered prefix with a different registeredPrefixId, |
| 393 | * even it if has the same prefix name. If there is no entry with the |
| 394 | * registeredPrefixId, do nothing. |
| 395 | * |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 396 | * unsetInterestFilter will use the same credentials as original |
| 397 | * setInterestFilter/registerPrefix command |
| 398 | * |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 399 | * @param registeredPrefixId The ID returned from registerPrefix |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 400 | */ |
| 401 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 402 | unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId); |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 403 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 404 | /** |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 405 | * @brief Remove previously set InterestFilter from library's FIB |
| 406 | * |
| 407 | * This method always succeeds and will **NOT** send any request to the connected |
| 408 | * forwarder. |
| 409 | * |
| 410 | * @param interestFilterId The ID returned from setInterestFilter. |
| 411 | */ |
| 412 | void |
| 413 | unsetInterestFilter(const InterestFilterId* interestFilterId); |
| 414 | |
| 415 | /** |
| 416 | * @brief Deregister prefix from RIB (or FIB) |
| 417 | * |
| 418 | * unregisterPrefix will use the same credentials as original |
| 419 | * setInterestFilter/registerPrefix command |
| 420 | * |
| 421 | * If registeredPrefixId was obtained using setInterestFilter, the corresponding |
| 422 | * InterestFilter will be unset too. |
| 423 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 424 | * @param registeredPrefixId The ID returned from registerPrefix |
| 425 | * @param onSuccess Callback to be called when operation succeeds |
| 426 | * @param onFailure Callback to be called when operation fails |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 427 | */ |
| 428 | void |
| 429 | unregisterPrefix(const RegisteredPrefixId* registeredPrefixId, |
| 430 | const UnregisterPrefixSuccessCallback& onSuccess, |
| 431 | const UnregisterPrefixFailureCallback& onFailure); |
| 432 | |
| 433 | /** |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 434 | * @brief (FOR DEBUG PURPOSES ONLY) Request direct NFD FIB management |
| 435 | */ |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 436 | void |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 437 | setDirectFibManagement(bool isDirectFibManagementRequested = false); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 438 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 439 | /** |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 440 | * @brief Publish data packet |
| 441 | * |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 442 | * This method can be called to satisfy the incoming Interest or to put Data packet into |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame^] | 443 | * the cache of the local NDN forwarder. |
| 444 | * |
| 445 | * @param data Data packet to publish. It is highly recommended to use Data packet that |
| 446 | * was created using make_shared<Data>(...). Otherwise, put() will make an |
| 447 | * extra copy of the Data packet to ensure validity of published Data until |
| 448 | * asynchronous put() operation finishes. |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 449 | */ |
| 450 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 451 | put(const Data& data); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 452 | |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 453 | /** |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 454 | * @brief Process any data to receive or call timeout callbacks. |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 455 | * |
| 456 | * This call will block forever (default timeout == 0) to process IO on the face. |
| 457 | * To exit, one expected to call face.shutdown() from one of the callback methods. |
| 458 | * |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 459 | * If positive timeout is specified, then processEvents will exit after this timeout, if |
| 460 | * not stopped earlier with face.shutdown() or when all active events finish. The call |
| 461 | * can be called repeatedly, if desired. |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 462 | * |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 463 | * If negative timeout is specified, then processEvents will not block and process only |
| 464 | * pending events. |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 465 | * |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 466 | * @param timeout maximum time to block the thread |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 467 | * @param keepThread Keep thread in a blocked state (in event processing), even when |
| 468 | * there are no outstanding events (e.g., no Interest/Data is expected) |
| 469 | * |
| 470 | * @throw This may throw an exception for reading data or in the callback for processing |
| 471 | * the data. If you call this from an main event loop, you may want to catch and |
| 472 | * log/disregard all exceptions. |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 473 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 474 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 475 | processEvents(const time::milliseconds& timeout = time::milliseconds::zero(), |
| 476 | bool keepThread = false); |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 477 | |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 478 | /** |
| 479 | * @brief Shutdown face operations |
| 480 | * |
| 481 | * This method cancels all pending operations and closes connection to NDN Forwarder. |
| 482 | * |
| 483 | * Note that this method does not stop IO service and if the same IO service is shared |
| 484 | * between multiple Faces or with other IO objects (e.g., Scheduler). |
| 485 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 486 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 487 | shutdown(); |
Yingdi Yu | 0d92081 | 2014-01-30 14:50:57 -0800 | [diff] [blame] | 488 | |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 489 | /** |
| 490 | * @brief Get shared_ptr of the IO service object |
| 491 | * |
| 492 | * @deprecated Use getIoService instead |
| 493 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 494 | DEPRECATED( |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 495 | shared_ptr<boost::asio::io_service> |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 496 | ioService()) |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 497 | { |
| 498 | return m_ioService; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * @brief Get reference to IO service object |
| 503 | */ |
| 504 | boost::asio::io_service& |
| 505 | getIoService() |
| 506 | { |
| 507 | return *m_ioService; |
| 508 | } |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 509 | |
| 510 | private: |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 511 | /** |
| 512 | * @throws Face::Error on unsupported protocol |
| 513 | */ |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 514 | void |
Alexander Afanasyev | 7682ccb | 2014-02-20 10:29:35 -0800 | [diff] [blame] | 515 | construct(const shared_ptr<Transport>& transport, |
Alexander Afanasyev | 505646e | 2014-02-24 20:13:37 -0800 | [diff] [blame] | 516 | const shared_ptr<boost::asio::io_service>& ioService); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 517 | |
| 518 | bool |
| 519 | isSupportedNfdProtocol(const std::string& protocol); |
| 520 | |
| 521 | bool |
| 522 | isSupportedNrdProtocol(const std::string& protocol); |
| 523 | |
Alexander Afanasyev | 691c3ce | 2014-04-23 14:28:04 -0700 | [diff] [blame] | 524 | class ProcessEventsTimeout |
| 525 | { |
| 526 | }; |
| 527 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 528 | void |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 529 | onReceiveElement(const Block& wire); |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 530 | |
Alexander Afanasyev | 7dced46 | 2014-03-19 15:12:32 -0700 | [diff] [blame] | 531 | void |
| 532 | asyncShutdown(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 533 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 534 | static void |
| 535 | fireProcessEventsTimeout(const boost::system::error_code& error); |
| 536 | |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 537 | private: |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 538 | shared_ptr<boost::asio::io_service> m_ioService; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 539 | |
Alexander Afanasyev | f39c537 | 2014-02-17 19:42:56 -0800 | [diff] [blame] | 540 | shared_ptr<Transport> m_transport; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 541 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 542 | shared_ptr<nfd::Controller> m_nfdController; |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 543 | bool m_isDirectNfdFibManagementRequested; |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 544 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 545 | class Impl; |
| 546 | shared_ptr<Impl> m_impl; |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 547 | }; |
| 548 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 549 | inline bool |
| 550 | Face::isSupportedNfdProtocol(const std::string& protocol) |
| 551 | { |
| 552 | return protocol == "nfd-0.1"; |
| 553 | } |
| 554 | |
| 555 | inline bool |
| 556 | Face::isSupportedNrdProtocol(const std::string& protocol) |
| 557 | { |
| 558 | return protocol == "nrd-0.1"; |
| 559 | } |
| 560 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 561 | inline void |
| 562 | Face::setDirectFibManagement(bool isDirectFibManagementRequested/* = false*/) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 563 | { |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 564 | m_isDirectNfdFibManagementRequested = isDirectFibManagementRequested; |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 565 | } |
| 566 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 567 | } // namespace ndn |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 568 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 569 | #endif // NDN_FACE_HPP |