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 | bf50a1a | 2013-08-20 18:01:01 -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. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_NODE_HPP |
| 9 | #define NDN_NODE_HPP |
| 10 | |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 11 | #include "common.hpp" |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 12 | #include "interest.hpp" |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 13 | #include "data.hpp" |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 14 | #include "transport/transport.hpp" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 15 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 16 | #include "management/controller.hpp" |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 17 | |
| 18 | #include "detail/registered-prefix.hpp" |
| 19 | #include "detail/pending-interest.hpp" |
| 20 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 21 | namespace ndn { |
| 22 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 23 | struct PendingInterestId; |
| 24 | struct RegisteredPrefixId; |
| 25 | |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 26 | /** |
| 27 | * An OnData function object is used to pass a callback to expressInterest. |
| 28 | */ |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 29 | typedef function<void(const shared_ptr<const Interest>&, const shared_ptr<Data>&)> OnData; |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * An OnTimeout function object is used to pass a callback to expressInterest. |
| 33 | */ |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 34 | typedef function<void(const shared_ptr<const Interest>&)> OnTimeout; |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 35 | |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 36 | /** |
| 37 | * An OnInterest function object is used to pass a callback to registerPrefix. |
| 38 | */ |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 39 | typedef function<void |
| 40 | (const shared_ptr<const Name>&, const shared_ptr<const Interest>&)> OnInterest; |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 41 | |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 42 | /** |
| 43 | * An OnRegisterFailed function object is used to report when registerPrefix fails. |
| 44 | */ |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 45 | typedef function<void(const Name&, const std::string&)> OnSetInterestFilterFailed; |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 46 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 47 | class Node { |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 48 | public: |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 49 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 50 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 51 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 52 | * Create a new Node for communication with an NDN hub with the given Transport object and connectionInfo. |
| 53 | * @param transport A shared_ptr to a Transport object used for communication. |
| 54 | * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport. |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 55 | */ |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 56 | Node(const shared_ptr<Transport>& transport, bool nfdMode = false); |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * @brief Alternative (special use case) version of the constructor, can be used to aggregate |
| 60 | * several Faces within one processing thread |
| 61 | * |
| 62 | * <code> |
| 63 | * Face face1(...); |
| 64 | * Face face2(..., face1.getAsyncService()); |
| 65 | * |
| 66 | * // Now the following ensures that events on both faces are processed |
| 67 | * face1.processEvents(); |
| 68 | * </code> |
| 69 | */ |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 70 | Node(const shared_ptr<Transport>& transport, const shared_ptr<boost::asio::io_service> &ioService, bool nfdMode = false); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 73 | * @brief Send the Interest through the transport, read the entire response and call onData(interest, data). |
| 74 | * |
| 75 | * @param interest A reference to the Interest. This copies the Interest. |
| 76 | * @param onData A function object to call when a matching data packet is received. |
| 77 | * @param onTimeout A function object to call if the interest times out. |
| 78 | * If onTimeout is an empty OnTimeout(), this does not use it. |
| 79 | * |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 80 | * @return The pending interest ID which can be used with removePendingInterest. |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 81 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 82 | const PendingInterestId* |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 83 | expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout); |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 86 | * Remove the pending interest entry with the pendingInterestId from the pending interest table. |
| 87 | * This does not affect another pending interest with a different pendingInterestId, even it if has the same interest name. |
| 88 | * If there is no entry with the pendingInterestId, do nothing. |
| 89 | * @param pendingInterestId The ID returned from expressInterest. |
| 90 | */ |
| 91 | void |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 92 | removePendingInterest(const PendingInterestId *pendingInterestId); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 95 | * Register prefix with the connected NDN hub and call onInterest when a matching interest is received. |
| 96 | * @param prefix A reference to a Name for the prefix to register. This copies the Name. |
| 97 | * @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 | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 98 | * use ref() as appropriate. |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 99 | * @param onRegisterFailed A function object to call if failed to retrieve the connected hub’s ID or failed to register the prefix. |
| 100 | * This calls onRegisterFailed(prefix) where prefix is the prefix given to registerPrefix. |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 101 | * @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] | 102 | * @return The registered prefix ID which can be used with removeRegisteredPrefix. |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 103 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 104 | const RegisteredPrefixId* |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 105 | setInterestFilter(const Name& prefix, |
| 106 | const OnInterest& onInterest, |
| 107 | const OnSetInterestFilterFailed& onSetInterestFilterFailed); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 110 | * Remove the registered prefix entry with the registeredPrefixId from the pending interest table. |
| 111 | * This does not affect another registered prefix with a different registeredPrefixId, even it if has the same prefix name. |
| 112 | * If there is no entry with the registeredPrefixId, do nothing. |
| 113 | * @param registeredPrefixId The ID returned from registerPrefix. |
| 114 | */ |
| 115 | void |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 116 | unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 117 | |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 118 | /** |
| 119 | * @brief Publish data packet |
| 120 | * |
| 121 | * This method can be called to satisfy the incoming Interest or to put Data packet into the cache |
| 122 | * of the local NDN forwarder |
| 123 | */ |
| 124 | void |
| 125 | put(const Data &data); |
| 126 | |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 127 | /** |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 128 | * Process any data to receive or call timeout callbacks. |
| 129 | * |
| 130 | * This call will block forever (default timeout == 0) to process IO on the face. |
| 131 | * To exit, one expected to call face.shutdown() from one of the callback methods. |
| 132 | * |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 133 | * If positive timeout is specified, then processEvents will exit after this timeout, |
| 134 | * 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] | 135 | * The call can be called repeatedly, if desired. |
| 136 | * |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 137 | * If negative timeout is specified, then processEvents will not block and process only pending |
| 138 | * events. |
| 139 | * |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 140 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 141 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 142 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 143 | void |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 144 | processEvents(Milliseconds timeout = 0, bool keepThread = false); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 145 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 146 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 147 | shutdown(); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 148 | |
Yingdi Yu | 0d92081 | 2014-01-30 14:50:57 -0800 | [diff] [blame] | 149 | shared_ptr<boost::asio::io_service> |
| 150 | ioService() { return ioService_; } |
| 151 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 152 | private: |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 153 | void |
| 154 | construct(const shared_ptr<Transport>& transport, const shared_ptr<boost::asio::io_service> &ioService, bool nfdMode); |
| 155 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 156 | struct ProcessEventsTimeout {}; |
| 157 | typedef std::list<shared_ptr<PendingInterest> > PendingInterestTable; |
| 158 | typedef std::list<shared_ptr<RegisteredPrefix> > RegisteredPrefixTable; |
| 159 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 160 | void |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 161 | asyncExpressInterest(const shared_ptr<const Interest> &interest, |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 162 | const OnData& onData, const OnTimeout& onTimeout); |
| 163 | |
| 164 | void |
| 165 | asyncRemovePendingInterest(const PendingInterestId *pendingInterestId); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 166 | |
| 167 | void |
| 168 | asyncUnsetInterestFilter(const RegisteredPrefixId *registeredPrefixId); |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 169 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 170 | void |
| 171 | onReceiveElement(const Block &wire); |
| 172 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 173 | |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 174 | static void |
| 175 | fireProcessEventsTimeout(const boost::system::error_code& error); |
| 176 | |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 177 | /** |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 178 | * Find the entry from the pit_ where the name conforms to the entry's interest selectors, and |
| 179 | * the entry interest name is the longest that matches name. |
| 180 | * @param name The name to find the interest for (from the incoming data packet). |
| 181 | * @return The index in pit_ of the pit entry, or -1 if not found. |
| 182 | */ |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 183 | PendingInterestTable::iterator |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 184 | getEntryIndexForExpressedInterest(const Name& name); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 185 | |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 186 | /** |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 187 | * Find the first entry from the registeredPrefixTable_ where the entry prefix is the longest that matches name. |
| 188 | * @param name The name to find the PrefixEntry for (from the incoming interest packet). |
| 189 | * @return A pointer to the entry, or 0 if not found. |
| 190 | */ |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 191 | RegisteredPrefixTable::iterator |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 192 | getEntryForRegisteredPrefix(const Name& name); |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 193 | |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 194 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 195 | void |
| 196 | checkPitExpire(); |
| 197 | |
| 198 | private: |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 199 | shared_ptr<boost::asio::io_service> ioService_; |
| 200 | shared_ptr<boost::asio::io_service::work> ioServiceWork_; // needed if thread needs to be preserved |
| 201 | shared_ptr<boost::asio::deadline_timer> pitTimeoutCheckTimer_; |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 202 | bool pitTimeoutCheckTimerActive_; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 203 | shared_ptr<boost::asio::deadline_timer> processEventsTimeoutTimer_; |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 204 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 205 | shared_ptr<Transport> transport_; |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 206 | |
| 207 | PendingInterestTable pendingInterestTable_; |
| 208 | RegisteredPrefixTable registeredPrefixTable_; |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 209 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 210 | shared_ptr<Controller> m_fwController; |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 213 | } // namespace ndn |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 214 | |
| 215 | #endif |