Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 19 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 20 | #ifndef NDN_CONTENT_STORE_H |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 21 | #define NDN_CONTENT_STORE_H |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 22 | |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 23 | #include "ns3/ndnSIM/model/ndn-common.hpp" |
| 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 | 0717901 | 2014-12-21 00:23:04 -0800 | [diff] [blame] | 29 | #include <tuple> |
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 | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 37 | class ContentStore; |
| 38 | |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 39 | /** |
| 40 | * @ingroup ndn |
| 41 | * @defgroup ndn-cs Content Store |
| 42 | */ |
| 43 | |
| 44 | /** |
| 45 | * @ingroup ndn-cs |
| 46 | * @brief Namespace for ContentStore operations |
| 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 | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 51 | * @ingroup ndn-cs |
| 52 | * @brief NDN content store entry |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 53 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 54 | class Entry : public SimpleRefCount<Entry> { |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 55 | public: |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 56 | /** |
| 57 | * \brief Construct content store entry |
| 58 | * |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 59 | * \param header Parsed Data header |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 60 | * \param packet Original Ndn packet |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 61 | * |
| 62 | * The constructor will make a copy of the supplied packet and calls |
| 63 | * RemoveHeader and RemoveTail on the copy. |
| 64 | */ |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 65 | Entry(Ptr<ContentStore> cs, shared_ptr<const Data> data); |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * \brief Get prefix of the stored entry |
| 69 | * \returns prefix of the stored entry |
| 70 | */ |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 71 | const Name& |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 72 | GetName() const; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 75 | * \brief Get Data of the stored entry |
| 76 | * \returns Data of the stored entry |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 77 | */ |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 78 | shared_ptr<const Data> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 79 | GetData() const; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 80 | |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 81 | /** |
| 82 | * @brief Get pointer to access store, to which this entry is added |
| 83 | */ |
| 84 | Ptr<ContentStore> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 85 | GetContentStore(); |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 87 | private: |
Spyridon Mastorakis | 1f1cd5e | 2014-12-04 11:12:40 -0800 | [diff] [blame] | 88 | Ptr<ContentStore> m_cs; ///< \brief content store to which entry is added |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 89 | shared_ptr<const Data> m_data; ///< \brief non-modifiable Data |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 92 | } // namespace cs |
| 93 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 94 | /** |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 95 | * @ingroup ndn-cs |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 96 | * \brief Base class for NDN content store |
| 97 | * |
| 98 | * Particular implementations should implement Lookup, Add, and Print methods |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 99 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 100 | class ContentStore : public Object { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 101 | public: |
| 102 | /** |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 103 | * \brief Interface ID |
| 104 | * |
| 105 | * \return interface ID |
| 106 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 107 | static TypeId |
| 108 | GetTypeId(); |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 111 | * @brief Virtual destructor |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 112 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 113 | virtual ~ContentStore(); |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 114 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 115 | /** |
| 116 | * \brief Find corresponding CS entry for the given interest |
| 117 | * |
Alexander Afanasyev | 5dee361 | 2015-08-25 16:09:04 -0700 | [diff] [blame^] | 118 | * \param interest Interest for which matching content store entry will be searched |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 119 | * |
| 120 | * If an entry is found, it is promoted to the top of most recent |
| 121 | * used entries index, \see m_contentStore |
| 122 | */ |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 123 | virtual shared_ptr<Data> |
| 124 | Lookup(shared_ptr<const Interest> interest) = 0; |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 126 | /** |
| 127 | * \brief Add a new content to the content store. |
Alexander Afanasyev | 5dee361 | 2015-08-25 16:09:04 -0700 | [diff] [blame^] | 128 | * \returns true if an existing entry was updated, false otherwise |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 129 | */ |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 130 | virtual bool |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 131 | Add(shared_ptr<const Data> data) = 0; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 132 | |
Alexander Afanasyev | 042b4a7 | 2012-11-09 17:47:48 -0800 | [diff] [blame] | 133 | // /* |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 134 | // * \brief Add a new content to the content store. |
| 135 | // * |
| 136 | // * \param header Interest header for which an entry should be removed |
| 137 | // * @returns true if an existing entry was removed, false otherwise |
| 138 | // */ |
| 139 | // virtual bool |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 140 | // Remove (shared_ptr<Interest> header) = 0; |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 141 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 142 | /** |
| 143 | * \brief Print out content store entries |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 144 | */ |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 145 | virtual void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 146 | Print(std::ostream& os) const = 0; |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * @brief Get number of entries in content store |
| 150 | */ |
| 151 | virtual uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 152 | GetSize() const = 0; |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * @brief Return first element of content store (no order guaranteed) |
| 156 | */ |
| 157 | virtual Ptr<cs::Entry> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 158 | Begin() = 0; |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * @brief Return item next after last (no order guaranteed) |
| 162 | */ |
| 163 | virtual Ptr<cs::Entry> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 164 | End() = 0; |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * @brief Advance the iterator |
| 168 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 169 | virtual Ptr<cs::Entry> Next(Ptr<cs::Entry>) = 0; |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 170 | |
| 171 | //////////////////////////////////////////////////////////////////////////// |
| 172 | //////////////////////////////////////////////////////////////////////////// |
| 173 | //////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 174 | |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 175 | /** |
| 176 | * @brief Static call to cheat python bindings |
| 177 | */ |
| 178 | static inline Ptr<ContentStore> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 179 | GetContentStore(Ptr<Object> node); |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 180 | |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 181 | public: |
| 182 | typedef void (*CacheHitsCallback)(shared_ptr<const Interest>, shared_ptr<const Data>); |
| 183 | typedef void (*CacheMissesCallback)(shared_ptr<const Interest>); |
| 184 | |
Alexander Afanasyev | 39485d8 | 2012-06-08 17:51:47 -0700 | [diff] [blame] | 185 | protected: |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 186 | TracedCallback<shared_ptr<const Interest>, |
| 187 | shared_ptr<const Data>> m_cacheHitsTrace; ///< @brief trace of cache hits |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 188 | |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 189 | TracedCallback<shared_ptr<const Interest>> m_cacheMissesTrace; ///< @brief trace of cache misses |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 190 | }; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 191 | |
| 192 | inline std::ostream& |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 193 | operator<<(std::ostream& os, const ContentStore& cs) |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 194 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 195 | cs.Print(os); |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 196 | return os; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 197 | } |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 199 | inline Ptr<ContentStore> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 200 | ContentStore::GetContentStore(Ptr<Object> node) |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 201 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 202 | return node->GetObject<ContentStore>(); |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 205 | } // namespace ndn |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 206 | } // namespace ns3 |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 207 | |
Spyridon Mastorakis | 1f1cd5e | 2014-12-04 11:12:40 -0800 | [diff] [blame] | 208 | #include <boost/functional/hash.hpp> |
| 209 | namespace boost { |
| 210 | inline std::size_t |
| 211 | hash_value(const ::ndn::name::Component component) |
| 212 | { |
| 213 | return boost::hash_range(component.wireEncode().wire(), |
| 214 | component.wireEncode().wire() + component.wireEncode().size()); |
| 215 | } |
| 216 | } |
| 217 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 218 | #endif // NDN_CONTENT_STORE_H |