blob: d364917505a1f7cd10aa6a72ef2f7181dd7a9a44 [file] [log] [blame]
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -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: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 */
20
Alexander Afanasyev070aa482011-08-20 00:38:25 -070021#ifndef CCNX_CONTENT_STORE_H
22#define CCNX_CONTENT_STORE_H
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070023
Alexander Afanasyev070aa482011-08-20 00:38:25 -070024#include "ns3/object.h"
25#include "ns3/ptr.h"
26
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070027#include <list>
28#include <string>
Alexander Afanasyev070aa482011-08-20 00:38:25 -070029#include <iostream>
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070030
Alexander Afanasyev070aa482011-08-20 00:38:25 -070031#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/mem_fun.hpp>
37
38#include "hash-helper.h"
39#include "ccnx-content-object-header.h"
40#include "ccnx-interest-header.h"
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070041#include "ccnx-name-components.h"
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070042
43namespace ns3
44{
Alexander Afanasyev070aa482011-08-20 00:38:25 -070045class Packet;
46
47/**
48 * \ingroup ccnx
49 * \brief NDN content store entry
50 *
51 * Content store entry stores separately pseudo header and content of
52 * ContentObject packet. It is responsibility of the caller to
53 * construct a fully formed CcnxPacket by calling Copy(), AddHeader(),
54 * AddTail() on the packet received by GetPacket() method.
55 *
56 * GetFullyFormedCcnxPacket method provided as a convenience
57 */
58class CcnxContentStoreEntry
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070059{
60public:
Alexander Afanasyev070aa482011-08-20 00:38:25 -070061 /**
62 * \brief Construct content store entry
63 *
64 * \param header Parsed CcnxContentObject header
65 * \param packet Original CCNx packet
66 *
67 * The constructor will make a copy of the supplied packet and calls
68 * RemoveHeader and RemoveTail on the copy.
69 */
70 CcnxContentStoreEntry (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet);
71
72 /**
73 * \brief Get prefix of the stored entry
74 * \returns prefix of the stored entry
75 */
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -070076 inline const CcnxNameComponents&
Alexander Afanasyev070aa482011-08-20 00:38:25 -070077 GetName () const;
78
79 /**
80 * \brief Get CcnxContentObjectHeader of the stored entry
81 * \returns CcnxContentObjectHeader of the stored entry
82 */
83 inline Ptr<const CcnxContentObjectHeader>
84 GetHeader () const;
85
86 /**
87 * \brief Get content of the stored entry
88 * \returns content of the stored entry
89 */
90 inline Ptr<const Packet>
91 GetPacket () const;
92
93 /**
94 * \brief Convenience method to create a fully formed CCNx packet from stored header and content
95 * \returns A read-write copy of the packet with CcnxContentObjectHeader and CcxnContentObjectTail
96 */
97 Ptr<Packet>
98 GetFullyFormedCcnxPacket () const;
99
100// Copy constructor is required by the container. Though, we're
101// storing only two pointers, so shouldn't be a problem
102// private:
103// CcnxContentStoreEntry (const CcnxContentStoreEntry &); ///< disabled copy constructor
104// CcnxContentStoreEntry& operator= (const CcnxContentStoreEntry&); ///< disabled copy operator
105
106private:
107 Ptr<CcnxContentObjectHeader> m_header; ///< \brief non-modifiable CcnxContentObjectHeader
108 Ptr<Packet> m_packet; ///< \brief non-modifiable content of the ContentObject packet
109
110 static CcnxContentObjectTail m_tail; ///< \internal for optimization purposes
111};
112
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700113/**
114 * \ingroup ccnx
115 * \brief Private namespace for CCNx content store implementation
116 */
117namespace __ccnx_private_content_store
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700118{
119class hash {}; ///< tag for Boost.Multiindex container (hashed prefix)
120class mru {}; ///< tag for Boost.Multiindex container (most recent used index)
121#ifdef _DEBUG
122class ordered {}; ///< tag for Boost.MultiIndex container (ordered by prefix)
123#endif
124};
125
126/**
127 * \ingroup ccnx
128 * \brief Typedef for content store container implemented as a Boost.MultiIndex container
129 *
130 * - First index (tag<prefix>) is a unique hash index based on NDN prefix of the stored content.
131 * - Second index (tag<mru>) is a sequential index used to maintain up to m_maxSize most recent used (MRU) entries in the content store
132 * - Third index (tag<ordered>) is just a helper to provide stored prefixes in ordered way. Should be disabled in production build
133 *
134 * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library
135 */
136struct CcnxContentStoreContainer
137{
138 typedef
139 boost::multi_index::multi_index_container<
140 CcnxContentStoreEntry,
141 boost::multi_index::indexed_by<
142 boost::multi_index::hashed_unique<
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700143 boost::multi_index::tag<__ccnx_private_content_store::hash>,
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700144 boost::multi_index::const_mem_fun<CcnxContentStoreEntry,
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -0700145 const CcnxNameComponents&,
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700146 &CcnxContentStoreEntry::GetName>,
147 CcnxPrefixHash>,
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700148 boost::multi_index::sequenced<boost::multi_index::tag<__ccnx_private_content_store::mru> >
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700149#ifdef _DEBUG
150 ,
151 boost::multi_index::ordered_unique<
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700152 boost::multi_index::tag<__ccnx_private_content_store::ordered>,
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700153 boost::multi_index::const_mem_fun<CcnxContentStoreEntry,
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -0700154 const CcnxNameComponents&,
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700155 &CcnxContentStoreEntry::GetName>
156 >
157#endif
158 >
159 > type;
160};
161
162/**
163 * \ingroup ccnx
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700164 * \brief NDN content store entry
165 */
166class CcnxContentStore : public Object
167{
168public:
169 /**
170 * \brief Interface ID
171 *
172 * \return interface ID
173 */
174 static TypeId GetTypeId ();
175
176 // typedef string_key_hash_t<CsEntry>::iterator CsIterator;
177 // typedef string_key_hash_t<CsEntry>::iterator CsRangeIterator;
178
179 /**
180 * Default constructor
181 */
182 CcnxContentStore( );
183 virtual ~CcnxContentStore( );
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700184
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700185 /**
186 * \brief Find corresponding CS entry for the given interest
187 *
188 * \param interest Interest for which matching content store entry
189 * will be searched
190 *
191 * If an entry is found, it is promoted to the top of most recent
192 * used entries index, \see m_contentStore
193 */
194 Ptr<Packet>
195 Lookup (Ptr<const CcnxInterestHeader> interest);
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700196
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700197 /**
198 * \brief Add a new content to the content store.
199 *
200 * \param header Fully parsed CcnxContentObjectHeader
201 * \param packet Fully formed CCNx packet to add to content store
202 * (will be copied and stripped down of headers)
203 *
204 * If entry with the same prefix exists, the old entry will be
205 * promoted to the top of the MRU hash
206 */
207 void
208 Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet);
209
210 /**
211 * \brief Set maximum size of content store
212 *
213 * \param size size in packets
214 */
215 inline void
216 SetMaxSize (uint32_t maxSize);
217
218 /**
219 * \brief Get maximum size of content store
220 *
221 * \returns size in packets
222 */
223 inline uint32_t
224 GetMaxSize () const;
225
226 /**
227 * \brief Print out content store entries
228 *
229 * Debug build provides dumping of content store entries in
230 * lexicographical order of corresponding prefixes
231 *
232 * Release build dumps everything in MRU order
233 */
234 void Print () const;
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700235
236protected:
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700237 // /**
238 // * \brief Move the given CS entry to the head of the list
239 // *
240 // * \param entry Content Store entry
241 // */
242 // void Promote( CsEntry &entry );
243
244 /**
245 * \todo Alex: DoDispose and NotifyNewAggregate are seem to be very
246 * important, but I'm not yet sure what exactly they are supposed to
247 * do
248 */
249 // virtual void DoDispose ();
250 // virtual void NotifyNewAggregate ();
251
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700252private:
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700253 CcnxContentStore (const CcnxContentStore &o); ///< Disabled copy constructor
254 CcnxContentStore& operator= (const CcnxContentStore &o); ///< Disabled copy operator
255
256private:
Alexander Afanasyevdd32de82011-08-20 00:47:28 -0700257 size_t m_maxSize; ///< \brief maximum number of entries in cache \internal
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700258 // string_key_hash_t<CsEntry> m_contentStore; ///< \brief actual content store \internal
259
260 /**
261 * \brief Content store implemented as a Boost.MultiIndex container
262 * \internal
263 */
264 CcnxContentStoreContainer::type m_contentStore;
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700265};
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700266
267inline std::ostream&
268operator<< (std::ostream &os, const CcnxContentStore &cs)
269{
270 cs.Print ();
271 return os;
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700272}
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700273
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -0700274const CcnxNameComponents&
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700275CcnxContentStoreEntry::GetName () const
276{
277 return m_header->GetName ();
278}
279
280Ptr<const CcnxContentObjectHeader>
281CcnxContentStoreEntry::GetHeader () const
282{
283 return m_header;
284}
285
286Ptr<const Packet>
287CcnxContentStoreEntry::GetPacket () const
288{
289 return m_packet;
290}
291
292
293inline void
294CcnxContentStore::SetMaxSize (uint32_t maxSize)
295{
296 m_maxSize = maxSize;
297}
298
299inline uint32_t
300CcnxContentStore::GetMaxSize () const
301{
302 return m_maxSize;
303}
304
305} //namespace ns3
306
307#endif // CCNX_CONTENT_STORE_H