blob: 0d1132e2f0715dd9040d645507b35ee17c2cacd4 [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
28#include "hash-helper.h"
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>
38#include <boost/multi_index/sequenced_index.hpp>
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -070039#include <map>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070040#include <iostream>
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -070041#include <algorithm>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080042#include <boost/tuple/tuple.hpp>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070043
44namespace ns3 {
45
46class Ccnx;
47class CcnxFace;
48class CcnxContentObjectHeader;
49class CcnxInterestHeader;
50
51/**
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};
61
62/**
63 * \ingroup ccnx
64 * \brief Typedef for RIT container implemented as a Boost.MultiIndex container
65 *
66 * - First index (tag<i_prefix>) is a unique hash index based on
67 * prefixes
68 * - Second index (tag<i_timestamp>) is a sequenced index based on
69 * arrival order (for clean-up optimizations)
70 *
71 * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library
72 */
73struct CcnxPitEntryContainer
74{
75 typedef
76 boost::multi_index::multi_index_container<
77 CcnxPitEntry,
78 boost::multi_index::indexed_by<
79 // indexed by hash
80 boost::multi_index::hashed_unique<
81 boost::multi_index::tag<__ccnx_private::i_prefix>,
82 boost::multi_index::const_mem_fun<CcnxPitEntry, const CcnxNameComponents&, &CcnxPitEntry::GetPrefix>,
83 CcnxPrefixHash
84 >,
85 // sequenced to implement MRU
86 boost::multi_index::sequenced<
87 boost::multi_index::tag<__ccnx_private::i_timestamp> >
88 >
89 > type;
90};
91
92// typedef std::map<int,int> PitCounter;
93// typedef std::map<int,int>::iterator PitCounterIterator;
94
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080095typedef std::map<int,double> PitBucket;
96typedef std::map<int,double>::iterator PitBucketIterator;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070097
98////////////////////////////////////////////////////////////////////////
99////////////////////////////////////////////////////////////////////////
100
101/**
102 * \ingroup ccnx
103 * \brief Class implementing Pending Interests Table
104 */
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700105class CcnxPit : public CcnxPitEntryContainer::type, public Object
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700106{
107public:
108 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700109 * \brief Interface ID
110 *
111 * \return interface ID
112 */
113 static TypeId GetTypeId ();
114
115 /**
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700116 * \brief PIT constructor
117 */
118 CcnxPit ();
119
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800120 /**
121 * \brief Destructor
122 */
123 virtual ~CcnxPit ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800124
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700125 /**
126 * \brief Find corresponding PIT entry for the given content name
127 * \param prefix Prefix for which to lookup the entry
128 * \returns const reference to Pit entry. If record not found,
129 * CcnxPitEntryNotFound exception will be thrown
130 */
131 const CcnxPitEntry&
132 Lookup (const CcnxContentObjectHeader &header) const;
133
134 /**
135 * \brief Find corresponding PIT entry for the given content name
136 * \param prefix Prefix for which to lookup the entry
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800137 * \returns a tuple:
138 * get<0>: `const CcnxPitEntry&`: a valid PIT entry (if record does not exist, it will be created)
139 * get<1>: `bool`: true if a new entry was created
140 * get<2>: `bool`: true if a PIT entry exists and Nonce that present in header has been already seen
141 *
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700142 */
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800143 boost::tuple<const CcnxPitEntry&, bool, bool>
144 Lookup (const CcnxInterestHeader &header);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700145
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800146 Time GetPitEntryPruningTimeout () const
147 {
148 return m_PitEntryPruningTimout;
149 }
150
151 /**
152 * \brief Set FIB table
153 */
154 void SetFib (Ptr<CcnxFib> fib);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700155
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800156protected:
157 // inherited from Object class
158 virtual void NotifyNewAggregate ();
159 virtual void DoDispose ();
160
161private:
162 /** \brief Remove expired records from PIT */
163 void CleanExpired ();
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700164
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700165 /**
166 * \brief Set cleanup timeout
167 *
168 * Side effect: current clean up even (if any) will be cancelled and a new event started
169 *
170 * \param timeout cleanup timeout
171 */
172 void SetCleanupTimeout (const Time &timeout);
173
174 /**
175 * \brief Get cleanup timeout
176 *
177 * \returns cleanup timeout
178 */
179 Time GetCleanupTimeout () const;
180
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700181 friend std::ostream& operator<< (std::ostream& os, const CcnxPit &fib);
182
183private:
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700184 Time m_cleanupTimeout; ///< \brief Configurable timeout of how often cleanup events are working
185 EventId m_cleanupEvent; ///< \brief Cleanup event
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800186
187 // configuration variables. Check implementation of GetTypeId for more details
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800188 Time m_PitEntryPruningTimout;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800189 Time m_PitEntryDefaultLifetime;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700190
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700191 Ptr<CcnxFib> m_fib; ///< \brief Link to FIB table
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800192 // PitBucket m_bucketsPerFace; ///< \brief pending interface counter per face
193
194 // /**
195 // * \brief maximum number of buckets. Automatically computed based on link capacity
196 // * averaging over 1 second (bandwidth * 1second)
197 // */
198 // PitBucket maxBucketsPerFace;
199
200 // PitBucket leakSize; ///< size of a periodic bucket leak
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700201};
202
203///////////////////////////////////////////////////////////////////////////////
204///////////////////////////////////////////////////////////////////////////////
205
206std::ostream& operator<< (std::ostream& os, const CcnxPit &fib);
207std::ostream& operator<< (std::ostream& os, const CcnxPitEntry &entry);
208// std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
209
210class CcnxPitEntryNotFound {};
211
212} // namespace ns3
213
214#endif /* CCNX_PIT_H */