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