blob: 66e766729f583db178a8f7f0b54ac52e2163f2a2 [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
23#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 Afanasyeveae83ee2013-03-15 15:01:10 -070037class ContentObject;
38class Interest;
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070039class Name;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070040class ContentStore;
41
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042namespace cs {
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070043
Alexander Afanasyev070aa482011-08-20 00:38:25 -070044/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070045 * \ingroup ndn
Alexander Afanasyev070aa482011-08-20 00:38:25 -070046 * \brief NDN content store entry
Alexander Afanasyev070aa482011-08-20 00:38:25 -070047 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048class Entry : public SimpleRefCount<Entry>
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070049{
50public:
Alexander Afanasyev070aa482011-08-20 00:38:25 -070051 /**
52 * \brief Construct content store entry
53 *
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070054 * \param header Parsed ContentObject header
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070055 * \param packet Original Ndn packet
Alexander Afanasyev070aa482011-08-20 00:38:25 -070056 *
57 * The constructor will make a copy of the supplied packet and calls
58 * RemoveHeader and RemoveTail on the copy.
59 */
Alexander Afanasyevb989b122013-07-10 17:15:46 -070060 Entry (Ptr<ContentStore> cs, Ptr<const ContentObject> data);
Alexander Afanasyev070aa482011-08-20 00:38:25 -070061
62 /**
63 * \brief Get prefix of the stored entry
64 * \returns prefix of the stored entry
65 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070066 const Name&
Alexander Afanasyev070aa482011-08-20 00:38:25 -070067 GetName () const;
68
69 /**
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070070 * \brief Get ContentObject of the stored entry
71 * \returns ContentObject of the stored entry
Alexander Afanasyev070aa482011-08-20 00:38:25 -070072 */
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070073 Ptr<const ContentObject>
Alexander Afanasyevb989b122013-07-10 17:15:46 -070074 GetData () const;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070075
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070076 /**
77 * @brief Get pointer to access store, to which this entry is added
78 */
79 Ptr<ContentStore>
80 GetContentStore ();
81
Alexander Afanasyev070aa482011-08-20 00:38:25 -070082private:
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070083 Ptr<ContentStore> m_cs; ///< \brief content store to which entry is added
Alexander Afanasyevb989b122013-07-10 17:15:46 -070084 Ptr<const ContentObject> m_data; ///< \brief non-modifiable ContentObject
Alexander Afanasyev070aa482011-08-20 00:38:25 -070085};
86
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070087} // namespace cs
88
Alexander Afanasyev070aa482011-08-20 00:38:25 -070089
90/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070091 * \ingroup ndn
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070092 * \brief Base class for NDN content store
93 *
94 * Particular implementations should implement Lookup, Add, and Print methods
Alexander Afanasyev070aa482011-08-20 00:38:25 -070095 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096class ContentStore : public Object
Alexander Afanasyev070aa482011-08-20 00:38:25 -070097{
98public:
99 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700100 * \brief Interface ID
101 *
102 * \return interface ID
103 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700104 static
105 TypeId GetTypeId ();
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700106
107 /**
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700108 * @brief Virtual destructor
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700109 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700110 virtual
111 ~ContentStore ();
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700112
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700113 /**
114 * \brief Find corresponding CS entry for the given interest
115 *
116 * \param interest Interest for which matching content store entry
117 * will be searched
118 *
119 * If an entry is found, it is promoted to the top of most recent
120 * used entries index, \see m_contentStore
121 */
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700122 virtual Ptr<ContentObject>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700123 Lookup (Ptr<const Interest> interest) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700124
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700125 /**
126 * \brief Add a new content to the content store.
127 *
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700128 * \param header Fully parsed ContentObject
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700129 * \param packet Fully formed Ndn packet to add to content store
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700130 * (will be copied and stripped down of headers)
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700131 * @returns true if an existing entry was updated, false otherwise
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700132 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700133 virtual bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700134 Add (Ptr<const ContentObject> data) = 0;
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700135
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800136 // /*
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700137 // * \brief Add a new content to the content store.
138 // *
139 // * \param header Interest header for which an entry should be removed
140 // * @returns true if an existing entry was removed, false otherwise
141 // */
142 // virtual bool
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700143 // Remove (Ptr<Interest> header) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700144
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700145 /**
146 * \brief Print out content store entries
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700147 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700148 virtual void
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700149 Print (std::ostream &os) const = 0;
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700150
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700151
152 /**
153 * @brief Get number of entries in content store
154 */
155 virtual uint32_t
156 GetSize () const = 0;
157
158 /**
159 * @brief Return first element of content store (no order guaranteed)
160 */
161 virtual Ptr<cs::Entry>
162 Begin () = 0;
163
164 /**
165 * @brief Return item next after last (no order guaranteed)
166 */
167 virtual Ptr<cs::Entry>
168 End () = 0;
169
170 /**
171 * @brief Advance the iterator
172 */
173 virtual Ptr<cs::Entry>
174 Next (Ptr<cs::Entry>) = 0;
175
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>
184 GetContentStore (Ptr<Object> node);
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700185
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700186protected:
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700187 TracedCallback<Ptr<const Interest>,
188 Ptr<const ContentObject> > m_cacheHitsTrace; ///< @brief trace of cache hits
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700189
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700190 TracedCallback<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 Afanasyev2b4c9472012-08-09 15:00:38 -0700194operator<< (std::ostream &os, const ContentStore &cs)
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700195{
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700196 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>
201ContentStore::GetContentStore (Ptr<Object> node)
202{
203 return node->GetObject<ContentStore> ();
204}
205
206
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700207} // namespace ndn
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700208} // namespace ns3
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700209
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700210#endif // NDN_CONTENT_STORE_H