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