blob: c47159e4b904d1318529c1f4554fb6267058914e [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
34NS_OBJECT_ENSURE_REGISTERED (CcnxContentStore);
35
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070036using namespace __ccnx_private_content_store;
Alexander Afanasyev070aa482011-08-20 00:38:25 -070037
38TypeId
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 ;
52
53 return tid;
54}
55
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
68 CcnxContentStoreContainer::type::index<hash>::type
69 type;
70};
71
72/**
73 * \ingroup ccnx
74 * \brief Typedef for MRU index of content store container
75 */
76struct CcnxContentStoreByMru
77{
78 typedef
79 CcnxContentStoreContainer::type::index<mru>::type
80 type;
81};
82
83#ifdef _DEBUG
84#define DUMP_INDEX_TAG ordered
85#define DUMP_INDEX CcnxContentStoreOrderedPrefix
86/**
87 * \ingroup ccnx
88 * \brief Typedef for ordered index of content store container
89 */
90struct CcnxContentStoreOrderedPrefix
91{
92 typedef
93 CcnxContentStoreContainer::type::index<ordered>::type
94 type;
95};
96#else
97#define DUMP_INDEX_TAG mru
98#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{
154 CcnxContentStoreContainer::type::iterator it = m_contentStore.get<hash> ().find (interest->GetName ());
155 if (it != m_contentStore.end ())
156 {
157 // promote entry to the top
158 m_contentStore.get<mru> ().relocate (m_contentStore.get<mru> ().begin (),
159 m_contentStore.project<mru> (it));
160
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 Afanasyev070aa482011-08-20 00:38:25 -0700170 CcnxContentStoreContainer::type::iterator it = m_contentStore.get<hash> ().find (header->GetName ());
171 if (it == m_contentStore.end ())
172 { // add entry to the top
173 m_contentStore.get<mru> ().push_front (CcnxContentStoreEntry (header, packet));
Alexander Afanasyevdd32de82011-08-20 00:47:28 -0700174 if (m_contentStore.size () > m_maxSize)
175 m_contentStore.get<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
180 m_contentStore.get<mru> ().relocate (m_contentStore.get<mru> ().begin (),
181 m_contentStore.project<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