Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 7 | #ifndef NDN_FACE_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 8 | #define NDN_FACE_HPP |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 9 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 10 | #include "common.hpp" |
| 11 | #include "interest.hpp" |
| 12 | #include "data.hpp" |
| 13 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 14 | #include "transport/transport.hpp" |
| 15 | #include "transport/unix-transport.hpp" |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 16 | #include "transport/tcp-transport.hpp" |
Jeff Thompson | beb8b7d | 2013-07-16 15:49:21 -0700 | [diff] [blame] | 17 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 18 | #include "management/controller.hpp" |
| 19 | |
| 20 | #include "detail/registered-prefix.hpp" |
| 21 | #include "detail/pending-interest.hpp" |
| 22 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 23 | namespace ndn { |
| 24 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 25 | struct PendingInterestId; |
| 26 | struct RegisteredPrefixId; |
| 27 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 28 | /** |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 29 | * An OnData function object is used to pass a callback to expressInterest. |
| 30 | */ |
| 31 | typedef function<void(const Interest&, Data&)> OnData; |
| 32 | |
| 33 | /** |
| 34 | * An OnTimeout function object is used to pass a callback to expressInterest. |
| 35 | */ |
| 36 | typedef function<void(const Interest&)> OnTimeout; |
| 37 | |
| 38 | /** |
| 39 | * An OnInterest function object is used to pass a callback to registerPrefix. |
| 40 | */ |
| 41 | typedef function<void (const Name&, const Interest&)> OnInterest; |
| 42 | |
| 43 | /** |
| 44 | * An OnRegisterFailed function object is used to report when registerPrefix fails. |
| 45 | */ |
| 46 | typedef function<void(const Name&, const std::string&)> OnSetInterestFilterFailed; |
| 47 | |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * @brief Abstraction to communicate with local or remote NDN forwarder |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 52 | */ |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 53 | class Face { |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 54 | public: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 55 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 58 | * @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport. |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 59 | */ |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 60 | Face(bool nfdMode = false); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 61 | |
| 62 | /** |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 63 | * @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport. |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 64 | * @param ioService A shared pointer to boost::io_service object that should control all IO operations |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 65 | */ |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 66 | Face(const ptr_lib::shared_ptr<boost::asio::io_service> &ioService, bool nfdMode = false); |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 67 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 68 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 69 | * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport. |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 70 | * @param host The host of the NDN hub. |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 71 | * @param port The port or service name of the NDN hub. If omitted. use 6363. |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 72 | */ |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 73 | Face(const std::string &host, const std::string &port = "6363", bool nfdMode = false); |
| 74 | |
| 75 | /** |
| 76 | * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo. |
| 77 | * @param transport A shared_ptr to a Transport object used for communication. |
| 78 | * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport. |
| 79 | */ |
| 80 | Face(const shared_ptr<Transport>& transport, bool nfdMode = false); |
| 81 | |
| 82 | /** |
| 83 | * @brief Alternative (special use case) version of the constructor, can be used to aggregate |
| 84 | * several Faces within one processing thread |
| 85 | * |
| 86 | * <code> |
| 87 | * Face face1(...); |
| 88 | * Face face2(..., face1.getAsyncService()); |
| 89 | * |
| 90 | * // Now the following ensures that events on both faces are processed |
| 91 | * face1.processEvents(); |
| 92 | * </code> |
| 93 | */ |
| 94 | Face(const shared_ptr<Transport>& transport, |
| 95 | const shared_ptr<boost::asio::io_service> &ioService, |
| 96 | bool nfdMode = false); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 97 | |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 98 | /** |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 99 | * @brief Express Interest |
| 100 | * |
| 101 | * @param interest A reference to the Interest. This copies the Interest. |
| 102 | * @param onData A function object to call when a matching data packet is received. |
| 103 | * @param onTimeout A function object to call if the interest times out. |
| 104 | * If onTimeout is an empty OnTimeout(), this does not use it. |
| 105 | * |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 106 | * @return The pending interest ID which can be used with removePendingInterest. |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 107 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 108 | const PendingInterestId* |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 109 | expressInterest(const Interest& interest, |
| 110 | const OnData& onData, const OnTimeout& onTimeout = OnTimeout()); |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 113 | * @brief Express Interest using name and Interest template |
| 114 | * |
| 115 | * @param name Name of the Interest |
| 116 | * @param tmpl Interest template to fill parameters |
| 117 | * @param onData A callback to call when a matching data packet is received. |
| 118 | * @param onTimeout A callback to call if the interest times out. |
| 119 | * If onTimeout is an empty OnTimeout(), this does not use it. |
| 120 | * |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 121 | * @return The pending interest ID which can be used with removePendingInterest. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 122 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 123 | const PendingInterestId* |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 124 | expressInterest(const Name& name, |
| 125 | const Interest &tmpl, |
| 126 | const OnData& onData, const OnTimeout& onTimeout = OnTimeout()); |
| 127 | |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Remove the pending interest entry with the pendingInterestId from the pending interest table. |
| 130 | * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name. |
| 131 | * If there is no entry with the pendingInterestId, do nothing. |
| 132 | * @param pendingInterestId The ID returned from expressInterest. |
| 133 | */ |
| 134 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 135 | removePendingInterest(const PendingInterestId *pendingInterestId); |
Jeff Thompson | cdf7e25 | 2013-07-31 12:41:47 -0700 | [diff] [blame] | 136 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 137 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 138 | * Register prefix with the connected NDN hub and call onInterest when a matching interest is received. |
| 139 | * @param prefix A reference to a Name for the prefix to register. This copies the Name. |
| 140 | * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 141 | * use ref() as appropriate. |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 142 | * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix. |
| 143 | * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix. |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 144 | * @param flags The flags for finer control of which interests are forward to the application. |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 145 | * @return The registered prefix ID which can be used with removeRegisteredPrefix. |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 146 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 147 | const RegisteredPrefixId* |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 148 | setInterestFilter(const Name& prefix, |
| 149 | const OnInterest& onInterest, |
| 150 | const OnSetInterestFilterFailed& onSetInterestFilterFailed); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Remove the registered prefix entry with the registeredPrefixId from the pending interest table. |
| 154 | * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name. |
| 155 | * If there is no entry with the registeredPrefixId, do nothing. |
| 156 | * @param registeredPrefixId The ID returned from registerPrefix. |
| 157 | */ |
| 158 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 159 | unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId); |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 160 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 161 | /** |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 162 | * @brief Publish data packet |
| 163 | * |
| 164 | * This method can be called to satisfy the incoming Interest or to put Data packet into the cache |
| 165 | * of the local NDN forwarder |
| 166 | */ |
| 167 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 168 | put(const Data &data); |
| 169 | |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 170 | /** |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 171 | * Process any data to receive or call timeout callbacks. |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 172 | * |
| 173 | * This call will block forever (default timeout == 0) to process IO on the face. |
| 174 | * To exit, one expected to call face.shutdown() from one of the callback methods. |
| 175 | * |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 176 | * If positive timeout is specified, then processEvents will exit after this timeout, |
| 177 | * if not stopped earlier with face.shutdown() or when all active events finish. |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 178 | * The call can be called repeatedly, if desired. |
| 179 | * |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 180 | * If negative timeout is specified, then processEvents will not block and process only pending |
| 181 | * events. |
| 182 | * |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 183 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 184 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 185 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 186 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 187 | processEvents(Milliseconds timeout = 0, bool keepThread = false); |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 188 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 189 | void |
| 190 | shutdown(); |
Yingdi Yu | 0d92081 | 2014-01-30 14:50:57 -0800 | [diff] [blame] | 191 | |
| 192 | shared_ptr<boost::asio::io_service> |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 193 | ioService() { return ioService_; } |
| 194 | |
| 195 | private: |
| 196 | void |
| 197 | construct(const shared_ptr<Transport>& transport, const shared_ptr<boost::asio::io_service> &ioService, bool nfdMode); |
| 198 | |
| 199 | struct ProcessEventsTimeout {}; |
| 200 | typedef std::list<shared_ptr<PendingInterest> > PendingInterestTable; |
| 201 | typedef std::list<shared_ptr<RegisteredPrefix> > RegisteredPrefixTable; |
| 202 | |
| 203 | void |
| 204 | asyncExpressInterest(const shared_ptr<const Interest> &interest, |
| 205 | const OnData& onData, const OnTimeout& onTimeout); |
| 206 | |
| 207 | void |
| 208 | asyncRemovePendingInterest(const PendingInterestId *pendingInterestId); |
| 209 | |
| 210 | void |
| 211 | asyncUnsetInterestFilter(const RegisteredPrefixId *registeredPrefixId); |
| 212 | |
| 213 | void |
| 214 | onReceiveElement(const Block &wire); |
| 215 | |
| 216 | |
| 217 | static void |
| 218 | fireProcessEventsTimeout(const boost::system::error_code& error); |
| 219 | |
| 220 | /** |
| 221 | * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and |
| 222 | * the entry interest name is the longest that matches name. |
| 223 | * @param name The name to find the interest for (from the incoming data packet). |
| 224 | * @return The index in pit_ of the pit entry, or -1 if not found. |
| 225 | */ |
| 226 | PendingInterestTable::iterator |
| 227 | getEntryIndexForExpressedInterest(const Name& name); |
| 228 | |
| 229 | /** |
| 230 | * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name. |
| 231 | * @param name The name to find the PrefixEntry for (from the incoming interest packet). |
| 232 | * @return A pointer to the entry, or 0 if not found. |
| 233 | */ |
| 234 | RegisteredPrefixTable::iterator |
| 235 | getEntryForRegisteredPrefix(const Name& name); |
| 236 | |
| 237 | |
| 238 | void |
| 239 | checkPitExpire(); |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 240 | |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 241 | private: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 242 | shared_ptr<boost::asio::io_service> ioService_; |
| 243 | shared_ptr<boost::asio::io_service::work> ioServiceWork_; // needed if thread needs to be preserved |
| 244 | shared_ptr<boost::asio::deadline_timer> pitTimeoutCheckTimer_; |
| 245 | bool pitTimeoutCheckTimerActive_; |
| 246 | shared_ptr<boost::asio::deadline_timer> processEventsTimeoutTimer_; |
| 247 | |
| 248 | shared_ptr<Transport> transport_; |
| 249 | |
| 250 | PendingInterestTable pendingInterestTable_; |
| 251 | RegisteredPrefixTable registeredPrefixTable_; |
| 252 | |
| 253 | shared_ptr<Controller> m_fwController; |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 254 | }; |
| 255 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 256 | } // namespace ndn |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 257 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame^] | 258 | #endif // NDN_FACE_HPP |