blob: e28eea05a3c59436a693de9d415ce09ed90a2c16 [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 Afanasyeva46844b2011-11-21 19:13:26 -080031#include <boost/tuple/tuple.hpp>
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 Afanasyev772f51b2013-08-01 18:53:25 -070039class Data;
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070040class Interest;
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070041class Name;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070042class ContentStore;
43
Alexander Afanasyev79206512013-07-27 16:49:12 -070044/**
45 * @ingroup ndn
46 * @defgroup ndn-cs Content Store
47 */
48
49/**
50 * @ingroup ndn-cs
51 * @brief Namespace for ContentStore operations
52 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070053namespace cs {
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070054
Alexander Afanasyev070aa482011-08-20 00:38:25 -070055/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070056 * @ingroup ndn-cs
57 * @brief NDN content store entry
Alexander Afanasyev070aa482011-08-20 00:38:25 -070058 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059class Entry : public SimpleRefCount<Entry> {
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070060public:
Alexander Afanasyev070aa482011-08-20 00:38:25 -070061 /**
62 * \brief Construct content store entry
63 *
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070064 * \param header Parsed Data header
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065 * \param packet Original Ndn packet
Alexander Afanasyev070aa482011-08-20 00:38:25 -070066 *
67 * The constructor will make a copy of the supplied packet and calls
68 * RemoveHeader and RemoveTail on the copy.
69 */
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070070 Entry(Ptr<ContentStore> cs, shared_ptr<const Data> data);
Alexander Afanasyev070aa482011-08-20 00:38:25 -070071
72 /**
73 * \brief Get prefix of the stored entry
74 * \returns prefix of the stored entry
75 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070076 const Name&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 GetName() const;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070078
79 /**
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070080 * \brief Get Data of the stored entry
81 * \returns Data of the stored entry
Alexander Afanasyev070aa482011-08-20 00:38:25 -070082 */
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070083 shared_ptr<const Data>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084 GetData() const;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070085
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070086 /**
87 * @brief Get pointer to access store, to which this entry is added
88 */
89 Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 GetContentStore();
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070091
Alexander Afanasyev070aa482011-08-20 00:38:25 -070092private:
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070093 Ptr<ContentStore> m_cs; ///< \brief content store to which entry is added
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070094 shared_ptr<const Data> m_data; ///< \brief non-modifiable Data
Alexander Afanasyev070aa482011-08-20 00:38:25 -070095};
96
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070097} // namespace cs
98
Alexander Afanasyev070aa482011-08-20 00:38:25 -070099/**
Alexander Afanasyev79206512013-07-27 16:49:12 -0700100 * @ingroup ndn-cs
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700101 * \brief Base class for NDN content store
102 *
103 * Particular implementations should implement Lookup, Add, and Print methods
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700104 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105class ContentStore : public Object {
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700106public:
107 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700108 * \brief Interface ID
109 *
110 * \return interface ID
111 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 static TypeId
113 GetTypeId();
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700114
115 /**
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700116 * @brief Virtual destructor
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700117 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 virtual ~ContentStore();
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700119
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700120 /**
121 * \brief Find corresponding CS entry for the given interest
122 *
123 * \param interest Interest for which matching content store entry
124 * will be searched
125 *
126 * If an entry is found, it is promoted to the top of most recent
127 * used entries index, \see m_contentStore
128 */
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700129 virtual shared_ptr<Data>
130 Lookup(shared_ptr<const Interest> interest) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700131
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700132 /**
133 * \brief Add a new content to the content store.
134 *
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700135 * \param header Fully parsed Data
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700136 * \param packet Fully formed Ndn packet to add to content store
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700137 * (will be copied and stripped down of headers)
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700138 * @returns true if an existing entry was updated, false otherwise
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700139 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700140 virtual bool
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700141 Add(shared_ptr<const Data> data) = 0;
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700142
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800143 // /*
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700144 // * \brief Add a new content to the content store.
145 // *
146 // * \param header Interest header for which an entry should be removed
147 // * @returns true if an existing entry was removed, false otherwise
148 // */
149 // virtual bool
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700150 // Remove (shared_ptr<Interest> header) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700151
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700152 /**
153 * \brief Print out content store entries
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700154 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700155 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800156 Print(std::ostream& os) const = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700157
158 /**
159 * @brief Get number of entries in content store
160 */
161 virtual uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800162 GetSize() const = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700163
164 /**
165 * @brief Return first element of content store (no order guaranteed)
166 */
167 virtual Ptr<cs::Entry>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800168 Begin() = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700169
170 /**
171 * @brief Return item next after last (no order guaranteed)
172 */
173 virtual Ptr<cs::Entry>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800174 End() = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700175
176 /**
177 * @brief Advance the iterator
178 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800179 virtual Ptr<cs::Entry> Next(Ptr<cs::Entry>) = 0;
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700180
181 ////////////////////////////////////////////////////////////////////////////
182 ////////////////////////////////////////////////////////////////////////////
183 ////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700184
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700185 /**
186 * @brief Static call to cheat python bindings
187 */
188 static inline Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800189 GetContentStore(Ptr<Object> node);
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700190
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700191protected:
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700192 TracedCallback<shared_ptr<const Interest>,
193 shared_ptr<const Data>> m_cacheHitsTrace; ///< @brief trace of cache hits
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700194
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700195 TracedCallback<shared_ptr<const Interest>> m_cacheMissesTrace; ///< @brief trace of cache misses
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700196};
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700197
198inline std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800199operator<<(std::ostream& os, const ContentStore& cs)
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700200{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800201 cs.Print(os);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700202 return os;
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700203}
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700204
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700205inline Ptr<ContentStore>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800206ContentStore::GetContentStore(Ptr<Object> node)
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700207{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800208 return node->GetObject<ContentStore>();
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700209}
210
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700211} // namespace ndn
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700212} // namespace ns3
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700213
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700214#endif // NDN_CONTENT_STORE_H