blob: b5263084d352edbb9efe461b429d7fbfc969a897 [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
36using namespace __ccnx_private;
37
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
58CcnxContentStoreEntry::CcnxContentStoreEntry (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
59 : m_header (header)
60{
61 m_packet = packet->Copy ();
62 m_packet->RemoveHeader (*header);
63 m_packet->RemoveTrailer (m_tail);
64}
65
66Ptr<Packet>
67CcnxContentStoreEntry::GetFullyFormedCcnxPacket () const
68{
69 Ptr<Packet> packet = m_packet->Copy ();
70 packet->AddHeader (*m_header);
71 packet->AddTrailer (m_tail);
72 return packet;
73}
74
75// /// Disabled copy constructor
76// CcnxContentStoreEntry::CcnxContentStoreEntry (const CcnxContentStoreEntry &o)
77// {
78// }
79
80// /// Disables copy operator
81// CcnxContentStoreEntry& CcnxContentStoreEntry::operator= (const CcnxContentStoreEntry &o)
82// {
83// return *this;
84// }
85
86
87
88CcnxContentStore::CcnxContentStore( )
89 : 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 -070090
91CcnxContentStore::~CcnxContentStore( )
Alexander Afanasyev070aa482011-08-20 00:38:25 -070092{ }
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070093
Alexander Afanasyev070aa482011-08-20 00:38:25 -070094/// Disabled copy constructor
95CcnxContentStore::CcnxContentStore (const CcnxContentStore &o)
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070096{
Alexander Afanasyev070aa482011-08-20 00:38:25 -070097}
98
99/// Disables copy operator
100CcnxContentStore& CcnxContentStore::operator= (const CcnxContentStore &o)
101{
102 return *this;
103}
104
105
106Ptr<Packet>
107CcnxContentStore::Lookup (Ptr<const CcnxInterestHeader> interest)
108{
109 CcnxContentStoreContainer::type::iterator it = m_contentStore.get<hash> ().find (interest->GetName ());
110 if (it != m_contentStore.end ())
111 {
112 // promote entry to the top
113 m_contentStore.get<mru> ().relocate (m_contentStore.get<mru> ().begin (),
114 m_contentStore.project<mru> (it));
115
116 // return fully formed CCNx packet
117 return it->GetFullyFormedCcnxPacket ();
118 }
119 return 0;
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700120}
121
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700122void
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700123CcnxContentStore::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700124{
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700125 CcnxContentStoreContainer::type::iterator it = m_contentStore.get<hash> ().find (header->GetName ());
126 if (it == m_contentStore.end ())
127 { // add entry to the top
128 m_contentStore.get<mru> ().push_front (CcnxContentStoreEntry (header, packet));
Alexander Afanasyevdd32de82011-08-20 00:47:28 -0700129 if (m_contentStore.size () > m_maxSize)
130 m_contentStore.get<mru> ().pop_back ();
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700131 }
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700132 else
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700133 {
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700134 // promote entry to the top
135 m_contentStore.get<mru> ().relocate (m_contentStore.get<mru> ().begin (),
136 m_contentStore.project<mru> (it));
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -0700137 }
138}
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700139
140void
141CcnxContentStore::Print() const
142{
143 for( DUMP_INDEX::type::iterator it=m_contentStore.get<DUMP_INDEX_TAG> ().begin ();
144 it != m_contentStore.get<DUMP_INDEX_TAG> ().end ();
145 it++
146 )
147 {
148 NS_LOG_INFO (it->GetName ());
149 }
150}
151
152} // namespace ns3