blob: 84b1685ce40de63ed4826a3e00a22405ca6bfe0f [file] [log] [blame]
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -07001/* -*- 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 Afanasyevcf133f02011-09-06 12:13:48 -070024#include "ns3/object.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070025#include "ns3/nstime.h"
26#include "ns3/event-id.h"
27
Alexander Afanasyev161a5c42012-04-17 15:14:04 -070028#include "ccnx-name-components-hash-helper.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070029#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 Moiseenko1c73cb52011-10-28 13:13:11 -070038#include <map>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070039#include <iostream>
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -070040#include <algorithm>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080041#include <boost/tuple/tuple.hpp>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070042
43namespace ns3 {
44
45class Ccnx;
46class CcnxFace;
47class CcnxContentObjectHeader;
48class CcnxInterestHeader;
49
Alexander Afanasyev6315ef72012-06-01 20:56:31 -070050/// @cond include_hidden
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070051/**
52 * \ingroup ccnx
53 * \private
54 * \brief Private namespace for CCNx PIT implementation
55 */
56namespace __ccnx_private
57{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070058// class i_prefix{}; ///< tag for prefix hash
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070059class i_timestamp {}; ///< tag for timestamp-ordered records (for cleanup optimization)
60};
Alexander Afanasyev6315ef72012-06-01 20:56:31 -070061/// @endcond
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070062
63/**
64 * \ingroup ccnx
65 * \brief Typedef for RIT container implemented as a Boost.MultiIndex container
66 *
67 * - First index (tag<i_prefix>) is a unique hash index based on
68 * prefixes
69 * - Second index (tag<i_timestamp>) is a sequenced index based on
70 * arrival order (for clean-up optimizations)
71 *
72 * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library
73 */
74struct CcnxPitEntryContainer
75{
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070076 /// @cond include_hidden
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070077 typedef
78 boost::multi_index::multi_index_container<
79 CcnxPitEntry,
80 boost::multi_index::indexed_by<
81 // indexed by hash
82 boost::multi_index::hashed_unique<
83 boost::multi_index::tag<__ccnx_private::i_prefix>,
84 boost::multi_index::const_mem_fun<CcnxPitEntry, const CcnxNameComponents&, &CcnxPitEntry::GetPrefix>,
85 CcnxPrefixHash
86 >,
87 // sequenced to implement MRU
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -080088 boost::multi_index::ordered_non_unique<
89 boost::multi_index::tag<__ccnx_private::i_timestamp>,
90 boost::multi_index::member<CcnxPitEntry, Time, &CcnxPitEntry::m_expireTime>
91 >
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070092 >
93 > type;
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070094 /// @endcond
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070095};
96
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070097////////////////////////////////////////////////////////////////////////
98////////////////////////////////////////////////////////////////////////
99
100/**
101 * \ingroup ccnx
102 * \brief Class implementing Pending Interests Table
103 */
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700104class CcnxPit : public CcnxPitEntryContainer::type, public Object
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700105{
106public:
107 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700108 * \brief Interface ID
109 *
110 * \return interface ID
111 */
112 static TypeId GetTypeId ();
113
114 /**
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700115 * \brief PIT constructor
116 */
117 CcnxPit ();
118
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800119 /**
120 * \brief Destructor
121 */
122 virtual ~CcnxPit ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800123
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700124 /**
125 * \brief Find corresponding PIT entry for the given content name
126 * \param prefix Prefix for which to lookup the entry
127 * \returns const reference to Pit entry. If record not found,
128 * CcnxPitEntryNotFound exception will be thrown
129 */
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700130 CcnxPitEntryContainer::type::iterator
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700131 Lookup (const CcnxContentObjectHeader &header) const;
132
133 /**
134 * \brief Find corresponding PIT entry for the given content name
135 * \param prefix Prefix for which to lookup the entry
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800136 * \returns a tuple:
137 * get<0>: `const CcnxPitEntry&`: a valid PIT entry (if record does not exist, it will be created)
138 * get<1>: `bool`: true if a new entry was created
139 * get<2>: `bool`: true if a PIT entry exists and Nonce that present in header has been already seen
140 *
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700141 */
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800142 boost::tuple<const CcnxPitEntry&, bool, bool>
143 Lookup (const CcnxInterestHeader &header);
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700144
145 /**
146 * @brief Get pruning timeout for PIT entries (configuration parameter)
147 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148 Time GetPitEntryPruningTimeout () const
149 {
150 return m_PitEntryPruningTimout;
151 }
152
153 /**
154 * \brief Set FIB table
155 */
156 void SetFib (Ptr<CcnxFib> fib);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700157
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800158protected:
159 // inherited from Object class
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700160 virtual void NotifyNewAggregate (); ///< @brief Even when object is aggregated to another Object
161 virtual void DoDispose (); ///< @brief Do cleanup
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800162
163private:
164 /** \brief Remove expired records from PIT */
165 void CleanExpired ();
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700166
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700167 /**
168 * \brief Set cleanup timeout
169 *
170 * Side effect: current clean up even (if any) will be cancelled and a new event started
171 *
172 * \param timeout cleanup timeout
173 */
174 void SetCleanupTimeout (const Time &timeout);
175
176 /**
177 * \brief Get cleanup timeout
178 *
179 * \returns cleanup timeout
180 */
181 Time GetCleanupTimeout () const;
182
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700183 friend std::ostream& operator<< (std::ostream& os, const CcnxPit &fib);
184
185private:
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700186 Time m_cleanupTimeout; ///< \brief Configurable timeout of how often cleanup events are working
187 EventId m_cleanupEvent; ///< \brief Cleanup event
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800188
189 // configuration variables. Check implementation of GetTypeId for more details
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800190 Time m_PitEntryPruningTimout;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800191 Time m_PitEntryDefaultLifetime;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700192
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700193 Ptr<CcnxFib> m_fib; ///< \brief Link to FIB table
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700194};
195
196///////////////////////////////////////////////////////////////////////////////
197///////////////////////////////////////////////////////////////////////////////
198
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800199std::ostream& operator<< (std::ostream& os, const CcnxPit &pit);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700200
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700201/**
202 * @brief Class for exception when PIT entry is not found
203 */
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700204class CcnxPitEntryNotFound {};
205
206} // namespace ns3
207
208#endif /* CCNX_PIT_H */