Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #ifndef _CCNX_RIT_H_ |
| 22 | #define _CCNX_RIT_H_ |
| 23 | |
| 24 | #include <list> |
| 25 | #include "ns3/nstime.h" |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 26 | #include "ns3/ccnx-name-components.h" |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 27 | #include "ns3/object.h" |
| 28 | #include "ns3/nstime.h" |
| 29 | #include "ns3/event-id.h" |
| 30 | |
| 31 | #include <boost/multi_index_container.hpp> |
| 32 | #include <boost/multi_index/tag.hpp> |
| 33 | #include <boost/multi_index/ordered_index.hpp> |
| 34 | #include <boost/multi_index/sequenced_index.hpp> |
| 35 | #include <boost/multi_index/hashed_index.hpp> |
| 36 | #include <boost/multi_index/member.hpp> |
| 37 | |
| 38 | namespace ns3 { |
| 39 | |
| 40 | class CcnxInterestHeader; |
| 41 | |
| 42 | /** |
| 43 | * \ingroup ccnx |
| 44 | * \brief Recently satisfied interest |
| 45 | * |
| 46 | * This entry holds information about recently satisfied interest |
| 47 | * |
| 48 | * RIT entries will stay in the table for a while before being cleaned out |
| 49 | */ |
| 50 | struct CcnxRitEntry |
| 51 | { |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 52 | CcnxNameComponents m_prefix; ///< \brief Prefix of the recently satisfied interest |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 53 | uint32_t m_nonce; ///< \brief Nonce of the recently satisfied interest |
| 54 | Time m_expireTime; ///< \brief Time when the record should be removed |
| 55 | |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 56 | CcnxRitEntry (const CcnxNameComponents &prefix, uint32_t nonce, const Time &timeout) |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 57 | : m_prefix (prefix) |
| 58 | , m_nonce (nonce) |
| 59 | , m_expireTime (timeout) |
| 60 | { } |
| 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * \ingroup ccnx |
| 65 | * \brief Private namespace for CCNx RIT implementation |
| 66 | */ |
| 67 | namespace __ccnx_private_rit |
| 68 | { |
| 69 | class nonce {}; ///< tag for nonce hash |
| 70 | class timestamp {}; ///< tag for timestamp-ordered records (for cleanup optimization) |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * \ingroup ccnx |
| 75 | * \brief Typedef for RIT container implemented as a Boost.MultiIndex container |
| 76 | * |
| 77 | * - First index (tag<nonce>) is a non-unique hash index based on |
| 78 | * nonce (there could be several records with the same nonce, |
| 79 | * provided that the prefixes are different |
| 80 | * - Second index (tag<timestamp>) is a sequenced index based on |
| 81 | * arrival order (for clean-up optimizations) |
| 82 | * |
| 83 | * Container allows having non-unique nonce values. In the |
| 84 | * implementation it is allowed only if prefixes are different. During |
| 85 | * the lookup process, the nonce field is checked first and, if a |
| 86 | * match found, the prefix field will be checked to match prefix of |
| 87 | * the interest. |
| 88 | * |
| 89 | * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library |
| 90 | */ |
| 91 | struct CcnxRitContainer |
| 92 | { |
| 93 | typedef |
| 94 | boost::multi_index::multi_index_container< |
| 95 | CcnxRitEntry, |
| 96 | boost::multi_index::indexed_by< |
| 97 | boost::multi_index::hashed_non_unique< |
| 98 | boost::multi_index::tag<__ccnx_private_rit::nonce>, |
| 99 | boost::multi_index::member<CcnxRitEntry, uint32_t, &CcnxRitEntry::m_nonce> |
| 100 | >, |
| 101 | boost::multi_index::sequenced< |
| 102 | boost::multi_index::tag<__ccnx_private_rit::timestamp> > |
| 103 | > |
| 104 | > type; |
| 105 | }; |
| 106 | |
Ilya Moiseenko | f194f39 | 2011-10-28 13:13:55 -0700 | [diff] [blame] | 107 | |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * \ingroup ccnx |
| 111 | * \brief Recently satisfied interest storage |
| 112 | * |
| 113 | * This storage holds information about all recently satisfied |
| 114 | * interests (prefix and nonce). |
| 115 | * |
| 116 | * There is no hard limit on number of entries (limited by the amount |
| 117 | * of the available memory). Entries are removed after preconfigured |
| 118 | * amount of time (RitTimeout, default is 1 second). |
| 119 | */ |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 120 | class CcnxRit : public CcnxRitContainer::type, public Object |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 121 | { |
| 122 | public: |
| 123 | /** |
| 124 | * \brief Interface ID |
| 125 | * |
| 126 | * \return interface ID |
| 127 | */ |
| 128 | static TypeId GetTypeId (); |
| 129 | |
| 130 | /** |
| 131 | * \brief Default constructor |
| 132 | */ |
| 133 | CcnxRit (); |
| 134 | virtual ~CcnxRit( ); |
| 135 | |
| 136 | /** |
| 137 | * \brief Find corresponding RIT entry for the given content name |
| 138 | * |
| 139 | * This check consists of two steps. First, we find records with |
| 140 | * the same nonce. Then check prefixes of all found records and if |
| 141 | * prefix of any of them matched prefix of the interest, this |
| 142 | * function returns true. In all other cases, it will return false. |
| 143 | * |
| 144 | * \param header header of the interest packet in question |
| 145 | * |
| 146 | * \returns true if the same interest was recently satisfied |
| 147 | */ |
| 148 | bool WasRecentlySatisfied (const CcnxInterestHeader &header); |
| 149 | |
| 150 | /** |
| 151 | * \brief Add a new RIT entry |
| 152 | * |
| 153 | * This function asserts (only in debug) if the same interest is |
| 154 | * already present in RIT. The caller is responsible of calling |
| 155 | * WasRecentlySatisfied before calling SetRecentlySatisfied. |
| 156 | * |
| 157 | * \param header header of the interest packet in question |
| 158 | */ |
| 159 | void SetRecentlySatisfied (const CcnxInterestHeader &header); |
| 160 | |
| 161 | /** |
| 162 | * \brief Set RIT entries lifetime |
| 163 | * |
| 164 | * \param lifetime of RIT entries timeout |
| 165 | */ |
| 166 | void SetRitTimeout (const Time &timeout); |
| 167 | |
| 168 | /** |
| 169 | * \brief Get RIT entries lifetime |
| 170 | * |
| 171 | * \returns lifetime of RIT entries timeout |
| 172 | */ |
| 173 | Time GetRitTimeout () const; |
| 174 | |
| 175 | /** |
| 176 | * \brief Set cleanup timeout |
| 177 | * |
| 178 | * Side effect: current clean up even (if any) will be cancelled and a new event started |
| 179 | * |
| 180 | * \param timeout cleanup timeout |
| 181 | */ |
| 182 | void SetCleanupTimeout (const Time &timeout); |
| 183 | |
| 184 | /** |
| 185 | * \brief Get cleanup timeout |
| 186 | * |
| 187 | * \returns cleanup timeout |
| 188 | */ |
| 189 | Time GetCleanupTimeout () const; |
| 190 | |
| 191 | private: |
| 192 | /** |
| 193 | * \brief Periodic even to clean up stalled entries |
| 194 | */ |
| 195 | void CleanExpired (); |
| 196 | |
| 197 | private: |
| 198 | Time m_ritTimeout; ///< \brief Configurable timeout of RIT entries |
| 199 | Time m_cleanupTimeout; ///< \brief Configurable timeout of how often cleanup events are working |
| 200 | EventId m_cleanupEvent; ///< \brief Cleanup event |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 201 | }; |
Ilya Moiseenko | f194f39 | 2011-10-28 13:13:55 -0700 | [diff] [blame] | 202 | |
| 203 | ////////////////////////////////////////////////////////////////////// |
| 204 | // Helper classes |
| 205 | ////////////////////////////////////////////////////////////////////// |
| 206 | /** |
| 207 | * \ingroup ccnx |
| 208 | * \brief Typedef for nonce hash index of RIT container |
| 209 | */ |
| 210 | struct CcnxRitByNonce |
| 211 | { |
| 212 | typedef |
| 213 | CcnxRitContainer::type::index<__ccnx_private_rit::nonce>::type |
| 214 | type; |
| 215 | }; |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 216 | |
| 217 | } // namespace ns3 |
| 218 | |
| 219 | #endif // _CCNX_RIT_H_ |