blob: cdbb1ea85623b1a3efd697950fe7ba0c04351e9a [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * 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 Moiseenko1c570bc2011-08-17 19:18:02 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * 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 Moiseenko1c570bc2011-08-17 19:18:02 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * 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 Moiseenko1c570bc2011-08-17 19:18:02 -070019
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070020#ifndef NDN_CONTENT_STORE_H
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080021#define NDN_CONTENT_STORE_H
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070022
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070023#include "ns3/ndnSIM/model/ndn-common.hpp"
24
Alexander Afanasyev070aa482011-08-20 00:38:25 -070025#include "ns3/object.h"
26#include "ns3/ptr.h"
Alexander Afanasyev39485d82012-06-08 17:51:47 -070027#include "ns3/traced-callback.h"
28
Alexander Afanasyev07179012014-12-21 00:23:04 -080029#include <tuple>
Alexander Afanasyev070aa482011-08-20 00:38:25 -070030
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070031namespace ns3 {
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070032
33class Packet;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070034
35namespace ndn {
36
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070037class ContentStore;
38
Alexander Afanasyev79206512013-07-27 16:49:12 -070039/**
40 * @ingroup ndn
41 * @defgroup ndn-cs Content Store
42 */
43
44/**
45 * @ingroup ndn-cs
46 * @brief Namespace for ContentStore operations
47 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048namespace cs {
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070049
Alexander Afanasyev070aa482011-08-20 00:38:25 -070050/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070051 * @ingroup ndn-cs
52 * @brief NDN content store entry
Alexander Afanasyev070aa482011-08-20 00:38:25 -070053 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054class Entry : public SimpleRefCount<Entry> {
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070055public:
Alexander Afanasyev070aa482011-08-20 00:38:25 -070056 /**
57 * \brief Construct content store entry
58 *
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070059 * \param header Parsed Data header
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070060 * \param packet Original Ndn packet
Alexander Afanasyev070aa482011-08-20 00:38:25 -070061 *
62 * The constructor will make a copy of the supplied packet and calls
63 * RemoveHeader and RemoveTail on the copy.
64 */
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070065 Entry(Ptr<ContentStore> cs, shared_ptr<const Data> data);
Alexander Afanasyev070aa482011-08-20 00:38:25 -070066
67 /**
68 * \brief Get prefix of the stored entry
69 * \returns prefix of the stored entry
70 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070071 const Name&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 GetName() const;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070073
74 /**
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070075 * \brief Get Data of the stored entry
76 * \returns Data of the stored entry
Alexander Afanasyev070aa482011-08-20 00:38:25 -070077 */
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070078 shared_ptr<const Data>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 GetData() const;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070080
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070081 /**
82 * @brief Get pointer to access store, to which this entry is added
83 */
84 Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085 GetContentStore();
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070086
Alexander Afanasyev070aa482011-08-20 00:38:25 -070087private:
Spyridon Mastorakis1f1cd5e2014-12-04 11:12:40 -080088 Ptr<ContentStore> m_cs; ///< \brief content store to which entry is added
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070089 shared_ptr<const Data> m_data; ///< \brief non-modifiable Data
Alexander Afanasyev070aa482011-08-20 00:38:25 -070090};
91
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070092} // namespace cs
93
Alexander Afanasyev070aa482011-08-20 00:38:25 -070094/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070095 * @ingroup ndn-cs
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070096 * \brief Base class for NDN content store
97 *
98 * Particular implementations should implement Lookup, Add, and Print methods
Alexander Afanasyev070aa482011-08-20 00:38:25 -070099 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100class ContentStore : public Object {
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700101public:
102 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700103 * \brief Interface ID
104 *
105 * \return interface ID
106 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 static TypeId
108 GetTypeId();
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700109
110 /**
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700111 * @brief Virtual destructor
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700112 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113 virtual ~ContentStore();
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700114
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700115 /**
116 * \brief Find corresponding CS entry for the given interest
117 *
118 * \param interest Interest for which matching content store entry
119 * will be searched
120 *
121 * If an entry is found, it is promoted to the top of most recent
122 * used entries index, \see m_contentStore
123 */
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700124 virtual shared_ptr<Data>
125 Lookup(shared_ptr<const Interest> interest) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700126
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700127 /**
128 * \brief Add a new content to the content store.
129 *
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700130 * \param header Fully parsed Data
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700131 * \param packet Fully formed Ndn packet to add to content store
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700132 * (will be copied and stripped down of headers)
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700133 * @returns true if an existing entry was updated, false otherwise
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700134 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700135 virtual bool
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700136 Add(shared_ptr<const Data> data) = 0;
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700137
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800138 // /*
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700139 // * \brief Add a new content to the content store.
140 // *
141 // * \param header Interest header for which an entry should be removed
142 // * @returns true if an existing entry was removed, false otherwise
143 // */
144 // virtual bool
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700145 // Remove (shared_ptr<Interest> header) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700146
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700147 /**
148 * \brief Print out content store entries
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700149 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700150 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800151 Print(std::ostream& os) const = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700152
153 /**
154 * @brief Get number of entries in content store
155 */
156 virtual uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800157 GetSize() const = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700158
159 /**
160 * @brief Return first element of content store (no order guaranteed)
161 */
162 virtual Ptr<cs::Entry>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800163 Begin() = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700164
165 /**
166 * @brief Return item next after last (no order guaranteed)
167 */
168 virtual Ptr<cs::Entry>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800169 End() = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700170
171 /**
172 * @brief Advance the iterator
173 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800174 virtual Ptr<cs::Entry> Next(Ptr<cs::Entry>) = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700175
176 ////////////////////////////////////////////////////////////////////////////
177 ////////////////////////////////////////////////////////////////////////////
178 ////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700179
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700180 /**
181 * @brief Static call to cheat python bindings
182 */
183 static inline Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800184 GetContentStore(Ptr<Object> node);
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700185
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700186protected:
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700187 TracedCallback<shared_ptr<const Interest>,
188 shared_ptr<const Data>> m_cacheHitsTrace; ///< @brief trace of cache hits
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700189
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700190 TracedCallback<shared_ptr<const Interest>> m_cacheMissesTrace; ///< @brief trace of cache misses
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700191};
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700192
193inline std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800194operator<<(std::ostream& os, const ContentStore& cs)
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700195{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800196 cs.Print(os);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700197 return os;
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700198}
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700199
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700200inline Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800201ContentStore::GetContentStore(Ptr<Object> node)
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700202{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800203 return node->GetObject<ContentStore>();
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700204}
205
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700206} // namespace ndn
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700207} // namespace ns3
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700208
Spyridon Mastorakis1f1cd5e2014-12-04 11:12:40 -0800209#include <boost/functional/hash.hpp>
210namespace boost {
211inline std::size_t
212hash_value(const ::ndn::name::Component component)
213{
214 return boost::hash_range(component.wireEncode().wire(),
215 component.wireEncode().wire() + component.wireEncode().size());
216}
217}
218
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700219#endif // NDN_CONTENT_STORE_H