Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -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: Ilya Moiseenko <iliamo@cs.ucla.edu> |
| 19 | */ |
| 20 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 21 | #ifndef CCNX_CONTENT_STORE_H |
| 22 | #define CCNX_CONTENT_STORE_H |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 24 | #include "ns3/object.h" |
| 25 | #include "ns3/ptr.h" |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 26 | #include "ns3/packet.h" |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 27 | |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 28 | #include <list> |
| 29 | #include <string> |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 30 | #include <iostream> |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 32 | #include <boost/multi_index_container.hpp> |
| 33 | #include <boost/multi_index/tag.hpp> |
| 34 | #include <boost/multi_index/ordered_index.hpp> |
| 35 | #include <boost/multi_index/sequenced_index.hpp> |
| 36 | #include <boost/multi_index/hashed_index.hpp> |
| 37 | #include <boost/multi_index/mem_fun.hpp> |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 38 | #include <boost/tuple/tuple.hpp> |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 40 | #include "ccnx.h" |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 41 | #include "hash-helper.h" |
| 42 | #include "ccnx-content-object-header.h" |
| 43 | #include "ccnx-interest-header.h" |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 44 | #include "ccnx-name-components.h" |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 45 | |
| 46 | namespace ns3 |
| 47 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 48 | /** |
| 49 | * \ingroup ccnx |
| 50 | * \brief NDN content store entry |
| 51 | * |
| 52 | * Content store entry stores separately pseudo header and content of |
| 53 | * ContentObject packet. It is responsibility of the caller to |
| 54 | * construct a fully formed CcnxPacket by calling Copy(), AddHeader(), |
| 55 | * AddTail() on the packet received by GetPacket() method. |
| 56 | * |
| 57 | * GetFullyFormedCcnxPacket method provided as a convenience |
| 58 | */ |
| 59 | class CcnxContentStoreEntry |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 60 | { |
| 61 | public: |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 62 | /** |
| 63 | * \brief Construct content store entry |
| 64 | * |
| 65 | * \param header Parsed CcnxContentObject header |
| 66 | * \param packet Original CCNx packet |
| 67 | * |
| 68 | * The constructor will make a copy of the supplied packet and calls |
| 69 | * RemoveHeader and RemoveTail on the copy. |
| 70 | */ |
| 71 | CcnxContentStoreEntry (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet); |
| 72 | |
| 73 | /** |
| 74 | * \brief Get prefix of the stored entry |
| 75 | * \returns prefix of the stored entry |
| 76 | */ |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 77 | inline const CcnxNameComponents& |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 78 | GetName () const; |
| 79 | |
| 80 | /** |
| 81 | * \brief Get CcnxContentObjectHeader of the stored entry |
| 82 | * \returns CcnxContentObjectHeader of the stored entry |
| 83 | */ |
| 84 | inline Ptr<const CcnxContentObjectHeader> |
| 85 | GetHeader () const; |
| 86 | |
| 87 | /** |
| 88 | * \brief Get content of the stored entry |
| 89 | * \returns content of the stored entry |
| 90 | */ |
| 91 | inline Ptr<const Packet> |
| 92 | GetPacket () const; |
| 93 | |
| 94 | /** |
| 95 | * \brief Convenience method to create a fully formed CCNx packet from stored header and content |
| 96 | * \returns A read-write copy of the packet with CcnxContentObjectHeader and CcxnContentObjectTail |
| 97 | */ |
| 98 | Ptr<Packet> |
| 99 | GetFullyFormedCcnxPacket () const; |
| 100 | |
| 101 | // Copy constructor is required by the container. Though, we're |
| 102 | // storing only two pointers, so shouldn't be a problem |
| 103 | // private: |
| 104 | // CcnxContentStoreEntry (const CcnxContentStoreEntry &); ///< disabled copy constructor |
| 105 | // CcnxContentStoreEntry& operator= (const CcnxContentStoreEntry&); ///< disabled copy operator |
| 106 | |
| 107 | private: |
| 108 | Ptr<CcnxContentObjectHeader> m_header; ///< \brief non-modifiable CcnxContentObjectHeader |
| 109 | Ptr<Packet> m_packet; ///< \brief non-modifiable content of the ContentObject packet |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 112 | /** |
| 113 | * \ingroup ccnx |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 114 | * \brief Typedef for content store container implemented as a Boost.MultiIndex container |
| 115 | * |
| 116 | * - First index (tag<prefix>) is a unique hash index based on NDN prefix of the stored content. |
| 117 | * - Second index (tag<mru>) is a sequential index used to maintain up to m_maxSize most recent used (MRU) entries in the content store |
| 118 | * - Third index (tag<ordered>) is just a helper to provide stored prefixes in ordered way. Should be disabled in production build |
| 119 | * |
| 120 | * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library |
| 121 | */ |
| 122 | struct CcnxContentStoreContainer |
| 123 | { |
| 124 | typedef |
| 125 | boost::multi_index::multi_index_container< |
| 126 | CcnxContentStoreEntry, |
| 127 | boost::multi_index::indexed_by< |
| 128 | boost::multi_index::hashed_unique< |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 129 | boost::multi_index::tag<__ccnx_private::i_prefix>, |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 130 | boost::multi_index::const_mem_fun<CcnxContentStoreEntry, |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 131 | const CcnxNameComponents&, |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 132 | &CcnxContentStoreEntry::GetName>, |
| 133 | CcnxPrefixHash>, |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 134 | boost::multi_index::sequenced<boost::multi_index::tag<__ccnx_private::i_mru> > |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 135 | #ifdef _DEBUG |
| 136 | , |
| 137 | boost::multi_index::ordered_unique< |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 138 | boost::multi_index::tag<__ccnx_private::i_ordered>, |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 139 | boost::multi_index::const_mem_fun<CcnxContentStoreEntry, |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 140 | const CcnxNameComponents&, |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 141 | &CcnxContentStoreEntry::GetName> |
| 142 | > |
| 143 | #endif |
| 144 | > |
| 145 | > type; |
| 146 | }; |
| 147 | |
| 148 | /** |
| 149 | * \ingroup ccnx |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 150 | * \brief NDN content store entry |
| 151 | */ |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 152 | class CcnxContentStore : public Object |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 153 | { |
| 154 | public: |
| 155 | /** |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 156 | * \brief Interface ID |
| 157 | * |
| 158 | * \return interface ID |
| 159 | */ |
| 160 | static TypeId GetTypeId (); |
| 161 | |
| 162 | /** |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 163 | * Default constructor |
| 164 | */ |
| 165 | CcnxContentStore( ); |
| 166 | virtual ~CcnxContentStore( ); |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 167 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 168 | /** |
| 169 | * \brief Find corresponding CS entry for the given interest |
| 170 | * |
| 171 | * \param interest Interest for which matching content store entry |
| 172 | * will be searched |
| 173 | * |
| 174 | * If an entry is found, it is promoted to the top of most recent |
| 175 | * used entries index, \see m_contentStore |
| 176 | */ |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 177 | boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> > |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 178 | Lookup (Ptr<const CcnxInterestHeader> interest); |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 179 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 180 | /** |
| 181 | * \brief Add a new content to the content store. |
| 182 | * |
| 183 | * \param header Fully parsed CcnxContentObjectHeader |
| 184 | * \param packet Fully formed CCNx packet to add to content store |
| 185 | * (will be copied and stripped down of headers) |
| 186 | * |
| 187 | * If entry with the same prefix exists, the old entry will be |
| 188 | * promoted to the top of the MRU hash |
| 189 | */ |
| 190 | void |
| 191 | Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet); |
| 192 | |
| 193 | /** |
| 194 | * \brief Set maximum size of content store |
| 195 | * |
| 196 | * \param size size in packets |
| 197 | */ |
| 198 | inline void |
| 199 | SetMaxSize (uint32_t maxSize); |
| 200 | |
| 201 | /** |
| 202 | * \brief Get maximum size of content store |
| 203 | * |
| 204 | * \returns size in packets |
| 205 | */ |
| 206 | inline uint32_t |
| 207 | GetMaxSize () const; |
| 208 | |
| 209 | /** |
| 210 | * \brief Print out content store entries |
| 211 | * |
| 212 | * Debug build provides dumping of content store entries in |
| 213 | * lexicographical order of corresponding prefixes |
| 214 | * |
| 215 | * Release build dumps everything in MRU order |
| 216 | */ |
| 217 | void Print () const; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 218 | |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 219 | private: |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 220 | CcnxContentStore (const CcnxContentStore &o); ///< Disabled copy constructor |
| 221 | CcnxContentStore& operator= (const CcnxContentStore &o); ///< Disabled copy operator |
| 222 | |
| 223 | private: |
Alexander Afanasyev | dd32de8 | 2011-08-20 00:47:28 -0700 | [diff] [blame] | 224 | size_t m_maxSize; ///< \brief maximum number of entries in cache \internal |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 225 | // string_key_hash_t<CsEntry> m_contentStore; ///< \brief actual content store \internal |
| 226 | |
| 227 | /** |
| 228 | * \brief Content store implemented as a Boost.MultiIndex container |
| 229 | * \internal |
| 230 | */ |
Alexander Afanasyev | c86c283 | 2011-12-23 02:56:22 -0800 | [diff] [blame] | 231 | CcnxContentStoreContainer::type m_contentStore; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 232 | }; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 233 | |
| 234 | inline std::ostream& |
| 235 | operator<< (std::ostream &os, const CcnxContentStore &cs) |
| 236 | { |
| 237 | cs.Print (); |
| 238 | return os; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 239 | } |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 240 | |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 241 | const CcnxNameComponents& |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 242 | CcnxContentStoreEntry::GetName () const |
| 243 | { |
| 244 | return m_header->GetName (); |
| 245 | } |
| 246 | |
| 247 | Ptr<const CcnxContentObjectHeader> |
| 248 | CcnxContentStoreEntry::GetHeader () const |
| 249 | { |
| 250 | return m_header; |
| 251 | } |
| 252 | |
| 253 | Ptr<const Packet> |
| 254 | CcnxContentStoreEntry::GetPacket () const |
| 255 | { |
| 256 | return m_packet; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | inline void |
| 261 | CcnxContentStore::SetMaxSize (uint32_t maxSize) |
| 262 | { |
| 263 | m_maxSize = maxSize; |
| 264 | } |
| 265 | |
| 266 | inline uint32_t |
| 267 | CcnxContentStore::GetMaxSize () const |
| 268 | { |
| 269 | return m_maxSize; |
| 270 | } |
| 271 | |
| 272 | } //namespace ns3 |
| 273 | |
| 274 | #endif // CCNX_CONTENT_STORE_H |