Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -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_PIT_H_ |
| 22 | #define _CCNX_PIT_H_ |
| 23 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 24 | #include "ns3/object.h" |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 25 | #include "ns3/nstime.h" |
| 26 | #include "ns3/event-id.h" |
| 27 | |
Alexander Afanasyev | 161a5c4 | 2012-04-17 15:14:04 -0700 | [diff] [blame] | 28 | #include "ccnx-name-components-hash-helper.h" |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 29 | #include "ccnx-pit-entry.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/composite_key.hpp> |
| 35 | #include <boost/multi_index/hashed_index.hpp> |
| 36 | #include <boost/multi_index/member.hpp> |
| 37 | #include <boost/multi_index/mem_fun.hpp> |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 38 | #include <map> |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 39 | #include <iostream> |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 40 | #include <algorithm> |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 41 | #include <boost/tuple/tuple.hpp> |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 42 | |
| 43 | namespace ns3 { |
| 44 | |
| 45 | class Ccnx; |
| 46 | class CcnxFace; |
| 47 | class CcnxContentObjectHeader; |
| 48 | class CcnxInterestHeader; |
| 49 | |
| 50 | /** |
| 51 | * \ingroup ccnx |
| 52 | * \private |
| 53 | * \brief Private namespace for CCNx PIT implementation |
| 54 | */ |
| 55 | namespace __ccnx_private |
| 56 | { |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 57 | // class i_prefix{}; ///< tag for prefix hash |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 58 | class i_timestamp {}; ///< tag for timestamp-ordered records (for cleanup optimization) |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * \ingroup ccnx |
| 63 | * \brief Typedef for RIT container implemented as a Boost.MultiIndex container |
| 64 | * |
| 65 | * - First index (tag<i_prefix>) is a unique hash index based on |
| 66 | * prefixes |
| 67 | * - Second index (tag<i_timestamp>) is a sequenced index based on |
| 68 | * arrival order (for clean-up optimizations) |
| 69 | * |
| 70 | * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library |
| 71 | */ |
| 72 | struct CcnxPitEntryContainer |
| 73 | { |
| 74 | typedef |
| 75 | boost::multi_index::multi_index_container< |
| 76 | CcnxPitEntry, |
| 77 | boost::multi_index::indexed_by< |
| 78 | // indexed by hash |
| 79 | boost::multi_index::hashed_unique< |
| 80 | boost::multi_index::tag<__ccnx_private::i_prefix>, |
| 81 | boost::multi_index::const_mem_fun<CcnxPitEntry, const CcnxNameComponents&, &CcnxPitEntry::GetPrefix>, |
| 82 | CcnxPrefixHash |
| 83 | >, |
| 84 | // sequenced to implement MRU |
Alexander Afanasyev | d76f4aa | 2011-12-15 21:30:16 -0800 | [diff] [blame] | 85 | boost::multi_index::ordered_non_unique< |
| 86 | boost::multi_index::tag<__ccnx_private::i_timestamp>, |
| 87 | boost::multi_index::member<CcnxPitEntry, Time, &CcnxPitEntry::m_expireTime> |
| 88 | > |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 89 | > |
| 90 | > type; |
| 91 | }; |
| 92 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 93 | //////////////////////////////////////////////////////////////////////// |
| 94 | //////////////////////////////////////////////////////////////////////// |
| 95 | |
| 96 | /** |
| 97 | * \ingroup ccnx |
| 98 | * \brief Class implementing Pending Interests Table |
| 99 | */ |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 100 | class CcnxPit : public CcnxPitEntryContainer::type, public Object |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 101 | { |
| 102 | public: |
| 103 | /** |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 104 | * \brief Interface ID |
| 105 | * |
| 106 | * \return interface ID |
| 107 | */ |
| 108 | static TypeId GetTypeId (); |
| 109 | |
| 110 | /** |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 111 | * \brief PIT constructor |
| 112 | */ |
| 113 | CcnxPit (); |
| 114 | |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 115 | /** |
| 116 | * \brief Destructor |
| 117 | */ |
| 118 | virtual ~CcnxPit (); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 120 | /** |
| 121 | * \brief Find corresponding PIT entry for the given content name |
| 122 | * \param prefix Prefix for which to lookup the entry |
| 123 | * \returns const reference to Pit entry. If record not found, |
| 124 | * CcnxPitEntryNotFound exception will be thrown |
| 125 | */ |
Alexander Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 126 | CcnxPitEntryContainer::type::iterator |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 127 | Lookup (const CcnxContentObjectHeader &header) const; |
| 128 | |
| 129 | /** |
| 130 | * \brief Find corresponding PIT entry for the given content name |
| 131 | * \param prefix Prefix for which to lookup the entry |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 132 | * \returns a tuple: |
| 133 | * get<0>: `const CcnxPitEntry&`: a valid PIT entry (if record does not exist, it will be created) |
| 134 | * get<1>: `bool`: true if a new entry was created |
| 135 | * get<2>: `bool`: true if a PIT entry exists and Nonce that present in header has been already seen |
| 136 | * |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 137 | */ |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 138 | boost::tuple<const CcnxPitEntry&, bool, bool> |
| 139 | Lookup (const CcnxInterestHeader &header); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 140 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 141 | Time GetPitEntryPruningTimeout () const |
| 142 | { |
| 143 | return m_PitEntryPruningTimout; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * \brief Set FIB table |
| 148 | */ |
| 149 | void SetFib (Ptr<CcnxFib> fib); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 150 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 151 | protected: |
| 152 | // inherited from Object class |
| 153 | virtual void NotifyNewAggregate (); |
| 154 | virtual void DoDispose (); |
| 155 | |
| 156 | private: |
| 157 | /** \brief Remove expired records from PIT */ |
| 158 | void CleanExpired (); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 159 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 160 | /** |
| 161 | * \brief Set cleanup timeout |
| 162 | * |
| 163 | * Side effect: current clean up even (if any) will be cancelled and a new event started |
| 164 | * |
| 165 | * \param timeout cleanup timeout |
| 166 | */ |
| 167 | void SetCleanupTimeout (const Time &timeout); |
| 168 | |
| 169 | /** |
| 170 | * \brief Get cleanup timeout |
| 171 | * |
| 172 | * \returns cleanup timeout |
| 173 | */ |
| 174 | Time GetCleanupTimeout () const; |
| 175 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 176 | friend std::ostream& operator<< (std::ostream& os, const CcnxPit &fib); |
| 177 | |
| 178 | private: |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 179 | Time m_cleanupTimeout; ///< \brief Configurable timeout of how often cleanup events are working |
| 180 | EventId m_cleanupEvent; ///< \brief Cleanup event |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 181 | |
| 182 | // configuration variables. Check implementation of GetTypeId for more details |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 183 | Time m_PitEntryPruningTimout; |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 184 | Time m_PitEntryDefaultLifetime; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 185 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 186 | Ptr<CcnxFib> m_fib; ///< \brief Link to FIB table |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | /////////////////////////////////////////////////////////////////////////////// |
| 190 | /////////////////////////////////////////////////////////////////////////////// |
| 191 | |
Alexander Afanasyev | a95b739 | 2012-03-09 10:54:10 -0800 | [diff] [blame] | 192 | std::ostream& operator<< (std::ostream& os, const CcnxPit &pit); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 193 | |
| 194 | class CcnxPitEntryNotFound {}; |
| 195 | |
| 196 | } // namespace ns3 |
| 197 | |
| 198 | #endif /* CCNX_PIT_H */ |