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 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 39 | #include "ccnx.h" |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 40 | #include "hash-helper.h" |
| 41 | #include "ccnx-content-object-header.h" |
| 42 | #include "ccnx-interest-header.h" |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 43 | #include "ccnx-name-components.h" |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 44 | |
| 45 | namespace ns3 |
| 46 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 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 | */ |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 76 | inline const CcnxNameComponents& |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 113 | /** |
| 114 | * \ingroup ccnx |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 115 | * \brief Typedef for content store container implemented as a Boost.MultiIndex container |
| 116 | * |
| 117 | * - First index (tag<prefix>) is a unique hash index based on NDN prefix of the stored content. |
| 118 | * - Second index (tag<mru>) is a sequential index used to maintain up to m_maxSize most recent used (MRU) entries in the content store |
| 119 | * - Third index (tag<ordered>) is just a helper to provide stored prefixes in ordered way. Should be disabled in production build |
| 120 | * |
| 121 | * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library |
| 122 | */ |
| 123 | struct CcnxContentStoreContainer |
| 124 | { |
| 125 | typedef |
| 126 | boost::multi_index::multi_index_container< |
| 127 | CcnxContentStoreEntry, |
| 128 | boost::multi_index::indexed_by< |
| 129 | boost::multi_index::hashed_unique< |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 130 | boost::multi_index::tag<__ccnx_private::i_prefix>, |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 131 | boost::multi_index::const_mem_fun<CcnxContentStoreEntry, |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 132 | const CcnxNameComponents&, |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 133 | &CcnxContentStoreEntry::GetName>, |
| 134 | CcnxPrefixHash>, |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 135 | boost::multi_index::sequenced<boost::multi_index::tag<__ccnx_private::i_mru> > |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 136 | #ifdef _DEBUG |
| 137 | , |
| 138 | boost::multi_index::ordered_unique< |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 139 | boost::multi_index::tag<__ccnx_private::i_ordered>, |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 140 | boost::multi_index::const_mem_fun<CcnxContentStoreEntry, |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 141 | const CcnxNameComponents&, |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 142 | &CcnxContentStoreEntry::GetName> |
| 143 | > |
| 144 | #endif |
| 145 | > |
| 146 | > type; |
| 147 | }; |
| 148 | |
| 149 | /** |
| 150 | * \ingroup ccnx |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 151 | * \brief NDN content store entry |
| 152 | */ |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 153 | class CcnxContentStore |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 154 | { |
| 155 | public: |
| 156 | /** |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 157 | * Default constructor |
| 158 | */ |
| 159 | CcnxContentStore( ); |
| 160 | virtual ~CcnxContentStore( ); |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 162 | /** |
| 163 | * \brief Find corresponding CS entry for the given interest |
| 164 | * |
| 165 | * \param interest Interest for which matching content store entry |
| 166 | * will be searched |
| 167 | * |
| 168 | * If an entry is found, it is promoted to the top of most recent |
| 169 | * used entries index, \see m_contentStore |
| 170 | */ |
| 171 | Ptr<Packet> |
| 172 | Lookup (Ptr<const CcnxInterestHeader> interest); |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 173 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 174 | /** |
| 175 | * \brief Add a new content to the content store. |
| 176 | * |
| 177 | * \param header Fully parsed CcnxContentObjectHeader |
| 178 | * \param packet Fully formed CCNx packet to add to content store |
| 179 | * (will be copied and stripped down of headers) |
| 180 | * |
| 181 | * If entry with the same prefix exists, the old entry will be |
| 182 | * promoted to the top of the MRU hash |
| 183 | */ |
| 184 | void |
| 185 | Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet); |
| 186 | |
| 187 | /** |
| 188 | * \brief Set maximum size of content store |
| 189 | * |
| 190 | * \param size size in packets |
| 191 | */ |
| 192 | inline void |
| 193 | SetMaxSize (uint32_t maxSize); |
| 194 | |
| 195 | /** |
| 196 | * \brief Get maximum size of content store |
| 197 | * |
| 198 | * \returns size in packets |
| 199 | */ |
| 200 | inline uint32_t |
| 201 | GetMaxSize () const; |
| 202 | |
| 203 | /** |
| 204 | * \brief Print out content store entries |
| 205 | * |
| 206 | * Debug build provides dumping of content store entries in |
| 207 | * lexicographical order of corresponding prefixes |
| 208 | * |
| 209 | * Release build dumps everything in MRU order |
| 210 | */ |
| 211 | void Print () const; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 212 | |
| 213 | protected: |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 214 | // /** |
| 215 | // * \brief Move the given CS entry to the head of the list |
| 216 | // * |
| 217 | // * \param entry Content Store entry |
| 218 | // */ |
| 219 | // void Promote( CsEntry &entry ); |
| 220 | |
| 221 | /** |
| 222 | * \todo Alex: DoDispose and NotifyNewAggregate are seem to be very |
| 223 | * important, but I'm not yet sure what exactly they are supposed to |
| 224 | * do |
| 225 | */ |
| 226 | // virtual void DoDispose (); |
| 227 | // virtual void NotifyNewAggregate (); |
| 228 | |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 229 | private: |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 230 | CcnxContentStore (const CcnxContentStore &o); ///< Disabled copy constructor |
| 231 | CcnxContentStore& operator= (const CcnxContentStore &o); ///< Disabled copy operator |
| 232 | |
| 233 | private: |
Alexander Afanasyev | dd32de8 | 2011-08-20 00:47:28 -0700 | [diff] [blame] | 234 | size_t m_maxSize; ///< \brief maximum number of entries in cache \internal |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 235 | // string_key_hash_t<CsEntry> m_contentStore; ///< \brief actual content store \internal |
| 236 | |
| 237 | /** |
| 238 | * \brief Content store implemented as a Boost.MultiIndex container |
| 239 | * \internal |
| 240 | */ |
| 241 | CcnxContentStoreContainer::type m_contentStore; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 242 | }; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 243 | |
| 244 | inline std::ostream& |
| 245 | operator<< (std::ostream &os, const CcnxContentStore &cs) |
| 246 | { |
| 247 | cs.Print (); |
| 248 | return os; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 249 | } |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 250 | |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 251 | const CcnxNameComponents& |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 252 | CcnxContentStoreEntry::GetName () const |
| 253 | { |
| 254 | return m_header->GetName (); |
| 255 | } |
| 256 | |
| 257 | Ptr<const CcnxContentObjectHeader> |
| 258 | CcnxContentStoreEntry::GetHeader () const |
| 259 | { |
| 260 | return m_header; |
| 261 | } |
| 262 | |
| 263 | Ptr<const Packet> |
| 264 | CcnxContentStoreEntry::GetPacket () const |
| 265 | { |
| 266 | return m_packet; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | inline void |
| 271 | CcnxContentStore::SetMaxSize (uint32_t maxSize) |
| 272 | { |
| 273 | m_maxSize = maxSize; |
| 274 | } |
| 275 | |
| 276 | inline uint32_t |
| 277 | CcnxContentStore::GetMaxSize () const |
| 278 | { |
| 279 | return m_maxSize; |
| 280 | } |
| 281 | |
| 282 | } //namespace ns3 |
| 283 | |
| 284 | #endif // CCNX_CONTENT_STORE_H |