Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 3 | * Copyright (c) 2011,2012 University of California, Los Angeles |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 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 | * |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Ilya Moiseenko <iliamo@cs.ucla.edu> |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 22 | #ifndef NDN_CONTENT_STORE_H |
| 23 | #define NDN_CONTENT_STORE_H |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 25 | #include "ns3/object.h" |
| 26 | #include "ns3/ptr.h" |
Alexander Afanasyev | 39485d8 | 2012-06-08 17:51:47 -0700 | [diff] [blame] | 27 | #include "ns3/traced-callback.h" |
| 28 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 29 | #include <boost/tuple/tuple.hpp> |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 31 | namespace ns3 { |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 32 | |
| 33 | class Packet; |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 37 | class ContentObject; |
| 38 | class Interest; |
Alexander Afanasyev | 73f06f6 | 2013-03-15 15:41:38 -0700 | [diff] [blame] | 39 | |
| 40 | typedef Interest InterestHeader; |
| 41 | typedef ContentObject ContentObjectHeader; |
| 42 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 43 | class Name; |
Alexander Afanasyev | 73f06f6 | 2013-03-15 15:41:38 -0700 | [diff] [blame] | 44 | typedef Name NameComponents; |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 46 | class ContentStore; |
| 47 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 48 | namespace cs { |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 50 | /** |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 51 | * \ingroup ndn |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 52 | * \brief NDN content store entry |
| 53 | * |
| 54 | * Content store entry stores separately pseudo header and content of |
| 55 | * ContentObject packet. It is responsibility of the caller to |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 56 | * construct a fully formed NDN Packet by calling Copy(), AddHeader(), |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 57 | * AddTail() on the packet received by GetPacket() method. |
| 58 | * |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 59 | * GetFullyFormedNdnPacket method provided as a convenience |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 60 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 61 | class Entry : public SimpleRefCount<Entry> |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 62 | { |
| 63 | public: |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 64 | /** |
| 65 | * \brief Construct content store entry |
| 66 | * |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 67 | * \param header Parsed ContentObject header |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 68 | * \param packet Original Ndn packet |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 69 | * |
| 70 | * The constructor will make a copy of the supplied packet and calls |
| 71 | * RemoveHeader and RemoveTail on the copy. |
| 72 | */ |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 73 | Entry (Ptr<ContentStore> cs, Ptr<const ContentObject> header, Ptr<const Packet> packet); |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * \brief Get prefix of the stored entry |
| 77 | * \returns prefix of the stored entry |
| 78 | */ |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 79 | const Name& |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 80 | GetName () const; |
| 81 | |
| 82 | /** |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 83 | * \brief Get ContentObject of the stored entry |
| 84 | * \returns ContentObject of the stored entry |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 85 | */ |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 86 | Ptr<const ContentObject> |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 87 | GetHeader () const; |
| 88 | |
| 89 | /** |
| 90 | * \brief Get content of the stored entry |
| 91 | * \returns content of the stored entry |
| 92 | */ |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 93 | Ptr<const Packet> |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 94 | GetPacket () const; |
| 95 | |
| 96 | /** |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 97 | * \brief Convenience method to create a fully formed Ndn packet from stored header and content |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 98 | * \returns A read-write copy of the packet with ContentObject and ContentObjectTail |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 99 | */ |
| 100 | Ptr<Packet> |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 101 | GetFullyFormedNdnPacket () const; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 103 | /** |
| 104 | * @brief Get pointer to access store, to which this entry is added |
| 105 | */ |
| 106 | Ptr<ContentStore> |
| 107 | GetContentStore (); |
| 108 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 109 | private: |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 110 | Ptr<ContentStore> m_cs; ///< \brief content store to which entry is added |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 111 | Ptr<const ContentObject> m_header; ///< \brief non-modifiable ContentObject |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 112 | Ptr<Packet> m_packet; ///< \brief non-modifiable content of the ContentObject packet |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 115 | } // namespace cs |
| 116 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 119 | * \ingroup ndn |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 120 | * \brief Base class for NDN content store |
| 121 | * |
| 122 | * Particular implementations should implement Lookup, Add, and Print methods |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 123 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 124 | class ContentStore : public Object |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 125 | { |
| 126 | public: |
| 127 | /** |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 128 | * \brief Interface ID |
| 129 | * |
| 130 | * \return interface ID |
| 131 | */ |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 132 | static |
| 133 | TypeId GetTypeId (); |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 134 | |
| 135 | /** |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 136 | * @brief Virtual destructor |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 137 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 138 | virtual |
| 139 | ~ContentStore (); |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 140 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 141 | /** |
| 142 | * \brief Find corresponding CS entry for the given interest |
| 143 | * |
| 144 | * \param interest Interest for which matching content store entry |
| 145 | * will be searched |
| 146 | * |
| 147 | * If an entry is found, it is promoted to the top of most recent |
| 148 | * used entries index, \see m_contentStore |
| 149 | */ |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 150 | virtual boost::tuple<Ptr<Packet>, Ptr<const ContentObject>, Ptr<const Packet> > |
| 151 | Lookup (Ptr<const Interest> interest) = 0; |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 152 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 153 | /** |
| 154 | * \brief Add a new content to the content store. |
| 155 | * |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 156 | * \param header Fully parsed ContentObject |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 157 | * \param packet Fully formed Ndn packet to add to content store |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 158 | * (will be copied and stripped down of headers) |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 159 | * @returns true if an existing entry was updated, false otherwise |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 160 | */ |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 161 | virtual bool |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 162 | Add (Ptr<const ContentObject> header, Ptr<const Packet> packet) = 0; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 163 | |
Alexander Afanasyev | 042b4a7 | 2012-11-09 17:47:48 -0800 | [diff] [blame] | 164 | // /* |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 165 | // * \brief Add a new content to the content store. |
| 166 | // * |
| 167 | // * \param header Interest header for which an entry should be removed |
| 168 | // * @returns true if an existing entry was removed, false otherwise |
| 169 | // */ |
| 170 | // virtual bool |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 171 | // Remove (Ptr<Interest> header) = 0; |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 172 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 173 | /** |
| 174 | * \brief Print out content store entries |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 175 | */ |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 176 | virtual void |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 177 | Print (std::ostream &os) const = 0; |
Alexander Afanasyev | 39485d8 | 2012-06-08 17:51:47 -0700 | [diff] [blame] | 178 | |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * @brief Get number of entries in content store |
| 182 | */ |
| 183 | virtual uint32_t |
| 184 | GetSize () const = 0; |
| 185 | |
| 186 | /** |
| 187 | * @brief Return first element of content store (no order guaranteed) |
| 188 | */ |
| 189 | virtual Ptr<cs::Entry> |
| 190 | Begin () = 0; |
| 191 | |
| 192 | /** |
| 193 | * @brief Return item next after last (no order guaranteed) |
| 194 | */ |
| 195 | virtual Ptr<cs::Entry> |
| 196 | End () = 0; |
| 197 | |
| 198 | /** |
| 199 | * @brief Advance the iterator |
| 200 | */ |
| 201 | virtual Ptr<cs::Entry> |
| 202 | Next (Ptr<cs::Entry>) = 0; |
| 203 | |
| 204 | //////////////////////////////////////////////////////////////////////////// |
| 205 | //////////////////////////////////////////////////////////////////////////// |
| 206 | //////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 207 | |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 208 | /** |
| 209 | * @brief Static call to cheat python bindings |
| 210 | */ |
| 211 | static inline Ptr<ContentStore> |
| 212 | GetContentStore (Ptr<Object> node); |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 213 | |
Alexander Afanasyev | 39485d8 | 2012-06-08 17:51:47 -0700 | [diff] [blame] | 214 | protected: |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 215 | TracedCallback<Ptr<const Interest>, |
| 216 | Ptr<const ContentObject> > m_cacheHitsTrace; ///< @brief trace of cache hits |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 217 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 218 | TracedCallback<Ptr<const Interest> > m_cacheMissesTrace; ///< @brief trace of cache misses |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 219 | }; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 220 | |
| 221 | inline std::ostream& |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 222 | operator<< (std::ostream &os, const ContentStore &cs) |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 223 | { |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 224 | cs.Print (os); |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 225 | return os; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 226 | } |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 227 | |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 228 | inline Ptr<ContentStore> |
| 229 | ContentStore::GetContentStore (Ptr<Object> node) |
| 230 | { |
| 231 | return node->GetObject<ContentStore> (); |
| 232 | } |
| 233 | |
| 234 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 235 | } // namespace ndn |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 236 | } // namespace ns3 |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 237 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 238 | #endif // NDN_CONTENT_STORE_H |