blob: 54901d9bc7328f4c5c34e08734a424ac89624a53 [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 Afanasyev73f06f62013-03-15 15:41:38 -070039
40typedef Interest InterestHeader;
41typedef ContentObject ContentObjectHeader;
42
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070043class Name;
Alexander Afanasyev73f06f62013-03-15 15:41:38 -070044typedef Name NameComponents;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070046class ContentStore;
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 Afanasyev4aac5572012-08-09 10:49:55 -070051 * \ingroup ndn
Alexander Afanasyev070aa482011-08-20 00:38:25 -070052 * \brief NDN content store entry
53 *
54 * Content store entry stores separately pseudo header and content of
55 * ContentObject packet. It is responsibility of the caller to
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070056 * construct a fully formed NDN Packet by calling Copy(), AddHeader(),
Alexander Afanasyev070aa482011-08-20 00:38:25 -070057 * AddTail() on the packet received by GetPacket() method.
58 *
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070059 * GetFullyFormedNdnPacket method provided as a convenience
Alexander Afanasyev070aa482011-08-20 00:38:25 -070060 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070061class Entry : public SimpleRefCount<Entry>
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070062{
63public:
Alexander Afanasyev070aa482011-08-20 00:38:25 -070064 /**
65 * \brief Construct content store entry
66 *
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070067 * \param header Parsed ContentObject header
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070068 * \param packet Original Ndn packet
Alexander Afanasyev070aa482011-08-20 00:38:25 -070069 *
70 * The constructor will make a copy of the supplied packet and calls
71 * RemoveHeader and RemoveTail on the copy.
72 */
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070073 Entry (Ptr<ContentStore> cs, Ptr<const ContentObject> header, Ptr<const Packet> packet);
Alexander Afanasyev070aa482011-08-20 00:38:25 -070074
75 /**
76 * \brief Get prefix of the stored entry
77 * \returns prefix of the stored entry
78 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070079 const Name&
Alexander Afanasyev070aa482011-08-20 00:38:25 -070080 GetName () const;
81
82 /**
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070083 * \brief Get ContentObject of the stored entry
84 * \returns ContentObject of the stored entry
Alexander Afanasyev070aa482011-08-20 00:38:25 -070085 */
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070086 Ptr<const ContentObject>
Alexander Afanasyev070aa482011-08-20 00:38:25 -070087 GetHeader () const;
88
89 /**
90 * \brief Get content of the stored entry
91 * \returns content of the stored entry
92 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070093 Ptr<const Packet>
Alexander Afanasyev070aa482011-08-20 00:38:25 -070094 GetPacket () const;
95
96 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070097 * \brief Convenience method to create a fully formed Ndn packet from stored header and content
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070098 * \returns A read-write copy of the packet with ContentObject and ContentObjectTail
Alexander Afanasyev070aa482011-08-20 00:38:25 -070099 */
100 Ptr<Packet>
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700101 GetFullyFormedNdnPacket () const;
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700102
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700103 /**
104 * @brief Get pointer to access store, to which this entry is added
105 */
106 Ptr<ContentStore>
107 GetContentStore ();
108
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700109private:
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700110 Ptr<ContentStore> m_cs; ///< \brief content store to which entry is added
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700111 Ptr<const ContentObject> m_header; ///< \brief non-modifiable ContentObject
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700112 Ptr<Packet> m_packet; ///< \brief non-modifiable content of the ContentObject packet
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700113};
114
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700115} // namespace cs
116
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700117
118/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700119 * \ingroup ndn
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700120 * \brief Base class for NDN content store
121 *
122 * Particular implementations should implement Lookup, Add, and Print methods
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700123 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700124class ContentStore : public Object
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700125{
126public:
127 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700128 * \brief Interface ID
129 *
130 * \return interface ID
131 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700132 static
133 TypeId GetTypeId ();
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700134
135 /**
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700136 * @brief Virtual destructor
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700137 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700138 virtual
139 ~ContentStore ();
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700140
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700141 /**
142 * \brief Find corresponding CS entry for the given interest
143 *
144 * \param interest Interest for which matching content store entry
145 * will be searched
146 *
147 * If an entry is found, it is promoted to the top of most recent
148 * used entries index, \see m_contentStore
149 */
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700150 virtual boost::tuple<Ptr<Packet>, Ptr<const ContentObject>, Ptr<const Packet> >
151 Lookup (Ptr<const Interest> interest) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700152
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700153 /**
154 * \brief Add a new content to the content store.
155 *
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700156 * \param header Fully parsed ContentObject
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700157 * \param packet Fully formed Ndn packet to add to content store
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700158 * (will be copied and stripped down of headers)
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700159 * @returns true if an existing entry was updated, false otherwise
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700160 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700161 virtual bool
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700162 Add (Ptr<const ContentObject> header, Ptr<const Packet> packet) = 0;
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700163
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800164 // /*
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700165 // * \brief Add a new content to the content store.
166 // *
167 // * \param header Interest header for which an entry should be removed
168 // * @returns true if an existing entry was removed, false otherwise
169 // */
170 // virtual bool
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700171 // Remove (Ptr<Interest> header) = 0;
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700172
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700173 /**
174 * \brief Print out content store entries
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700175 */
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700176 virtual void
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700177 Print (std::ostream &os) const = 0;
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700178
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700179
180 /**
181 * @brief Get number of entries in content store
182 */
183 virtual uint32_t
184 GetSize () const = 0;
185
186 /**
187 * @brief Return first element of content store (no order guaranteed)
188 */
189 virtual Ptr<cs::Entry>
190 Begin () = 0;
191
192 /**
193 * @brief Return item next after last (no order guaranteed)
194 */
195 virtual Ptr<cs::Entry>
196 End () = 0;
197
198 /**
199 * @brief Advance the iterator
200 */
201 virtual Ptr<cs::Entry>
202 Next (Ptr<cs::Entry>) = 0;
203
204 ////////////////////////////////////////////////////////////////////////////
205 ////////////////////////////////////////////////////////////////////////////
206 ////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700207
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700208 /**
209 * @brief Static call to cheat python bindings
210 */
211 static inline Ptr<ContentStore>
212 GetContentStore (Ptr<Object> node);
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700213
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700214protected:
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700215 TracedCallback<Ptr<const Interest>,
216 Ptr<const ContentObject> > m_cacheHitsTrace; ///< @brief trace of cache hits
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700217
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700218 TracedCallback<Ptr<const Interest> > m_cacheMissesTrace; ///< @brief trace of cache misses
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700219};
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700220
221inline std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700222operator<< (std::ostream &os, const ContentStore &cs)
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700223{
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700224 cs.Print (os);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700225 return os;
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700226}
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700227
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700228inline Ptr<ContentStore>
229ContentStore::GetContentStore (Ptr<Object> node)
230{
231 return node->GetObject<ContentStore> ();
232}
233
234
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700235} // namespace ndn
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700236} // namespace ns3
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700237
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700238#endif // NDN_CONTENT_STORE_H