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