blob: 23266c47e458692aeb2b2b2f366838190b851df3 [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
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 Afanasyeva46844b2011-11-21 19:13:26 -080029#include <boost/tuple/tuple.hpp>
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 Afanasyev772f51b2013-08-01 18:53:25 -070037class Data;
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070038class Interest;
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070039class Name;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070040class ContentStore;
41
Alexander Afanasyev79206512013-07-27 16:49:12 -070042/**
43 * @ingroup ndn
44 * @defgroup ndn-cs Content Store
45 */
46
47/**
48 * @ingroup ndn-cs
49 * @brief Namespace for ContentStore operations
50 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070051namespace cs {
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070052
Alexander Afanasyev070aa482011-08-20 00:38:25 -070053/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070054 * @ingroup ndn-cs
55 * @brief NDN content store entry
Alexander Afanasyev070aa482011-08-20 00:38:25 -070056 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057class Entry : public SimpleRefCount<Entry> {
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070058public:
Alexander Afanasyev070aa482011-08-20 00:38:25 -070059 /**
60 * \brief Construct content store entry
61 *
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070062 * \param header Parsed Data header
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070063 * \param packet Original Ndn packet
Alexander Afanasyev070aa482011-08-20 00:38:25 -070064 *
65 * The constructor will make a copy of the supplied packet and calls
66 * RemoveHeader and RemoveTail on the copy.
67 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 Entry(Ptr<ContentStore> cs, Ptr<const Data> data);
Alexander Afanasyev070aa482011-08-20 00:38:25 -070069
70 /**
71 * \brief Get prefix of the stored entry
72 * \returns prefix of the stored entry
73 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070074 const Name&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 GetName() const;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070076
77 /**
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070078 * \brief Get Data of the stored entry
79 * \returns Data of the stored entry
Alexander Afanasyev070aa482011-08-20 00:38:25 -070080 */
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070081 Ptr<const Data>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 GetData() const;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070083
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070084 /**
85 * @brief Get pointer to access store, to which this entry is added
86 */
87 Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 GetContentStore();
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070089
Alexander Afanasyev070aa482011-08-20 00:38:25 -070090private:
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070091 Ptr<ContentStore> m_cs; ///< \brief content store to which entry is added
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070092 Ptr<const Data> m_data; ///< \brief non-modifiable Data
Alexander Afanasyev070aa482011-08-20 00:38:25 -070093};
94
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095} // namespace cs
96
Alexander Afanasyev070aa482011-08-20 00:38:25 -070097/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070098 * @ingroup ndn-cs
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070099 * \brief Base class for NDN content store
100 *
101 * Particular implementations should implement Lookup, Add, and Print methods
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700102 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103class ContentStore : public Object {
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700104public:
105 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700106 * \brief Interface ID
107 *
108 * \return interface ID
109 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 static TypeId
111 GetTypeId();
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700112
113 /**
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700114 * @brief Virtual destructor
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700115 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 virtual ~ContentStore();
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700117
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700118 /**
119 * \brief Find corresponding CS entry for the given interest
120 *
121 * \param interest Interest for which matching content store entry
122 * will be searched
123 *
124 * If an entry is found, it is promoted to the top of most recent
125 * used entries index, \see m_contentStore
126 */
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700127 virtual Ptr<Data>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 Lookup(Ptr<const Interest> interest) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700129
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700130 /**
131 * \brief Add a new content to the content store.
132 *
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700133 * \param header Fully parsed Data
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700134 * \param packet Fully formed Ndn packet to add to content store
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700135 * (will be copied and stripped down of headers)
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700136 * @returns true if an existing entry was updated, false otherwise
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700137 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700138 virtual bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800139 Add(Ptr<const Data> data) = 0;
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700140
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800141 // /*
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700142 // * \brief Add a new content to the content store.
143 // *
144 // * \param header Interest header for which an entry should be removed
145 // * @returns true if an existing entry was removed, false otherwise
146 // */
147 // virtual bool
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700148 // Remove (Ptr<Interest> header) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700149
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700150 /**
151 * \brief Print out content store entries
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700152 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700153 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800154 Print(std::ostream& os) const = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700155
156 /**
157 * @brief Get number of entries in content store
158 */
159 virtual uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800160 GetSize() const = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700161
162 /**
163 * @brief Return first element of content store (no order guaranteed)
164 */
165 virtual Ptr<cs::Entry>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800166 Begin() = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700167
168 /**
169 * @brief Return item next after last (no order guaranteed)
170 */
171 virtual Ptr<cs::Entry>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800172 End() = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700173
174 /**
175 * @brief Advance the iterator
176 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800177 virtual Ptr<cs::Entry> Next(Ptr<cs::Entry>) = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700178
179 ////////////////////////////////////////////////////////////////////////////
180 ////////////////////////////////////////////////////////////////////////////
181 ////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700182
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700183 /**
184 * @brief Static call to cheat python bindings
185 */
186 static inline Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800187 GetContentStore(Ptr<Object> node);
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700188
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700189protected:
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700190 TracedCallback<Ptr<const Interest>,
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800191 Ptr<const Data>> m_cacheHitsTrace; ///< @brief trace of cache hits
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700192
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800193 TracedCallback<Ptr<const Interest>> m_cacheMissesTrace; ///< @brief trace of cache misses
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700194};
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700195
196inline std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800197operator<<(std::ostream& os, const ContentStore& cs)
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700198{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800199 cs.Print(os);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700200 return os;
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700201}
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700202
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700203inline Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800204ContentStore::GetContentStore(Ptr<Object> node)
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700205{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800206 return node->GetObject<ContentStore>();
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700207}
208
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700209} // namespace ndn
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700210} // namespace ns3
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700211
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700212#endif // NDN_CONTENT_STORE_H