blob: 4875386559358a8d2c3ffb786d8bd0393a8aade9 [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 Afanasyevcf133f02011-09-06 12:13:48 -070038TypeId
39CcnxContentStore::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 Afanasyevcf133f02011-09-06 12:13:48 -070053 return tid;
54}
Alexander Afanasyev070aa482011-08-20 00:38:25 -070055
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070056//////////////////////////////////////////////////////////////////////
57// Helper classes
58//////////////////////////////////////////////////////////////////////
59/**
60 * \ingroup ccnx
61 * \brief Typedef for hash index of content store container
62 */
63struct CcnxContentStoreByPrefix
64{
65 typedef
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070066 CcnxContentStoreContainer::type::index<i_prefix>::type
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070067 type;
68};
69
70/**
71 * \ingroup ccnx
72 * \brief Typedef for MRU index of content store container
73 */
74struct CcnxContentStoreByMru
75{
76 typedef
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070077 CcnxContentStoreContainer::type::index<i_mru>::type
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070078 type;
79};
80
81#ifdef _DEBUG
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070082#define DUMP_INDEX_TAG i_ordered
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070083#define DUMP_INDEX CcnxContentStoreOrderedPrefix
84/**
85 * \ingroup ccnx
86 * \brief Typedef for ordered index of content store container
87 */
88struct CcnxContentStoreOrderedPrefix
89{
90 typedef
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070091 CcnxContentStoreContainer::type::index<i_ordered>::type
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070092 type;
93};
94#else
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070095#define DUMP_INDEX_TAG i_mru
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070096#define DUMP_INDEX CcnxContentStoreByMru
97#endif
98
99//////////////////////////////////////////////////////////////////////
100
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700101CcnxContentStoreEntry::CcnxContentStoreEntry (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
102 : m_header (header)
103{
Alexander Afanasyev8accdf62011-09-20 11:33:59 -0700104 static CcnxContentObjectTail tail; ///< \internal for optimization purposes
105
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700106 m_packet = packet->Copy ();
Ilya Moiseenko7e6d0c92011-10-28 13:17:59 -0700107 //m_packet->RemoveHeader (*header);//causes bug
108 //m_packet->RemoveTrailer (tail);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700109}
110
111Ptr<Packet>
112CcnxContentStoreEntry::GetFullyFormedCcnxPacket () const
113{
Alexander Afanasyev8accdf62011-09-20 11:33:59 -0700114 static CcnxContentObjectTail tail; ///< \internal for optimization purposes
115
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700116 Ptr<Packet> packet = m_packet->Copy ();
117 packet->AddHeader (*m_header);
Alexander Afanasyev8accdf62011-09-20 11:33:59 -0700118 packet->AddTrailer (tail);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700119 return packet;
120}
121
122// /// Disabled copy constructor
123// CcnxContentStoreEntry::CcnxContentStoreEntry (const CcnxContentStoreEntry &o)
124// {
125// }
126
127// /// Disables copy operator
128// CcnxContentStoreEntry& CcnxContentStoreEntry::operator= (const CcnxContentStoreEntry &o)
129// {
130// return *this;
131// }
132
133
134
135CcnxContentStore::CcnxContentStore( )
136 : 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 -0700137
138CcnxContentStore::~CcnxContentStore( )
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700139{ }
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700140
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700141/// Disabled copy constructor
142CcnxContentStore::CcnxContentStore (const CcnxContentStore &o)
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700143{
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700144}
145
146/// Disables copy operator
147CcnxContentStore& CcnxContentStore::operator= (const CcnxContentStore &o)
148{
149 return *this;
150}
151
152
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800153boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader> >
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700154CcnxContentStore::Lookup (Ptr<const CcnxInterestHeader> interest)
155{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700156 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700157 CcnxContentStoreContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (interest->GetName ());
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700158 if (it != m_contentStore.end ())
159 {
160 // promote entry to the top
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700161 m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (),
162 m_contentStore.project<i_mru> (it));
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700163
164 // return fully formed CCNx packet
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800165 return boost::make_tuple (it->GetFullyFormedCcnxPacket (), it->GetHeader ());
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700166 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800167 return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader> > (0, 0);
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700168}
169
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700170void
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700171CcnxContentStore::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700172{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700173 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700174 CcnxContentStoreContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (header->GetName ());
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700175 if (it == m_contentStore.end ())
176 { // add entry to the top
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700177 m_contentStore.get<i_mru> ().push_front (CcnxContentStoreEntry (header, packet));
Alexander Afanasyevdd32de82011-08-20 00:47:28 -0700178 if (m_contentStore.size () > m_maxSize)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700179 m_contentStore.get<i_mru> ().pop_back ();
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700180 }
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700181 else
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700182 {
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700183 // promote entry to the top
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700184 m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (),
185 m_contentStore.project<i_mru> (it));
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700186 }
187}
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700188
189void
190CcnxContentStore::Print() const
191{
192 for( DUMP_INDEX::type::iterator it=m_contentStore.get<DUMP_INDEX_TAG> ().begin ();
193 it != m_contentStore.get<DUMP_INDEX_TAG> ().end ();
194 it++
195 )
196 {
197 NS_LOG_INFO (it->GetName ());
198 }
199}
200
201} // namespace ns3