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" |
| 26 | |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 27 | #include <list> |
| 28 | #include <string> |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame^] | 29 | #include <iostream> |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame^] | 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/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" |
| 41 | #include "name-components.h" |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 42 | |
| 43 | namespace ns3 |
| 44 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame^] | 45 | class 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 | */ |
| 58 | class CcnxContentStoreEntry |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 59 | { |
| 60 | public: |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame^] | 61 | /** |
| 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 | */ |
| 76 | inline const Name::Components& |
| 77 | 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 | |
| 106 | private: |
| 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 | |
| 113 | /// \brief Private namespace for CCNx implementation |
| 114 | namespace __ccnx_private |
| 115 | { |
| 116 | class hash {}; ///< tag for Boost.Multiindex container (hashed prefix) |
| 117 | class mru {}; ///< tag for Boost.Multiindex container (most recent used index) |
| 118 | #ifdef _DEBUG |
| 119 | class ordered {}; ///< tag for Boost.MultiIndex container (ordered by prefix) |
| 120 | #endif |
| 121 | }; |
| 122 | |
| 123 | /** |
| 124 | * \ingroup ccnx |
| 125 | * \brief Typedef for content store container implemented as a Boost.MultiIndex container |
| 126 | * |
| 127 | * - First index (tag<prefix>) is a unique hash index based on NDN prefix of the stored content. |
| 128 | * - Second index (tag<mru>) is a sequential index used to maintain up to m_maxSize most recent used (MRU) entries in the content store |
| 129 | * - Third index (tag<ordered>) is just a helper to provide stored prefixes in ordered way. Should be disabled in production build |
| 130 | * |
| 131 | * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library |
| 132 | */ |
| 133 | struct CcnxContentStoreContainer |
| 134 | { |
| 135 | typedef |
| 136 | boost::multi_index::multi_index_container< |
| 137 | CcnxContentStoreEntry, |
| 138 | boost::multi_index::indexed_by< |
| 139 | boost::multi_index::hashed_unique< |
| 140 | boost::multi_index::tag<__ccnx_private::hash>, |
| 141 | boost::multi_index::const_mem_fun<CcnxContentStoreEntry, |
| 142 | const Name::Components&, |
| 143 | &CcnxContentStoreEntry::GetName>, |
| 144 | CcnxPrefixHash>, |
| 145 | boost::multi_index::sequenced<boost::multi_index::tag<__ccnx_private::mru> > |
| 146 | #ifdef _DEBUG |
| 147 | , |
| 148 | boost::multi_index::ordered_unique< |
| 149 | boost::multi_index::tag<__ccnx_private::ordered>, |
| 150 | boost::multi_index::const_mem_fun<CcnxContentStoreEntry, |
| 151 | const Name::Components&, |
| 152 | &CcnxContentStoreEntry::GetName> |
| 153 | > |
| 154 | #endif |
| 155 | > |
| 156 | > type; |
| 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * \ingroup ccnx |
| 161 | * \brief Typedef for hash index of content store container |
| 162 | */ |
| 163 | struct CcnxContentStoreByPrefix |
| 164 | { |
| 165 | typedef |
| 166 | CcnxContentStoreContainer::type::index<__ccnx_private::hash>::type |
| 167 | type; |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * \ingroup ccnx |
| 172 | * \brief Typedef for MRU index of content store container |
| 173 | */ |
| 174 | struct CcnxContentStoreByMru |
| 175 | { |
| 176 | typedef |
| 177 | CcnxContentStoreContainer::type::index<__ccnx_private::mru>::type |
| 178 | type; |
| 179 | }; |
| 180 | |
| 181 | #ifdef _DEBUG |
| 182 | #define DUMP_INDEX_TAG ordered |
| 183 | #define DUMP_INDEX CcnxContentStoreOrderedPrefix |
| 184 | /** |
| 185 | * \ingroup ccnx |
| 186 | * \brief Typedef for ordered index of content store container |
| 187 | */ |
| 188 | struct CcnxContentStoreOrderedPrefix |
| 189 | { |
| 190 | typedef |
| 191 | CcnxContentStoreContainer::type::index<__ccnx_private::ordered>::type |
| 192 | type; |
| 193 | }; |
| 194 | #else |
| 195 | #define DUMP_INDEX_TAG mru |
| 196 | #define DUMP_INDEX CcnxContentStoreByMru |
| 197 | #endif |
| 198 | |
| 199 | /** |
| 200 | * \ingroup ccnx |
| 201 | * \brief NDN content store entry |
| 202 | */ |
| 203 | class CcnxContentStore : public Object |
| 204 | { |
| 205 | public: |
| 206 | /** |
| 207 | * \brief Interface ID |
| 208 | * |
| 209 | * \return interface ID |
| 210 | */ |
| 211 | static TypeId GetTypeId (); |
| 212 | |
| 213 | // typedef string_key_hash_t<CsEntry>::iterator CsIterator; |
| 214 | // typedef string_key_hash_t<CsEntry>::iterator CsRangeIterator; |
| 215 | |
| 216 | /** |
| 217 | * Default constructor |
| 218 | */ |
| 219 | CcnxContentStore( ); |
| 220 | virtual ~CcnxContentStore( ); |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 221 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame^] | 222 | /** |
| 223 | * \brief Find corresponding CS entry for the given interest |
| 224 | * |
| 225 | * \param interest Interest for which matching content store entry |
| 226 | * will be searched |
| 227 | * |
| 228 | * If an entry is found, it is promoted to the top of most recent |
| 229 | * used entries index, \see m_contentStore |
| 230 | */ |
| 231 | Ptr<Packet> |
| 232 | Lookup (Ptr<const CcnxInterestHeader> interest); |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 233 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame^] | 234 | /** |
| 235 | * \brief Add a new content to the content store. |
| 236 | * |
| 237 | * \param header Fully parsed CcnxContentObjectHeader |
| 238 | * \param packet Fully formed CCNx packet to add to content store |
| 239 | * (will be copied and stripped down of headers) |
| 240 | * |
| 241 | * If entry with the same prefix exists, the old entry will be |
| 242 | * promoted to the top of the MRU hash |
| 243 | */ |
| 244 | void |
| 245 | Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet); |
| 246 | |
| 247 | /** |
| 248 | * \brief Set maximum size of content store |
| 249 | * |
| 250 | * \param size size in packets |
| 251 | */ |
| 252 | inline void |
| 253 | SetMaxSize (uint32_t maxSize); |
| 254 | |
| 255 | /** |
| 256 | * \brief Get maximum size of content store |
| 257 | * |
| 258 | * \returns size in packets |
| 259 | */ |
| 260 | inline uint32_t |
| 261 | GetMaxSize () const; |
| 262 | |
| 263 | /** |
| 264 | * \brief Print out content store entries |
| 265 | * |
| 266 | * Debug build provides dumping of content store entries in |
| 267 | * lexicographical order of corresponding prefixes |
| 268 | * |
| 269 | * Release build dumps everything in MRU order |
| 270 | */ |
| 271 | void Print () const; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 272 | |
| 273 | protected: |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame^] | 274 | // /** |
| 275 | // * \brief Move the given CS entry to the head of the list |
| 276 | // * |
| 277 | // * \param entry Content Store entry |
| 278 | // */ |
| 279 | // void Promote( CsEntry &entry ); |
| 280 | |
| 281 | /** |
| 282 | * \todo Alex: DoDispose and NotifyNewAggregate are seem to be very |
| 283 | * important, but I'm not yet sure what exactly they are supposed to |
| 284 | * do |
| 285 | */ |
| 286 | // virtual void DoDispose (); |
| 287 | // virtual void NotifyNewAggregate (); |
| 288 | |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 289 | private: |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame^] | 290 | CcnxContentStore (const CcnxContentStore &o); ///< Disabled copy constructor |
| 291 | CcnxContentStore& operator= (const CcnxContentStore &o); ///< Disabled copy operator |
| 292 | |
| 293 | private: |
| 294 | int m_maxSize; ///< \brief maximum number of entries in cache \internal |
| 295 | // string_key_hash_t<CsEntry> m_contentStore; ///< \brief actual content store \internal |
| 296 | |
| 297 | /** |
| 298 | * \brief Content store implemented as a Boost.MultiIndex container |
| 299 | * \internal |
| 300 | */ |
| 301 | CcnxContentStoreContainer::type m_contentStore; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 302 | }; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame^] | 303 | |
| 304 | inline std::ostream& |
| 305 | operator<< (std::ostream &os, const CcnxContentStore &cs) |
| 306 | { |
| 307 | cs.Print (); |
| 308 | return os; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 309 | } |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame^] | 310 | |
| 311 | const Name::Components& |
| 312 | CcnxContentStoreEntry::GetName () const |
| 313 | { |
| 314 | return m_header->GetName (); |
| 315 | } |
| 316 | |
| 317 | Ptr<const CcnxContentObjectHeader> |
| 318 | CcnxContentStoreEntry::GetHeader () const |
| 319 | { |
| 320 | return m_header; |
| 321 | } |
| 322 | |
| 323 | Ptr<const Packet> |
| 324 | CcnxContentStoreEntry::GetPacket () const |
| 325 | { |
| 326 | return m_packet; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | inline void |
| 331 | CcnxContentStore::SetMaxSize (uint32_t maxSize) |
| 332 | { |
| 333 | m_maxSize = maxSize; |
| 334 | } |
| 335 | |
| 336 | inline uint32_t |
| 337 | CcnxContentStore::GetMaxSize () const |
| 338 | { |
| 339 | return m_maxSize; |
| 340 | } |
| 341 | |
| 342 | } //namespace ns3 |
| 343 | |
| 344 | #endif // CCNX_CONTENT_STORE_H |