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 |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 23 | #define NDN_CONTENT_STORE_H |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 24 | |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 25 | #include "ns3/ndnSIM/model/ndn-common.hpp" |
| 26 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 27 | #include "ns3/object.h" |
| 28 | #include "ns3/ptr.h" |
Alexander Afanasyev | 39485d8 | 2012-06-08 17:51:47 -0700 | [diff] [blame] | 29 | #include "ns3/traced-callback.h" |
| 30 | |
Alexander Afanasyev | 0717901 | 2014-12-21 00:23:04 -0800 | [diff] [blame^] | 31 | #include <tuple> |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 33 | namespace ns3 { |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 34 | |
| 35 | class Packet; |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
| 38 | |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 39 | class ContentStore; |
| 40 | |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 41 | /** |
| 42 | * @ingroup ndn |
| 43 | * @defgroup ndn-cs Content Store |
| 44 | */ |
| 45 | |
| 46 | /** |
| 47 | * @ingroup ndn-cs |
| 48 | * @brief Namespace for ContentStore operations |
| 49 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 50 | namespace cs { |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 52 | /** |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 53 | * @ingroup ndn-cs |
| 54 | * @brief NDN content store entry |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 55 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 56 | class Entry : public SimpleRefCount<Entry> { |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 57 | public: |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 58 | /** |
| 59 | * \brief Construct content store entry |
| 60 | * |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 61 | * \param header Parsed Data header |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 62 | * \param packet Original Ndn packet |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 63 | * |
| 64 | * The constructor will make a copy of the supplied packet and calls |
| 65 | * RemoveHeader and RemoveTail on the copy. |
| 66 | */ |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 67 | Entry(Ptr<ContentStore> cs, shared_ptr<const Data> data); |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * \brief Get prefix of the stored entry |
| 71 | * \returns prefix of the stored entry |
| 72 | */ |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 73 | const Name& |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | GetName() const; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 77 | * \brief Get Data of the stored entry |
| 78 | * \returns Data of the stored entry |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 79 | */ |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 80 | shared_ptr<const Data> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 81 | GetData() const; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 82 | |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 83 | /** |
| 84 | * @brief Get pointer to access store, to which this entry is added |
| 85 | */ |
| 86 | Ptr<ContentStore> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 87 | GetContentStore(); |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 88 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 89 | private: |
Spyridon Mastorakis | 1f1cd5e | 2014-12-04 11:12:40 -0800 | [diff] [blame] | 90 | Ptr<ContentStore> m_cs; ///< \brief content store to which entry is added |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 91 | shared_ptr<const Data> m_data; ///< \brief non-modifiable Data |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 94 | } // namespace cs |
| 95 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 96 | /** |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 97 | * @ingroup ndn-cs |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 98 | * \brief Base class for NDN content store |
| 99 | * |
| 100 | * Particular implementations should implement Lookup, Add, and Print methods |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 101 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 102 | class ContentStore : public Object { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 103 | public: |
| 104 | /** |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 105 | * \brief Interface ID |
| 106 | * |
| 107 | * \return interface ID |
| 108 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 109 | static TypeId |
| 110 | GetTypeId(); |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 113 | * @brief Virtual destructor |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 114 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 115 | virtual ~ContentStore(); |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 116 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 117 | /** |
| 118 | * \brief Find corresponding CS entry for the given interest |
| 119 | * |
| 120 | * \param interest Interest for which matching content store entry |
| 121 | * will be searched |
| 122 | * |
| 123 | * If an entry is found, it is promoted to the top of most recent |
| 124 | * used entries index, \see m_contentStore |
| 125 | */ |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 126 | virtual shared_ptr<Data> |
| 127 | Lookup(shared_ptr<const Interest> interest) = 0; |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 128 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 129 | /** |
| 130 | * \brief Add a new content to the content store. |
| 131 | * |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 132 | * \param header Fully parsed Data |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 133 | * \param packet Fully formed Ndn packet to add to content store |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 134 | * (will be copied and stripped down of headers) |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 135 | * @returns true if an existing entry was updated, false otherwise |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 136 | */ |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 137 | virtual bool |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 138 | Add(shared_ptr<const Data> data) = 0; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 139 | |
Alexander Afanasyev | 042b4a7 | 2012-11-09 17:47:48 -0800 | [diff] [blame] | 140 | // /* |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 141 | // * \brief Add a new content to the content store. |
| 142 | // * |
| 143 | // * \param header Interest header for which an entry should be removed |
| 144 | // * @returns true if an existing entry was removed, false otherwise |
| 145 | // */ |
| 146 | // virtual bool |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 147 | // Remove (shared_ptr<Interest> header) = 0; |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 148 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 149 | /** |
| 150 | * \brief Print out content store entries |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 151 | */ |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 152 | virtual void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 153 | Print(std::ostream& os) const = 0; |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * @brief Get number of entries in content store |
| 157 | */ |
| 158 | virtual uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 159 | GetSize() const = 0; |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * @brief Return first element of content store (no order guaranteed) |
| 163 | */ |
| 164 | virtual Ptr<cs::Entry> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 165 | Begin() = 0; |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * @brief Return item next after last (no order guaranteed) |
| 169 | */ |
| 170 | virtual Ptr<cs::Entry> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 171 | End() = 0; |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * @brief Advance the iterator |
| 175 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 176 | virtual Ptr<cs::Entry> Next(Ptr<cs::Entry>) = 0; |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 177 | |
| 178 | //////////////////////////////////////////////////////////////////////////// |
| 179 | //////////////////////////////////////////////////////////////////////////// |
| 180 | //////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 181 | |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 182 | /** |
| 183 | * @brief Static call to cheat python bindings |
| 184 | */ |
| 185 | static inline Ptr<ContentStore> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 186 | GetContentStore(Ptr<Object> node); |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 187 | |
Alexander Afanasyev | 39485d8 | 2012-06-08 17:51:47 -0700 | [diff] [blame] | 188 | protected: |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 189 | TracedCallback<shared_ptr<const Interest>, |
| 190 | shared_ptr<const Data>> m_cacheHitsTrace; ///< @brief trace of cache hits |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 191 | |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 192 | TracedCallback<shared_ptr<const Interest>> m_cacheMissesTrace; ///< @brief trace of cache misses |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 193 | }; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 194 | |
| 195 | inline std::ostream& |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 196 | operator<<(std::ostream& os, const ContentStore& cs) |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 197 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 198 | cs.Print(os); |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 199 | return os; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 200 | } |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 201 | |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 202 | inline Ptr<ContentStore> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 203 | ContentStore::GetContentStore(Ptr<Object> node) |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 204 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 205 | return node->GetObject<ContentStore>(); |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 208 | } // namespace ndn |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 209 | } // namespace ns3 |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 210 | |
Spyridon Mastorakis | 1f1cd5e | 2014-12-04 11:12:40 -0800 | [diff] [blame] | 211 | #include <boost/functional/hash.hpp> |
| 212 | namespace boost { |
| 213 | inline std::size_t |
| 214 | hash_value(const ::ndn::name::Component component) |
| 215 | { |
| 216 | return boost::hash_range(component.wireEncode().wire(), |
| 217 | component.wireEncode().wire() + component.wireEncode().size()); |
| 218 | } |
| 219 | } |
| 220 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 221 | #endif // NDN_CONTENT_STORE_H |