blob: 1a27969d824e97321a3ed74ba5d369feb3278964 [file] [log] [blame]
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
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 *
18 * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
Alexander Afanasyev070aa482011-08-20 00:38:25 -070019 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070020 */
21
22#include "ccnx-content-store.h"
23#include "ns3/log.h"
Alexander Afanasyev070aa482011-08-20 00:38:25 -070024#include "ns3/packet.h"
25#include "ns3/ccnx-interest-header.h"
26#include "ns3/ccnx-content-object-header.h"
27#include "ns3/uinteger.h"
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070028
29NS_LOG_COMPONENT_DEFINE ("CcnxContentStore");
30
31namespace ns3
32{
Alexander Afanasyev070aa482011-08-20 00:38:25 -070033
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070034// NS_OBJECT_ENSURE_REGISTERED (CcnxContentStore);
Alexander Afanasyev070aa482011-08-20 00:38:25 -070035
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070036using namespace __ccnx_private;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070037
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070038// TypeId
39// CcnxContentStore::GetTypeId (void)
40// {
41// static TypeId tid = TypeId ("ns3::CcnxContentStore")
42// .SetGroupName ("Ccnx")
43// .SetParent<Object> ()
44// .AddConstructor<CcnxContentStore> ()
45// .AddAttribute ("Size",
46// "Maximum number of packets that content storage can hold",
47// UintegerValue (100),
48// MakeUintegerAccessor (&CcnxContentStore::SetMaxSize,
49// &CcnxContentStore::GetMaxSize),
50// MakeUintegerChecker<uint32_t> ())
51// ;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070052
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070053// return tid;
54// }
Alexander Afanasyev070aa482011-08-20 00:38:25 -070055
56CcnxContentObjectTail CcnxContentStoreEntry::m_tail;
57
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070058//////////////////////////////////////////////////////////////////////
59// Helper classes
60//////////////////////////////////////////////////////////////////////
61/**
62 * \ingroup ccnx
63 * \brief Typedef for hash index of content store container
64 */
65struct CcnxContentStoreByPrefix
66{
67 typedef
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070068 CcnxContentStoreContainer::type::index<i_prefix>::type
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070069 type;
70};
71
72/**
73 * \ingroup ccnx
74 * \brief Typedef for MRU index of content store container
75 */
76struct CcnxContentStoreByMru
77{
78 typedef
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070079 CcnxContentStoreContainer::type::index<i_mru>::type
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070080 type;
81};
82
83#ifdef _DEBUG
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070084#define DUMP_INDEX_TAG i_ordered
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070085#define DUMP_INDEX CcnxContentStoreOrderedPrefix
86/**
87 * \ingroup ccnx
88 * \brief Typedef for ordered index of content store container
89 */
90struct CcnxContentStoreOrderedPrefix
91{
92 typedef
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070093 CcnxContentStoreContainer::type::index<i_ordered>::type
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070094 type;
95};
96#else
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070097#define DUMP_INDEX_TAG i_mru
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070098#define DUMP_INDEX CcnxContentStoreByMru
99#endif
100
101//////////////////////////////////////////////////////////////////////
102
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700103CcnxContentStoreEntry::CcnxContentStoreEntry (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
104 : m_header (header)
105{
106 m_packet = packet->Copy ();
107 m_packet->RemoveHeader (*header);
108 m_packet->RemoveTrailer (m_tail);
109}
110
111Ptr<Packet>
112CcnxContentStoreEntry::GetFullyFormedCcnxPacket () const
113{
114 Ptr<Packet> packet = m_packet->Copy ();
115 packet->AddHeader (*m_header);
116 packet->AddTrailer (m_tail);
117 return packet;
118}
119
120// /// Disabled copy constructor
121// CcnxContentStoreEntry::CcnxContentStoreEntry (const CcnxContentStoreEntry &o)
122// {
123// }
124
125// /// Disables copy operator
126// CcnxContentStoreEntry& CcnxContentStoreEntry::operator= (const CcnxContentStoreEntry &o)
127// {
128// return *this;
129// }
130
131
132
133CcnxContentStore::CcnxContentStore( )
134 : m_maxSize(100) { } // this value shouldn't matter, NS-3 should call SetSize with default value specified in AddAttribute earlier
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700135
136CcnxContentStore::~CcnxContentStore( )
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700137{ }
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700138
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700139/// Disabled copy constructor
140CcnxContentStore::CcnxContentStore (const CcnxContentStore &o)
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700141{
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700142}
143
144/// Disables copy operator
145CcnxContentStore& CcnxContentStore::operator= (const CcnxContentStore &o)
146{
147 return *this;
148}
149
150
151Ptr<Packet>
152CcnxContentStore::Lookup (Ptr<const CcnxInterestHeader> interest)
153{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700154 CcnxContentStoreContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (interest->GetName ());
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700155 if (it != m_contentStore.end ())
156 {
157 // promote entry to the top
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700158 m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (),
159 m_contentStore.project<i_mru> (it));
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700160
161 // return fully formed CCNx packet
162 return it->GetFullyFormedCcnxPacket ();
163 }
164 return 0;
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700165}
166
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700167void
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700168CcnxContentStore::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700169{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700170 CcnxContentStoreContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (header->GetName ());
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700171 if (it == m_contentStore.end ())
172 { // add entry to the top
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700173 m_contentStore.get<i_mru> ().push_front (CcnxContentStoreEntry (header, packet));
Alexander Afanasyevdd32de82011-08-20 00:47:28 -0700174 if (m_contentStore.size () > m_maxSize)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700175 m_contentStore.get<i_mru> ().pop_back ();
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700176 }
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700177 else
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700178 {
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700179 // promote entry to the top
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700180 m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (),
181 m_contentStore.project<i_mru> (it));
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700182 }
183}
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700184
185void
186CcnxContentStore::Print() const
187{
188 for( DUMP_INDEX::type::iterator it=m_contentStore.get<DUMP_INDEX_TAG> ().begin ();
189 it != m_contentStore.get<DUMP_INDEX_TAG> ().end ();
190 it++
191 )
192 {
193 NS_LOG_INFO (it->GetName ());
194 }
195}
196
197} // namespace ns3