blob: f1e0ef7ab2c5a5a266a08c41df17ec155a80cb88 [file] [log] [blame]
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -07003 * Copyright (c) 2011,2012 University of California, Los Angeles
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -07004 *
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 Afanasyevd9fecdd2012-06-08 16:22:24 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070020 */
21
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#ifndef NDN_CONTENT_STORE_H
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080023#define NDN_CONTENT_STORE_H
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070024
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070025#include "ns3/ndnSIM/model/ndn-common.hpp"
26
Alexander Afanasyev070aa482011-08-20 00:38:25 -070027#include "ns3/object.h"
28#include "ns3/ptr.h"
Alexander Afanasyev39485d82012-06-08 17:51:47 -070029#include "ns3/traced-callback.h"
30
Alexander Afanasyev07179012014-12-21 00:23:04 -080031#include <tuple>
Alexander Afanasyev070aa482011-08-20 00:38:25 -070032
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033namespace ns3 {
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070034
35class Packet;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036
37namespace ndn {
38
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070039class ContentStore;
40
Alexander Afanasyev79206512013-07-27 16:49:12 -070041/**
42 * @ingroup ndn
43 * @defgroup ndn-cs Content Store
44 */
45
46/**
47 * @ingroup ndn-cs
48 * @brief Namespace for ContentStore operations
49 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070050namespace cs {
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070051
Alexander Afanasyev070aa482011-08-20 00:38:25 -070052/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070053 * @ingroup ndn-cs
54 * @brief NDN content store entry
Alexander Afanasyev070aa482011-08-20 00:38:25 -070055 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080056class Entry : public SimpleRefCount<Entry> {
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070057public:
Alexander Afanasyev070aa482011-08-20 00:38:25 -070058 /**
59 * \brief Construct content store entry
60 *
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070061 * \param header Parsed Data header
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070062 * \param packet Original Ndn packet
Alexander Afanasyev070aa482011-08-20 00:38:25 -070063 *
64 * The constructor will make a copy of the supplied packet and calls
65 * RemoveHeader and RemoveTail on the copy.
66 */
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070067 Entry(Ptr<ContentStore> cs, shared_ptr<const Data> data);
Alexander Afanasyev070aa482011-08-20 00:38:25 -070068
69 /**
70 * \brief Get prefix of the stored entry
71 * \returns prefix of the stored entry
72 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070073 const Name&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 GetName() const;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070075
76 /**
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070077 * \brief Get Data of the stored entry
78 * \returns Data of the stored entry
Alexander Afanasyev070aa482011-08-20 00:38:25 -070079 */
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070080 shared_ptr<const Data>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 GetData() const;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070082
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070083 /**
84 * @brief Get pointer to access store, to which this entry is added
85 */
86 Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 GetContentStore();
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070088
Alexander Afanasyev070aa482011-08-20 00:38:25 -070089private:
Spyridon Mastorakis1f1cd5e2014-12-04 11:12:40 -080090 Ptr<ContentStore> m_cs; ///< \brief content store to which entry is added
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070091 shared_ptr<const Data> m_data; ///< \brief non-modifiable Data
Alexander Afanasyev070aa482011-08-20 00:38:25 -070092};
93
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070094} // namespace cs
95
Alexander Afanasyev070aa482011-08-20 00:38:25 -070096/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070097 * @ingroup ndn-cs
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070098 * \brief Base class for NDN content store
99 *
100 * Particular implementations should implement Lookup, Add, and Print methods
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700101 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102class ContentStore : public Object {
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700103public:
104 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700105 * \brief Interface ID
106 *
107 * \return interface ID
108 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 static TypeId
110 GetTypeId();
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700111
112 /**
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700113 * @brief Virtual destructor
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700114 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 virtual ~ContentStore();
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700116
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700117 /**
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 Mastorakis53e922f2014-10-17 17:29:26 -0700126 virtual shared_ptr<Data>
127 Lookup(shared_ptr<const Interest> interest) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700128
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700129 /**
130 * \brief Add a new content to the content store.
131 *
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700132 * \param header Fully parsed Data
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700133 * \param packet Fully formed Ndn packet to add to content store
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700134 * (will be copied and stripped down of headers)
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700135 * @returns true if an existing entry was updated, false otherwise
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700136 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700137 virtual bool
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700138 Add(shared_ptr<const Data> data) = 0;
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700139
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800140 // /*
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700141 // * \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 Mastorakis53e922f2014-10-17 17:29:26 -0700147 // Remove (shared_ptr<Interest> header) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700148
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700149 /**
150 * \brief Print out content store entries
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700151 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700152 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800153 Print(std::ostream& os) const = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700154
155 /**
156 * @brief Get number of entries in content store
157 */
158 virtual uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800159 GetSize() const = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700160
161 /**
162 * @brief Return first element of content store (no order guaranteed)
163 */
164 virtual Ptr<cs::Entry>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800165 Begin() = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700166
167 /**
168 * @brief Return item next after last (no order guaranteed)
169 */
170 virtual Ptr<cs::Entry>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800171 End() = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700172
173 /**
174 * @brief Advance the iterator
175 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800176 virtual Ptr<cs::Entry> Next(Ptr<cs::Entry>) = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700177
178 ////////////////////////////////////////////////////////////////////////////
179 ////////////////////////////////////////////////////////////////////////////
180 ////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700181
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700182 /**
183 * @brief Static call to cheat python bindings
184 */
185 static inline Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800186 GetContentStore(Ptr<Object> node);
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700187
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700188protected:
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700189 TracedCallback<shared_ptr<const Interest>,
190 shared_ptr<const Data>> m_cacheHitsTrace; ///< @brief trace of cache hits
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700191
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700192 TracedCallback<shared_ptr<const Interest>> m_cacheMissesTrace; ///< @brief trace of cache misses
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700193};
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700194
195inline std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800196operator<<(std::ostream& os, const ContentStore& cs)
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700197{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800198 cs.Print(os);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700199 return os;
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700200}
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700201
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700202inline Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800203ContentStore::GetContentStore(Ptr<Object> node)
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700204{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800205 return node->GetObject<ContentStore>();
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700206}
207
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700208} // namespace ndn
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700209} // namespace ns3
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700210
Spyridon Mastorakis1f1cd5e2014-12-04 11:12:40 -0800211#include <boost/functional/hash.hpp>
212namespace boost {
213inline std::size_t
214hash_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 Afanasyev4aac5572012-08-09 10:49:55 -0700221#endif // NDN_CONTENT_STORE_H