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 | |
| 107 | |
| 108 | /** |
| 109 | * \ingroup ccnx |
| 110 | * \brief Recently satisfied interest storage |
| 111 | * |
| 112 | * This storage holds information about all recently satisfied |
| 113 | * interests (prefix and nonce). |
| 114 | * |
| 115 | * There is no hard limit on number of entries (limited by the amount |
| 116 | * of the available memory). Entries are removed after preconfigured |
| 117 | * amount of time (RitTimeout, default is 1 second). |
| 118 | */ |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 119 | class CcnxRit : public CcnxRitContainer::type, public Object |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 120 | { |
| 121 | public: |
| 122 | /** |
| 123 | * \brief Interface ID |
| 124 | * |
| 125 | * \return interface ID |
| 126 | */ |
| 127 | static TypeId GetTypeId (); |
| 128 | |
| 129 | /** |
| 130 | * \brief Default constructor |
| 131 | */ |
| 132 | CcnxRit (); |
| 133 | virtual ~CcnxRit( ); |
| 134 | |
| 135 | /** |
| 136 | * \brief Find corresponding RIT entry for the given content name |
| 137 | * |
| 138 | * This check consists of two steps. First, we find records with |
| 139 | * the same nonce. Then check prefixes of all found records and if |
| 140 | * prefix of any of them matched prefix of the interest, this |
| 141 | * function returns true. In all other cases, it will return false. |
| 142 | * |
| 143 | * \param header header of the interest packet in question |
| 144 | * |
| 145 | * \returns true if the same interest was recently satisfied |
| 146 | */ |
| 147 | bool WasRecentlySatisfied (const CcnxInterestHeader &header); |
| 148 | |
| 149 | /** |
| 150 | * \brief Add a new RIT entry |
| 151 | * |
| 152 | * This function asserts (only in debug) if the same interest is |
| 153 | * already present in RIT. The caller is responsible of calling |
| 154 | * WasRecentlySatisfied before calling SetRecentlySatisfied. |
| 155 | * |
| 156 | * \param header header of the interest packet in question |
| 157 | */ |
| 158 | void SetRecentlySatisfied (const CcnxInterestHeader &header); |
| 159 | |
| 160 | /** |
| 161 | * \brief Set RIT entries lifetime |
| 162 | * |
| 163 | * \param lifetime of RIT entries timeout |
| 164 | */ |
| 165 | void SetRitTimeout (const Time &timeout); |
| 166 | |
| 167 | /** |
| 168 | * \brief Get RIT entries lifetime |
| 169 | * |
| 170 | * \returns lifetime of RIT entries timeout |
| 171 | */ |
| 172 | Time GetRitTimeout () const; |
| 173 | |
| 174 | /** |
| 175 | * \brief Set cleanup timeout |
| 176 | * |
| 177 | * Side effect: current clean up even (if any) will be cancelled and a new event started |
| 178 | * |
| 179 | * \param timeout cleanup timeout |
| 180 | */ |
| 181 | void SetCleanupTimeout (const Time &timeout); |
| 182 | |
| 183 | /** |
| 184 | * \brief Get cleanup timeout |
| 185 | * |
| 186 | * \returns cleanup timeout |
| 187 | */ |
| 188 | Time GetCleanupTimeout () const; |
| 189 | |
| 190 | private: |
| 191 | /** |
| 192 | * \brief Periodic even to clean up stalled entries |
| 193 | */ |
| 194 | void CleanExpired (); |
| 195 | |
| 196 | private: |
| 197 | Time m_ritTimeout; ///< \brief Configurable timeout of RIT entries |
| 198 | Time m_cleanupTimeout; ///< \brief Configurable timeout of how often cleanup events are working |
| 199 | EventId m_cleanupEvent; ///< \brief Cleanup event |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | } // namespace ns3 |
| 203 | |
| 204 | #endif // _CCNX_RIT_H_ |