blob: 54c177bde1e9bfd282fe5f48ae08f47e5459d256 [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 *
Alexander Afanasyev5dee3612015-08-25 16:09:04 -0700118 * \param interest Interest for which matching content store entry will be searched
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700119 *
120 * If an entry is found, it is promoted to the top of most recent
121 * used entries index, \see m_contentStore
122 */
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700123 virtual shared_ptr<Data>
124 Lookup(shared_ptr<const Interest> interest) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700125
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700126 /**
127 * \brief Add a new content to the content store.
Alexander Afanasyev5dee3612015-08-25 16:09:04 -0700128 * \returns true if an existing entry was updated, false otherwise
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700129 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700130 virtual bool
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700131 Add(shared_ptr<const Data> data) = 0;
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700132
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800133 // /*
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700134 // * \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 Mastorakis53e922f2014-10-17 17:29:26 -0700140 // Remove (shared_ptr<Interest> header) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700141
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700142 /**
143 * \brief Print out content store entries
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700144 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700145 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800146 Print(std::ostream& os) const = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700147
148 /**
149 * @brief Get number of entries in content store
150 */
151 virtual uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800152 GetSize() const = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700153
154 /**
155 * @brief Return first element of content store (no order guaranteed)
156 */
157 virtual Ptr<cs::Entry>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800158 Begin() = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700159
160 /**
161 * @brief Return item next after last (no order guaranteed)
162 */
163 virtual Ptr<cs::Entry>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800164 End() = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700165
166 /**
167 * @brief Advance the iterator
168 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800169 virtual Ptr<cs::Entry> Next(Ptr<cs::Entry>) = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700170
171 ////////////////////////////////////////////////////////////////////////////
172 ////////////////////////////////////////////////////////////////////////////
173 ////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700174
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700175 /**
176 * @brief Static call to cheat python bindings
177 */
178 static inline Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800179 GetContentStore(Ptr<Object> node);
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700180
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700181public:
182 typedef void (*CacheHitsCallback)(shared_ptr<const Interest>, shared_ptr<const Data>);
183 typedef void (*CacheMissesCallback)(shared_ptr<const Interest>);
184
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700185protected:
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700186 TracedCallback<shared_ptr<const Interest>,
187 shared_ptr<const Data>> m_cacheHitsTrace; ///< @brief trace of cache hits
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700188
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700189 TracedCallback<shared_ptr<const Interest>> m_cacheMissesTrace; ///< @brief trace of cache misses
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700190};
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700191
192inline std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800193operator<<(std::ostream& os, const ContentStore& cs)
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700194{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800195 cs.Print(os);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700196 return os;
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700197}
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700198
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700199inline Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800200ContentStore::GetContentStore(Ptr<Object> node)
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700201{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800202 return node->GetObject<ContentStore>();
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700203}
204
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700205} // namespace ndn
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700206} // namespace ns3
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700207
Spyridon Mastorakis1f1cd5e2014-12-04 11:12:40 -0800208#include <boost/functional/hash.hpp>
209namespace boost {
210inline std::size_t
211hash_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 Afanasyev4aac5572012-08-09 10:49:55 -0700218#endif // NDN_CONTENT_STORE_H