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